This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
executable file
·87 lines (78 loc) · 2.08 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
#!/usr/bin/env node
'use strict'
var opn = require('opn')
var cmd = require('comandante')
var fs = require('fs')
var _ = require('lodash')
var Q = require('kew')
var sink = require('stream-sink')
var run = function (command) {
var split = command.split(' ')
return cmd(split[0], split.slice(1))
}
// var LOADER = (fs.readFileSync(__dirname + '/loader-hash').toString()).trim()
var pack = function (cb) {
var conf = {main: 'index.js'}
var buildsteps = {}
try {
conf = JSON.parse(fs.readFileSync(process.cwd() + '/package.json'))
} catch (e) {
console.log(e)
}
try {
buildsteps = conf['ipfs-web-app'].build
} catch (e) {}
if (typeof buildsteps === 'string') {
buildsteps = [buildsteps]
}
Q.all(_.map(buildsteps, function (command, file) {
console.log(command + ' > ' + file)
var def = Q.defer()
var writestream = fs.createWriteStream(file)
.on('error', cb)
.on('close', function () {
def.resolve(true)
})
run(command).pipe(writestream)
return def.promise
})).then(function () {
run('ipfs add -r dist')
.pipe(sink())
.on('error', cb)
.on('data', function (data) {
var rows = data.split('\n')
if (rows.length > 1) {
var hash = rows[rows.length - 2].split(' ')[1]
cb(null, hash)
} else {
console.log('missing `dist` directory')
process.exit(1)
}
})
})
}
var main = function (argv) {
if (argv[0] === 'run') {
pack(function (err, hash) {
if (err) throw err
var url = 'http://localhost:5001/ipfs/' + hash
opn(url)
})
} else if (argv[0] === 'publish') {
pack(function (err, hash) {
if (err) throw err
console.log('app published as ' + hash)
})
} else {
console.log()
console.log('USAGE:')
console.log()
console.log(' ipfs-app run')
console.log(' - spins up a browser with your app running')
console.log()
console.log(' ipfs-app publish')
console.log(' - builds your app and returns its hash')
console.log()
}
}
main(process.argv.slice(2))