-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathalloy.js
More file actions
203 lines (183 loc) · 6.02 KB
/
alloy.js
File metadata and controls
203 lines (183 loc) · 6.02 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Alloy
* Copyright TiDev, Inc. 04/07/2022-Present
* See LICENSE for more information on licensing.
*/
exports.cliVersion = '>=3.X';
exports.version = '1.0.1';
var SILENT = true;
exports.init = function (logger, config, cli, appc) {
var path = require('path'),
fs = require('fs'),
afs = appc.fs,
i18n = appc.i18n(__dirname),
__ = i18n.__,
__n = i18n.__n,
pkginfo = appc.pkginfo.package(module),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
parallel = appc.async.parallel;
if (!process.env.sdk) {
process.env.sdk = cli.sdk.name;
}
function run(deviceFamily, deployType, target, finished, silent) {
var appDir = path.join(cli.argv['project-dir'], 'app');
if (!afs.exists(appDir)) {
logger.info(__('Project not an Alloy app, continuing'));
finished();
return;
}
logger.info(__('Found Alloy app in %s', appDir.cyan));
// TODO: Make this check specific to a TiSDK version
// create a .alloynewcli file to tell old plugins not to run
var buildDir = path.join(cli.argv['project-dir'], 'build');
if (!afs.exists(buildDir)) {
fs.mkdirSync(buildDir);
}
fs.writeFileSync(path.join(buildDir, '.alloynewcli'), '');
var cRequire = afs.resolvePath(__dirname, '..', 'Alloy', 'commands', 'compile', 'index.js'),
config = {
platform: /(?:iphone|ipad)/.test(cli.argv.platform) ? 'ios' : cli.argv.platform,
version: '0',
simtype: 'none',
devicefamily: /(?:iphone|ios)/.test(cli.argv.platform) ? deviceFamily : 'none',
deploytype: deployType || cli.argv['deploy-type'] || 'development',
target: target
};
if (silent) {
// turn off all logging output for code analyzer build hook
config.noBanner = 'true';
config.logLevel = '-1';
}
if (cli.argv.theme) {
config.theme = cli.argv.theme;
}
config = Object.keys(config).map(function (c) {
return c + '=' + config[c];
}).join(',');
if (afs.exists(cRequire)) {
// we're being invoked from the actual alloy directory!
// no need to subprocess, just require() and run
var origLimit = Error.stackTraceLimit;
Error.stackTraceLimit = Infinity;
try {
require(cRequire)({}, {
config: config,
outputPath: cli.argv['project-dir'],
_version: pkginfo.version
});
} catch (e) {
logger.error(__('Alloy compiler failed'));
e.toString().split('\n').forEach(function (line) {
if (line) { logger.error(line); }
});
process.exit(1);
}
Error.stackTraceLimit = origLimit;
finished();
} else {
// we have no clue where alloy is installed, so we're going to subprocess
// alloy and hope it's in the system path or a well known place
var paths = {};
var locatorCmd = process.platform === 'win32' ? 'where' : 'which';
parallel(this, ['alloy', 'node'].map(function (bin) {
return function (done) {
var envName = 'ALLOY_' + (bin === 'node' ? 'NODE_' : '') + 'PATH';
paths[bin] = process.env[envName];
if (paths[bin]) {
done();
} else if (process.platform === 'win32' && bin === 'alloy') {
paths.alloy = 'alloy.cmd';
done();
} else {
exec(locatorCmd + ' ' + bin, function (err, stdout, strerr) {
if (!err) {
paths[bin] = stdout.trim();
done();
} else {
parallel(this, [
'/usr/local/bin/' + bin,
'/opt/local/bin/' + bin,
path.join(process.env.HOME, 'local/bin', bin),
'/opt/bin/' + bin,
'/usr/bin/' + bin
].map(function (p) {
return function (cb) {
if (afs.exists(p)) { paths[bin] = p; }
cb();
};
}), done);
}
});
}
};
}), function () {
if (!paths.alloy) {
logger.error('The alloy CLI is not installed');
logger.error('Please install it with [sudo] npm i alloy -g');
process.exit(1);
}
// compose alloy command execution
var cmd = [paths.node, paths.alloy, 'compile', appDir, '--config', config];
if (cli.argv['no-colors'] || cli.argv['color'] === false) { cmd.push('--no-colors'); }
// process each line of output from alloy
function checkLine(line) {
var re = new RegExp(
'^(?:\u001b\\[\\d+m)?\\[?(' +
logger.getLevels().join('|') +
')\\]?\s*(?:\u001b\\[\\d+m)?(.*)', 'i'
);
if (line) {
var m = line.match(re);
if (m) {
logger[m[1].toLowerCase()](m[2].trim());
} else {
logger.debug(line);
}
}
}
// execute alloy in os-specific manner
var child;
if (process.platform === 'win32' && paths.alloy === 'alloy.cmd') {
cmd.shift();
logger.info(__('Executing Alloy compile: %s',
['cmd', '/s', '/c'].concat(cmd).join(' ').cyan));
// arg processing from https://github.com/MarcDiethelm/superspawn
child = spawn('cmd', [['/s', '/c', '"' +
cmd.map(function(a) {
if (/^[^"].* .*[^"]/.test(a)) return '"' + a + '"'; return a;
}).join(' ') + '"'].join(' ')], {
stdio: 'inherit',
windowsVerbatimArguments: true
});
} else {
logger.info(__('Executing Alloy compile: %s', cmd.join(' ').cyan));
child = spawn(cmd.shift(), cmd);
child.stdout.on('data', function (data) {
data.toString().split('\n').forEach(checkLine);
});
child.stderr.on('data', function (data) {
data.toString().split('\n').forEach(checkLine);
});
}
// handle the completion of alloy, success or otherwise
child.on('exit', function (code) {
if (code) {
logger.error(__('Alloy compiler failed'));
process.exit(1);
} else {
logger.info(__('Alloy compiler completed successfully'));
afs.exists(path.join(cli.argv['project-dir'], 'build', 'i18n')) && process.argv.push('--i18n-dir', 'build');
afs.exists(path.join(cli.argv['project-dir'], 'build', 'platform')) && (cli.argv['platform-dir'] = 'build/platform');
}
finished();
});
});
}
}
cli.addHook('build.pre.compile', function (build, finished) {
var deployType = build.deployType,
target = build.target;
run(build.deviceFamily, deployType, target, finished);
});
};