Skip to content

Commit 1d7f52d

Browse files
j-ljharb
andcommitted
[New] add simple CLI util
- 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- <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent 9458928 commit 1d7f52d

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

Diff for: .eslintrc

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
"sort-keys": 0,
2626
},
2727
"overrides": [
28+
{
29+
"files": "bin/**",
30+
"rules": {
31+
"no-process-exit": "off",
32+
},
33+
},
2834
{
2935
"files": "example/**",
3036
"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
"exports": {
1114
".": [
@@ -38,7 +41,7 @@
3841
"prepublishOnly": "safe-publish-latest",
3942
"prepublish": "not-in-publish || npm run prepublishOnly",
4043
"prelint": "eclint check $(git ls-files | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
41-
"lint": "eslint --ext=js,mjs --no-eslintrc -c .eslintrc .",
44+
"lint": "eslint --ext=js,mjs --no-eslintrc -c .eslintrc . 'bin/**'",
4245
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
4346
"tests-only": "tape test/*.js",
4447
"pretest": "npm run lint",
@@ -69,6 +72,7 @@
6972
},
7073
"dependencies": {
7174
"is-core-module": "^2.8.0",
72-
"path-parse": "^1.0.7"
75+
"path-parse": "^1.0.7",
76+
"supports-preserve-symlinks-flag": "^1.0.0"
7377
}
7478
}

0 commit comments

Comments
 (0)