-
Notifications
You must be signed in to change notification settings - Fork 36
/
bin.js
executable file
·244 lines (218 loc) · 6.48 KB
/
bin.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
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
#!/usr/bin/env node
var mapLimit = require('async-collection/map-limit')
var series = require('async-collection/series')
var ansi = require('ansi-escape-sequences')
var inquirer = require('inquirer')
var minimist = require('minimist')
var dedent = require('dedent')
var rimraf = require('rimraf')
var path = require('path')
var lib = require('./')
var TRAIN = '🚂🚋🚋'
var USAGE = `
$ ${clr('create-choo-app', 'bold')} ${clr('<project-directory>', 'green')} [options]
Options:
-h, --help print usage
-v, --version print version
-q, --quiet don't output any logs
Examples:
Create a new Choo application
${clr('$ create-choo-app', 'cyan')}
Running into trouble? Feel free to file an issue:
${clr('https://github.com/choojs/create-choo-app/issues/new', 'cyan')}
Do you enjoy using this software? Become a backer:
${clr('https://opencollective.com/choo', 'cyan')}
`.replace(/\n$/, '').replace(/^\n/, '')
var NODIR = `
Please specify the project directory:
${clr('$ create-choo-app', 'cyan')} ${clr('<project-directory>', 'green')}
For example:
${clr('$ create-choo-app', 'cyan')} ${clr('my-choo-app', 'green')}
Run ${clr('create-choo-app --help', 'cyan')} to see all options.
`.replace(/\n$/, '').replace(/^\n/, '')
var argv = minimist(process.argv.slice(2), {
alias: {
help: 'h',
quiet: 'q',
version: 'v'
},
boolean: [
'help',
'quiet',
'version'
]
})
;(function main (argv) {
var dir = argv._[0]
var description = argv._[1]
if (argv.help) {
console.log(USAGE)
} else if (argv.version) {
console.log(require('./package.json').version)
} else if (!dir) {
console.log(NODIR)
process.exit(1)
} else {
create(path.join(process.cwd(), dir), description, argv)
}
})(argv)
async function create (dir, description, argv) {
var written = []
var cmds = [
function (done) {
print('Creating a new Choo app in ' + clr(dir, 'green') + '.\n')
lib.mkdir(dir, done)
},
function (done) {
var filename = 'package.json'
printFile(filename)
written.push(path.join(dir, filename))
lib.writePackage(dir, done)
},
function (done) {
print('\nInstalling packages, this might take a couple of minutes.')
written.push(path.join(dir, 'node_modules'))
var pkgs = [
'choo',
'choo-service-worker',
'sheetify',
'tachyons'
]
var msg = clrInstall(pkgs)
print('Installing ' + msg + '…')
lib.install(dir, pkgs, done)
},
function (done) {
var pkgs = [
'bankai',
'choo-devtools',
'choo-scaffold',
'dependency-check',
'standard'
]
var msg = clrInstall(pkgs)
print('Installing ' + msg + '…')
lib.devInstall(dir, pkgs, done)
},
function (done) {
print('')
var filename = '.gitignore'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeIgnore(dir, done)
},
function (done) {
var filename = 'README.md'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeReadme(dir, description, done)
},
function (done) {
var filename = 'index.js'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeIndex(dir, done)
},
function (done) {
var filename = 'stores/clicks.js'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeStore(dir, done)
},
function (done) {
var filename = 'sw.js'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeServiceWorker(dir, done)
},
function (done) {
var filename = 'views/main.js'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeMainView(dir, done)
},
function (done) {
var filename = 'views/404.js'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeNotFoundView(dir, done)
},
function (done) {
var filename = 'manifest.json'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeManifest(dir, description, done)
},
function (done) {
var filename = 'assets/icon.png'
printFile(filename)
written.push(path.join(dir, filename))
lib.writeIcon(dir, done)
},
function (done) {
var message = '.'
print('\nInitializing ' + clr('git', 'cyan'))
written.push(path.join(dir, '.git'))
lib.createGit(dir, message, done)
}
]
if (!description) {
var answers = await inquirer.prompt([
{
type: 'input',
name: 'description',
message: "What's the purpose of your project? (This is used in the readme & web app manifest.)\n>"
}
])
description = answers.description
}
series(cmds, function (err) {
if (err) {
print('\nAborting installation. The following error occured:')
print(' ' + clr(err.message, 'red') + '\n')
mapLimit(written, 1, cleanFile, function (err) {
if (err) throw err
console.log('Cleanup completed, please try again sometime.')
process.exit(1)
})
} else {
var msg = dedent`
App created in ${clr(dir, 'green')}.
${clr('All done, good job!', 'magenta')} ${TRAIN}
The following commands are available:
${clr('npm start', 'cyan')} Start the development server
${clr('npm test', 'cyan')} Lint, validate deps & run tests
${clr('npm run build', 'cyan')} Compile all files to ${clr('dist/', 'green')}
${clr('npm run inspect', 'cyan')} Inspect the bundle dependencies
Do you enjoy using this software? Become a backer:
${clr('https://opencollective.com/choo', 'cyan')}
`.replace(/\n$/g, '')
print('\n' + msg)
}
})
function print (val) {
if (!argv.quiet) console.log(val)
}
function printFile (filename) {
print('Creating file ' + clr(filename, 'cyan') + '…')
}
}
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}
function clrInstall (pkgs) {
return pkgs.reduce(function (str, pkg, i) {
pkg = clr(pkg, 'cyan')
if (i === pkgs.length - 1) {
return str + pkg
} else if (i === pkgs.length - 2) {
return str + pkg + ', and '
} else {
return str + pkg + ', '
}
}, '')
}
function cleanFile (file, cb) {
console.log('Deleting generated file… ' + clr(path.basename(file), 'cyan'))
rimraf(file, cb)
}