-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.ts
466 lines (408 loc) · 11.9 KB
/
main.ts
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// The nodejs executable for generating typescript library
// The program is working off a config given by consumer. From the config,
// the program will composite corresponding components and generate files for
// expected typescript project
// Third party libraries
import fs from 'fs';
import inquirer from 'inquirer';
import path from 'path';
import shell from 'shelljs';
// Interfaces
export interface IBaseConfig {
libName: string;
libDesc: string;
hasDemo: boolean;
hasTest: boolean;
hasTsLint: boolean;
isExecutable: boolean;
author: string;
license: 'MIT' | 'GNU' | 'Apache' | 'other'; // TODO: add more licence options
testRunner: 'jest'; // TODO: 'mocha' | 'jest'
dependencyManager: 'yarn'; // TODO: 'npm' | 'yarn'
}
export interface IBundlerConfig extends IBaseConfig {
compileMethod: 'bundler';
bundler: 'webpack'; // TODO: 'parcel' | 'browserify' | 'webpack'
}
export interface ITscConfig extends IBaseConfig {
compileMethod: 'tsc';
tsCompileModule: 'commonjs' | 'es6'; // 'none' | 'commonjs' | 'amd' | 'system' | 'umd' | 'es2015' | 'es6' | 'ESNext';
}
export type IConfig = IBundlerConfig | ITscConfig;
// Constants
const CWD = process.env.NODE_ENV === 'production' ? process.cwd() : path.resolve(__dirname, './test-temp');
const PACKAGE_JSON_PATH = path.resolve(CWD, './package.json');
const TEMPLATES_PATH = path.resolve(__dirname, process.env.NODE_ENV === 'production' ? '../templates' : './templates');
// Utils
export function copyTemplatesFile(
filePath: string,
options: { rename?: string, sedMap?: {[key: string]: string} } = {}) {
const {
rename,
sedMap,
} = options;
const dirPath = path.resolve(filePath, '../');
let destFilePath = path.resolve(CWD, filePath);
if (rename) {
destFilePath = path.resolve(dirPath, `./${rename}`);
}
shell.mkdir('-p', path.resolve(CWD, dirPath));
shell.cp(path.resolve(TEMPLATES_PATH, filePath), destFilePath);
if (sedMap) {
for (const sedKey in sedMap) {
if (sedMap.hasOwnProperty(sedKey)) {
shell.sed('-i', new RegExp(`{{${sedKey}}}`, 'g'), sedMap[sedKey], destFilePath);
}
}
}
}
export function addBundlerPackageJson(runtimeConfig: IBundlerConfig) {
const {
bundler,
dependencyManager,
libName,
libDesc,
author,
license,
} = runtimeConfig;
switch (bundler) {
case 'webpack':
copyTemplatesFile('./package.json.webpack.tmpl', {
rename: 'package.json',
sedMap: {
dependencyManager,
libName,
libDesc,
author,
license,
},
});
break;
default:
throw new Error(`Invalid bundler: ${bundler}`);
}
}
export function addTscPackageJson(runtimeConfig: ITscConfig) {
const {
dependencyManager,
libName,
libDesc,
author,
license,
tsCompileModule,
} = runtimeConfig;
const packageJsonPath = path.resolve(CWD, './package.json');
copyTemplatesFile('./package.json.tsc.tmpl', {
rename: 'package.json',
sedMap: {
dependencyManager,
libName,
libDesc,
author,
license,
},
});
switch (tsCompileModule) {
case 'commonjs':
shell.sed('-i', '{{tscScript}}', 'tsc', packageJsonPath);
break;
case 'es6':
shell.sed('-i', '{{tscScript}}', 'tsc -m es6 --outDir dist', packageJsonPath);
break;
default:
throw new Error(`Invalid tsc compile module: ${tsCompileModule}`);
}
}
/**
* Add package.json file based on config
*/
export function addPackageJson(runtimeConfig: IConfig) {
switch (runtimeConfig.compileMethod) {
case 'bundler':
addBundlerPackageJson(runtimeConfig);
break;
case 'tsc':
addTscPackageJson(runtimeConfig);
break;
default:
throw new Error('Invalid compile method');
}
}
export function addSourceCode(runtimeConfig: IConfig) {
// const destEntryFileDir = path.resolve(CWD, './src');
// const destEntryFilePath = path.resolve(destEntryFileDir, `./${runtimeConfig.libName}.ts`);
// shell.mkdir('-p', destEntryFileDir);
// shell.cp(path.resolve(TEMPLATES_PATH, './src/myLib.ts.tmpl'), destEntryFilePath);
copyTemplatesFile('./src/myLib.ts.tmpl', {
rename: `${runtimeConfig.libName}.ts`,
});
}
export function addTestCodeForBundler(runtimeConfig: IBundlerConfig) {
copyTemplatesFile('./tests/bundler.webpack.test.ts.tmpl', {
rename: 'bundler.webpack.test.ts',
sedMap: {
libName: runtimeConfig.libName,
},
});
}
export function addTestCodeForTsc(runtimeConfig: ITscConfig) {
const {
libName,
dependencyManager,
} = runtimeConfig;
switch (runtimeConfig.tsCompileModule) {
case 'commonjs':
copyTemplatesFile('./tests/tsc.commonjs.test.ts.tmpl', {
rename: 'tsc.test.ts',
sedMap: {
libName,
dependencyManager,
},
});
break;
case 'es6':
copyTemplatesFile('./tests/tsc.esm.test.ts.tmpl', {
rename: 'tsc.test.ts',
sedMap: {
libName,
dependencyManager,
},
});
break;
}
}
export function addTestCode(runtimeConfig: IConfig) {
// Add test for entry file
const {
libName,
} = runtimeConfig;
const destEntryFileDir = path.resolve(CWD, './src');
const destEntryTestFileDir = destEntryFileDir;
const destEntryFilePath = path.resolve(destEntryFileDir, `./${libName}.ts`);
const fromTestFileToEntryFilePath = `./${path.relative(destEntryTestFileDir, destEntryFilePath).slice(0, -3)}`;
copyTemplatesFile('./src/myLib.test.ts.tmpl', {
rename: `${runtimeConfig.libName}.test.ts`,
sedMap: {
entryFilePath: fromTestFileToEntryFilePath,
},
});
// Add test for bundler or tsc
switch (runtimeConfig.compileMethod) {
case 'bundler':
addTestCodeForBundler(runtimeConfig);
break;
case 'tsc':
addTestCodeForTsc(runtimeConfig);
break;
}
}
export function addBundler(runtimeConfig: IBundlerConfig) {
const libName = runtimeConfig.libName;
switch (runtimeConfig.bundler) {
case 'webpack':
copyTemplatesFile('./webpack.config.ts.tmpl', {
rename: 'webpack.config.ts',
sedMap: {
libName,
entryFilePath: `./src/${libName}.ts`,
},
});
break;
default:
throw new Error('Invalid bundler');
}
}
export function getPackageJson() {
return JSON.parse(fs.readFileSync(PACKAGE_JSON_PATH, {
encoding: 'utf8',
}));
}
export function setPackageJson(packageJson: object) {
fs.writeFileSync(PACKAGE_JSON_PATH, JSON.stringify(packageJson, null, 2));
}
export function updatePackageJson(callback: (packageJson: any) => object) {
setPackageJson(callback(getPackageJson()));
}
export async function askRuntimeConfig(): Promise<IConfig> {
return inquirer
.prompt([{
type: 'input',
name: 'libName',
message: 'What\'s the name of your library?',
}, {
type: 'input',
name: 'libDesc',
message: (answers: any) => `Great! What does ${answers.libName} do?`,
}, {
type: 'list',
name: 'license',
message: 'Should we use MIT license?',
choices: ['MIT'], // 'MIT', 'GNU', 'Apache', 'other'
default: 'MIT',
}, {
type: 'confirm',
name: 'isExecutable',
message: (answers: any) => `Is ${answers.libName} a NodeJS executable?`,
}, {
type: 'list',
name: 'dependencyManager',
message: 'How do you want to manage code dependencies?',
choices: ['yarn', 'npm'],
default: 'yarn',
}, {
type: 'list',
name: 'compileMethod',
message: 'How do you want to compile your codes?',
choices: ['bundler', 'tsc'],
default: 'bundler',
}, {
type: 'list',
name: 'bundler',
message: 'Let\'s bundle the code! Which code bundler do you want to use?',
choices: ['webpack'], // 'parcel' | 'browserify' | 'webpack'
default: 'webpack',
when: (answers: any) => answers.compileMethod === 'bundler',
}, {
type: 'list',
name: 'tsCompileModule',
message: 'Let\'s use typescirpt compiler! Which consumer you are targeting?',
choices: ['commonjs', 'es6'], // 'parcel' | 'browserify' | 'webpack'
default: 'commonjs',
when: (answers: any) => answers.compileMethod === 'tsc',
}, {
type: 'confirm',
name: 'hasTest',
message: 'Should we include tests for the library?',
}, {
type: 'list',
name: 'testRunner',
message: 'Sure! Which test runner should we use?',
choices: ['jest'], // 'jest', 'mocha'
when: (answers: any) => !!answers.hasTest,
}, {
type: 'confirm',
name: 'hasTsLint',
message: 'Should we include tslint for the library?',
}, {
type: 'confirm',
name: 'hasDemo',
message: 'Should we include a demo directory for the library?',
}, {
type: 'input',
name: 'author',
message: 'Sounds good. Now, who are you?',
default: 'Awesome Anonymous <[email protected]>',
}]);
}
export async function main(runtimeConfig?: IConfig) {
if (!runtimeConfig) {
runtimeConfig = await askRuntimeConfig();
}
if (process.env.NODE_ENV === 'development') {
console.log('Create typescript library with runtime config');
console.log('---------------------------------------------');
console.log(JSON.stringify(runtimeConfig, null, 2));
}
const {
author,
libName,
libDesc,
dependencyManager,
} = runtimeConfig;
addPackageJson(runtimeConfig);
addSourceCode(runtimeConfig);
// Add tests
if (runtimeConfig.hasTest) {
addTestCode(runtimeConfig);
}
// Add bundler config files
if (runtimeConfig.compileMethod === 'bundler') {
addBundler(runtimeConfig);
}
if (runtimeConfig.hasTsLint) {
copyTemplatesFile('./tslint.json.tmpl', {
rename: 'tslint.json',
});
}
if (runtimeConfig.hasDemo) {
copyTemplatesFile('./demo/demo.ts.tmpl', {
rename: 'demo.ts',
sedMap: {
libName,
},
});
copyTemplatesFile('./demo/demo.test.ts.tmpl', {
rename: 'demo.test.ts',
});
copyTemplatesFile('./demo/jest.config.js.tmpl', {
rename: 'jest.config.js',
});
copyTemplatesFile('./demo/package.json.tmpl', {
rename: 'package.json',
sedMap: {
libName,
dependencyManager,
},
});
// Update package.json test script
console.log('PACKAGE_JSON_PATH', PACKAGE_JSON_PATH);
shell.sed('-i', '{{testScript}}', `jest && ${dependencyManager} run demo:test`, PACKAGE_JSON_PATH);
shell.sed('-i', '{{testCoverageScript}}',
`jest --coverage && ${dependencyManager} run demo:test`, PACKAGE_JSON_PATH);
updatePackageJson((packageJson) => {
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts['demo:test'] =
`${dependencyManager} run build && cd demo && ` +
`${dependencyManager} install && ${dependencyManager} run add-self && ${dependencyManager} test`;
return packageJson;
});
} else {
shell.sed('-i', '{{testScript}}', 'jest', PACKAGE_JSON_PATH);
shell.sed('-i', '{{testCoverageScript}}', 'jest --coverage', PACKAGE_JSON_PATH);
}
switch (runtimeConfig.license) {
case 'MIT':
copyTemplatesFile('./LICENSE.mit.tmpl', {
rename: 'LICENSE',
sedMap: {
author,
},
});
break;
}
if (runtimeConfig.isExecutable) {
updatePackageJson((packageJson) => {
packageJson.bin = {};
packageJson.bin[packageJson.name] = `./dist/${packageJson.name}.min.js`;
return packageJson;
});
}
copyTemplatesFile('./tsconfig.json.tmpl', {
rename: 'tsconfig.json',
});
copyTemplatesFile('./.gitignore.tmpl', {
rename: '.gitignore',
});
copyTemplatesFile('./.npmignore.tmpl', {
rename: '.npmignore',
});
copyTemplatesFile('./.travis.yml.tmpl', {
rename: '.travis.yml',
sedMap: {
dependencyManager,
},
});
copyTemplatesFile('./jest.config.js.tmpl', {
rename: 'jest.config.js',
});
copyTemplatesFile('./nodemon.json.tmpl', {
rename: 'nodemon.json',
});
copyTemplatesFile('./README.md.tmpl', {
rename: 'README.md',
sedMap: {
libName,
libDesc,
},
});
}