|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | + * copy of this software and associated documentation files (the "Software"), |
| 6 | + * to deal in the Software without restriction, including without limitation |
| 7 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | + * Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | + * DEALINGS IN THE SOFTWARE. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +/* eslint-env node */ |
| 25 | + |
| 26 | +"use strict"; |
| 27 | + |
| 28 | +var fs = require("fs-extra"), |
| 29 | + path = require("path"), |
| 30 | + spawn = require("child_process").spawn; |
| 31 | + |
| 32 | +var Errors = { |
| 33 | + NPM_INSTALL_FAILED: "NPM_INSTALL_FAILED" |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Private function to run "npm install --production" command in the extension directory. |
| 38 | + * |
| 39 | + * @param {string} installDirectory Directory to remove |
| 40 | + * @param {function} callback NodeJS style callback to call after finish |
| 41 | + */ |
| 42 | +function _performNpmInstall(installDirectory, npmOptions, callback) { |
| 43 | + var npmPath = path.resolve(path.dirname(require.resolve("npm")), "..", "bin", "npm-cli.js"); |
| 44 | + var args = [npmPath, "install"]; |
| 45 | + |
| 46 | + // npmOptions can contain additional args like { "production": true, "proxy": "http://127.0.0.1:8888" } |
| 47 | + Object.keys(npmOptions).forEach(function (key) { |
| 48 | + var value = npmOptions[key]; |
| 49 | + if (value === true) { |
| 50 | + args.push("--" + key); |
| 51 | + } else if (typeof value === "string" && value.length > 0) { |
| 52 | + args.push("--" + key, value); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + console.log("running npm " + args.slice(1).join(" ") + " in " + installDirectory); |
| 57 | + |
| 58 | + var child = spawn(process.execPath, args, { cwd: installDirectory }); |
| 59 | + |
| 60 | + child.on("error", function (err) { |
| 61 | + return callback(err); |
| 62 | + }); |
| 63 | + |
| 64 | + var stdout = []; |
| 65 | + child.stdout.addListener("data", function (buffer) { |
| 66 | + stdout.push(buffer); |
| 67 | + }); |
| 68 | + |
| 69 | + var stderr = []; |
| 70 | + child.stderr.addListener("data", function (buffer) { |
| 71 | + stderr.push(buffer); |
| 72 | + }); |
| 73 | + |
| 74 | + var exitCode = 0; |
| 75 | + child.addListener("exit", function (code) { |
| 76 | + exitCode = code; |
| 77 | + }); |
| 78 | + |
| 79 | + child.addListener("close", function () { |
| 80 | + stderr = Buffer.concat(stderr).toString(); |
| 81 | + stdout = Buffer.concat(stdout).toString(); |
| 82 | + if (exitCode > 0) { |
| 83 | + console.error("npm-stderr: " + stderr); |
| 84 | + return callback(new Error(stderr)); |
| 85 | + } |
| 86 | + if (stderr) { |
| 87 | + console.warn("npm-stderr: " + stderr); |
| 88 | + } |
| 89 | + console.log("npm-stdout: " + stdout); |
| 90 | + return callback(); |
| 91 | + }); |
| 92 | + |
| 93 | + child.stdin.end(); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Checks package.json of the extracted extension for npm dependencies |
| 98 | + * and runs npm install when required. |
| 99 | + * @param {Object} validationResult return value of the validation procedure |
| 100 | + * @param {Function} callback function to be called after the end of validation procedure |
| 101 | + */ |
| 102 | +function performNpmInstallIfRequired(npmOptions, validationResult, callback) { |
| 103 | + |
| 104 | + function finish() { |
| 105 | + callback(null, validationResult); |
| 106 | + } |
| 107 | + |
| 108 | + var installDirectory = path.join(validationResult.extractDir, validationResult.commonPrefix); |
| 109 | + var packageJson; |
| 110 | + |
| 111 | + try { |
| 112 | + packageJson = fs.readJsonSync(path.join(installDirectory, "package.json")); |
| 113 | + } catch (e) { |
| 114 | + packageJson = null; |
| 115 | + } |
| 116 | + |
| 117 | + if (!packageJson || !packageJson.dependencies) { |
| 118 | + return finish(); |
| 119 | + } |
| 120 | + |
| 121 | + _performNpmInstall(installDirectory, npmOptions, function (err) { |
| 122 | + if (err) { |
| 123 | + validationResult.errors.push([Errors.NPM_INSTALL_FAILED, err.toString()]); |
| 124 | + } |
| 125 | + finish(); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +exports.performNpmInstallIfRequired = performNpmInstallIfRequired; |
0 commit comments