Skip to content

Commit 6f01c78

Browse files
author
wenli.lw
committed
add test & extend path resolve area
1 parent 032c487 commit 6f01c78

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@
44
55
## install
66

7-
> npm install -g node-fire
8-
> fire ./test.js
7+
```
8+
$ npm install -g node-fire
9+
```
10+
11+
## How to use
12+
```
13+
$ fire ./test.js
14+
```
15+
16+
## LICENSE
17+
Released under MIT license

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ module.exports = function (...argv) {
3030

3131
debug(' > loopup script:', script);
3232
script = patternRequire(script);
33+
3334
if (script == null) {
34-
console.error('Not find a valid js file');
35+
console.error('Not find a valid js/json file');
3536
return;
3637
}
38+
39+
debug(`> RUN ${script}\n`);
40+
3741
debug(' > ha, got it', script);
3842

3943
let fn = require(script);

libs/util.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const requireg = require('./requireg/index');
2+
const { existsSync } = require('fs');
23
const {
3-
resolve,
44
extname,
55
join
66
} = require('path');
7+
const resolve = require('resolve').sync;
8+
const cwd = process.cwd();
79

810
const PKG_PREFIX = '';
911

@@ -13,11 +15,12 @@ exports.isObject = function(obj) {
1315

1416
exports.patternRequire = function patternRequire(filepath = '.') {
1517
filepath = String(filepath);
16-
let fullpath = filepath;
17-
if (/^[\.\\\/]/.test(filepath)) {
18+
let fullpath = resolve(filepath, { basedir: cwd });
19+
20+
if (/^[\.\\\/]/.test(filepath) && existsSync(fullpath)) {
1821
return fullpath;
1922
} else if (!(fullpath = requireg.resolve(PKG_PREFIX + filepath))) {
20-
return resolve(filepath);
23+
return;
2124
}
2225
return fullpath;
2326
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fire",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A library for automatically generating command line CLIs from any nodejs project.",
55
"bin": {
66
"fire": "./bin/index.js"

test/_exec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { exec } = require('shelljs');
2+
module.exports = function(script) {
3+
return exec('node ./bin/index ' + script);
4+
}

test/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
const test = require('ava');
2-
test('todo', (t) => {
2+
const exec = require('./_exec');
3+
test('exec', (t) => {
4+
exec('./test/fixtures/test.js');
35
t.pass();
6+
});
7+
8+
test('path autocomplete', (t) => {
9+
exec('./test/fixtures/test');
10+
t.pass();
11+
});
12+
13+
14+
test('json & multi params', (t) => {
15+
exec('./test/fixtures/test.json name');
16+
t.pass();
17+
});
18+
19+
test('test2', (t) => {
20+
exec('./test/fixtures/test2.js');
21+
});
22+
23+
test('test2 with second param', (t) => {
24+
exec('./test/fixtures/test2.js a');
425
});

0 commit comments

Comments
 (0)