-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
59 lines (57 loc) · 1.47 KB
/
lib.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
var EOL = require('os').EOL
var resolve = require('path').resolve
var isAbsolute = require('path').isAbsolute
var _resolveFilename = require('module')._resolveFilename
var bgRed = require('chalk').bgRed
var builtindir = require('./vars').builtindir
var globaldir = require('./vars').config.globaldir
exports.stdpipe = function stdpipe(from, to){
from.stdout.pipe(to.stdout)
from.stderr.pipe(to.stderr)
from.on('close', function(code){
if (code) { // code != 0
to.stdout.write(
bgRed('exit with code:', code) + EOL
)
}
})
}
exports.tofile = function tofile(target){
target = target || './'
var file
try {
// builtin path
// --version or --help or --config
// -v or -h or -c
var cmdmap = {
'-c': '--config',
'-v': '--version',
'-h': '--help'
}
target = cmdmap[target] || target
if (target.substr(0, 2) === '--') {
file = resolve(builtindir, target.substr(2))
return _resolveFilename(file)
}
// absolute path
// D:\mydir\mytask or /mydir/mytask
if (isAbsolute(target)) {
file = resolve(target)
return _resolveFilename(file)
}
// relative path
// ./mydir/mytask
if (target.substr(0, 2) === './') {
file = resolve(target)
return _resolveFilename(file)
}
// global path
// mydir/mytask
if (globaldir) {
file = resolve(globaldir, target)
return _resolveFilename(file)
}
} catch(e) {
return null // not found
}
}