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 #4150 from jkarsrud/fix-npm-duplication
Code Quality: npm-install.js, npm-uninstall.js D -> A
- Loading branch information
Showing
3 changed files
with
78 additions
and
112 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
// Runs `npm install` in cwd | ||
|
||
var Promise = require('../ext/promise'); | ||
var chalk = require('chalk'); | ||
var Task = require('../models/task'); | ||
|
||
module.exports = Task.extend({ | ||
// The command to run: can be 'install' or 'uninstall' | ||
command: '', | ||
// Message to send to ui.startProgress | ||
startProgressMessage: '', | ||
// Message to send to ui.writeLine on completion | ||
completionMessage: '', | ||
|
||
init: function() { | ||
this.npm = this.npm || require('npm'); | ||
}, | ||
// Options: Boolean verbose | ||
run: function(options) { | ||
this.ui.startProgress(chalk.green(this.startProgressMessage), chalk.green('.')); | ||
|
||
var npmOptions = { | ||
loglevel: options.verbose ? 'verbose' : 'error', | ||
logstream: this.ui.outputStream, | ||
color: 'always', | ||
'save-dev': !!options['save-dev'], | ||
'save-exact': !!options['save-exact'] | ||
}; | ||
var packages = options.packages || []; | ||
|
||
// npm otherwise is otherwise noisy, already submitted PR for npm to fix | ||
// misplaced console.log | ||
this.disableLogger(); | ||
|
||
var load = Promise.denodeify(this.npm.load); | ||
|
||
return load(npmOptions) | ||
.then(function() { | ||
// if install is denodeified outside load.then(), | ||
// it throws "Call npm.load(config, cb) before using this command." | ||
var install = Promise.denodeify(this.npm.commands[this.command]); | ||
|
||
return install(packages); | ||
}.bind(this)) | ||
.finally(this.finally.bind(this)) | ||
.then(this.announceCompletion.bind(this)); | ||
}, | ||
|
||
announceCompletion: function() { | ||
this.ui.writeLine(chalk.green(this.completionMessage)); | ||
}, | ||
|
||
finally: function() { | ||
this.ui.stopProgress(); | ||
this.restoreLogger(); | ||
}, | ||
|
||
disableLogger: function() { | ||
this.oldLog = console.log; | ||
console.log = function() {}; | ||
}, | ||
|
||
restoreLogger: function() { | ||
console.log = this.oldLog; // Hack, see above | ||
} | ||
}); |
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