Skip to content

Commit 77e996c

Browse files
author
Maya Shavin
committed
add: vue-cli plugin for cloudinary api
1 parent c2409ab commit 77e996c

13 files changed

Lines changed: 206 additions & 3 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
},
3838
"homepage": "https://cloudinary-build-url.netlify.app/",
3939
"repository": {
40-
"url": "https://github.com/mayashavin/cloudinary-api"
40+
"type": "git",
41+
"url": "git+https://github.com/mayashavin/cloudinary-api"
4142
},
4243
"devDependencies": {
4344
"@typescript-eslint/eslint-plugin": "^4.7.0",

packages/types/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"sideEffects": false,
1313
"repository": {
1414
"type": "git",
15-
"url": "git+https://github.com/mayashavin/cloudinary-api.git"
15+
"url": "git+https://github.com/mayashavin/cloudinary-api.git",
16+
"directory": "packages/types"
1617
},
1718
"scripts": {
1819
"bundlewatch": "bundlewatch --config bundlewatch.config.json",

packages/url/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
"publishConfig": {
5959
"access": "public"
6060
},
61+
"repository": {
62+
"type": "git",
63+
"url": "git+https://github.com/mayashavin/cloudinary-api.git",
64+
"directory": "packages/url"
65+
},
6166
"gitHead": "dcef68eee9274fe77f860b2f7e7165fde41f8bb6"
6267
}

packages/utils/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"license": "MIT",
88
"repository": {
99
"type": "git",
10-
"url": "git+https://github.com/mayashavin/cloudinary-api.git"
10+
"url": "git+https://github.com/mayashavin/cloudinary-api.git",
11+
"directory": "packages/utils"
1112
},
1213
"bugs": {
1314
"url": "https://github.com/mayashavin/cloudinary-api/issues"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `vue-cli-plugin-cloudinary-api`
2+
3+
> Vue-cli plugin for installing Cloudinary light APIs in Vue project.
4+
5+
## Usage
6+
7+
```bash
8+
vue add cloudinary-api
9+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/** Generator */
2+
module.exports = (api, opts) => {
3+
api.extendPackage({
4+
dependencies: {
5+
"cloudinary-build-url": "latest",
6+
},
7+
});
8+
9+
const urlModes = opts.urlModes.reduce(
10+
(opt, mode) => ({
11+
image: opt.image || mode === "buildImageUrl",
12+
video: opt.video || mode === "buildVideoUrl",
13+
}),
14+
{
15+
image: false,
16+
video: false,
17+
}
18+
);
19+
20+
api.render("./template", {
21+
cloudName: opts.cloudName,
22+
...urlModes,
23+
features: opts.urlModes,
24+
});
25+
26+
api.onCreateComplete(() => {
27+
// // inject to main.js
28+
const fs = require("fs");
29+
const mainPath = api.resolve(api.entryFile);
30+
31+
// get content
32+
let contentMain = fs.readFileSync(mainPath, { encoding: "utf-8" });
33+
34+
const lines = contentMain.split(/\r?\n/g).reverse();
35+
// inject import
36+
const lastImportIndex = lines.findIndex((line) => line.match(/^import/));
37+
38+
if (contentMain.includes("createApp(")) {
39+
//Vue 3x
40+
lines[
41+
lastImportIndex
42+
] += `\nimport useCloudinary from './plugins/cloudinary'`;
43+
44+
const lastCreateAppIndex = lines.findIndex((line) =>
45+
line.match(/^createApp\(/)
46+
);
47+
48+
if (lastCreateAppIndex !== -1) {
49+
const sublines = lines[lastCreateAppIndex].split(".");
50+
sublines[0] = `const app = ${sublines[0]};`;
51+
const newLines = sublines.map((line, index) =>
52+
index ? `app.${line};` : line
53+
);
54+
55+
const createAppLines = newLines.join("\n");
56+
57+
lines[lastCreateAppIndex] = createAppLines;
58+
}
59+
60+
contentMain = lines.reverse().join("\n");
61+
contentMain += `\nuseCloudinary(app);`;
62+
} else {
63+
//Vue 2x
64+
lines[lastImportIndex] += `\nimport './plugins/cloudinary'`;
65+
contentMain = lines.reverse().join("\n");
66+
}
67+
68+
// modify app
69+
fs.writeFileSync(mainPath, contentMain, { encoding: "utf-8" });
70+
71+
api.exitLog("🖼️ Learn more how to use url builder: https://cloudinary-build-url.netlify.com");
72+
api.exitLog("💻 Github: https://github.com/mayashavin/cloudinary-api");
73+
api.exitLog(
74+
"❤️ Have fun optimizing your images and videos with Cloudinary ❤️"
75+
);
76+
});
77+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Vue from 'vue';
2+
import { setConfig, <%= features %> } from 'cloudinary-build-url';
3+
4+
setConfig({
5+
cloudName: '<%= cloudName %>'
6+
})
7+
8+
if (Vue && Vue.version && Vue.version.startsWith('2')) {
9+
/** Vue 2x */
10+
<% if (image) {%>Vue.prototype.$imageUrl = buildImageUrl;<% } %>
11+
<% if (video) {%>Vue.prototype.$videoUrl = buildVideoUrl;<% } %>
12+
}
13+
14+
const useCloudinary = (app) => {
15+
if (!app || !app.config || !app.config.globalProperties) return
16+
17+
<% if (image) {%>app.config.globalProperties.$imageUrl = buildImageUrl;<% } %>
18+
<% if (video) {%>app.config.globalProperties.$videoUrl = buildVideoUrl;<% } %>
19+
}
20+
21+
export default useCloudinary
9.96 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = () => {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "vue-cli-plugin-cloudinary-api",
3+
"version": "0.1.1",
4+
"description": "Cloudinary light APIs plugin for vue-cli (3.0+)",
5+
"keywords": [
6+
"vue",
7+
"vue-cli",
8+
"plugin",
9+
"url",
10+
"cloudinary",
11+
"sdk",
12+
"image",
13+
"optimization",
14+
"video",
15+
"vue-cli-3",
16+
"vue-cli-4",
17+
"vue-cli cloudinary"
18+
],
19+
"author": "Maya Shavin <maya@cloudinary.com>",
20+
"homepage": "https://cloudinary-build-url.netlify.app",
21+
"license": "MIT",
22+
"main": "index.js",
23+
"repository": {
24+
"type": "git",
25+
"url": "git+https://github.com/mayashavin/cloudinary-api.git",
26+
"directory": "packages/vue-cli-plugin-cloudinary-api"
27+
},
28+
"scripts": {
29+
"lint": "eslint --ext .js,.vue .",
30+
"test": "jest"
31+
},
32+
"bugs": {
33+
"url": "https://github.com/mayashavin/cloudinary-api/issues"
34+
}
35+
}

0 commit comments

Comments
 (0)