-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·312 lines (271 loc) · 9.2 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env node
const path = require("path");
const fs = require("fs-extra");
const argv = require("minimist")(process.argv.slice(2));
const inquirer = require("inquirer");
const chalk = require("chalk");
const { execSync } = require("child_process");
function logHelp() {
console.log(`
Usage: create-lumapps-extension [folder] [--options]
Update usage : create-lumapps-extension --update
Options:
--help, -h [boolean] show help
--version, -v [boolean] show version
--template, -t [string] use specified template (react)
--update, -u [boolean] update extension packages
`);
}
async function backAndCopyFile(fileName) {
const tsConfigExtension = path.join(process.cwd(), fileName);
if (fs.existsSync(tsConfigExtension)) {
fs.renameSync(
tsConfigExtension,
path.join(process.cwd(), `${fileName}.old`),
function (err) {
if (err) console.log("ERROR: " + err);
}
);
}
await fs.copy(
path.join(__dirname, "updateFiles", fileName),
tsConfigExtension
);
}
function getDependencies(refDependencies, optional = false) {
const dependencies = [];
for (package in refDependencies) {
if (optional) {
const pkg = require(path.join(process.cwd(), `package.json`));
if (pkg.dependencies[package] || pkg.devDependencies[package]) {
dependencies.push(`${package}@${refDependencies[package]}`);
}
} else {
dependencies.push(`${package}@${refDependencies[package]}`);
}
}
return dependencies.join(" ");
}
async function updateExtension() {
await inquirer
.prompt([
{
type: "confirm",
name: "confirmUpdate",
message:
"➤ This will update your extension dependencies, do you want to continue ?",
default: true,
},
])
.then(async (answers) => {
if (answers.confirmUpdate) {
const {
forcedDependencies,
optionalDependencies,
} = require("./updateFiles/updatePackages");
// FORCED DEP
const dependencies = getDependencies(
forcedDependencies.dependencies
);
console.log(
chalk.blue("➤ Installing/Updating mandatory dependencies")
);
try {
execSync(`yarn add ${dependencies}`, { stdio: "inherit" });
} catch (error) {
console.log(
chalk.red(`An error occured while installing the dependencies.
You should check the yarn logs.
Some of your dependencies might not be available with this version of node.
Try to fix this issues before launching the update again.`)
);
return;
}
console.log(chalk.green("➤ Mandatory dependencies installed !"));
console.log("-------------------------------------------------");
// FORCED DEV DEP
const devDependencies = getDependencies(
forcedDependencies.devDependencies
);
console.log(
chalk.blue("➤ Installing/Updating mandatory devDependecies")
);
try {
execSync(
`yarn add -D ${devDependencies}`,
{ stdio: "inherit" },
(err) => {
if (err) {
console.log(err);
return;
}
}
);
} catch (error) {
console.log(error);
}
console.log(chalk.green("➤ Mandatory devDependencies installed !"));
console.log("-------------------------------------------------");
// OPTIONAL
const optDependencies = getDependencies(optionalDependencies, true);
console.log(chalk.blue("➤ Updating optional dependencies"));
try {
execSync(
`yarn up ${optDependencies}`,
{ stdio: "inherit" },
(err) => {
if (err) {
console.log(err);
return;
}
}
);
} catch (error) {
console.log(error);
}
console.log(chalk.green("➤ Optional dependencies updated !"));
console.log("-------------------------------------------------");
await inquirer
.prompt([
{
type: "confirm",
name: "confirmElsintUpdate",
message:
"➤ Do you want to update your .eslint.json file ? (the old one will be backed up)",
default: true,
},
])
.then(async (answers) => {
if (answers.confirmElsintUpdate) {
await backAndCopyFile(".eslintrc.json");
console.log(
chalk.green(
"➤ .eslint.json file updated successfully !"
)
);
}
});
await inquirer
.prompt([
{
type: "confirm",
name: "confirmTsConfig",
message:
"➤ Do you want to update your tsconfig.json file ? (the old one will be backed up)",
default: true,
},
])
.then(async (answers) => {
if (answers.confirmTsConfig) {
await backAndCopyFile("tsconfig.json");
console.log(
chalk.green(
"➤ tsconfig.json file updated successfully !"
)
);
}
});
console.log(
chalk.green(`
-------------------------------------------------
➤ Your extension has been updated, successfully !
➤ You can launch it using yarn start.
➤ You might encounter some lint issues, you can try to use the command "npx eslint . --fix" to fix it automatically
-------------------------------------------------`)
);
}
});
}
console.log(
chalk.cyan(`create-lumapps-extension v${require("./package.json").version}`)
);
async function init() {
const targetDir = argv._[0];
const { help, h, template, t, version, v, update, u } = argv;
const userNode = process.version;
if (!userNode.startsWith("v20")) {
console.log(
chalk.red(
`You are using node version ${userNode}, you need to use the lts (V20.x.x)`
)
);
return;
}
if (!targetDir && (update || u)) {
updateExtension();
return;
}
if (!targetDir) {
console.error(chalk.red("Please provide a target folder !"));
logHelp();
process.exit(1);
}
const cwd = process.cwd();
const root = path.join(cwd, targetDir);
const renameFiles = {
_gitignore: ".gitignore",
_yarnrc_yml: ".yarnrc.yml",
};
if (help || h) {
logHelp();
return;
} else if (version || v) {
// noop, already logged
return;
}
// Template prompt
let choosedTemplate = t || template;
if (!choosedTemplate) {
const choices = [
{ key: 1, name: "Widget Extension", value: "widget-extension" },
{ key: 2, name: "Share Extension", value: "share-extension" },
{ key: 3, name: "Empty Extension", value: "empty-extension" },
{ key: 4, name: "Search Extension - BETA", value: "search-extension" },
{
key: 5,
name: "Backend Extension - BETA",
value: "backend-extension",
},
];
choice = await inquirer.prompt({
type: "list",
message: "Choose a template",
name: "template",
choices,
});
choosedTemplate = choice.template;
}
console.log("\n--------------------");
console.log(`\nScaffolding project in ${root}...`);
await fs.ensureDir(root);
const templateDir = path.join(__dirname, `template-${choosedTemplate}`);
const write = async (file, content) => {
const targetPath = renameFiles[file]
? path.join(root, renameFiles[file])
: path.join(root, file);
if (content) {
await fs.writeFile(targetPath, content);
} else {
await fs.copy(path.join(templateDir, file), targetPath);
}
};
const files = await fs.readdir(templateDir);
for (const file of files.filter((f) => f !== "package.json")) {
await write(file);
}
const pkg = require(path.join(templateDir, `package.json`));
pkg.name = path.basename(root);
await write("package.json", JSON.stringify(pkg, null, 2));
console.log(`\n${chalk.green("Done")}. Now run:\n`);
console.log(
chalk.cyan(`
${root !== cwd && `cd ${path.relative(cwd, root)}`}
npm install (or \`yarn\`)
npm run start (or \`yarn start\`)
`)
);
console.log();
}
init().catch((e) => {
console.error(e);
});