-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
472 lines (403 loc) · 13.7 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
var exec = require('child_process').exec
var dedent = require('dedent')
var mkdirp = require('mkdirp')
var path = require('path')
var pump = require('pump')
var fs = require('fs-extra')
exports.mkdir = function (dir, cb) {
mkdirp(dir, function (err) {
if (err) return cb(new Error('Could not create directory ' + dir))
fs.readdir(dir, function (err, files) {
if (err) return cb(new Error('Could not read directory ' + dir))
if (files.length) return cb(new Error('Directory contains files. This might create conflicts.'))
cb()
})
})
}
exports.writePackage = function (dir, cb) {
var filename = path.join(dir, 'package.json')
var name = path.basename(dir)
var file = dedent`
{
"name": "${name}",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "export NODE_ENV=production && bankai build index.js",
"deploy": "npx now -e NODE_ENV=\"production\" --public",
"dev": "bankai start index.js",
"create": "choo-scaffold",
"inspect": "bankai inspect index.js",
"start": "serve ./dist --single",
"test": "standard && npm run test-deps",
"test-deps": "dependency-check . && dependency-check . --extra --no-dev -i tachyons"
}
}
`
write(filename, file, cb)
}
exports.writeIgnore = function (dir, cb) {
var filename = path.join(dir, '.gitignore')
var file = dedent`
node_modules/
.nyc_output/
coverage/
dist/
tmp/
npm-debug.log*
.DS_Store
`
write(filename, file, cb)
}
exports.writeReadme = function (dir, description, cb) {
var filename = path.join(dir, 'README.md')
var name = path.basename(dir)
var file = dedent`
# ${name}
${description}
## Routes
Route | File | Description |
-------------------|--------------------|---------------------------------|
\`/\` | \`views/main.js\` | The main view
\`/*\` | \`views/404.js\` | Display unhandled routes
## Commands
Command | Description |
-----------------------|--------------------------------------------------|
\`$ npm start\` | Start the file server
\`$ npm run dev\` | Start the development server
\`$ npm run deploy\` | Deploy with now
\`$ npm test\` | Lint, validate deps & run tests
\`$ npm run build\` | Compile all files into \`dist/\`
\`$ npm run inspect\` | Inspect the bundle's dependencies
\`$ npm run create\` | Generate a scaffold file
\`$ npm run inspect\` | Inspect the bundle's dependencies
`
write(filename, file, cb)
}
exports.writeIndex = function (dir, cb) {
var filename = path.join(dir, 'index.js')
var file = dedent`
var css = require('sheetify')
var choo = require('choo')
var store = require('./stores/clicks')
css('tachyons')
var app = choo()
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-devtools')())
} else {
app.use(require('choo-service-worker')())
}
app.use(store)
app.route('/', require('./views/main'))
app.route('/*', require('./views/404'))
if (!module.parent) app.mount('body')
else module.exports = app\n
`
write(filename, file, cb)
}
exports.writeServiceWorker = function (dir, cb) {
var filename = path.join(dir, 'sw.js')
var file = dedent`
/* global self */
var VERSION = require('./package.json').version
var URLS = process.env.FILE_LIST
// Respond with cached resources
self.addEventListener('fetch', function (e) {
e.respondWith(self.caches.match(e.request).then(function (request) {
if (request) return request
else return self.fetch(e.request)
}))
})
// Register worker
self.addEventListener('install', function (e) {
e.waitUntil(self.caches.open(VERSION).then(function (cache) {
return cache.addAll(URLS)
}))
})
// Remove outdated resources
self.addEventListener('activate', function (e) {
e.waitUntil(self.caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key, i) {
if (keyList[i] !== VERSION) return self.caches.delete(keyList[i])
}))
}))
})\n
`
write(filename, file, cb)
}
exports.writeManifest = function (dir, description, cb) {
var filename = path.join(dir, 'manifest.json')
var name = path.basename(dir)
var file = dedent`
{
"name": "${name}",
"short_name": "${name}",
"description": "${description}",
"start_url": "/",
"display": "standalone",
"background_color": "#000",
"theme_color": "#000",
"icons": [{
"src": "/assets/icon.png",
"type": "image/png",
"sizes": "512x512"
}]
}
`
write(filename, file, cb)
}
exports.writeNotFoundView = function (dir, cb) {
var dirname = path.join(dir, 'views')
var filename = path.join(dirname, '404.js')
var projectname = path.basename(dir)
var file = dedent`
var html = require('choo/html')
var TITLE = '${projectname} - route not found'
module.exports = view
function view (state, emit) {
if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
return html\`
<body class="sans-serif pa3">
<h1>Route not found.</h1>
<a class="pt2" href="/">Back to main.</a>
</body>
\`
}\n
`
mkdirp(dirname, function (err) {
if (err) return cb(new Error('Could not create directory ' + dirname))
write(filename, file, cb)
})
}
exports.writeMainView = function (dir, cb) {
var dirname = path.join(dir, 'views')
var filename = path.join(dirname, 'main.js')
var projectname = path.basename(dir)
var file = dedent`
var html = require('choo/html')
var TITLE = '${projectname} - main'
module.exports = view
function view (state, emit) {
if (state.title !== TITLE) emit(state.events.DOMTITLECHANGE, TITLE)
return html\`
<body class="code lh-copy">
<main class="pa3 cf center">
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>1.</h2>
<p>
Welcome to your new Choo application.
We're very happy you've made it this far.
</p>
<p>
You're now in control of your own Choo app. The moment you decide to
deploy it, it'll work offline and on any device.
</p>
<br>
</section>
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>2.</h2>
<p>
We've outfitted your project with a small selection of commands to
help you achieve results faster:
</p>
<ul>
<li class="mb3">
<strong>npm start</strong><br>
start your project for local development.
</li>
<li class="mb3">
<strong>npm run build</strong><br>
compile your project for production.
</li>
<li class="mb3">
<strong>npm run inspect</strong><br>
visualize your project's dependencies.
</li>
<li class="mb3">
<strong>npm run create</strong><br>
scaffold a new file.
</li>
</ul>
<br>
</section>
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>3.</h2>
<p>
Your project also comes with a few directories. These names have
special meanings for the build tool, so it's good to know what they
do.
</p>
<ul>
<li class="mb3">
<strong>assets/</strong><br>
Static files that can be served up, such as images and fonts.
</li>
<li class="mb3">
<strong>components/</strong><br>
Reusable fragments that can be composed into views.
</li>
<li class="mb3">
<strong>stores/</strong><br>
Pieces of logic that are shared by multiple components.
</li>
<li class="mb3">
<strong>views/</strong><br>
Combinations of components that are mapped to routes.
</li>
</ul>
<br>
</section>
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>4.</h2>
<p>
So far we've provided you with one base view, <a
href="/oh-no">one fallback view</a>, and one store. This serves
as an example. A place to start from. It's your project now, so
go ahead and delete them once you know how they work.
</p>
<p>Number of clicks stored: \${state.totalClicks}</p>
<button class="dim ph3 ba bw1 pv2 b--black pointer bg-white"
onclick=\${handleClick}>
Emit a click event
</button>
<br><br>
</section>
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>5.</h2>
<p>
To make your development journey more pleasant, we've also
included <a
href="https://github.com/choojs/choo-devtools">devtools</a>. If
you open your browser console, here's a selection of the
commands that are at your disposal:
<ul>
<li class="mb3">
<strong>choo.state</strong><br>
Log the current application state.
</li>
<li class="mb3">
<strong>choo.log</strong><br>
Log the last 150 events received by the event bus.
</li>
<li class="mb3">
<strong>choo.emit</strong><br>
Emit an event inside the application event bus.
</li>
<li class="mb3">
<strong>choo.help</strong><br>
See an overview of all available commands.
</li>
</ul>
</p>
</section>
<section class="fl mw6 w-50-m w-third-l pa3">
<h2>6.</h2>
<p>
And that's about it! Thanks for reading. If you have any
questions, check out the <a href="https://choo.io">docs</a> or reach
out on <a href="https://github.com/choojs/choo">GitHub</a> or <a
href="https://www.irccloud.com/irc/freenode/channel/choo">IRC</a>.
We're online everyday, and always around to help. Happy hacking!
</p>
</section>
</main>
</body>
\`
function handleClick () {
emit('clicks:add', 1)
}
}\n
`
file = file.replace(/\\\$/g, '$')
mkdirp(dirname, function (err) {
if (err) return cb(new Error('Could not create directory ' + dirname))
write(filename, file, cb)
})
}
exports.writeIcon = function (dir, cb) {
var iconPath = path.join(__dirname, 'assets/icon.png')
var dirname = path.join(dir, 'assets')
var filename = path.join(dirname, 'icon.png')
mkdirp(dirname, function (err) {
if (err) return cb(new Error('Could not create directory ' + dirname))
var source = fs.createReadStream(iconPath)
var sink = fs.createWriteStream(filename)
pump(source, sink, function (err) {
if (err) return cb(new Error('Could not write file ' + filename))
cb()
})
})
}
exports.writeStore = function (dir, cb) {
var filename = path.join(dir, 'stores/clicks.js')
var file = dedent`
module.exports = store
function store (state, emitter) {
state.totalClicks = 0
emitter.on('DOMContentLoaded', function () {
emitter.on('clicks:add', function (count) {
state.totalClicks += count
emitter.emit(state.events.RENDER)
})
})
}\n
`
mkdirp(path.dirname(filename), function (err) {
if (err) return cb(err)
write(filename, file, cb)
})
}
exports.install = function (dir, packages, cb) {
packages = packages.join(' ')
var cmd = 'npm install --save --cache-min Infinity --loglevel error ' + packages
var popd = pushd(dir)
exec(cmd, function (err) {
if (err) return cb(new Error(cmd))
popd()
cb()
})
}
exports.devInstall = function (dir, packages, cb) {
packages = packages.join(' ')
var cmd = 'npm install --save-dev --cache-min Infinity --loglevel error ' + packages
var popd = pushd(dir)
exec(cmd, function (err) {
if (err) return cb(new Error(cmd))
popd()
cb()
})
}
exports.createGit = function (dir, message, cb) {
var init = 'git init'
var add = 'git add -A'
var config = 'git config user.email'
var commit = 'git commit -m "' + message + '"'
var popd = pushd(dir)
exec(init, function (err) {
if (err) return cb(new Error(init))
exec(add, function (err) {
if (err) return cb(new Error(add))
exec(config, function (err) {
if (err) return cb(new Error(config))
exec(commit, function (err) {
if (err) return cb(new Error(commit))
popd()
cb()
})
})
})
})
}
function pushd (dir) {
var prev = process.cwd()
process.chdir(dir)
return function popd () {
process.chdir(prev)
}
}
function write (filename, file, cb) {
fs.writeFile(filename, file, function (err) {
if (err) return cb(new Error('Could not write file ' + filename))
cb()
})
}