From e550ce1dd34af3a6e1bcff7bbfa7b3a44013bc49 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 11 Mar 2019 05:39:16 +0000 Subject: [PATCH] Require Node.js 8, add TypeScript definition (#7) --- .gitattributes | 3 +-- .travis.yml | 2 +- index.d.ts | 12 ++++++++++++ index.js | 7 ++++++- index.test-d.ts | 4 ++++ package.json | 12 +++++++----- readme.md | 2 +- test.js | 4 ++-- 8 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts 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/.travis.yml b/.travis.yml index e0cc348..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: node_js node_js: + - '10' - '8' - - '6' diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..c3b2c52 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,12 @@ +/** + * Check if your code is running as an [npm](https://docs.npmjs.com/misc/scripts) or [yarn](https://yarnpkg.com/lang/en/docs/cli/run/) script. + * + * @example + * + * import {isNpm} from 'isNpm'; + * + * if (isNpm) { + * console.log('Running as a npm script!'); + * } + */ +export const isNpm: boolean; diff --git a/index.js b/index.js index b5f3c27..b6293b1 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,9 @@ 'use strict'; -module.exports = 'npm_config_username' in process.env || + +const isNpm = + 'npm_config_username' in process.env || 'npm_package_name' in process.env || 'npm_config_heading' in process.env; + +// TODO: This named export should be replaced by a default export as soon as we move to ES modules +exports.isNpm = isNpm; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..aaeff42 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,4 @@ +import {expectType} from 'tsd-check'; +import {isNpm} from '.'; + +expectType(isNpm); diff --git a/package.json b/package.json index 9ba665f..4958315 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd-check" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "npm", @@ -29,7 +30,8 @@ "script" ], "devDependencies": { - "ava": "*", - "xo": "*" + "ava": "^1.3.1", + "tsd-check": "^0.3.0", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 12cd3c0..abe616e 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ $ npm install is-npm ## Usage ```js -const isNpm = require('is-npm'); +const {isNpm} = require('is-npm'); console.log(isNpm); ``` diff --git a/test.js b/test.js index c138b01..7b0cf7a 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ import test from 'ava'; -import m from '.'; +import {isNpm} from '.'; test('main', t => { - t.true(m); + t.true(isNpm); });