-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
redrun.js
344 lines (277 loc) Β· 8.9 KB
/
redrun.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
'use strict';
const test = require('tape');
const redrun = require('..');
test('simplest parse', (t) => {
const cmd = 'echo "hello world"';
const result = redrun('two', {
two: cmd
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('simplest parse: name with "."', (t) => {
const cmd = 'bin/redrun.js lint*';
const result = redrun('two', {
two: cmd
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('simplest parse: name with "-"', (t) => {
const cmd = 'babel lib/*.js';
const result = redrun('build', {
'build:js': 'echo \'hello\'',
'build:js-native-full': 'babel lib/*.js',
'build': 'redrun build:js-native-full',
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('simplest parse: "npm install"', (t) => {
const cmd = 'npm install';
const result = redrun('two', {
two: cmd
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('simplest parse: "--"', (t) => {
const cmd = 'echo -- --hello';
const result = redrun('echo--', {
'echo--': cmd
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('simplest parse: &&', (t) => {
const cmd = 'nodemon --exec "bin/iocmd.js" && pwd';
const result = redrun('run', {
run: 'redrun watch:iocmd && pwd',
watcher: 'nodemon --exec',
'watch:iocmd': 'npm run watcher -- bin/iocmd.js'
});
t.equal(result, cmd, 'should return cmd');
t.end();
});
test('infinite loop', (t) => {
const result = redrun('one', {
one: 'redrun one',
});
const expected = 'echo "Inifinite loop detected: one -> one"';
t.equal(result, expected);
t.end();
});
test('infinite loop: one step', (t) => {
const result = redrun('one', {
one: 'npm run two',
two: 'npm run one'
});
const expected = 'echo "Inifinite loop detected: one: npm run two -> npm run one"';
t.equal(result, expected, 'should determine infinite loop');
t.end();
});
test('infinite loop: more steps', (t) => {
const result = redrun('one', {
one: 'npm run two',
two: 'npm run three',
three: 'npm run four',
four: 'npm run one',
});
const expected = 'echo "Inifinite loop detected: one: npm run two -> npm run three -> npm run four -> npm run one"';
t.equal(result, expected, 'should determine infinite loop');
t.end();
});
test('similar name', (t) => {
const cmd = 'redrun.js two';
const result = redrun('one', {
one: 'redrun.js two'
});
t.equal(result, cmd, 'should not try to parse similar name');
t.end();
});
test('parse one level deep', (t) => {
const cmd = 'echo "hello world"';
const result = redrun('one', {
one: 'npm run two',
two: cmd
});
t.equal(result, cmd, 'should parse command one leve deep');
t.end();
});
test('parse arguments', (t) => {
const cmd = 'git "--version"';
const result = redrun('one', {
one: 'npm run two -- --version',
two: 'git'
});
t.equal(result, cmd, 'should parse command one leve deep');
t.end();
});
test('parse reserved names: npm test', (t) => {
const cmd = 'tape test/*.js';
const result = redrun('one', {
one: 'npm run two',
two: 'npm test',
test: cmd
});
t.equal(result, cmd, 'should parse script test');
t.end();
});
test('parse redrun args', (t) => {
const result = redrun('one', {
one: 'npm run two',
two: 'redrun --parallel test lint',
test: 'tape test/*.js',
lint: 'jshint lib test'
});
t.equal(result, 'tape test/*.js & jshint lib test', 'should parse script test');
t.end();
});
test('parse redrun args: "*"', (t) => {
const result = redrun('one', {
one: 'npm run two',
two: 'redrun --parallel lint*',
'lint:jscs': 'jscs test/*.js',
'lint:jshint': 'jshint lib test'
});
t.equal(result, 'jscs test/*.js & jshint lib test', 'should parse script test');
t.end();
});
test('parse redrun args: "."', (t) => {
const result = redrun('one.start', {
'one.start': 'npm run two',
two: 'redrun --parallel lint*',
'lint:jscs': 'jscs test/*.js',
'lint:jshint': 'jshint lib test'
});
t.equal(result, 'jscs test/*.js & jshint lib test', 'should parse script test');
t.end();
});
test('parse redrun args: "--": npm run', (t) => {
const expect = 'nodemon -w lib --exec "nyc tape test.js"';
const result = redrun('watch-coverage', {
watcher: 'nodemon -w lib --exec',
coverage: 'nyc npm test',
'watch-coverage': 'npm run watcher -- "npm run coverage"',
test: 'tape test.js'
});
t.equal(result, expect, 'should add quotes to arguments');
t.end();
});
test('parse redrun args: "--": npm run', (t) => {
const expect = 'nodemon -w lib --exec \'nyc tape test.js\'';
const result = redrun('watch-coverage', {
watcher: 'nodemon -w lib --exec',
coverage: 'nyc npm test',
'watch-coverage': 'npm run watcher -- \'npm run coverage\'',
test: 'tape test.js'
});
t.equal(result, expect, 'should not add quotes when there is one');
t.end();
});
test('parse redrun args: "--": quotes', (t) => {
const expect = 'nodemon -w test -w lib --exec "tape \'lib/**/*.spec.js\'"';
const result = redrun('watch:test', {
"test": "tape 'lib/**/*.spec.js'",
"watch:test": "npm run watcher -- npm test",
"watcher": "nodemon -w test -w lib --exec",
});
t.equal(result, expect, 'should add quotes to arguments');
t.end();
});
test('parse redrun args: "--": redrun', (t) => {
const expect = 'nodemon -w lib --exec "bin/iocmd.js"';
const result = redrun('watch:iocmd', {
'watch:iocmd': 'redrun watcher -- bin/iocmd.js',
'watcher': 'nodemon -w lib --exec',
});
t.equal(result, expect, 'should add quotes to arguments');
t.end();
});
test('parse redrun args: "--": deep npm run', (t) => {
const expect = 'echo "es5" && echo "es6"';
const result = redrun('echo:*', {
'echo': 'echo',
'echo:es5': 'npm run echo -- "es5"',
'echo:es6': 'npm run echo -- "es6"'
});
t.equal(result, expect, 'should add quotes to arguments');
t.end();
});
test('parse redrun args: "--": should not quote "--"', (t) => {
const expect = 'browserify -s nessy "src/nessy.js" "-o" "dist/nessy.es6.js"';
const result = redrun('es6', {
'bundle': 'browserify -s nessy',
'es6:base': 'npm run bundle -- src/nessy.js',
'es6': 'npm run es6:base -- -o dist/nessy.es6.js',
});
t.equal(result, expect, 'should add quotes to arguments');
t.end();
});
test('parse redrun args: unrecognized', (t) => {
const result = redrun('one', {
one: 'npm run two',
two: 'redrun hello --fix',
hello: 'echo'
});
t.equal(result, 'echo --fix is not a redrun option. See \'redrun --help\'', 'should return error');
t.end();
});
test('parse redrun args with ENV set', (t) => {
const result = redrun('good', {
good: 'NODE_ENV=development DEBUG=iocmd* redrun -p t*',
t1: 'tape test/*.js',
t2: 'jshint lib test'
});
t.equal(result, 'NODE_ENV=development DEBUG=iocmd* tape test/*.js & jshint lib test', 'should parse script test');
t.end();
});
test('parse a few redrun scripts', (t) => {
const result = redrun('one', {
one: 'redrun -p two three',
two: 'redrun four five',
three: 'echo \'hello\'',
four: 'jshint lib',
five: 'jscs test'
});
t.equal(result, 'jshint lib && jscs test & echo \'hello\'', 'should parse script test');
t.end();
});
test('parse a few levels deep', (t) => {
const cmd = 'echo "hello world"';
const result = redrun('one', {
one: 'npm run two',
two: 'npm run three',
three: 'npm run four',
four: 'npm run five',
five: 'npm run six',
six: cmd
});
t.equal(result, cmd, 'should parse command a few levels deep');
t.end();
});
test('npx', (t) => {
const cmd = 'npx pug -b src src/pages -o dist';
const body = redrun('build', {
'build': 'npx redrun build:html',
'build:html': cmd,
});
t.equal(body, cmd, 'should count npx');
t.end();
});
test('args: no name', (t) => {
t.throws(redrun, /name should be string!/, 'should throw when no name');
t.end();
});
test('args: name is empty', (t) => {
const fn = () => redrun('');
t.throws(fn, /name should not be empty!/, 'should throw when name is empty');
t.end();
});
test('args: no json', (t) => {
const fn = () => {
redrun('on');
};
t.throws(fn, /json should be object!/, 'should throw when no json');
t.end();
});