forked from mikeal/ipjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·68 lines (57 loc) · 2.07 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
#!/usr/bin/env node
import argv from './src/argv.js'
import build from './src/build.js'
import { spawnSync } from 'child_process'
import { promises as fs } from 'fs'
// import run from './src/run.js'
// import { createServer } from './src/serve.js'
// import seed from './src/seed.js'
// import api from './src/api.js'
// import * as registry from './src/registry/index.js'
const [, , command, ...args] = process.argv
const commands = {}
const help = () => {
if (!command) console.error('Forgot to add a command')
else if (!commands[command]) console.error(`Unknown command "${command}"`)
process.exit(1)
}
const helpflags = ['--help', '-h']
const nodeEnv = { onConsole: console.log, cwd: process.cwd(), stdout: process.stdout }
commands.help = help
// commands.run = args => run(args, nodeEnv)
// Future Demo
/*
commands.registry = async args => {
const subcommand = args.shift()
const cmd = registry[subcommand]
if (!cmd) throw new Error('Unknown registry command')
const opts = await argv(cmd.schema || {})(args)
cmd(opts)
}
commands.serve = async args => {
const filename = args.shift()
const server = await createServer(filename, process.cwd())
await new Promise((resolve, reject) => {
server.listen(8282, e => {
if (e) reject(e)
resolve()
})
})
console.log(`Serving ${filename} on http://localhost:8282`)
}
commands.seed = async args => seed(await _argv(args))
*/
commands.build = async args => build({ ...await argv(build.schema)(args), ...nodeEnv })
commands.publish = async args => {
const cwd = process.cwd() // we do not support positional options for cwd in publish
const packagejson = JSON.parse(await fs.readFile(cwd + '/package.json'))
if (packagejson.scripts.build) {
spawnSync('npm', ['run', 'build'], { stdio: 'inherit' })
} else {
await commands.build(args)
await fs.copyFile(cwd + '/README.md', cwd + '/dist/README.md').catch(() => {})
}
spawnSync('npm', ['publish', cwd + '/dist', '--verbose'], { stdio: 'inherit' })
}
if (!command || !commands[command] || helpflags.includes(command)) help()
commands[command](args)