Skip to content

Commit 613efb2

Browse files
j-ljharb
andcommitted
[New] add simple CLI util (#94)
- requires it is executed directly, not via `node`, and not required - supports `--preserve-symlinks`, only when node itself supports it - supports `--` to stop further argument parsing - errors if a specifier is omitted Co-authored-by: j- <j@skeoh.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent e538c8a commit 613efb2

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Diff for: .eslintrc

+12
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@
3030
"sort-keys": 0,
3131
},
3232
"overrides": [
33+
{
34+
"files": "bin/**",
35+
"rules": {
36+
"no-process-exit": "off",
37+
},
38+
},
39+
{
40+
"files": "example/**",
41+
"rules": {
42+
"no-console": 0,
43+
},
44+
},
3345
{
3446
"files": "test/resolver/nested_symlinks/mylib/*.js",
3547
"rules": {

Diff for: bin/resolve

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
var path = require('path');
6+
var fs = require('fs');
7+
8+
if (
9+
!process.argv
10+
|| process.argv.length < 2
11+
|| (process.argv[1] !== __filename && fs.statSync(process.argv[1]).ino !== fs.statSync(__filename).ino)
12+
|| (process.env._ && path.resolve(process.env._) !== __filename)
13+
) {
14+
console.error('Error: `resolve` must be run directly as an executable');
15+
process.exit(1);
16+
}
17+
18+
var supportsPreserveSymlinkFlag = require('supports-preserve-symlinks-flag');
19+
20+
var preserveSymlinks = false;
21+
for (var i = 2; i < process.argv.length; i += 1) {
22+
if (process.argv[i].slice(0, 2) === '--') {
23+
if (supportsPreserveSymlinkFlag && process.argv[i] === '--preserve-symlinks') {
24+
preserveSymlinks = true;
25+
} else if (process.argv[i].length > 2) {
26+
console.error('Unknown argument ' + process.argv[i].replace(/[=].*$/, ''));
27+
process.exit(2);
28+
}
29+
process.argv.splice(i, 1);
30+
i -= 1;
31+
if (process.argv[i] === '--') { break; } // eslint-disable-line no-restricted-syntax
32+
}
33+
}
34+
35+
if (process.argv.length < 3) {
36+
console.error('Error: `resolve` expects a specifier');
37+
process.exit(2);
38+
}
39+
40+
var resolve = require('../');
41+
42+
var result = resolve.sync(process.argv[2], {
43+
basedir: process.cwd(),
44+
preserveSymlinks: preserveSymlinks
45+
});
46+
47+
console.log(result);

Diff for: package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"type": "git",
77
"url": "git://github.com/browserify/resolve.git"
88
},
9+
"bin": {
10+
"resolve": "./bin/resolve"
11+
},
912
"main": "index.js",
1013
"keywords": [
1114
"resolve",
@@ -17,7 +20,7 @@
1720
"prepublishOnly": "safe-publish-latest && cp node_modules/is-core-module/core.json ./lib/ ||:",
1821
"prepublish": "not-in-publish || npm run prepublishOnly",
1922
"prelint": "eclint check $(git ls-files | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
20-
"lint": "eslint --ext=js,mjs --no-eslintrc -c .eslintrc .",
23+
"lint": "eslint --ext=js,mjs --no-eslintrc -c .eslintrc . 'bin/**'",
2124
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
2225
"tests-only": "tape test/*.js",
2326
"pretest": "npm run lint",
@@ -48,6 +51,7 @@
4851
},
4952
"dependencies": {
5053
"is-core-module": "^2.8.0",
51-
"path-parse": "^1.0.7"
54+
"path-parse": "^1.0.7",
55+
"supports-preserve-symlinks-flag": "^1.0.0"
5256
}
5357
}

0 commit comments

Comments
 (0)