-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (177 loc) · 5.06 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
#!/usr/bin/env node
// @ts-check
const { Program } = require('3h-cli'),
{ existsSync, promises: fsPromises } = require('fs'),
{ execSync } = require('child_process'),
{ join } = require('path'),
// @ts-ignore
pkgInfo = require('./package.json');
/* Template data slot: __var__ */
const TEMPLATE_PATH = './template',
TEMPLATE_ENCODING = 'utf8';
const templateDirectories = [
'public',
'src',
'test',
];
const templateFiles = [
'src/index.js',
'test/index.html',
'public/index.html',
'public/index.css',
'LICENSE',
'README.md',
'rollup.config.js',
'dev-server.js',
];
/**
* @typedef TemplateData
* @property {string} name
* @property {string} desc
* @property {string} author
* @property {string} year
*/
/**
* @param {TemplateData} data
*/
const renderTemplates = async data => {
await templateDirectories.reduce(
(prevPromise, dir) => prevPromise.then(() => fsPromises.mkdir(dir)),
Promise.resolve()
);
await Promise.all(
templateFiles.map(async path => {
let content = await fsPromises.readFile(
join(__dirname, TEMPLATE_PATH, path),
TEMPLATE_ENCODING
);
Object.entries(data).forEach(([key, value]) => {
content = content.replace(
new RegExp(`__${key}__`, 'g'),
value
);
});
await fsPromises.writeFile(path, content);
})
);
};
const program = new Program(pkgInfo.name, {
title: pkgInfo.description
});
program
.option({
name: '--name',
alias: '-n',
value: '<pkg>',
help: 'The name of the package'
})
.option({
name: '--author',
alias: '-a',
value: '<name>',
help: 'The author of the package'
})
.option({
name: '--desc',
alias: '-d',
value: '<description>',
help: 'The description of the package'
})
.option({
name: '--keywords',
alias: '-k',
value: '<words...>',
help: 'The keywords of the package'
})
.option({
name: '--repo',
alias: '-r',
value: '<repository>',
help: 'The repository of the package'
})
.option({
name: '--no-install',
help: 'Do not install dependencies instantly'
})
.option({
name: '--help',
alias: '-h',
help: 'Show help info'
})
.parse(process.argv)
.then(async args => {
const { options } = args;
if (options.has('--help')) {
return program.help();
}
if (!options.has('--name')) {
throw 'Package name is not provided';
}
const name = options.get('--name')[0];
if (existsSync(name)) {
throw `Path "${name}" already exists`;
}
if (!options.has('--author')) {
throw 'Package author is not provided';
}
const data = {
name,
desc: args.getOption('--desc').join(' ') || `This is ${name}.`,
author: options.get('--author')[0],
year: (new Date()).getFullYear() + '',
};
await fsPromises.mkdir(name);
process.chdir(name);
console.time('time used');
console.log('Generating files...');
await renderTemplates(data);
/**
* it seems npm will somehow convert .gitignore
* into .npmignore when installing the package,
* so this file is added manually
*/
await fsPromises.writeFile('.gitignore', [
'node_modules',
'/public/index.js',
].join('\n') + '\n');
await fsPromises.writeFile('package.json', JSON.stringify(
{
name,
version: '0.1.0',
description: data.desc,
author: data.author,
license: 'MIT',
private: true,
scripts: {
build: 'rollup -c',
test: 'node dev-server',
},
repository: args.getOption('--repo')[0]
|| `${data.author}/${name}`,
keywords: args.getOption('--keywords'),
devDependencies: {
'@babel/core': '^7.10.0',
'@babel/preset-env': '^7.10.0',
herver: '^0.6.0',
rollup: '^2.26.0',
'@rollup/plugin-babel': '^5.2.0',
'rollup-plugin-terser': '^7.0.0',
terser: '^5.2.0',
},
},
null, // replacer
2, // indent
));
if (options.has('--no-install')) {
console.log('Dependencies not installed.');
} else {
console.log('Installing dev dependencies...');
execSync('npm i', { stdio: 'inherit' });
}
console.log('Finished!');
console.timeEnd('time used');
})
.catch(err => {
console.error(err);
process.exit(1);
});