forked from Truonghau/runner-haucn6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (82 loc) · 2.86 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
/**
* @author Stanislav Kalashnik <darkpark.main@gmail.com>
* @license GNU GENERAL PUBLIC LICENSE Version 3
*/
import chokidar from 'chokidar';
import Runner from 'cjs-runner';
import log from 'runner-logger';
const runner = new Runner();
// should be removed after rework!
global.DEVELOP = true;
// global configuration for tasks
runner.config = {};
runner.alias = function ( alias, taskId ) {
runner.tasks[alias] = runner.tasks[taskId];
};
// @todo: move to runner-plugin-watcher ???
runner.watch = function ( glob, task ) {
console.assert(arguments.length === 2, 'wrong arguments number');
console.assert(typeof task === 'string' || typeof task === 'function', 'task should be a string or a function');
console.assert(!!task, 'task is empty');
const taskId = typeof task === 'string' ? task : task.name || '<noname>';
const handler = name => {
log.info('changed: %s run: %s', log.colors.magenta(name), log.colors.cyan(taskId));
runner.run(task);
};
return chokidar.watch(glob, runner.watch.config)
.on('change', handler)
.on('unlink', handler)
.on('add', handler);
};
runner.watch.config = {
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 50
}
};
// @todo: move to runner-plugin-keystroke
runner.keystrokes = {};
runner.keystroke = function ( id, rule ) {
//const key = [];
if ( rule && runner.tasks[id] ) {
//rule = rule.toLowerCase().split('+');
// rule.map(function ( part ) {
// return part.trim();
// });
//
// if ( rule.indexOf('ctrl') !== -1 ) { key.push('ctrl'); }
// if ( rule.indexOf('alt') !== -1 ) { key.push('alt'); }
// if ( rule.indexOf('shift') !== -1 ) { key.push('shift'); }
//
// key.push('shift');
runner.keystrokes[rule] = id;
}
};
process.stdin.on('keypress', ( str, key ) => {
let keystroke = [];
key.ctrl && keystroke.push('ctrl');
key.meta && keystroke.push('alt');
key.shift && keystroke.push('shift');
keystroke.push(key.name);
keystroke = keystroke.join('+');
if ( runner.keystrokes[keystroke] ) {
runner.run(runner.keystrokes[keystroke]);
}
});
runner.addListener('start', event => {
log.info('starting %s ...', log.colors.cyan(event.id));
});
runner.addListener('finish', event => {
log.info('finished %s after %s ms', log.colors.cyan(event.id), log.colors.magenta(event.time));
});
runner.addListener('error', event => {
if ( event.code === 404 ) {
log.fail('task %s is missing', log.colors.bold(event.id));
}
});
// possible conflict detection
// if ( path.dirname(path.dirname(path.dirname(process.mainModule.filename))) !== path.dirname(__dirname) ) {
// log.fail('both global and local node-runner instances are used at the same time!');
// }
// public
export default runner;