-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from 201-created/add-shrinkwrap
Add support for npm-shrinkwrap.json
- Loading branch information
Showing
31 changed files
with
720 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./lib/dependency-checker'); | ||
var Reporter = require('./lib/reporter'); | ||
var DependencyChecker = require('./lib/dependency-checker'); | ||
|
||
module.exports = function emberCliDependencyCheckerAddon(project) { | ||
var reporter = new Reporter(); | ||
var dependencyChecker = new DependencyChecker(project, reporter); | ||
dependencyChecker.checkDependencies(); | ||
|
||
return { | ||
name: 'ember-cli-dependency-checker' | ||
}; | ||
}; |
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,50 @@ | ||
'use strict'; | ||
|
||
function installCommand(type) { | ||
switch (type) { | ||
case 'npm': | ||
return 'npm install'; | ||
case 'npm-shrinkwrap': | ||
return 'rm -rf node_modules/ && npm install'; | ||
case 'bower': | ||
return 'bower install'; | ||
} | ||
} | ||
|
||
var Reporter = function() { | ||
this.messages = []; | ||
}; | ||
|
||
Reporter.prototype.unsatisifedPackages = function(type, packages) { | ||
this.chalk = this.chalk || require('chalk'); | ||
this.EOL = this.EOL || require('os').EOL; | ||
|
||
var chalk = this.chalk; | ||
var EOL = this.EOL; | ||
|
||
if (packages.length > 0) { | ||
var message = ''; | ||
message += EOL + chalk.red('Missing ' + type + ' packages: ') + EOL; | ||
|
||
packages.forEach(function(pkg) { | ||
message += chalk.reset('Package: ') + chalk.cyan(pkg.name) + EOL; | ||
if (pkg.parents) { | ||
message += chalk.reset('Required by: ') + chalk.cyan(pkg.parents.join(' / ')) + EOL; | ||
} | ||
message += chalk.grey(' * Specified: ') + chalk.reset(pkg.versionSpecified) + EOL; | ||
message += chalk.grey(' * Installed: ') + chalk.reset(pkg.versionInstalled || '(not installed)') + EOL + EOL; | ||
}); | ||
|
||
message += chalk.red('Run `'+ installCommand(type) +'` to install missing dependencies.') + EOL; | ||
this.messages.push(message); | ||
} | ||
}; | ||
|
||
Reporter.prototype.report = function() { | ||
if (this.messages.length) { | ||
var DependencyError = require('./dependency-error'); | ||
throw new DependencyError(this.messages.join('')); | ||
} | ||
}; | ||
|
||
module.exports = Reporter; |
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 readPackageJSON = require('./utils/read-package-json'); | ||
var ShrinkwrapPackage = require('./shrinkwrap-package'); | ||
var path = require('path'); | ||
|
||
var ShrinkwrapChecker = function(root, name, versionSpecified, parents){ | ||
this.root = root; | ||
this.name = name; | ||
this.versionSpecified = versionSpecified; | ||
this.parents = parents; | ||
}; | ||
|
||
ShrinkwrapChecker.prototype.check = function() { | ||
var packageJSON = readPackageJSON(this.root) || {}; | ||
var versionInstalled = packageJSON.version; | ||
|
||
return new ShrinkwrapPackage( | ||
this.name, this.versionSpecified, versionInstalled, this.parents); | ||
}; | ||
|
||
ShrinkwrapChecker.checkDependencies = function(root, shrinkWrapJSON) { | ||
var resolvedDependencies = []; | ||
var currentNode; | ||
|
||
var nodesToCheck = [{ | ||
root: root, | ||
parents: [], | ||
childDependencies: shrinkWrapJSON.dependencies, | ||
name: shrinkWrapJSON.name, | ||
version: shrinkWrapJSON.version | ||
}]; | ||
|
||
var checker, resolved; | ||
|
||
while (currentNode = nodesToCheck.pop()) { | ||
checker = new ShrinkwrapChecker( | ||
currentNode.root, currentNode.name, currentNode.version, currentNode.parents); | ||
|
||
resolved = checker.check(); | ||
resolvedDependencies.push(resolved); | ||
|
||
if (!resolved.needsUpdate && currentNode.childDependencies) { | ||
/* jshint loopfunc:true*/ | ||
var parents = currentNode.parents.concat(currentNode.name); | ||
Object.keys(currentNode.childDependencies).forEach(function(childDepName){ | ||
var childDep = currentNode.childDependencies[childDepName]; | ||
|
||
nodesToCheck.push({ | ||
root: path.join(currentNode.root, 'node_modules', childDepName), | ||
parents: parents, | ||
name: childDepName, | ||
childDependencies: childDep.dependencies, | ||
version: childDep.version | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
return resolvedDependencies; | ||
}; | ||
|
||
module.exports = ShrinkwrapChecker; |
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,22 @@ | ||
'use strict'; | ||
|
||
var Package = require('./package'); | ||
|
||
function ShrinkwrapPackage(name, versionSpecified, versionInstalled, parents) { | ||
this._super$init.call(this, name, versionSpecified, versionInstalled); | ||
this.init(name, versionSpecified, versionInstalled, parents); | ||
} | ||
|
||
ShrinkwrapPackage.prototype = Object.create(Package.prototype); | ||
ShrinkwrapPackage.prototype.constructor = ShrinkwrapPackage; | ||
|
||
ShrinkwrapPackage.prototype._super$init = Package.prototype.init; | ||
ShrinkwrapPackage.prototype.init = function(name, versionSpecified, versionInstalled, parents) { | ||
this.parents = parents; | ||
}; | ||
|
||
ShrinkwrapPackage.prototype.updateRequired = function() { | ||
return this.versionSpecified !== this.versionInstalled; | ||
}; | ||
|
||
module.exports = ShrinkwrapPackage; |
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,18 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var fs = require('fs'); | ||
function readFile(path){ | ||
if (fs.existsSync(path)) { | ||
return fs.readFileSync(path); | ||
} | ||
} | ||
|
||
module.exports = function readPackageJSON(projectRoot) { | ||
var filePath = path.join(projectRoot, 'package.json'); | ||
try { | ||
return JSON.parse(readFile(filePath)); | ||
} catch (e) { | ||
return null; | ||
} | ||
}; |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/project-shrinkwrap-check/node_modules/minimist/package.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
tests/fixtures/project-shrinkwrap-check/npm-shrinkwrap.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/fixtures/project-shrinkwrap-nested-deps-check/node_modules/.bin/mkdirp
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
tests/fixtures/project-shrinkwrap-nested-deps-check/node_modules/mkdirp/.npmignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.