-
Notifications
You must be signed in to change notification settings - Fork 14
/
cli.js
135 lines (116 loc) · 2.54 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const fs = require('fs-extra')
const path = require('path')
const meow = require('meow')
const prompts = require('prompts/dist')
const chalk = require('chalk')
const fetchRepoDir = require('fetch-repo-dir');
const exec = require('shelljs.exec');
const logo = chalk.bold('[Svelte-Docs]')
const log = (...args) => {
console.log(logo, ...args)
}
log.error = (...args) => {
console.log(chalk.red('[ERROR]'), ...args)
}
function npminstall (dir) {
return new Promise((resolve, reject) => {
const child = spawn('npm', [ '--prefix', dir, 'install' ], {
stdio: 'inherit'
})
child.on('close', code => {
if (code !== 0) {
reject()
return
}
resolve()
})
})
}
const themes = [
{ name: 'Default', path: 'default' },
]
const cli = meow(`
Usage
$ npm init svelte-docs
$ npx create-svelte-docs
Options
--name Directory name for docs
-y Create docs without confirmation step
`, {
booleanDefault: undefined,
flags: {
help: {
type: 'boolean',
alias: 'h'
},
version: {
type: 'boolean',
alias: 'v'
},
name: {
type: 'string'
},
confirm: {
type: 'boolean',
alias: 'y'
}
}
})
const form = [
/*
{
type: 'select',
name: 'template',
message: 'Choose a base template',
choices: templates.map(({ name }, i) => ({ title: name, value: i }))
},
*/
{
type: 'text',
name: 'name',
message: 'Choose a name for the docs sources folder',
initial: 'docs_src'
},
{
type: 'confirm',
name: 'confirm',
message: (prev, values) => `Create docs sources in ${values.name}?`,
initial: true
}
]
const run = async opts => {
prompts.inject(opts)
const response = await prompts(form)
if (!response.confirm) {
log('aborted')
process.exit(0)
}
const { name } = response
const theme = themes[response.theme] || themes[0]
log('Creating docs ...')
if (!name) {
log.error('name is required')
process.exit(1)
}
if (!theme) {
log.error('theme not found')
process.exit(1)
}
try{
log('Downloading docs template...');
await fetchRepoDir([
{src: 'alexxnb/svelte-docs/template', dir:name},
]);
log('Installing NPM packages...');
exec(`npm --prefix ${name} install`);
log('Docs created succesfully!')
log(chalk.green(`Go to the ${name} and execute 'npm run dev' command`));
process.exit(0)
}catch(err){
log.error('Failed to create docs')
log.error(err)
process.exit(1)
}
}
run(cli.flags);