From 15717af4475878965e37642fb11db97ac5574f07 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sun, 10 Mar 2019 15:15:18 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#4) --- .editorconfig | 2 +- .gitattributes | 3 +- .gitignore | 1 + .npmrc | 1 + .travis.yml | 4 +-- index.d.ts | 52 ++++++++++++++++++++++++++++++ index.js | 51 ++++++++++++++++------------- index.test-d.ts | 10 ++++++ package.json | 85 ++++++++++++++++++++++++------------------------- readme.md | 4 +++ test.js | 8 ++--- 11 files changed, 146 insertions(+), 75 deletions(-) create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 97519af..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: node_js node_js: - - '6' - - '4' + - '10' + - '8' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..2692c6e --- /dev/null +++ b/index.d.ts @@ -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; diff --git a/index.js b/index.js index 56f31e4..a4816e2 100644 --- a/index.js +++ b/index.js @@ -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; }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..ead2821 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,10 @@ +import {expectType} from 'tsd-check'; +import npmRunPath, {ProcessEnv} from '.'; + +expectType(npmRunPath()); +expectType(npmRunPath({cwd: '/foo'})); +expectType(npmRunPath({path: '/usr/local/bin'})); + +expectType(npmRunPath.env()); +expectType(npmRunPath.env({cwd: '/foo'})); +expectType(npmRunPath.env({env: process.env})); diff --git a/package.json b/package.json index 3c27504..55ccd2e 100644 --- a/package.json +++ b/package.json @@ -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": "sindresorhus@gmail.com", - "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": "sindresorhus@gmail.com", + "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" + } } diff --git a/readme.md b/readme.md index 4ff4722..2fe8b03 100644 --- a/readme.md +++ b/readme.md @@ -35,6 +35,8 @@ childProcess.execFileSync('foo', { ### npmRunPath([options]) +Returns the augmented path string. + #### options ##### cwd @@ -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 diff --git a/test.js b/test.js index a67676e..c85dec7 100644 --- a/test.js +++ b/test.js @@ -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') ); });