UNPKG

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