-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin-init.js
180 lines (160 loc) Β· 4.42 KB
/
plugin-init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#! /usr/bin/env node
const path = require("path");
const fs = require("fs");
const ejs = require("ejs");
const prompts = require("prompts");
const chalk = require("chalk");
const shelljs = require("shelljs");
const pkg = require("./package");
// Object to store data from user input response.
const result = {
npmName: "",
update: false,
};
// Exit the process.
function onCancel() {
console.log("Byee π");
process.exit();
}
async function displayInstalledVersion() {
const current = pkg.version;
console.log(chalk.magenta(`Vue Plugin CLI v${current}`));
}
async function getName() {
const question = {
type: "text",
name: "npmName",
message: "What is the name of your Vue plugin?",
validate(val) {
return val !== "";
},
};
const response = await prompts(question, {
onCancel: onCancel,
});
result.npmName = response.npmName;
}
function createPluginProject(options) {
let savePath = process.env.INIT_CWD;
if (savePath === undefined) {
savePath = ".";
}
console.log(
`β¨ Creating project in ${chalk.yellow(savePath + "/" + options.npmName)}`
);
console.log(`π Invoking generator...`);
const vars = {
npmName: options.npmName,
pluginName: pascalify(options.npmName),
};
const files = {
common: [
".browserslistrc",
".editorconfig",
".eslintrc.js",
".prettierrc",
"jsconfig.json",
"babel.config.js",
"vue.config.js",
"README.md",
{ "plugin-package.json": "package.json" },
],
src: [
"src/components/Counter.vue",
"src/components/HelloWorld.vue",
"src/index.js",
],
dev: ["dev/serve.js", "dev/serve.vue"],
build: ["build/rollup.config.js"],
vueRouter: [
"src/router/routes.js",
"src/router/layouts/PluginLayout.vue",
"src/router/views/Welcome.vue",
"dev/router/index.js",
"dev/router/routes.js",
"dev/router/views/Home.vue",
],
vuex: [
"dev/state/store.js",
"src/store/counter/actions.js",
"src/store/counter/mutations.js",
"src/store/counter/state.js",
"src/store/counter/index.js",
],
unit: ["tests/unit/HelloWorld.spec.js", "jest.config.js"],
e2e: [
"tests/e2e/.eslintrc.js",
"tests/e2e/plugins/index.js",
"tests/e2e/specs/test.js",
"tests/e2e/support/commands.js",
"tests/e2e/support/index.js",
"cypress.json",
],
};
const fileActions = [
...files.common.filter((entry) => entry),
...files.src.filter((entry) => entry),
...files.dev.filter((entry) => entry),
...files.build.filter((entry) => entry),
...files.vueRouter.filter((entry) => entry),
...files.vuex.filter((entry) => entry),
...files.unit.filter((entry) => entry),
...files.e2e.filter((entry) => entry),
];
fileActions.forEach((fileAction) => {
let srcPath;
let destPath;
if (typeof fileAction === "string") {
srcPath = fileAction;
destPath = fileAction;
} else {
[[srcPath, destPath]] = Object.entries(fileAction);
}
srcPath = path.join.apply(null, [
__dirname,
"templates",
"plugin",
...srcPath.split("/"),
]);
destPath = path.join.apply(null, [
savePath + "/" + options.npmName,
...destPath.split("/"),
]);
ensureDirectoryExists(destPath);
fs.writeFileSync(
destPath,
ejs.render(fs.readFileSync(srcPath).toString(), vars)
);
});
// Change current dir
process.chdir(savePath);
process.chdir(`./${vars.npmName}`);
console.log(
`π¦ Installing additional dependencies. This might take a while...`
);
shelljs.exec("yarn");
console.log(`\n`);
console.log(`π Successfully created project ${chalk.yellow(vars.npmName)}.`);
console.log(`π Get started with the following commands:`);
console.log(`\n`);
console.log(` ${chalk.gray("$")} ${chalk.cyan("cd " + vars.npmName)}`);
console.log(` ${chalk.gray("$")} ${chalk.cyan("yarn serve")}`);
console.log(`\n`);
}
const pascalify = (str) => {
const camelized = str.replace(/-([a-z])/g, (c) => c[1].toUpperCase());
return camelized.charAt(0).toUpperCase() + camelized.slice(1);
};
const ensureDirectoryExists = (filePath) => {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
ensureDirectoryExists(dirname);
return fs.mkdirSync(dirname);
};
displayInstalledVersion()
.then(getName)
.then(() => {
createPluginProject(result);
});