forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpmScripts.js
More file actions
143 lines (120 loc) · 3.84 KB
/
Copy pathnpmScripts.js
File metadata and controls
143 lines (120 loc) · 3.84 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
var Q = require('q');
var path = require('path');
var _ = require('underscore');
var fs = require('fs');
var IonicAppLib = require('ionic-app-lib');
var log = IonicAppLib.logging.logger;
var DEV_SERVER_COMPLETION_STRING = 'dev server running';
/**
* Consolidate a list of options based on those passed to it.
*
* @param {Array} list of option names. Consolidate will be to the first array element name
* @param {Object} key/value object that is to be used as the source of consolidation
* @return {Object} key/value object with consolidated options
*/
function consolidateOptions(list, options) {
var newOptions = _.extend({}, options);
list.forEach(function(optionName) {
delete newOptions[optionName];
});
var value = list.reduce(function(final, optionName) {
if (options.hasOwnProperty(optionName) && !!options[optionName]) {
final = options[optionName];
}
return final;
}, null);
if (value !== null) {
newOptions[list[0]] = value;
}
return newOptions;
}
function optionsToArray(options) {
return Object.keys(options)
.filter(function(itemName) {
return itemName !== '_' && itemName.indexOf('$') !== 0;
})
.reduce(function(array, optionName) {
if (typeof options[optionName] === 'boolean' && options[optionName]) {
array.push('--' + optionName);
} else if (typeof options[optionName] !== 'boolean') {
array.push('--' + optionName, options[optionName]);
}
return array;
}, []);
}
function getScriptName(name) {
return 'ionic:' + name;
}
function hasIonicScript(name) {
return getPackageJsonContents().then(function(packageJsonContents) {
return packageJsonContents.hasOwnProperty('scripts') &&
packageJsonContents.scripts.hasOwnProperty(getScriptName(name));
});
}
function runIonicScript(name, argv) {
var crossSpawn = require('cross-spawn');
var scriptName = getScriptName(name);
var q = Q.defer();
process.env['FORCE_COLOR'] = true;
var scriptSpawn = crossSpawn.spawn('npm', ['run', scriptName, '--'].concat(argv || []), {
stdio: [process.stdin, 'pipe', process.stderr]
})
.on('error', function(err) {
log.debug('Spawn command', scriptName, 'failed');
q.reject('Unable to run spawn command ' + err);
});
scriptSpawn.stdout.pipe(process.stdout);
scriptSpawn.stdout.on('data', function(data) {
var dataLines = data.toString().split('\n').find(function(line) {
return line.indexOf(DEV_SERVER_COMPLETION_STRING) > -1;
});
if (dataLines && dataLines.length > 0) {
return q.resolve();
}
});
scriptSpawn.on('exit', function(code) {
log.debug('Spawn command', scriptName, 'completed');
if (code !== 0) {
return q.reject('There was an error with the spawned command: ' + name);
}
return q.resolve();
});
// If this process ends ensure that we killed the spawned child
process.on('exit', function() {
scriptSpawn.kill();
});
return q.promise;
}
/**
* Function is memoized so that it should only access the filesystem one time.
* Everytime after that it just returns the saved packageJson wrapped in a resolved promise.
*/
var getPackageJsonContents = (function() {
var packageJson;
return function f() {
var packageJsonPath = path.resolve(process.cwd() + '/package.json');
var q = Q.defer();
if (packageJson) {
return Q.resolve(packageJson);
}
try {
fs.readFile(packageJsonPath, 'utf8', function(err, dataString) {
if (!err) {
packageJson = JSON.parse(dataString);
}
q.resolve(packageJson);
});
} catch (e) {
q.resolve(packageJson);
}
return q.promise;
};
})();
module.exports = {
optionsToArray: optionsToArray,
consolidateOptions: consolidateOptions,
getPackageJsonContents: getPackageJsonContents,
hasIonicScript: hasIonicScript,
runIonicScript: runIonicScript
};