-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·220 lines (203 loc) · 7.17 KB
/
cli.js
File metadata and controls
executable file
·220 lines (203 loc) · 7.17 KB
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const util = require('util');
const { spawn } = require('child_process');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const mergeUtil = require('merge-util');
const chalk = require('chalk');
const { mkdirp } = require('mkdirp');
const inquirer = require('inquirer');
const devDependency = {
webpack: ['webpack', 'webpack-cli', 'url-loader', 'mini-svg-data-uri'],
typescript: ['typescript', 'ts-loader', '@turbowarp/types']
};
const cmdline = {
npm: ['npm install --save %s', 'npm install --save-dev %s'],
yarn: ['yarn add %s', 'yarn add -D %s'],
berry: ['yarn add %s', 'yarn add -D %s'],
pnpm: ['pnpm add %s', 'pnpm add -D %s']
};
const scripts = {
plain: {
'build:dist': 'NODE_ENV=production npm run build'
},
yarn: {
'build:dist': 'NODE_ENV=production yarn run build'
},
berry: {
'build:dist': 'NODE_ENV=production yarn run build'
},
pnpm: {
'build:dist': 'NODE_ENV=production pnpm run build'
},
webpack: {
'build': 'webpack --bail'
}
};
const copyFormatFiles = {
plain: [{ from: '.gitignore_', to: '.gitignore' }],
javascript: [
{ from: 'cjs.webpack.config.js', to: 'webpack.config.js' },
{ from: 'index.cjs', to: 'index.js' }
],
typescript: [{ from: 'ts.webpack.config.js', to: 'webpack.config.js' }, 'tsconfig.json', 'images.d.ts', 'index.ts']
};
const copyFiles = {
plain: ['assets'],
typescript: ['typings', 'utils']
};
function runCmd (str) {
process.stdout.write(chalk.cyan(`$ ${str}\n`));
const [cmd, ...arg] = str.split(' ').filter(v => v.length);
const sp = spawn(cmd, arg, { cwd: globalPath, encoding: 'utf-8', stdio: 'inherit', shell: process.platform === 'win32' });
return new Promise((resolve, reject) => {
sp.on('close', code => resolve(code));
});
}
function convertAuthor (author) {
return author.includes(',') ? author.split(',').map(v => v.trim()) : author;
}
function createPackage (types, meta, root) {
let script = scripts.plain;
for (const type of types) script = mergeUtil(script, scripts[type]);
const pkgInfo = {
author: convertAuthor(meta.author),
scripts: script
};
return Promise.all([
fs.promises.writeFile(path.join(root, 'package.json'), JSON.stringify(pkgInfo, null, 4)),
]);
}
async function installDependency (pkg, types) {
const dep = [];
const dev = [];
for (const type of types) {
if (devDependency.hasOwnProperty(type)) dev.push(...devDependency[type]);
}
return runCmd(util.format(cmdline[pkg][1], dev.join(' ')));
}
function formatString (data, fmt) {
for (const key in fmt) data = data.replace(RegExp(`(?<!%)%\\[${key}\\]`, 'g'), fmt[key]);
return data;
}
function copyFileWithFormat (from, to, fmt) {
if (fs.statSync(from).isDirectory()) {
const files = fs.readdirSync(from);
if (!fs.existsSync(to)) fs.mkdirSync(to);
return Promise.all(files.map(file => copyFileWithFormat(path.join(from, file), path.join(to, file), fmt)));
}
return new Promise((resolve, reject) => {
fs.promises.readFile(from, { encoding: 'utf-8' })
.then(data => fs.promises.writeFile(to, formatString(data, fmt), { encoding: 'utf-8' }))
.then(_ => {
process.stdout.write(`Copied ${from} -> ${to}.\n`);
resolve();
});
});
}
function copyFile (from, to) {
if (fs.statSync(from).isDirectory()) {
const files = fs.readdirSync(from);
if (!fs.existsSync(to)) fs.mkdirSync(to);
return Promise.all(files.map(file => copyFile(path.join(from, file), path.join(to, file))));
}
return new Promise((resolve, reject) => {
fs.promises.copyFile(from, to).then(_ => {
process.stdout.write(`Copied ${from} -> ${to}.\n`);
resolve();
});
});
}
function copyFilesToDir (types, root, fmt) {
const pr = [];
for (const type of types) {
if (copyFiles.hasOwnProperty(type)) {
pr.push(copyFiles[type].map(file => (typeof (file) === 'string'
? copyFile(
path.join(path.dirname(__filename), 'template', file),
path.join(root, file)
)
: copyFile(
path.join(path.dirname(__filename), 'template', file.from),
path.join(root, file.to)
))));
}
if (copyFormatFiles.hasOwnProperty(type)) {
pr.push(copyFormatFiles[type].map(file => (typeof (file) === 'string'
? copyFileWithFormat(
path.join(path.dirname(__filename), 'template', file),
path.join(root, file), fmt
)
: copyFileWithFormat(
path.join(path.dirname(__filename), 'template', file.from),
path.join(root, file.to), fmt
))));
}
}
return Promise.all(pr);
}
async function interactive () {
console.log(`
Welcome to use ${chalk.cyan('dango-extension-cli')}!
Version: ${chalk.yellow(require('./package.json').version)}
\n`);
const packageMeta = await inquirer.prompt([{
type: 'input',
name: 'id',
message: 'Extension ID:',
validate: v => (/^[a-z0-9_]+$/.test(v) ? true : 'Unvalid ID.')
}, {
type: 'input',
name: 'name',
message: 'Name:'
}, {
type: 'input',
name: 'author',
message: 'Author:'
}]);
let { lang, pkg, bundler, git } = await inquirer.prompt([{
type: 'list',
name: 'lang',
message: 'Choose your development language:',
choices: ['javascript (commonjs)', 'typescript']
}, {
type: 'list',
name: 'pkg',
message: 'Choose your package manager:',
choices: ['npm', 'yarn', 'berry', 'pnpm']
}, {
type: 'list',
name: 'bundler',
message: 'Choose your bundler:',
choices: ['webpack']
}, {
type: 'confirm',
name: 'git',
message: 'Use git?'
}]);
if (lang === 'javascript (commonjs)') lang = 'javascript';
if (pkg === 'berry') {
await runCmd('yarn set version berry');
await runCmd('yarn set version latest');
}
globalPath = path.resolve(`./${packageMeta.name}`);
mkdirp.sync(`./${packageMeta.name}`);
await createPackage(['plain', pkg, bundler, lang], packageMeta, `./${packageMeta.name}`);
await copyFilesToDir(['plain', pkg, bundler, lang], `./${packageMeta.name}`, { ...packageMeta });
if (git) await runCmd('git init');
await installDependency(pkg, ['plain', pkg, bundler, lang]);
console.log(`Done! Project is initialized in ${chalk.gray(`${packageMeta.name}`)} folder.`);
console.log(`Please do not load index.js directly. please run ${chalk.gray(`${pkg} run build:dist`)}`);
console.log(`and load ${chalk.gray(`dist/extension.js`)}`);
}
yargs(hideBin(process.argv))
.usage('Generate Scratch extension project.')
.options({
version: {
alias: 'v',
description: 'Show version.'
}
});
interactive();