|
| 1 | +/*eslint-disable no-console */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// Runs `npm install` in cwd |
| 5 | + |
| 6 | +var chalk = require('chalk'); |
| 7 | +var Task = require('ember-cli/lib/models/task'); |
| 8 | +var npm = require('../utilities/npm'); |
| 9 | + |
| 10 | +module.exports = Task.extend({ |
| 11 | + // The command to run: can be 'install' or 'uninstall' |
| 12 | + command: '', |
| 13 | + // Message to send to ui.startProgress |
| 14 | + startProgressMessage: '', |
| 15 | + // Message to send to ui.writeLine on completion |
| 16 | + completionMessage: '', |
| 17 | + |
| 18 | + init: function() { |
| 19 | + this.npm = this.npm || require('npm'); |
| 20 | + }, |
| 21 | + // Options: Boolean verbose |
| 22 | + run: function(options) { |
| 23 | + this.ui.startProgress(chalk.green(this.startProgressMessage), chalk.green('.')); |
| 24 | + |
| 25 | + var npmOptions = { |
| 26 | + loglevel: options.verbose ? 'verbose' : 'error', |
| 27 | + progress: false, |
| 28 | + logstream: this.ui.outputStream, |
| 29 | + color: 'always', |
| 30 | + // by default, do install peoples optional deps |
| 31 | + 'optional': 'optional' in options ? options.optional : true, |
| 32 | + 'save-dev': !!options['save-dev'], |
| 33 | + 'save-exact': !!options['save-exact'] |
| 34 | + }; |
| 35 | + |
| 36 | + var packages = options.packages || []; |
| 37 | + |
| 38 | + // npm otherwise is otherwise noisy, already submitted PR for npm to fix |
| 39 | + // misplaced console.log |
| 40 | + this.disableLogger(); |
| 41 | + |
| 42 | + return npm(this.command, packages, npmOptions, this.npm). |
| 43 | + finally(this.finally.bind(this)). |
| 44 | + then(this.announceCompletion.bind(this)); |
| 45 | + }, |
| 46 | + |
| 47 | + announceCompletion: function() { |
| 48 | + this.ui.writeLine(chalk.green(this.completionMessage)); |
| 49 | + }, |
| 50 | + |
| 51 | + finally: function() { |
| 52 | + this.ui.stopProgress(); |
| 53 | + this.restoreLogger(); |
| 54 | + }, |
| 55 | + |
| 56 | + disableLogger: function() { |
| 57 | + this.oldLog = console.log; |
| 58 | + console.log = function() {}; |
| 59 | + }, |
| 60 | + |
| 61 | + restoreLogger: function() { |
| 62 | + console.log = this.oldLog; // Hack, see above |
| 63 | + } |
| 64 | +}); |
0 commit comments