forked from maboiteaspam/npi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·138 lines (109 loc) · 4.18 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
#!/usr/bin/env node
function usage () {/*
Usage
npi [module1 module2]
npi [opts] -- [module1 module2]
npi --explicit Run explicit-deps
npi --changelog Run changelog-maker --simple
npi --config Show config
npi --add [module] Add workflow
npi --default [module] Set default workflow
npi --wd [path] Set the working directory
Options
-v verbose
-h show help
-w Workflow to use
--explicit Invoke rvagg/node-explicit --yes.
--add Add new init workflow.
--default Set the default workflow
--wd Set the working directory
Examples
npi debug minimist @maboiteaspam/set-verbosity @maboiteaspam/show-help
npi -v -- debug minimist multiline
npi -w @maboiteaspam/npi-regular -- debug minimist multiline
npi --explicit
npi -h
*/}
var pkg = require('./package.json')
var argv = require('minimist')(process.argv.slice(2));
var debug = require('@maboiteaspam/set-verbosity')(pkg.name, process.argv);
require('@maboiteaspam/show-help')(usage, process.argv, pkg)
if (argv.wd) process.chdir(argv.wd)
require('@maboiteaspam/console.md')();
var Configstore = require('configstore');
var eventStream = require('@maboiteaspam/event-stream-writer')
var streamMsger = require('@maboiteaspam/stream-messenger')
var messageRouter = require('@maboiteaspam/stream-message-router')
var spawn = require('@maboiteaspam/npi-utils/spawn')
var extract = require('@maboiteaspam/npi-utils/extract')
var pipeSpawned = require('@maboiteaspam/npi-utils/pipespawned')
var print = require('@maboiteaspam/npi-utils/print')
var notBool = require('@maboiteaspam/npi-utils/not-bool')
var setConfig = require('@maboiteaspam/npi-utils/set-config')
var action = 'npi';
if (argv.explicit) action = 'explicit'
else if (argv.changelog) action = 'changelog'
else if (argv.add) action = 'add'
else if (argv.default) action = 'default'
else if (argv.config) action = 'config'
var conf = new Configstore(pkg.name, {workflow: ''});
var workflow = argv.w || argv.workflow || conf.get('workflow') || '@maboiteaspam/npi-regular'
console.log('npi %s using %s', pkg.version, workflow)
var main = streamMsger('main')
try {
var npi = messageRouter('npi')
npi.pipe(require(workflow)(pkg, argv, conf))
npi.on('error', function (err) {
console.error('GOT ERROR %j', err);
});
main.pipe(npi);
}catch(ex){
debug(ex)
console.error('can not load workflow: '+workflow)
}
var explicit = messageRouter('explicit');
explicit
.pipe(spawn('node', [
__dirname+'/node_modules/npm-explicit-deps/bin/npm-explicit-deps.js',
'-y'
]))
var changelog = messageRouter('changelog');
changelog
.pipe(spawn('node', [
__dirname+'/node_modules/changelog-maker/changelog-maker.js', '--simple', '-a'// to improve, remove -a
], {stdio: 'pipe'}))
.on('message', function (d){
if (d.message==='spawn')
d.body.stdout.pipe(require('fs').createWriteStream('CHANGELOG.md')) // create and add stream-file-prepend
})
var add = messageRouter('add');
add.pipe(notBool(argv, 'add', 'a'))
.pipe(spawn('npm', ['i', '--prefix', __dirname, (argv.add || argv.a)]))
.pipe(setConfig(conf, 'workflow', __dirname+'/node_modules/'+(argv.add || argv.a), true))
var setDefault = messageRouter('default');
setDefault.pipe(notBool(argv, 'default', 'd'))
.pipe(setConfig(conf, 'workflow', __dirname+'/node_modules/'+(argv.default || argv.d)))
.pipe(spawn('npm', ['i', '--prefix', __dirname, (argv.default || argv.d)]))
var viewConfig = messageRouter('config');
viewConfig.pipe(print(JSON.stringify(conf.all, null, 4)))
main.pipe(explicit);
main.pipe(changelog);
main.pipe(add);
main.pipe(setDefault);
main.pipe(viewConfig);
var msgListener = eventStream('message', main);
msgListener
.pipe(messageRouter('file'))
.pipe(extract('body', function (file) {
console.mdline("file\t`%s`", file)
}));
msgListener
.pipe(messageRouter('spawn'))
.pipe(extract('cmd', function (cmd) {
console.mdline("spawn\t`%s`", cmd)
}))
.pipe(pipeSpawned());
main.write({
message : action,
body : argv
});