/
ghetto
executable file
·48 lines (38 loc) · 1.17 KB
/
ghetto
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
#!/usr/bin/env node
'use strict';
let path = require('path')
let fs = require('fs-extra')
let argv = require('yargs').argv
let crypto = require('crypto')
let base64url = require('base64url')
let hogan = require('hogan.js')
switch (argv._[0]) {
case 'init':
let appName = argv._[1]
let templatePath = path.join(__dirname, 'template')
let appPath = path.join(process.cwd(), appName)
fs.mkdirSync(appPath)
// copy files
let files = ['routes', '.babelrc', 'ghetto.js', 'router.js']
for (let fn of files) {
fs.copySync(path.join(templatePath, fn), path.join(appPath, fn))
}
// write config file
let text = fs.readFileSync(path.join(templatePath, 'config.js'), {encoding: 'utf8'})
let context = {
secret: base64url(crypto.randomBytes(48))
}
fs.writeFileSync(
path.join(appPath, 'config.js'),
hogan.compile(text).render(context)
)
console.log(`Created empty ghetto app ${appName}.`)
break
case 'run':
let ghettoFile = argv._[1] || 'ghetto.js'
let ghetto = require(path.join(process.cwd(), ghettoFile))
ghetto.run(argv.port)
break
default:
console.error('No command supplied')
}