This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
install: Refactor module validation for ealier/better checks
This does a few things: 1. It fixes the calls to npm-install-checks– we were passing a read-package-tree node, and it wants package.json data. 2. This moves checks to be at "install" time (resolveWithNewModule), so they happen earlier. 3. Arguments passed in from the command line are checked even earlier. This is where "don't install yourself" checks are done. PR-URL: #9039 Fixes: #8637 Fixes: #8921
- Loading branch information
Showing
10 changed files
with
352 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
var validate = require('aproba') | ||
var asyncMap = require('slide').asyncMap | ||
var chain = require('slide').chain | ||
var npmInstallChecks = require('npm-install-checks') | ||
var checkEngine = npmInstallChecks.checkEngine | ||
var checkPlatform = npmInstallChecks.checkPlatform | ||
var npm = require('../npm.js') | ||
|
||
module.exports = function (idealTree, args, next) { | ||
validate('OAF', arguments) | ||
var force = npm.config.get('force') | ||
|
||
asyncMap(args, function (pkg, done) { | ||
chain([ | ||
[checkSelf, idealTree, pkg, force], | ||
[isInstallable, pkg] | ||
], done) | ||
}, next) | ||
} | ||
|
||
var isInstallable = module.exports.isInstallable = function (pkg, next) { | ||
var force = npm.config.get('force') | ||
var nodeVersion = npm.config.get('node-version') | ||
var strict = npm.config.get('engine-strict') | ||
chain([ | ||
[checkEngine, pkg, npm.version, nodeVersion, force, strict], | ||
[checkPlatform, pkg, force] | ||
], next) | ||
} | ||
|
||
function checkSelf (idealTree, pkg, force, next) { | ||
if (idealTree.package.name !== pkg.name) return next() | ||
if (force) { | ||
var warn = new Error("Wouldn't install " + pkg.name + ' as a dependency of itself, but being forced') | ||
warn.code = 'ENOSELF' | ||
idealTree.warnings.push(warn) | ||
next() | ||
} else { | ||
var er = new Error('Refusing to install ' + pkg.name + ' as a dependency of itself') | ||
er.code = 'ENOSELF' | ||
next(er) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict' | ||
var path = require('path') | ||
var fs = require('fs') | ||
var test = require('tap').test | ||
var osenv = require('osenv') | ||
var mkdirp = require('mkdirp') | ||
var rimraf = require('rimraf') | ||
var common = require('../common-tap.js') | ||
|
||
var base = path.join(__dirname, path.basename(__filename, '.js')) | ||
var installFrom = path.join(base, 'from') | ||
var installIn = path.join(base, 'in') | ||
|
||
var json = { | ||
name: 'check-cpu-reqs', | ||
version: '0.0.1', | ||
description: 'fixture', | ||
cpu: ['fake-cpu'] | ||
} | ||
|
||
test('setup', function (t) { | ||
setup() | ||
t.end() | ||
}) | ||
|
||
var INSTALL_OPTS = ['--loglevel', 'silly'] | ||
var EXEC_OPTS = {cwd: installIn} | ||
|
||
test('install bad cpu', function (t) { | ||
common.npm(['install', installFrom].concat(INSTALL_OPTS), EXEC_OPTS, function (err, code) { | ||
t.ifError(err, 'npm ran without issue') | ||
t.is(code, 1, 'npm install refused to install a package in itself') | ||
t.end() | ||
}) | ||
}) | ||
test('force install bad cpu', function (t) { | ||
common.npm(['install', '--force', installFrom].concat(INSTALL_OPTS), EXEC_OPTS, function (err, code) { | ||
t.ifError(err, 'npm ran without issue') | ||
t.is(code, 0, 'npm install happily installed a package in itself with --force') | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('cleanup', function (t) { | ||
cleanup() | ||
t.end() | ||
}) | ||
|
||
function cleanup () { | ||
process.chdir(osenv.tmpdir()) | ||
rimraf.sync(base) | ||
} | ||
|
||
function setup () { | ||
cleanup() | ||
mkdirp.sync(path.resolve(installFrom, 'node_modules')) | ||
fs.writeFileSync( | ||
path.join(installFrom, 'package.json'), | ||
JSON.stringify(json, null, 2) | ||
) | ||
mkdirp.sync(path.resolve(installIn, 'node_modules')) | ||
process.chdir(base) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict' | ||
var path = require('path') | ||
var fs = require('fs') | ||
var test = require('tap').test | ||
var osenv = require('osenv') | ||
var mkdirp = require('mkdirp') | ||
var rimraf = require('rimraf') | ||
var common = require('../common-tap.js') | ||
|
||
var base = path.join(__dirname, path.basename(__filename, '.js')) | ||
var installFrom = path.join(base, 'from') | ||
var installIn = path.join(base, 'in') | ||
|
||
var json = { | ||
name: 'check-engine-reqs', | ||
version: '0.0.1', | ||
description: 'fixture', | ||
engines: { | ||
node: '1.0.0-not-a-real-version' | ||
} | ||
} | ||
|
||
test('setup', function (t) { | ||
setup() | ||
t.end() | ||
}) | ||
|
||
var INSTALL_OPTS = ['--loglevel', 'silly'] | ||
var EXEC_OPTS = {cwd: installIn} | ||
|
||
test('install bad engine', function (t) { | ||
common.npm(['install', '--engine-strict', installFrom].concat(INSTALL_OPTS), EXEC_OPTS, function (err, code) { | ||
t.ifError(err, 'npm ran without issue') | ||
t.is(code, 1, 'npm install refused to install a package in itself') | ||
t.end() | ||
}) | ||
}) | ||
test('force install bad engine', function (t) { | ||
common.npm(['install', '--engine-strict', '--force', installFrom].concat(INSTALL_OPTS), EXEC_OPTS, function (err, code) { | ||
t.ifError(err, 'npm ran without issue') | ||
t.is(code, 0, 'npm install happily installed a package in itself with --force') | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('cleanup', function (t) { | ||
cleanup() | ||
t.end() | ||
}) | ||
|
||
function cleanup () { | ||
process.chdir(osenv.tmpdir()) | ||
rimraf.sync(base) | ||
} | ||
|
||
function setup () { | ||
cleanup() | ||
mkdirp.sync(path.resolve(installFrom, 'node_modules')) | ||
fs.writeFileSync( | ||
path.join(installFrom, 'package.json'), | ||
JSON.stringify(json, null, 2) | ||
) | ||
mkdirp.sync(path.resolve(installIn, 'node_modules')) | ||
process.chdir(base) | ||
} |
Oops, something went wrong.