Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
move npm installation to the package extraction step
Browse files Browse the repository at this point in the history
  • Loading branch information
zaggino committed Feb 13, 2017
1 parent 1c3fa56 commit 74bec9e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 77 deletions.
71 changes: 1 addition & 70 deletions src/extensibility/node/ExtensionManagerDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var semver = require("semver"),
request = require("request"),
fs = require("fs-extra"),
temp = require("temp"),
spawn = require('child_process').spawn,
validate = require("./package-validator").validate;

// Automatically clean up temp files on exit
Expand Down Expand Up @@ -80,56 +79,6 @@ function _removeFailedInstallation(installDirectory) {
});
}

/**
* Private function to run 'npm install --production' command in the extension directory.
*
* @param {string} installDirectory Directory to remove
* @param {function} callback NodeJS style callback to call after finish
*/
function _performNpmInstall(installDirectory, callback) {
var npmPath = path.resolve(path.dirname(require.resolve("npm")), "..", "bin", "npm-cli.js");
var args = [npmPath, 'install', '--production'];

console.log("running npm install --production in " + installDirectory);

var child = spawn(process.execPath, args, { cwd: installDirectory });

child.on("error", function (err) {
return callback(err);
});

var stdout = [];
child.stdout.addListener("data", function (buffer) {
stdout.push(buffer);
});

var stderr = [];
child.stderr.addListener("data", function (buffer) {
stderr.push(buffer);
});

var exitCode = 0;
child.addListener("exit", function (code) {
exitCode = code;
});

child.addListener("close", function () {
stderr = Buffer.concat(stderr).toString();
stdout = Buffer.concat(stdout).toString();
if (exitCode > 0) {
console.error("npm-stderr: " + stderr);
return callback(new Error(stderr));
}
if (stderr) {
console.warn("npm-stderr: " + stderr);
}
console.log("npm-stdout: " + stdout);
return callback();
});

child.stdin.end();
}

/**
* Private function to unzip to the correct directory.
*
Expand Down Expand Up @@ -166,25 +115,7 @@ function _performInstall(packagePath, installDirectory, validationResult, callba
if (err) {
return fail(err);
}

var packageJson;

try {
packageJson = fs.readJsonSync(path.join(installDirectory, "package.json"));
} catch (e) {
packageJson = null;
}

if (!packageJson || !packageJson.dependencies) {
return finish();
}

_performNpmInstall(installDirectory, function (err) {
if (err) {
return fail(err);
}
finish();
});
finish();
});
});
}
Expand Down
119 changes: 119 additions & 0 deletions src/extensibility/node/npm-installer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

/* eslint-env node */

"use strict";

var fs = require("fs-extra"),
path = require("path"),
spawn = require("child_process").spawn;

var Errors = {
NPM_INSTALL_FAILED: "NPM_INSTALL_FAILED"
};

/**
* Private function to run "npm install --production" command in the extension directory.
*
* @param {string} installDirectory Directory to remove
* @param {function} callback NodeJS style callback to call after finish
*/
function _performNpmInstall(installDirectory, callback) {
var npmPath = path.resolve(path.dirname(require.resolve("npm")), "..", "bin", "npm-cli.js");
var args = [npmPath, "install", "--production"];

console.log("running npm install --production in " + installDirectory);

var child = spawn(process.execPath, args, { cwd: installDirectory });

child.on("error", function (err) {
return callback(err);
});

var stdout = [];
child.stdout.addListener("data", function (buffer) {
stdout.push(buffer);
});

var stderr = [];
child.stderr.addListener("data", function (buffer) {
stderr.push(buffer);
});

var exitCode = 0;
child.addListener("exit", function (code) {
exitCode = code;
});

child.addListener("close", function () {
stderr = Buffer.concat(stderr).toString();
stdout = Buffer.concat(stdout).toString();
if (exitCode > 0) {
console.error("npm-stderr: " + stderr);
return callback(new Error(stderr));
}
if (stderr) {
console.warn("npm-stderr: " + stderr);
}
console.log("npm-stdout: " + stdout);
return callback();
});

child.stdin.end();
}

/**
* Checks package.json of the extracted extension for npm dependencies
* and runs npm install when required.
* @param {Object} validationResult return value of the validation procedure
* @param {Function} callback function to be called after the end of validation procedure
*/
function performNpmInstallIfRequired(validationResult, callback) {

function finish() {
callback(null, validationResult);
}

var installDirectory = path.join(validationResult.extractDir, validationResult.commonPrefix);
var packageJson;

try {
packageJson = fs.readJsonSync(path.join(installDirectory, "package.json"));
} catch (e) {
packageJson = null;
}

if (!packageJson || !packageJson.dependencies) {
return finish();
}

_performNpmInstall(installDirectory, function (err) {
if (err) {
validationResult.errors.push([Errors.NPM_INSTALL_FAILED, err.toString()]);
}
finish();
});
}

exports.performNpmInstallIfRequired = performNpmInstallIfRequired;
16 changes: 9 additions & 7 deletions src/extensibility/node/package-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

"use strict";

var DecompressZip = require("decompress-zip"),
semver = require("semver"),
path = require("path"),
temp = require("temp"),
fs = require("fs-extra");
var DecompressZip = require("decompress-zip"),
semver = require("semver"),
path = require("path"),
temp = require("temp"),
fs = require("fs-extra"),
performNpmInstallIfRequired = require("./npm-installer").performNpmInstallIfRequired;

// Track and cleanup files at exit
temp.track();
Expand Down Expand Up @@ -293,12 +294,13 @@ function extractAndValidateFiles(zipPath, extractDir, options, callback) {
if (!isTheme && !fs.existsSync(mainJS)) {
errors.push([Errors.MISSING_MAIN, zipPath, mainJS]);
}
callback(null, {

performNpmInstallIfRequired({
errors: errors,
metadata: metadata,
commonPrefix: commonPrefix,
extractDir: extractDir
});
}, callback);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ define({
"INVALID_VERSION_NUMBER" : "The package version number ({0}) is invalid.",
"INVALID_BRACKETS_VERSION" : "The {APP_NAME} compatibility string ({0}) is invalid.",
"DISALLOWED_WORDS" : "The words ({1}) are not allowed in the {0} field.",
"NPM_INSTALL_FAILED" : "npm install command failed: {0}",
"API_NOT_COMPATIBLE" : "The extension isn't compatible with this version of {APP_NAME}. It's installed in your disabled extensions folder.",
"MISSING_MAIN" : "The package has no main.js file.",
"EXTENSION_ALREADY_INSTALLED" : "Installing this package will overwrite a previously installed extension. Overwrite the old extension?",
Expand Down

0 comments on commit 74bec9e

Please sign in to comment.