Socket
Socket
Sign inDemoInstall

load-from-cwd-or-npm

Package Overview
Dependencies
14
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.1 to 2.2.2

128

index.js

@@ -18,89 +18,83 @@ /*!

function createModuleNotFoundRejection(moduleId, cwd, npmCliDirPath) {
const error = new Error(`Failed to load "${
moduleId
}" module from the current working directory (${
cwd
}).${npmCliDirPath ? ` Then tried to load "${
moduleId
}" from the npm CLI directory (${
npmCliDirPath
}), but it also failed.` : ''} Install "${moduleId}" and try again. (\`npm install ${moduleId}\`)`);
const error = new Error(`Failed to load "${
moduleId
}" module from the current working directory (${
cwd
}).${npmCliDirPath ? ` Then tried to load "${
moduleId
}" from the npm CLI directory (${
npmCliDirPath
}), but it also failed.` : ''} Install "${moduleId}" and try again. (\`npm install ${moduleId}\`)`);
error.code = 'MODULE_NOT_FOUND';
error.id = moduleId;
error.triedPaths = {cwd};
error.code = 'MODULE_NOT_FOUND';
error.id = moduleId;
error.triedPaths = {cwd};
if (npmCliDirPath) {
error.triedPaths.npm = npmCliDirPath;
error.npmVersion = require(path.join(npmCliDirPath, './package.json')).version;
}
if (npmCliDirPath) {
error.triedPaths.npm = npmCliDirPath;
error.npmVersion = require(path.join(npmCliDirPath, './package.json')).version;
}
return Promise.reject(error);
return Promise.reject(error);
}
module.exports = function loadFromCwdOrNpm(moduleId, compareFn) {
if (typeof moduleId !== 'string') {
return Promise.reject(new TypeError(`${MODULE_ID_ERROR}, but got ${inspectWithKind(moduleId)}.`));
}
if (typeof moduleId !== 'string') {
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.length === 0) {
return Promise.reject(new Error(`${MODULE_ID_ERROR}, but got '' (empty string).`));
}
if (moduleId.charAt(0) === '@') {
return new Promise(resolve => resolve(require(moduleId)));
}
if (moduleId.charAt(0) === '@') {
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, for example \`request\` \`semver\`.`));
}
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, for example \`request\` \`semver\`.`));
}
if (compareFn && typeof compareFn !== 'function') {
return Promise.reject(new TypeError(
`Expected a function to compare two package versions, but got ${
inspectWithKind(compareFn)
}.`
));
}
if (compareFn && typeof compareFn !== 'function') {
return Promise.reject(new TypeError(`Expected a function to compare two package versions, but got ${
inspectWithKind(compareFn)
}.`));
}
const modulePkgId = `${moduleId}/package.json`;
const tasks = [resolveFromNpm(modulePkgId)];
const modulePkgId = `${moduleId}/package.json`;
const tasks = [resolveFromNpm(modulePkgId)];
if (!compareFn) {
tasks.push(resolveSemverFromNpm);
}
if (!compareFn) {
tasks.push(resolveSemverFromNpm);
}
const cwd = process.cwd();
const cwd = process.cwd();
return Promise.all(tasks).then(function chooseOneModuleFromCwdAndNpm(results) {
const packageJsonPathFromNpm = results[0];
return Promise.all(tasks).then(function chooseOneModuleFromCwdAndNpm(results) {
const packageJsonPathFromNpm = results[0];
if (!compareFn) {
compareFn = require(results[1]).gte;
}
if (!compareFn) {
compareFn = require(results[1]).gte;
}
if (compareFn((optional(modulePkgId) || {version: '0.0.0-0'}).version, require(packageJsonPathFromNpm).version)) {
const result = optional(moduleId);
if (compareFn((optional(modulePkgId) || {version: '0.0.0-0'}).version, require(packageJsonPathFromNpm).version)) {
const result = optional(moduleId);
if (result !== null) {
return result;
}
}
if (result !== null) {
return result;
}
}
return require(path.dirname(packageJsonPathFromNpm));
}, function fallbackToCwd() {
const result = optional(moduleId);
return require(path.dirname(packageJsonPathFromNpm));
}, function fallbackToCwd() {
const result = optional(moduleId);
if (result === null) {
return npmCliDir().then(npmCliDirPath => {
return createModuleNotFoundRejection(moduleId, cwd, npmCliDirPath);
}, () => {
return createModuleNotFoundRejection(moduleId, cwd, null);
});
}
if (result === null) {
return npmCliDir().then(npmCliDirPath => createModuleNotFoundRejection(moduleId, cwd, npmCliDirPath), () => createModuleNotFoundRejection(moduleId, cwd, null));
}
return result;
});
return result;
});
};
{
"name": "load-from-cwd-or-npm",
"version": "2.2.1",
"description": "Load a module from either CWD or npm CLI directory",
"repository": "shinnn/load-from-cwd-or-npm",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --format=codeframe index.js test.js",
"test": "nyc --reporter=html --reporter=text node test.js"
},
"license": "MIT",
"files": [
"index.js"
],
"keywords": [
"load",
"resolve",
"require",
"fallback",
"failsafe",
"cwd",
"npm",
"module",
"compare",
"comparison",
"find",
"promise",
"promises",
"then"
],
"dependencies": {
"inspect-with-kind": "^1.0.2",
"npm-cli-dir": "^2.0.1",
"optional": "^0.1.3",
"resolve-from-npm": "^2.0.2"
},
"devDependencies": {
"@shinnn/eslint-config-node": "^4.0.0",
"clear-module": "^2.1.0",
"eslint": "^4.1.1",
"nyc": "^11.0.2",
"osenv": "0.0.2",
"path-key": "^2.0.1",
"request": "*",
"semver": "^5.3.0",
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
"write-json-file": "^2.2.0"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
"name": "load-from-cwd-or-npm",
"version": "2.2.2",
"description": "Load a module from either CWD or npm CLI directory",
"repository": "shinnn/load-from-cwd-or-npm",
"author": "Shinnosuke Watanabe (https://github.com/shinnn)",
"scripts": {
"pretest": "eslint --fix --format=codeframe .",
"test": "nyc --reporter=html --reporter=text node test.js"
},
"license": "ISC",
"files": [
"index.js"
],
"keywords": [
"load",
"resolve",
"require",
"fallback",
"failsafe",
"cwd",
"npm",
"module",
"compare",
"comparison",
"find",
"promise",
"promises",
"then"
],
"dependencies": {
"inspect-with-kind": "^1.0.4",
"npm-cli-dir": "^2.0.1",
"optional": "^0.1.4",
"resolve-from-npm": "^2.0.4"
},
"devDependencies": {
"@shinnn/eslint-config-node": "^5.0.0",
"clear-module": "^2.1.0",
"eslint": "^4.19.1",
"nyc": "^12.0.2",
"osenv": "0.0.2",
"path-key": "^2.0.1",
"request": "*",
"semver": "^5.5.0",
"tape": "^4.9.1",
"write-json-file": "^2.3.0"
},
"eslintConfig": {
"extends": "@shinnn/node"
}
}
# load-from-cwd-or-npm
[![NPM version](https://img.shields.io/npm/v/load-from-cwd-or-npm.svg)](https://www.npmjs.com/package/load-from-cwd-or-npm)
[![npm version](https://img.shields.io/npm/v/load-from-cwd-or-npm.svg)](https://www.npmjs.com/package/load-from-cwd-or-npm)
[![Build Status](https://travis-ci.org/shinnn/load-from-cwd-or-npm.svg?branch=master)](https://travis-ci.org/shinnn/load-from-cwd-or-npm)

@@ -24,3 +24,3 @@ [![Build status](https://ci.appveyor.com/api/projects/status/fgiptpa87nh51g0v/branch/master?svg=true)](https://ci.appveyor.com/project/ShinnosukeWatanabe/load-from-cwd-or-npm/branch/master)

[Use npm.](https://docs.npmjs.com/cli/install)
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).

@@ -48,3 +48,3 @@ ```

If the module ins't installed in CWD but included in the [npm CLI dependencies](https://github.com/npm/npm/blob/d48783c339ca8d47a12685b38f2b1ac540e5bf7f/package.json#L33-L99), it loads the module from npm CLI directory.
If the module ins't installed in CWD but included in the [npm CLI dependencies](https://github.com/npm/npm/blob/v5.5.1/package.json#L36-L129), it loads the module from npm CLI directory.

@@ -55,5 +55,5 @@ ```javascript

loadFromCwdOrNpm('nopt').then(nopt => {
nopt; //=> {[Function: nopt], clean: [Function: clean] ...}
});
(async () => {
const nopt = await loadFromCwdOrNpm('nopt'); //=> {[Function: nopt], clean: [Function: clean] ...}
})();
```

@@ -65,8 +65,8 @@

// $ npm ls eslint
// > └── eslint@2.13.1
// > └── eslint@4.11.0
// npm doesn't depend on `eslint` module.
loadFromCwdOrNpm('eslint').then(eslint => {
eslint; //=> {linter: EventEmitter { ... }, ...}
});
(async () => {
// npm doesn't depend on `eslint` module.
const eslint = await loadFromCwdOrNpm('eslint'); //=> {linter: EventEmitter { ... }, ...}
})();
```

@@ -80,5 +80,6 @@

loadFromCwdOrNpm('rimraf').then(rimraf => {
rimraf; // Loaded from npm CLI directory because the CWD version is older
});
(async () => {
// Loaded from npm CLI directory because the CWD version is older
const rimraf = await loadFromCwdOrNpm('rimraf');
})();
```

@@ -94,3 +95,3 @@

It takes two `String` arguments, package versions of the CWD one and the npm dependency one. the former will be loaded when `compareFn` returns `true`, otherwise the latter will be loaded.
It takes two `string` arguments, package versions of the CWD one and the npm dependency one. the former will be loaded when `compareFn` returns `true`, otherwise the latter will be loaded.

@@ -107,4 +108,2 @@ ```javascript

Copyright (c) 2015 - 2017 [Shinnosuke Watanabe](https://github.com/shinnn)
Licensed under [the MIT License](./LICENSE).
[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc