Skip to content

Commit 032c487

Browse files
author
wenli.lw
committed
add test
1 parent ed477c8 commit 032c487

File tree

15 files changed

+3278
-0
lines changed

15 files changed

+3278
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
.history
40+
.vscode
41+
**/.DS_Store

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# node-fire
2+
3+
> Node Fire is a library for creating command line interfaces (CLIs) from absolutely any Node object.
4+
5+
## install
6+
7+
> npm install -g node-fire
8+
> fire ./test.js

bin/index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env node
2+
3+
const debug = require('debug')('node-fire:bin');
4+
const join = require('path').join;
5+
6+
const colors = require('colors');
7+
global.Promise = require('bluebird'); //replace default promise for debug
8+
require('shelljs/global'); //for convien
9+
10+
const pkg = require(join(__dirname, '../package.json'));
11+
12+
const argv = require('yargs')
13+
.help('help')
14+
.describe('separator', 'Sets the separator to commit array params, default is , ')
15+
.describe('verbose', "Show more info when excute a script")
16+
.argv;
17+
18+
const SPEARATOR = String(argv.separator || ',');
19+
20+
const parseArgs = function (argv) {
21+
let normalArgs = [];
22+
if (!argv._.length) normalArgs.push(undefined); //第一个参数只能为字符串
23+
for (let arg of argv._) {
24+
if (~String.prototype.indexOf.call(arg, SPEARATOR)) {
25+
normalArgs.push(arg.toString().split(SPEARATOR).map((i) => i.trim()));
26+
} else {
27+
normalArgs.push(arg);
28+
}
29+
}
30+
normalArgs.push(argv);
31+
if (argv.verbose) {
32+
process.env['DEBUG'] = argv.verbose == 'true' ? '*' : argv.verbose;
33+
}
34+
35+
if (argv.version) {
36+
console.log(`\n${pkg.name} version: ${pkg.version}\n`.yellow);
37+
return;
38+
}
39+
return normalArgs;
40+
};
41+
42+
const start = function (argv) {
43+
let args = parseArgs(argv);
44+
if (args == null) return;
45+
let opts = args[args.length - 1];
46+
debug('args', args);
47+
return require('../index.js').apply(opts, args);
48+
}
49+
50+
const ret = start(argv);
51+
if (ret != null) {
52+
if (typeof ret === 'object' && ret.then) {
53+
ret.then(function (ret) {
54+
if (ret != null) {
55+
console.log(ret);
56+
}
57+
}).catch(function (e) {
58+
console.error(e.red);
59+
})
60+
} else {
61+
console.log(ret);
62+
}
63+
}

index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict'
2+
const {
3+
join
4+
} = require('path');
5+
6+
const {
7+
isObject,
8+
isEmpty,
9+
patternRequire,
10+
getFuncLength
11+
} = require('./libs/util');
12+
13+
const co = require('co');
14+
const debug = require('debug')('node-fire');
15+
16+
module.exports = function (...argv) {
17+
debug('input args', argv);
18+
this.cwd = process.cwd();
19+
try {
20+
this.package = require(join(this.cwd, 'package.json'))
21+
} catch (e) {}
22+
23+
let script = argv[0],
24+
args = argv.slice(1);
25+
26+
if (!script) {
27+
console.error(`Not find script ${argv[0]}`);
28+
return;
29+
}
30+
31+
debug(' > loopup script:', script);
32+
script = patternRequire(script);
33+
if (script == null) {
34+
console.error('Not find a valid js file');
35+
return;
36+
}
37+
debug(' > ha, got it', script);
38+
39+
let fn = require(script);
40+
41+
if (typeof fn !== 'function') {
42+
if (isEmpty(fn)) {
43+
//may be a script empty or not have any exports
44+
debug('Not exports any function');
45+
return;
46+
} else if (isObject(fn)) {
47+
if (args.length < 2) {
48+
console.log('Please exports a default function or use second param to chose a function!\nFunction List: \n', fn);
49+
return;
50+
}
51+
let subFnName;
52+
if ((subFnName = args[0]) in fn) {
53+
fn = fn[subFnName];
54+
args = args.slice(1);
55+
if (typeof fn !== 'function') {
56+
return fn;
57+
}
58+
} else {
59+
console.log(script, 'Not exists a object named', subFnName);
60+
return;
61+
}
62+
}
63+
}
64+
65+
debug('fn', fn);
66+
67+
let fnLen = getFuncLength(fn);
68+
69+
//if input arguments long than accept arguments
70+
debug('func.length', fnLen, args.length);
71+
72+
if (args.length < fnLen) {
73+
let opts = args.pop();
74+
debug('options === this', opts === this);
75+
if (opts === this) {
76+
args = args.concat(new Array(fnLen - args.length - 1));
77+
}
78+
args.push(opts);
79+
}
80+
81+
let wrap = co.wrap(fn);
82+
debug('fn', fn);
83+
debug('wrap', wrap);
84+
if (typeof wrap !== 'function') return wrap;
85+
return wrap.apply(this, args);
86+
}

libs/.DS_Store

6 KB
Binary file not shown.

libs/requireg/LICENSE

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright 2013 Tomas Aparicio
2+
All rights reserved.
3+
4+
Permission is hereby granted, free of charge, to any person
5+
obtaining a copy of this software and associated documentation
6+
files (the "Software"), to deal in the Software without
7+
restriction, including without limitation the rights to use,
8+
copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the
10+
Software is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
OTHER DEALINGS IN THE SOFTWARE.

libs/requireg/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var { resolvers, getPrefix } = require('./resolvers');
4+
5+
'use strict';
6+
7+
module.exports = require2
8+
9+
function require2(module) {
10+
try {
11+
return require(resolve(module))
12+
} catch (e) {
13+
throw new Error("Cannot find global module '"+ module +"'")
14+
}
15+
}
16+
17+
require2.resolve = resolve
18+
19+
require2.globalize = function () {
20+
global.require2 = require2
21+
}
22+
23+
require2.getPrefix = getPrefix;
24+
25+
function resolve(module, dirname) {
26+
var i, resolver, modulePath
27+
28+
for (var resolver in resolvers) {
29+
if (modulePath = resolvers[resolver](module, dirname)) {
30+
break
31+
}
32+
}
33+
return modulePath
34+
}

libs/requireg/resolvers.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var fs = require('fs')
2+
var path = require('path')
3+
var resolve = require('resolve').sync
4+
var rc = require('rc')
5+
var isWin32 = process.platform === 'win32'
6+
7+
'use strict';
8+
9+
// resolvers
10+
exports.resolvers = {
11+
nativeResolve,
12+
nodePathResolve,
13+
userHomeResolve,
14+
nodeModulesResolve,
15+
prefixResolve,
16+
execPathResolve
17+
};
18+
exports.getPrefix = getPrefix;
19+
20+
function resolveFn(module, basePath, dirname) {
21+
try {
22+
return resolve(module, {
23+
basedir: path.join(basePath, dirname ||  '')
24+
})
25+
} catch (e) {}
26+
}
27+
28+
// resolve using native require() function
29+
// if NODE_PATH is defined, a global module should be natively resolved
30+
function nativeResolve(module, dirname) {
31+
try {
32+
return require.resolve(module, dirname)
33+
} catch (e) {}
34+
}
35+
36+
// See: http://nodejs.org/docs/latest/api/modules.html#modules_loading_from_the_global_folders
37+
// required?
38+
function nodePathResolve(module, dirname) {
39+
var i, l, modulePath
40+
var nodePath = process.env.NODE_PATH
41+
42+
if (!nodePath) {
43+
return
44+
}
45+
46+
nodePath = nodePath.split(path.delimiter).map(function (nodepath) {
47+
return path.normalize(nodepath)
48+
})
49+
50+
for (i = 0, l = nodePath.length; i < l; i += 1)  {
51+
if (modulePath = resolveFn(module, dirname || nodePath[i])) {
52+
break
53+
}
54+
}
55+
56+
return modulePath
57+
}
58+
59+
function userHomeResolve(module) {
60+
var i, l, modulePath
61+
var homePath = isWin32 ? process.env['USERPROFILE'] : process.env['HOME']
62+
63+
var paths = [
64+
'node_modules',
65+
'node_libraries',
66+
'node_packages'
67+
]
68+
69+
for (i = 0, l = paths.length; i < l; i += 1) {
70+
if (modulePath = resolveFn(module, homePath, paths[i])) {
71+
break;
72+
}
73+
}
74+
75+
return modulePath
76+
}
77+
78+
function getPrefix() {
79+
var modulePath, dirname
80+
var prefix = rc('npm').prefix
81+
82+
if (isWin32) {
83+
prefix = prefix || path.join(process.env.APPDATA, 'npm')
84+
dirname = prefix
85+
} else {
86+
prefix = prefix || path.join(path.dirname(process.execPath), '..')
87+
dirname = path.join(prefix, 'lib')
88+
}
89+
90+
dirname = path.join(dirname, 'node_modules');
91+
return dirname;
92+
}
93+
94+
// See: https://npmjs.org/doc/files/npm-folders.html#prefix-Configuration
95+
// it uses execPath to discover the default prefix on *nix and %APPDATA% on Windows
96+
function prefixResolve(module) {
97+
return resolveFn(module, getPrefix())
98+
}
99+
100+
// Resolves packages using the node installation path
101+
// Useful for resolving global packages such as npm when the prefix has been overriden by the user
102+
function execPathResolve(module) {
103+
var modulePath, dirname
104+
var execPath = path.dirname(process.execPath)
105+
106+
if (isWin32) {
107+
dirname = execPath
108+
} else {
109+
dirname = path.join(execPath, '..', 'lib')
110+
}
111+
112+
dirname = path.join(dirname, 'node_modules')
113+
modulePath = resolveFn(module, dirname)
114+
115+
return modulePath
116+
}
117+
118+
function nodeModulesResolve(module) {
119+
var i, l, modulePath
120+
var nodeModules = process.env['NODE_MODULES']
121+
122+
if (typeof nodeModules === 'string') {
123+
nodeModules = nodeModules.split(path.delimiter)
124+
for (i = 0, l = nodeModules.length; i < l; i += 1) {
125+
if (modulePath = resolveFn(module, nodeModules[i])) {
126+
break;
127+
}
128+
}
129+
}
130+
return modulePath
131+
}

0 commit comments

Comments
 (0)