|
1 | 1 | #!/usr/bin/env node
|
2 |
| -const path = require('path') |
3 |
| -const fs = require('fs-extra') |
4 |
| -const argv = require('minimist')(process.argv.slice(2)) |
5 |
| -const inquirer = require('inquirer'); |
6 |
| -const chalk = require('chalk') |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs-extra"); |
| 4 | +const argv = require("minimist")(process.argv.slice(2)); |
| 5 | +const inquirer = require("inquirer"); |
| 6 | +const chalk = require("chalk"); |
| 7 | +const pkg = require(path.join(process.cwd(), `package.json`)); |
| 8 | + |
| 9 | +const { execSync } = require("child_process"); |
7 | 10 |
|
8 | 11 | function logHelp() {
|
9 |
| - console.log(` |
| 12 | + console.log(` |
10 | 13 | Usage: create-lumapps-extension [folder] [--options]
|
11 | 14 |
|
| 15 | + Update usage : create-lumapps-extension --update |
| 16 | +
|
12 | 17 | Options:
|
13 | 18 | --help, -h [boolean] show help
|
14 | 19 | --version, -v [boolean] show version
|
15 | 20 | --template, -t [string] use specified template (react)
|
16 |
| - `) |
| 21 | + --update, -u [boolean] update extension packages |
| 22 | + `); |
| 23 | +} |
| 24 | + |
| 25 | +async function backAndCopyFile(fileName) { |
| 26 | + const tsConfigExtension = path.join(process.cwd(), fileName); |
| 27 | + |
| 28 | + if (fs.existsSync(tsConfigExtension)) { |
| 29 | + fs.renameSync( |
| 30 | + tsConfigExtension, |
| 31 | + path.join(process.cwd(), `${fileName}.old`), |
| 32 | + function (err) { |
| 33 | + if (err) console.log("ERROR: " + err); |
| 34 | + } |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + await fs.copy( |
| 39 | + path.join(__dirname, "updateFiles", fileName), |
| 40 | + tsConfigExtension |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function getDependencies(refDependencies, optional = false) { |
| 45 | + const dependencies = []; |
| 46 | + |
| 47 | + for (package in refDependencies) { |
| 48 | + if (optional) { |
| 49 | + if (pkg.dependencies[package] || pkg.devDependencies[package]) { |
| 50 | + dependencies.push(`${package}@${refDependencies[package]}`); |
| 51 | + } |
| 52 | + } else { |
| 53 | + dependencies.push(`${package}@${refDependencies[package]}`); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return dependencies.join(" "); |
17 | 58 | }
|
18 | 59 |
|
19 |
| -console.log(chalk.cyan(`create-lumapps-extension v${require('./package.json').version}`)) |
| 60 | +async function updateExtension() { |
| 61 | + await inquirer |
| 62 | + .prompt([ |
| 63 | + { |
| 64 | + type: "confirm", |
| 65 | + name: "confirmUpdate", |
| 66 | + message: |
| 67 | + "➤ This will update your extension dependencies, do you want to continue ?", |
| 68 | + default: true, |
| 69 | + }, |
| 70 | + ]) |
| 71 | + .then(async (answers) => { |
| 72 | + if (answers.confirmUpdate) { |
| 73 | + const { |
| 74 | + forcedDependencies, |
| 75 | + optionalDependencies, |
| 76 | + } = require("./updatePackages"); |
| 77 | + |
| 78 | + // FORCED DEP |
| 79 | + const dependencies = getDependencies( |
| 80 | + forcedDependencies.dependencies |
| 81 | + ); |
| 82 | + |
| 83 | + console.log( |
| 84 | + chalk.blue("➤ Installing/Updating mandatory dependencies") |
| 85 | + ); |
| 86 | + try { |
| 87 | + execSync(`yarn add ${dependencies}`, { stdio: "inherit" }); |
| 88 | + } catch (error) { |
| 89 | + console.log( |
| 90 | + chalk.red(`An error occured while installing the dependencies. |
| 91 | +You should check the yarn logs. |
| 92 | +Some of your dependencies might not be available with this version of node. |
| 93 | +Try to fix this issues before launching the update again.`) |
| 94 | + ); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + console.log(chalk.green("➤ Mandatory dependencies installed !")); |
| 99 | + console.log("-------------------------------------------------"); |
| 100 | + |
| 101 | + // FORCED DEV DEP |
| 102 | + const devDependencies = getDependencies( |
| 103 | + forcedDependencies.devDependencies |
| 104 | + ); |
| 105 | + |
| 106 | + console.log( |
| 107 | + chalk.blue("➤ Installing/Updating mandatory devDependecies") |
| 108 | + ); |
| 109 | + try { |
| 110 | + execSync( |
| 111 | + `yarn add -D ${devDependencies}`, |
| 112 | + { stdio: "inherit" }, |
| 113 | + (err) => { |
| 114 | + if (err) { |
| 115 | + console.log(err); |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + ); |
| 120 | + } catch (error) { |
| 121 | + console.log(error); |
| 122 | + } |
| 123 | + |
| 124 | + console.log(chalk.green("➤ Mandatory devDependencies installed !")); |
| 125 | + console.log("-------------------------------------------------"); |
| 126 | + |
| 127 | + // OPTIONAL |
| 128 | + const optDependencies = getDependencies(optionalDependencies, true); |
| 129 | + |
| 130 | + console.log(chalk.blue("➤ Updating optional dependencies")); |
| 131 | + |
| 132 | + try { |
| 133 | + execSync( |
| 134 | + `yarn up ${optDependencies}`, |
| 135 | + { stdio: "inherit" }, |
| 136 | + (err) => { |
| 137 | + if (err) { |
| 138 | + console.log(err); |
| 139 | + return; |
| 140 | + } |
| 141 | + } |
| 142 | + ); |
| 143 | + } catch (error) { |
| 144 | + console.log(error); |
| 145 | + } |
| 146 | + |
| 147 | + console.log(chalk.green("➤ Optional dependencies updated !")); |
| 148 | + console.log("-------------------------------------------------"); |
| 149 | + |
| 150 | + await inquirer |
| 151 | + .prompt([ |
| 152 | + { |
| 153 | + type: "confirm", |
| 154 | + name: "confirmElsintUpdate", |
| 155 | + message: |
| 156 | + "➤ Do you want to update your .eslint.json file ? (the old one will be backed up)", |
| 157 | + default: true, |
| 158 | + }, |
| 159 | + ]) |
| 160 | + .then(async (answers) => { |
| 161 | + if (answers.confirmElsintUpdate) { |
| 162 | + await backAndCopyFile(".eslintrc.json"); |
| 163 | + console.log( |
| 164 | + chalk.green( |
| 165 | + "➤ .eslint.json file updated successfully !" |
| 166 | + ) |
| 167 | + ); |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + await inquirer |
| 172 | + .prompt([ |
| 173 | + { |
| 174 | + type: "confirm", |
| 175 | + name: "confirmTsConfig", |
| 176 | + message: |
| 177 | + "➤ Do you want to update your tsconfig.json file ? (the old one will be backed up)", |
| 178 | + default: true, |
| 179 | + }, |
| 180 | + ]) |
| 181 | + .then(async (answers) => { |
| 182 | + if (answers.confirmTsConfig) { |
| 183 | + await backAndCopyFile("tsconfig.json"); |
| 184 | + console.log( |
| 185 | + chalk.green( |
| 186 | + "➤ tsconfig.json file updated successfully !" |
| 187 | + ) |
| 188 | + ); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + console.log( |
| 193 | + chalk.green(` |
| 194 | +------------------------------------------------- |
| 195 | +➤ Your extension has been updated, successfully ! |
| 196 | +➤ You can launch it using yarn start. |
| 197 | +➤ You might encounter some lint issues, you can try to use the command "npx eslint . --fix" to fix it automatically |
| 198 | +-------------------------------------------------`) |
| 199 | + ); |
| 200 | + } |
| 201 | + }); |
| 202 | +} |
| 203 | + |
| 204 | +console.log( |
| 205 | + chalk.cyan(`create-lumapps-extension v${require("./package.json").version}`) |
| 206 | +); |
| 207 | + |
20 | 208 | async function init() {
|
21 |
| - const targetDir = argv._[0] |
22 |
| - if(!targetDir) { |
23 |
| - console.error(chalk.red('Please provide a target folder !')) |
24 |
| - logHelp() |
25 |
| - process.exit(1) |
26 |
| - } |
27 |
| - const cwd = process.cwd() |
28 |
| - const root = path.join(cwd, targetDir) |
29 |
| - const renameFiles = { |
30 |
| - _gitignore: '.gitignore' |
31 |
| - } |
32 |
| - |
33 |
| - const { help, h, template, t, version, v } = argv |
34 |
| - |
35 |
| - if(help || h) { |
36 |
| - logHelp() |
37 |
| - return |
38 |
| - } else if (version || v) { |
39 |
| - // noop, already logged |
40 |
| - return |
41 |
| - } |
42 |
| - |
43 |
| - // Template prompt |
44 |
| - let choosedTemplate = t || template |
45 |
| - if(!choosedTemplate) { |
46 |
| - const choices = [ |
47 |
| - {key: 1, name: 'Widget Extension', value: 'widget-extension'}, |
48 |
| - {key: 2, name: 'Share Extension', value: 'share-extension'}, |
49 |
| - {key: 3, name: 'Empty Extension', value: 'empty-extension'}, |
50 |
| - {key: 4, name: 'Search Extension - BETA', value: 'search-extension'}, |
51 |
| - {key: 5, name: 'Backend Extension - BETA', value: 'backend-extension'} |
52 |
| - ]; |
53 |
| - |
54 |
| - choice = await inquirer.prompt({ |
55 |
| - type: 'list', |
56 |
| - message: 'Choose a template', |
57 |
| - name: 'template', |
58 |
| - choices |
59 |
| - }) |
60 |
| - choosedTemplate = choice.template |
61 |
| - } |
62 |
| - |
63 |
| - console.log("\n--------------------") |
64 |
| - console.log(`\nScaffolding project in ${root}...`) |
65 |
| - |
66 |
| - await fs.ensureDir(root) |
67 |
| - |
68 |
| - const templateDir = path.join( |
69 |
| - __dirname, |
70 |
| - `template-${choosedTemplate}` |
71 |
| - ) |
72 |
| - const write = async (file, content) => { |
73 |
| - const targetPath = renameFiles[file] |
74 |
| - ? path.join(root, renameFiles[file]) |
75 |
| - : path.join(root, file) |
76 |
| - if (content) { |
77 |
| - await fs.writeFile(targetPath, content) |
78 |
| - } else { |
79 |
| - await fs.copy(path.join(templateDir, file), targetPath) |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - |
84 |
| - const files = await fs.readdir(templateDir) |
85 |
| - for (const file of files.filter((f) => f !== 'package.json')) { |
86 |
| - await write(file) |
87 |
| - } |
88 |
| - |
89 |
| - const pkg = require(path.join(templateDir, `package.json`)) |
90 |
| - pkg.name = path.basename(root) |
91 |
| - await write('package.json', JSON.stringify(pkg, null, 2)) |
92 |
| - |
93 |
| - console.log(`\n${chalk.green('Done')}. Now run:\n`) |
94 |
| - console.log(chalk.cyan(` |
| 209 | + const targetDir = argv._[0]; |
| 210 | + const { help, h, template, t, version, v, update, u } = argv; |
| 211 | + |
| 212 | + const userNode = process.version; |
| 213 | + |
| 214 | + if (!userNode.startsWith("v20")) { |
| 215 | + console.log( |
| 216 | + chalk.red( |
| 217 | + `You are using node version ${userNode}, you need to use the lts (V20.x.x)` |
| 218 | + ) |
| 219 | + ); |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + if (!targetDir && (update || u)) { |
| 224 | + updateExtension(); |
| 225 | + return; |
| 226 | + } |
| 227 | + |
| 228 | + if (!targetDir) { |
| 229 | + console.error(chalk.red("Please provide a target folder !")); |
| 230 | + logHelp(); |
| 231 | + process.exit(1); |
| 232 | + } |
| 233 | + const cwd = process.cwd(); |
| 234 | + const root = path.join(cwd, targetDir); |
| 235 | + const renameFiles = { |
| 236 | + _gitignore: ".gitignore", |
| 237 | + _yarnrc_yml: ".yarnrc.yml", |
| 238 | + }; |
| 239 | + |
| 240 | + if (help || h) { |
| 241 | + logHelp(); |
| 242 | + return; |
| 243 | + } else if (version || v) { |
| 244 | + // noop, already logged |
| 245 | + return; |
| 246 | + } |
| 247 | + |
| 248 | + // Template prompt |
| 249 | + let choosedTemplate = t || template; |
| 250 | + if (!choosedTemplate) { |
| 251 | + const choices = [ |
| 252 | + { key: 1, name: "Widget Extension", value: "widget-extension" }, |
| 253 | + { key: 2, name: "Share Extension", value: "share-extension" }, |
| 254 | + { key: 3, name: "Empty Extension", value: "empty-extension" }, |
| 255 | + { key: 4, name: "Search Extension - BETA", value: "search-extension" }, |
| 256 | + { |
| 257 | + key: 5, |
| 258 | + name: "Backend Extension - BETA", |
| 259 | + value: "backend-extension", |
| 260 | + }, |
| 261 | + ]; |
| 262 | + |
| 263 | + choice = await inquirer.prompt({ |
| 264 | + type: "list", |
| 265 | + message: "Choose a template", |
| 266 | + name: "template", |
| 267 | + choices, |
| 268 | + }); |
| 269 | + choosedTemplate = choice.template; |
| 270 | + } |
| 271 | + |
| 272 | + console.log("\n--------------------"); |
| 273 | + console.log(`\nScaffolding project in ${root}...`); |
| 274 | + |
| 275 | + await fs.ensureDir(root); |
| 276 | + |
| 277 | + const templateDir = path.join(__dirname, `template-${choosedTemplate}`); |
| 278 | + const write = async (file, content) => { |
| 279 | + const targetPath = renameFiles[file] |
| 280 | + ? path.join(root, renameFiles[file]) |
| 281 | + : path.join(root, file); |
| 282 | + if (content) { |
| 283 | + await fs.writeFile(targetPath, content); |
| 284 | + } else { |
| 285 | + await fs.copy(path.join(templateDir, file), targetPath); |
| 286 | + } |
| 287 | + }; |
| 288 | + |
| 289 | + const files = await fs.readdir(templateDir); |
| 290 | + for (const file of files.filter((f) => f !== "package.json")) { |
| 291 | + await write(file); |
| 292 | + } |
| 293 | + |
| 294 | + const pkg = require(path.join(templateDir, `package.json`)); |
| 295 | + pkg.name = path.basename(root); |
| 296 | + await write("package.json", JSON.stringify(pkg, null, 2)); |
| 297 | + |
| 298 | + console.log(`\n${chalk.green("Done")}. Now run:\n`); |
| 299 | + console.log( |
| 300 | + chalk.cyan(` |
95 | 301 | ${root !== cwd && `cd ${path.relative(cwd, root)}`}
|
96 | 302 | npm install (or \`yarn\`)
|
97 | 303 | npm run start (or \`yarn start\`)
|
98 |
| - `)) |
99 |
| - console.log() |
| 304 | + `) |
| 305 | + ); |
| 306 | + console.log(); |
100 | 307 | }
|
101 | 308 |
|
102 | 309 | init().catch((e) => {
|
103 |
| - console.error(e) |
104 |
| -}) |
| 310 | + console.error(e); |
| 311 | +}); |
0 commit comments