-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·115 lines (98 loc) · 2.68 KB
/
cli.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
#!/usr/bin/env node
const path = require('path')
const fs = require('fs').promises
const mri = require('mri')
const signale = require('signale')
const generator = require('./src/generator')
const { HELP, printCommands, VERSION } = require('./src/content.js')
const parseConfig = require('./src/parse-config.js')
const { prompt, confirmation } = require('./src/prompts')
const { setup } = require('./src/init.js')
const userDir = process.cwd()
const argv = process.argv.slice(2)
const CLI = mri(argv, {
alias: {
v: 'version',
i: 'init',
t: 'templates',
o: 'output',
fl: 'flat',
l: 'logs',
h: 'help'
},
default: {
logs: true
}
})
async function main (cli) {
if (cli.version) {
signale.log(`
conjurate@${VERSION}`)
process.exit()
}
if (cli.help) {
signale.log(HELP)
process.exit()
}
if (cli.init) {
const response = await prompt({
question: 'Choose a folder to keep your templates',
initial: './conjurate'
})
await setup({ cwd: userDir, response, flags: CLI })
process.exit()
}
const {
error: configFileError,
templates,
templatesSource
} = await parseConfig({ cwd: userDir })
if (configFileError) {
signale.error(configFileError)
process.exit()
}
if (cli.templates) {
signale.log(printCommands(templates))
process.exit()
}
if (cli._.length >= 2) {
const [folder, param] = cli._
if (!Object.keys(templates).includes(folder)) {
signale.error(`Template not found, try one of:
${printCommands(templates)}`
)
process.exit()
}
const dest = cli.output
? path.resolve(userDir, cli.output, param)
: cli.flat
? path.resolve(userDir, templates[folder])
: path.resolve(userDir, templates[folder], param)
const templatesFolder = path.resolve(userDir, templatesSource, folder)
try {
await fs.open(templatesFolder)
} catch (error) {
if (error.code === 'ENOENT') {
signale.error(`${templatesSource}/${folder} template does not exist`)
signale.log(`
The template "${folder}" is configured but you have no corresponding folder inside your ${templatesSource}`)
process.exit()
}
}
try {
const exist = await fs.open(dest)
if (!cli.flat && exist) {
const { confirm } = await confirmation({ question: `Folder "${param}" already exists. Do you want to rewrite it?`, initial: false })
if (!confirm) {
signale.error('Canceled')
process.exit()
}
}
} catch (error) {}
await generator({ dest, templatesFolder, param, flags: CLI })
process.exit()
}
signale.log(HELP)
process.exit()
}
main(CLI)