-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (96 loc) · 2.76 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
#!/usr/bin/env node
const chalk = require('chalk');
const program = require('commander');
const pkg = require('./package');
const path = require('path');
const fs = require('fs-extra');
const question = require('./src/utils/question');
const generator = require('./src/utils/generator');
let config = {
name: '',
enableBabelImport: '',
ts: '',
preprocessor: '',
pkg: ''
};
process.on("unhandledRejection", err => {
throw err;
});
program
.name(pkg.name)
.version(pkg.version)
.arguments('[projectName]')
.option('-t, --typescript', 'enable typescript', function() {
config.ts = true;
})
.option('-p, --preprocessor [preprocessor]', 'css preprocessor [sass | scss | less]', function(cssPre) {
if(cssPre && !/^(less|sass|scss)$/.test(cssPre)) {
console.log(chalk.red('css preprocessor just supported "sass"、"scss" or "less"'));
process.exit(-1);
}
config.preprocessor = cssPre;
})
.option('-g, --package [package]', 'package manager [yarn | npm]', function(pkg) {
console.log('pkg', pkg);
if (pkg && pkg !== 'yarn' && pkg !== 'npm') {
console.log(chalk.red('package just supported "yarn" or "npm"'));
process.exit(-1);
}
config.pkg = pkg;
})
.action(execute)
.parse(process.argv);
async function execute(projectName) {
config.name = projectName;
if (!config.name) {
config.name = await question('project name?', 'input');
}
if(!/^[a-z\-_@]+$/.test(config.name)) {
console.error(`Project names can only be composed of lowercase letters or -_@`)
config.name = '';
await execute()
return
}
if (!config.preprocessor) {
config.preprocessor = await question('css preprocessor?', 'list', {
choices: [
{
name: '1) sass & scss',
value: 'scss'
},
{
name: '2) less',
value: 'less'
}
]
});
}
if(config.ts === '') {
config.ts = await question('enable typescript?', 'confirm');
}
if(config.enableBabelImport === '') {
config.enableBabelImport = await question('enable babel-plugin-import supported generated outputs?', 'confirm');
}
if (!config.pkg) {
config.pkg = await question('package manager?', 'list', {
choices: [
{
name: '1) yarn',
value: 'yarn'
},
{
name: '2) npm',
value: 'npm'
}
]
});
}
const targetDir = path.resolve(process.cwd(), config.name);
console.log('Creating a new React Component Library in ', chalk.green(targetDir), '.');
console.log();
console.log('config', chalk.green('css preprocessor'), ': ', config.preprocessor);
console.log('config', chalk.green('babel-plugin-import outputs support'), ': ', config.enableBabelImport ? 'enabled' : 'disabled');
console.log('config', chalk.green('package manager'), ': ', config.pkg);
console.log('config', chalk.green('typescript'), ': ', config.ts ? 'enabled' : 'disabled');
await generator(config);
}