-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·72 lines (63 loc) · 1.69 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
#!/usr/bin/env node --harmony
const path = require('path')
const inquirer = require('inquirer')
const createBlueprint = require('./createBlueprint')
const chalk = require('chalk')
const program = require('commander')
program
.arguments('<destination>')
.option('-n, --name <name>', 'The name of the created component')
.option(
'-t, --type <type>',
'The name of the component type defined in the config'
)
.action(async (destination) => {
const configs = findConfigs() || {}
let { name, type } = program
if (!name || !type) {
;({ name, type } = await getInquirer(configs))
}
try {
const createdFiles = await createBlueprint(
type,
name,
destination,
configs
)
success(destination, name, type, createdFiles)
} catch (e) {
process.stderr.write(chalk.red(e.stack + '\n'))
}
})
.parse(process.argv)
function getInquirer(config) {
const questions = [
{
type: 'list',
name: 'type',
message: 'Type of the blueprint?',
choices: Object.keys(config || {}),
},
{
type: 'input',
name: 'name',
message: 'Name for the blueprint?',
},
]
return inquirer.prompt(questions)
}
function findConfigs() {
return require(path.resolve(process.cwd(), 'package.json'))
.createFromBlueprint
}
function success(destination, name, type) {
const blueprintPath = path.resolve(process.cwd(), destination, name)
const successMessage = [
`Created ${chalk.bgWhite.black.bold(' ' + name + ' ')}`,
`Type ${chalk.bold(type)}`,
`Path ${chalk.bold(blueprintPath)}`,
]
successMessage.forEach((line) => {
process.stdout.write(line + '\n')
})
}