Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 10, 2019
1 parent 4d95631 commit 15717af
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
52 changes: 52 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export interface RunPathOptions {
/**
* Working directory.
*
* @default process.cwd()
*/
readonly cwd?: string;

/**
* PATH to be appended. Default: [`PATH`](https://github.com/sindresorhus/path-key).
*
* Set it to an empty string to exclude the default PATH.
*/
readonly path?: string;
}

export interface ProcessEnv {
[key: string]: string | undefined;
}

export interface EnvOptions {
/**
* Working directory.
*
* @default process.cwd()
*/
readonly cwd?: string;

/**
* Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
*/
readonly env?: ProcessEnv;
}

/**
* Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
*/
declare const npmRunPath: {
/**
* Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
*
* @returns The augmented path string.
*/
(options?: RunPathOptions): string;

/**
* @returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
*/
env(options?: EnvOptions): ProcessEnv;
};

export default npmRunPath;
51 changes: 28 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,43 @@
const path = require('path');
const pathKey = require('path-key');

module.exports = opts => {
opts = Object.assign({
const npmRunPath = options => {
options = {
cwd: process.cwd(),
path: process.env[pathKey()]
}, opts);

let prev;
let pth = path.resolve(opts.cwd);
const ret = [];

while (prev !== pth) {
ret.push(path.join(pth, 'node_modules/.bin'));
prev = pth;
pth = path.resolve(pth, '..');
path: process.env[pathKey()],
...options
};

let previous;
let cwdPath = path.resolve(options.cwd);
const result = [];

while (previous !== cwdPath) {
result.push(path.join(cwdPath, 'node_modules/.bin'));
previous = cwdPath;
cwdPath = path.resolve(cwdPath, '..');
}

// ensure the running `node` binary is used
ret.push(path.dirname(process.execPath));
// Ensure the running `node` binary is used
result.push(path.dirname(process.execPath));

return ret.concat(opts.path).join(path.delimiter);
return result.concat(options.path).join(path.delimiter);
};

module.exports.env = opts => {
opts = Object.assign({
env: process.env
}, opts);
module.exports = npmRunPath;
module.exports.default = npmRunPath;

module.exports.env = options => {
options = {
env: process.env,
...options
};

const env = Object.assign({}, opts.env);
const env = {...options.env};
const path = pathKey({env});

opts.path = env[path];
env[path] = module.exports(opts);
options.path = env[path];
env[path] = module.exports(options);

return env;
};
10 changes: 10 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {expectType} from 'tsd-check';
import npmRunPath, {ProcessEnv} from '.';

expectType<string>(npmRunPath());
expectType<string>(npmRunPath({cwd: '/foo'}));
expectType<string>(npmRunPath({path: '/usr/local/bin'}));

expectType<ProcessEnv>(npmRunPath.env());
expectType<ProcessEnv>(npmRunPath.env({cwd: '/foo'}));
expectType<ProcessEnv>(npmRunPath.env({env: process.env}));
85 changes: 42 additions & 43 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
{
"name": "npm-run-path",
"version": "2.0.2",
"description": "Get your PATH prepended with locally installed binaries",
"license": "MIT",
"repository": "sindresorhus/npm-run-path",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"npm",
"run",
"path",
"package",
"bin",
"binary",
"binaries",
"script",
"cli",
"command-line",
"execute",
"executable"
],
"dependencies": {
"path-key": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "npm-run-path",
"version": "2.0.2",
"description": "Get your PATH prepended with locally installed binaries",
"license": "MIT",
"repository": "sindresorhus/npm-run-path",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"npm",
"run",
"path",
"package",
"bin",
"binary",
"binaries",
"script",
"cli",
"command-line",
"execute",
"executable"
],
"dependencies": {
"path-key": "^2.0.0"
},
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ childProcess.execFileSync('foo', {

### npmRunPath([options])

Returns the augmented path string.

#### options

##### cwd
Expand All @@ -54,6 +56,8 @@ Set it to an empty string to exclude the default PATH.

### npmRunPath.env([options])

The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.

#### options

##### cwd
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import path from 'path';
import test from 'ava';
import m from './';
import npmRunPath from '.';

test(t => {
test('npmRunPath', t => {
t.is(
m({path: ''}).split(path.delimiter)[0],
npmRunPath({path: ''}).split(path.delimiter)[0],
path.join(__dirname, 'node_modules/.bin')
);
t.is(
m.env({env: {PATH: 'foo'}}).PATH.split(path.delimiter)[0],
npmRunPath.env({env: {PATH: 'foo'}}).PATH.split(path.delimiter)[0],
path.join(__dirname, 'node_modules/.bin')
);
});

0 comments on commit 15717af

Please sign in to comment.