load-from-cwd-or-npm
Advanced tools
Comparing version 2.0.0 to 2.0.1
55
index.js
@@ -9,2 +9,3 @@ /*! | ||
const inspectWithKind = require('inspect-with-kind'); | ||
const npmCliDir = require('npm-cli-dir'); | ||
@@ -16,10 +17,13 @@ const optional = require('optional'); | ||
const MODULE_ID_ERROR = 'Expected a string of npm package name, for example `glob`, `graceful-fs`'; | ||
module.exports = function loadFromCwdOrNpm(moduleId, compareFn) { | ||
if (typeof moduleId !== 'string') { | ||
return Promise.reject(new TypeError( | ||
String(moduleId) + ' is not a string. Expected a string of npm package name ' + | ||
'(e.g. `glob`, `graceful-fs`).' | ||
)); | ||
return Promise.reject(new TypeError(`${MODULE_ID_ERROR}, but got ${inspectWithKind(moduleId)}.`)); | ||
} | ||
if (moduleId.length === 0) { | ||
return Promise.reject(new Error(`${MODULE_ID_ERROR}, but got '' (empty string).`)); | ||
} | ||
if (moduleId.charAt(0) === '@') { | ||
@@ -30,6 +34,5 @@ return new Promise(resolve => resolve(require(moduleId))); | ||
if (moduleId.indexOf('/') !== -1 || moduleId.indexOf('\\') !== -1) { | ||
return Promise.reject(new Error( | ||
'"' + moduleId + '" includes path separator(s). The string must be an npm package name ' + | ||
'(e.g. `request`, `semver`).' | ||
)); | ||
return Promise.reject(new Error(`"${ | ||
moduleId | ||
}" includes path separator(s). The string must be an npm package name, for example \`request\` \`semver\`.`)); | ||
} | ||
@@ -39,7 +42,11 @@ | ||
return Promise.reject(new TypeError( | ||
String(compareFn) + ' is not a function. Expected a function to compare two package versions.' | ||
`Expected a function to compare two package versions, but got ${ | ||
inspectWithKind(compareFn) | ||
}.` | ||
)); | ||
} | ||
const tasks = [resolveFromNpm(moduleId + '/package.json')]; | ||
const modulePkgId = `${moduleId}/package.json`; | ||
const tasks = [resolveFromNpm(modulePkgId)]; | ||
if (!compareFn) { | ||
@@ -58,7 +65,5 @@ tasks.push(resolveSemverFromNpm); | ||
if (compareFn( | ||
(optional(moduleId + '/package.json') || {version: '0.0.0-0'}).version, | ||
require(packageJsonPathFromNpm).version | ||
)) { | ||
if (compareFn((optional(modulePkgId) || {version: '0.0.0-0'}).version, require(packageJsonPathFromNpm).version)) { | ||
const result = optional(moduleId); | ||
if (result !== null) { | ||
@@ -75,14 +80,12 @@ return result; | ||
return npmCliDir().then(npmCliDirPath => { | ||
const err = new Error( | ||
'Failed to load "' + | ||
moduleId + | ||
'" module from the current working directory (' + | ||
cwd + | ||
'). ' + | ||
'Then tried to load "' + | ||
moduleId + | ||
'" from the npm CLI directory (' + | ||
npmCliDirPath + | ||
'), but it also failed.' | ||
); | ||
const err = new Error(`Failed to load "${ | ||
moduleId | ||
}" module from the current working directory (${ | ||
cwd | ||
}). Then tried to load "${ | ||
moduleId | ||
}" from the npm CLI directory (${ | ||
npmCliDirPath | ||
}), but it also failed. Install "${moduleId}" and try again. (\`npm install ${moduleId}\`)`); | ||
err.code = 'MODULE_NOT_FOUND'; | ||
@@ -89,0 +92,0 @@ |
{ | ||
"name": "load-from-cwd-or-npm", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Load a module from either CWD or npm CLI directory", | ||
@@ -8,5 +8,4 @@ "repository": "shinnn/load-from-cwd-or-npm", | ||
"scripts": { | ||
"pretest": "eslint --fix --config @shinnn/node index.js test.js", | ||
"test": "node --strong_mode --throw-deprecation test.js | tap-spec", | ||
"coverage": "node --strong_mode --throw-deprecation node_modules/.bin/istanbul cover test.js" | ||
"pretest": "eslint --fix --format=codeframe index.js test.js", | ||
"test": "nyc --reporter=html --reporter=text node test.js" | ||
}, | ||
@@ -34,15 +33,20 @@ "license": "MIT", | ||
"dependencies": { | ||
"npm-cli-dir": "^2.0.0", | ||
"inspect-with-kind": "^1.0.1", | ||
"npm-cli-dir": "^2.0.1", | ||
"optional": "^0.1.3", | ||
"resolve-from-npm": "^2.0.0" | ||
"resolve-from-npm": "^2.0.2" | ||
}, | ||
"devDependencies": { | ||
"@shinnn/eslint-config-node": "^2.0.0", | ||
"eslint": "^2.13.1", | ||
"istanbul": "^0.4.4", | ||
"@shinnn/eslint-config-node": "^3.0.0", | ||
"eslint": "^3.19.0", | ||
"nyc": "^11.0.1", | ||
"osenv": "0.0.2", | ||
"request": "*", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.6.0" | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.2.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@shinnn/node" | ||
} | ||
} |
@@ -7,3 +7,2 @@ # load-from-cwd-or-npm | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/load-from-cwd-or-npm.svg)](https://coveralls.io/github/shinnn/load-from-cwd-or-npm?branch=master) | ||
[![Dependency Status](https://david-dm.org/shinnn/load-from-cwd-or-npm.svg)](https://david-dm.org/shinnn/load-from-cwd-or-npm) | ||
@@ -15,11 +14,9 @@ Load a module from either CWD or [`npm` CLI](https://github.com/npm/npm) directory | ||
// $ npm ls npm-regustry-client | ||
// $ npm ls validate-npm-package-name | ||
// > └── (empty) | ||
loadFromCwdOrNpm('npm-regustry-client').then(RegClient => { | ||
const client = new RegClient(); | ||
client.get('https://registry.npmjs.org/npm', {timeout: 1000}, (error, data, raw, res) => { | ||
// ... | ||
}); | ||
}); | ||
(async () => { | ||
require('validate-npm-package-name'); // throws a `MODULE_NOT_FOUND` error | ||
const RegistryClient = await loadFromCwdOrNpm('validate-npm-package-name'); // doesn't throw | ||
})(); | ||
``` | ||
@@ -43,5 +40,5 @@ | ||
*moduleId*: `String` (a module ID without path separators (`/`, `\\`)) | ||
*moduleId*: `string` (a module ID without path separators (`/`, `\\`)) | ||
*compareFn*: `Function` (a function to compare two package versions) | ||
Return: `Object` (a [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise) instance) | ||
Return: `Promise<any>` | ||
@@ -107,4 +104,4 @@ It loads a module with the given module ID from either of these two directories: | ||
Copyright (c) 2015 - 2016 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
Copyright (c) 2015 - 2017 [Shinnosuke Watanabe](https://github.com/shinnn) | ||
Licensed under [the MIT License](./LICENSE). |
Sorry, the diff of this file is not supported yet
8794
4
8
72
104
+ Addedinspect-with-kind@^1.0.1
Updatednpm-cli-dir@^2.0.1
Updatedresolve-from-npm@^2.0.2