This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
npm-installer.js
129 lines (108 loc) · 4.28 KB
/
npm-installer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* 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, npmOptions, callback) {
var npmPath = path.resolve(path.dirname(require.resolve("npm")), "..", "bin", "npm-cli.js");
var args = [npmPath, "install"];
// npmOptions can contain additional args like { "production": true, "proxy": "http://127.0.0.1:8888" }
Object.keys(npmOptions).forEach(function (key) {
var value = npmOptions[key];
if (value === true) {
args.push("--" + key);
} else if (typeof value === "string" && value.length > 0) {
args.push("--" + key, value);
}
});
console.log("running npm " + args.slice(1).join(" ") + " 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(npmOptions, 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 || !Object.keys(packageJson.dependencies).length) {
return finish();
}
_performNpmInstall(installDirectory, npmOptions, function (err) {
if (err) {
validationResult.errors.push([Errors.NPM_INSTALL_FAILED, err.toString()]);
}
finish();
});
}
exports.performNpmInstallIfRequired = performNpmInstallIfRequired;