forked from jasonkneen/RESTe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appc-npm
367 lines (278 loc) · 8.59 KB
/
appc-npm
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var child_process = require('child_process');
var OS_WINDOWS = (process.platform === 'win32');
install();
function install() {
var pkg;
try {
pkg = JSON.parse(fs.readFileSync('package.json', {
encoding: 'utf-8'
}));
} catch (e) {
die('Could not read package.json');
}
var appcNPM = pkg['appc-npm'];
if (typeof appcNPM !== 'object') {
die('Could not find \'appc-npm\' in package.json');
}
var target = findTarget();
if (!target) {
die('Could not find project');
}
var targetPath = (typeof appcNPM.target === 'object') ? appcNPM.target[target.name] : appcNPM.target;
if (typeof targetPath !== 'string') {
die('Could not find \'appc-npm.target.' + target.name + '\' or \'appc-npm.target\' in package.json');
}
targetPath = path.join(target.base, targetPath);
var zipFiles = toArray(appcNPM.unzip);
// unzip
if (zipFiles.length > 0) {
if (OS_WINDOWS) {
mkdirsSync(targetPath);
}
unzip(zipFiles, targetPath, configure);
// copy
} else {
var ignore = toArray(appcNPM.ignore);
ignore.push('appc-npm', '.gitignore', '.npmignore');
try {
copySync(__dirname, targetPath, function (fullPath) {
var relPath = fullPath.substr(__dirname.length + 1);
return ignore.indexOf(relPath) === -1;
});
} catch (e) {
die('Could not copy \'' + __dirname + '\' to \'' + targetPath + '\'');
}
console.log('Copied \'' + __dirname + '\' to \'' + targetPath + '\'');
configure();
}
// configure tiapp.xml and config.json
function configure() {
// config.json
if (appcNPM.config) {
var configPath = path.join(target.base, 'app', 'config.json');
var config;
try {
config = require(configPath);
} catch (e) {
config = {};
}
if (!config.dependencies) {
config.dependencies = {};
}
for (var id in appcNPM.config) {
if (config.dependencies[id] !== appcNPM.config[id]) {
config.dependencies[id] = appcNPM.config[id];
}
}
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
console.log('Updated app/config.json');
} catch (e) {
die('Could not write updated app/config.json');
}
}
// tiapp.xml
if (appcNPM.tiapp) {
var tiappPath = path.join(target.base, 'tiapp.xml');
var tiapp;
try {
tiapp = fs.readFileSync(tiappPath, {
encoding: 'utf-8'
});
} catch (e) {
die('Could not read tiapp.xml' + e + ' ' + tiappPath);
}
var add = [];
appcNPM.tiapp.forEach(function(mod) {
var xml = '<module platform="' + mod.platform + '" version="' + mod.version + '">' + mod.name + '</module>';
var reModule = new RegExp('<module[^>]+>' + mod.name + '</module>');
// update existing
if (reModule.test(tiapp)) {
tiapp = tiapp.replace(reModule, xml);
// queue to be added
} else {
add.push(xml);
}
});
if (add.length > 0) {
var reModules = /(\n)?(\s*<\/modules>)/;
var reModulesClosed = /<modules\s*\/>/;
var reTiapp = /(\n)?(\s*<\/ti:app>)/;
var xml = '\t\t' + add.join('\n\t\t') + '\n';
var modulesXml = '<modules>\n' + xml + '\t</modules>';
// has <modules>..</modules>
if (reModules.test(tiapp)) {
tiapp = tiapp.replace(reModules, '$1' + xml + '$2');
// has <modules/>
} else if (reModulesClosed.test(tiapp)) {
tiapp = tiapp.replace(reModulesClosed, modulesXml);
// insert before </ti:app>
} else if (reTiapp.test(tiapp)) {
tiapp = tiapp.replace(reTiapp, '$1\t' + modulesXml + '\n$2');
// invalid tiapp.xml
} else {
die('Could not update tiapp.xml');
}
}
try {
fs.writeFileSync(tiappPath, tiapp);
console.log('Updated tiapp.xml');
} catch (e) {
die('Could not write updated tiapp.xml');
}
}
console.log();
process.exit(0);
}
}
function unzip(files, targetPath, next) {
var file = files.shift();
var filePath = path.join(__dirname, file);
var command;
if (OS_WINDOWS) {
command = '/c cd "' + targetPath + '" && jar xf "' + filePath + '"';
} else {
command = 'unzip -o "' + filePath + '" -d "' + targetPath + '"';
}
return child_process.exec(command, function(error, stdout, stderr) {
if (error) {
var err = 'Could not unzip \'' + filePath + '\': ' + stderr;
if (OS_WINDOWS) {
var EOL = require('os').EOL;
err += EOL + 'Make sure you have Java JDK installed and added to PATH: ' + EOL + 'http://docs.oracle.com/javase/8/docs/technotes/guides/install/windows_jdk_install.html#BABGDJFH';
}
die(err);
} else {
console.log('Unzipped ' + file);
if (files.length > 0) {
unzip(files, targetPath, next);
} else {
next();
}
}
});
}
function findTarget(dir) {
if (dir) {
if (fs.existsSync(path.join(dir, 'appc.json'))) {
return {
name: 'arrow',
base: dir
};
} else if (fs.existsSync(path.join(dir, 'app', 'controllers', 'index.js'))) {
return {
name: 'alloy',
base: dir
};
} else if (fs.existsSync(path.join(dir, 'tiapp.xml'))) {
return {
name: 'titanium',
base: dir
};
}
} else {
dir = __dirname;
}
dirUp = path.resolve(dir, '..', '..');
if (!dirUp || dirUp === dir) {
return;
}
return findTarget(dirUp);
}
function die(err) {
console.error(err);
console.error();
process.exit(1);
}
function toArray(val) {
if (typeof val === 'string') {
return [val];
} else if (Object.prototype.toString.call(val) === '[object Array]') {
return val;
} else {
return [];
}
}
/* jshint ignore:start */
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-sync.js
function copySync (src, dest, options) {
if (typeof options === 'function' || options instanceof RegExp) {
options = {filter: options}
}
options = options || {}
options.recursive = !!options.recursive
// default to true for now
options.clobber = 'clobber' in options ? !!options.clobber : true
options.filter = options.filter || function () { return true }
var stats = options.recursive ? fs.lstatSync(src) : fs.statSync(src)
var destFolder = path.dirname(dest)
var destFolderExists = fs.existsSync(destFolder)
var performCopy = false
if (stats.isFile()) {
if (options.filter instanceof RegExp) performCopy = options.filter.test(src)
else if (typeof options.filter === 'function') performCopy = options.filter(src)
if (performCopy) {
if (!destFolderExists) mkdirsSync(destFolder)
copyFileSync(src, dest, options.clobber)
}
} else if (stats.isDirectory()) {
if (!fs.existsSync(dest)) mkdirsSync(dest)
var contents = fs.readdirSync(src)
contents.forEach(function (content) {
copySync(path.join(src, content), path.join(dest, content), {filter: options.filter, recursive: true})
})
} else if (options.recursive && stats.isSymbolicLink()) {
var srcPath = fs.readlinkSync(src)
fs.symlinkSync(srcPath, dest)
}
}
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/copy/copy-file-sync.js
function copyFileSync (srcFile, destFile, clobber) {
if (fs.existsSync(destFile) && !clobber) {
throw Error('EXIST')
}
// simplified to work with vanilla fs
fs.writeFileSync(destFile, fs.readFileSync(srcFile));
}
// https://github.com/jprichardson/node-fs-extra/blob/master/lib/mkdirs/mkdirs.js
var o777 = parseInt('0777', 8)
function mkdirsSync (p, opts, made) {
if (!opts || typeof opts !== 'object') {
opts = { mode: opts }
}
var mode = opts.mode
var xfs = opts.fs || fs
if (mode === undefined) {
mode = o777 & (~process.umask())
}
if (!made) made = null
p = path.resolve(p)
try {
xfs.mkdirSync(p)//, mode) <!-- failed
made = made || p
} catch (err0) {
switch (err0.code) {
case 'ENOENT' :
made = mkdirsSync(path.dirname(p), opts, made)
mkdirsSync(p, opts, made)
break
// In the case of any other error, just see if there's a dir
// there already. If so, then hooray! If not, then something
// is borked.
default:
var stat
try {
stat = xfs.statSync(p)
} catch (err1) {
throw err0
}
if (!stat.isDirectory()) throw err0
break
}
}
return made
}
/* jshint ignore:end */