1 | /**
|
2 | * @fileoverview Build file
|
3 | * @author nzakas
|
4 | * @copyright jQuery Foundation and other contributors, https://jquery.org/
|
5 | * MIT License
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 | // Requirements
|
12 | //------------------------------------------------------------------------------
|
13 |
|
14 | const path = require("path"),
|
15 | childProcess = require("child_process");
|
16 |
|
17 | //------------------------------------------------------------------------------
|
18 | // Private
|
19 | //------------------------------------------------------------------------------
|
20 |
|
21 |
|
22 | module.exports = {
|
23 |
|
24 | /**
|
25 | * Returns an environment object that has been modified to work with local
|
26 | * nod executables.
|
27 | * @param {string} [platform] Platform identifier (same values as process.platform).
|
28 | * @param {Object} [defaultEnv] The default environment object (mostly used for testing).
|
29 | * @returns {Object} a modified environment object.
|
30 | */
|
31 | getModifiedEnv(platform = process.platform, defaultEnv = process.env) {
|
32 | const env = {},
|
33 | pathSeparator = platform === "win32" ? ";" : ":";
|
34 |
|
35 | Object.keys(defaultEnv).forEach(key => {
|
36 | env[key] = defaultEnv[key];
|
37 | });
|
38 |
|
39 | // modify PATH to use local node_modules
|
40 | env.PATH = path.resolve(__dirname, "../node_modules/.bin") + pathSeparator + env.PATH;
|
41 |
|
42 | return env;
|
43 | },
|
44 |
|
45 | /**
|
46 | * Executes a command and returns the output instead of printing it to stdout.
|
47 | * If there's an error, then the process exits and prints out the error info.
|
48 | * @param {string} cmd The command string to execute.
|
49 | * @returns {string} The result of the executed command.
|
50 | * @private
|
51 | */
|
52 | execSilent(cmd) {
|
53 | try {
|
54 | return childProcess.execSync(cmd, {
|
55 | cwd: process.cwd(),
|
56 | env: this.getModifiedEnv()
|
57 | }).toString();
|
58 | } catch (ex) {
|
59 | console.error(ex.output[1].toString());
|
60 | this.exit(ex.status);
|
61 | return null;
|
62 | }
|
63 | },
|
64 |
|
65 | /**
|
66 | * Executes a command.
|
67 | * @param {string} cmd The command to execute.
|
68 | * @returns {void}
|
69 | * @throws {Error} If the command exits with a nonzero exit code.
|
70 | * @private
|
71 | */
|
72 | exec(cmd) {
|
73 | console.log(`+ ${cmd.replace(/--otp=\d+/g, "--otp=(redacted)")}`);
|
74 | const result = this.execSilent(cmd);
|
75 |
|
76 | console.log(result);
|
77 | },
|
78 |
|
79 | /**
|
80 | * Exits the process with the given code. This is just a wrapper around
|
81 | * process.exit to allow for easier stubbing and testing.
|
82 | * @param {int} code The exit code.
|
83 | * @returns {void}
|
84 | */
|
85 | exit(code) {
|
86 | process.exit(code); // eslint-disable-line no-process-exit
|
87 | }
|
88 | };
|