From 6f0ebfb7595dc31934eeb2e4a669c20111fbdb9a Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Thu, 30 Jun 2016 13:18:49 +0100 Subject: [PATCH] fix(npm): update to npm 3.10.2 (#1250) The npm task from our current ember-cli install uses npm 2.x.x, which was causing issues with some of the dependencies. This PR copies over all files needed for the npm-install task but uses a local 3.10.2 npm version instead. Fix #1186 (main issue) Fix #1191 Fix #1201 Fix #1209 Fix #1207 Fix #1248 --- addon/ng2/commands/init.js | 3 +- addon/ng2/tasks/npm-install.js | 11 ++++++ addon/ng2/tasks/npm-task.js | 64 ++++++++++++++++++++++++++++++++++ addon/ng2/utilities/npm.js | 38 ++++++++++++++++++++ package.json | 1 + 5 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 addon/ng2/tasks/npm-install.js create mode 100644 addon/ng2/tasks/npm-task.js create mode 100644 addon/ng2/utilities/npm.js diff --git a/addon/ng2/commands/init.js b/addon/ng2/commands/init.js index f77a1c2852b7..7c7fff8aa460 100644 --- a/addon/ng2/commands/init.js +++ b/addon/ng2/commands/init.js @@ -7,6 +7,7 @@ var validProjectName = require('ember-cli/lib/utilities/valid-project-name'); var normalizeBlueprint = require('ember-cli/lib/utilities/normalize-blueprint-option'); var GitInit = require('../tasks/git-init'); var LinkCli = require('../tasks/link-cli'); +var NpmInstall = require('../tasks/npm-install'); module.exports = Command.extend({ name: 'init', @@ -68,7 +69,7 @@ module.exports = Command.extend({ } if (!commandOptions.skipNpm) { - var npmInstall = new this.tasks.NpmInstall({ + var npmInstall = new NpmInstall({ ui: this.ui, analytics: this.analytics, project: this.project diff --git a/addon/ng2/tasks/npm-install.js b/addon/ng2/tasks/npm-install.js new file mode 100644 index 000000000000..40fa58af9cbe --- /dev/null +++ b/addon/ng2/tasks/npm-install.js @@ -0,0 +1,11 @@ +'use strict'; + +// Runs `npm install` in cwd + +var NpmTask = require('./npm-task'); + +module.exports = NpmTask.extend({ + command: 'install', + startProgressMessage: 'Installing packages for tooling via npm', + completionMessage: 'Installed packages for tooling via npm.' +}); diff --git a/addon/ng2/tasks/npm-task.js b/addon/ng2/tasks/npm-task.js new file mode 100644 index 000000000000..4f720da73175 --- /dev/null +++ b/addon/ng2/tasks/npm-task.js @@ -0,0 +1,64 @@ +/*eslint-disable no-console */ +'use strict'; + +// Runs `npm install` in cwd + +var chalk = require('chalk'); +var Task = require('ember-cli/lib/models/task'); +var npm = require('../utilities/npm'); + +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', + progress: false, + logstream: this.ui.outputStream, + color: 'always', + // by default, do install peoples optional deps + 'optional': 'optional' in options ? options.optional : true, + '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(); + + return npm(this.command, packages, npmOptions, this.npm). + 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 + } +}); diff --git a/addon/ng2/utilities/npm.js b/addon/ng2/utilities/npm.js new file mode 100644 index 000000000000..fd88e14538d1 --- /dev/null +++ b/addon/ng2/utilities/npm.js @@ -0,0 +1,38 @@ +'use strict'; + +var Promise = require('ember-cli/lib/ext/promise'); + +// + +/** + Runs the npm command `command` with the supplied args and load options. + + Please note that the loaded module appears to retain some state, so do not + expect multiple invocations within the same process to work without quirks. + This problem is likely fixable. + + @method npm + @param {String} command The npm command to run. + @param {Array} npmArgs The arguments passed to the npm command. + @param {Array} options The options passed when loading npm. + @param {Module} [npm] A reference to the npm module. +*/ +module.exports = function npm(command, npmArgs, options/*, npm*/) { + var lib; + if (arguments.length === 4) { + lib = arguments[3]; + } else { + lib = require('npm'); + } + + var load = Promise.denodeify(lib.load); + + return load(options) + .then(function() { + // if install is denodeified outside load.then(), + // it throws "Call npm.load(config, cb) before using this command." + var operation = Promise.denodeify(lib.commands[command]); + + return operation(npmArgs || []); + }); +}; diff --git a/package.json b/package.json index 1cce9c57981e..b20bb8bc3f52 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "handlebars": "^4.0.5", "leek": "0.0.21", "lodash": "^4.11.1", + "npm": "3.10.2", "opn": "4.0.1", "resolve": "^1.1.7", "shelljs": "^0.7.0",