forked from MobileChromeApps/mobile-chrome-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmca.js
executable file
·722 lines (660 loc) · 23.3 KB
/
mca.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//usr/bin/env node $0 $*; exit $?
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
The top line of this file will allow this script to be run as
a UNIX shell script, as well as being a valid Node.js program.
*/
/******************************************************************************/
/******************************************************************************/
if (typeof WScript != 'undefined') {
function wscriptWrapper() {
var shell = WScript.CreateObject("WScript.Shell");
var args = [];
for (var i = 0; i < WScript.Arguments.Length; ++i) {
args.push('"' + WScript.Arguments.Item(i) + '"');
}
var ret;
try {
// Don't worry about passing along arguments here. It's stricly a double-click convenience.
var cmd = 'cmd /c node "' + WScript.ScriptFullName + '" ' + args.join(' ') + ' --pause_on_exit';
ret = shell.Run(cmd, 1, true);
} catch (e) {
shell.Popup('NodeJS is not installed. Please install it from http://nodejs.org');
ret = 1;
}
WScript.Quit(ret);
}
wscriptWrapper();
}
/******************************************************************************/
/******************************************************************************/
// System modules.
var childProcess = require('child_process');
var fs = require('fs');
var os = require('os');
var path = require('path');
// Third-party modules.
var ncp = require('ncp');
var optimist = require('optimist');
var commandLineFlags = null;
var origDir = process.cwd();
var isWindows = process.platform.slice(0, 3) == 'win';
var eventQueue = [];
var scriptDir = path.dirname(process.argv[1]);
var scriptName = path.basename(process.argv[1]);
var hasAndroidSdk = false;
var hasAndroidPlatform = false;
var hasXcode = false;
var command = null;
/******************************************************************************/
/******************************************************************************/
var ACTIVE_PLUGINS = [
path.join(scriptDir, 'cordova', 'cordova-plugin-file'),
path.join(scriptDir, 'cordova', 'cordova-plugin-inappbrowser'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome-bootstrap'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.alarms'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.fileSystem'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.i18n'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.identity'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.socket'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.storage'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'chrome.syncFileSystem'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'directoryFinder'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'fileChooser'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'polyfill-CustomEvent'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'polyfill-Function.bind'),
path.join(scriptDir, 'chrome-cordova', 'plugins', 'polyfill-xhr-blob')
];
function cordovaCmd(args) {
return '"' + process.argv[0] + '" "' + path.join(scriptDir, 'cordova', 'cordova-cli', 'bin', 'cordova') + '" ' + args.join(' ');
}
/******************************************************************************/
/******************************************************************************/
// Utility Functions
function pump() {
if (eventQueue.length) {
eventQueue.shift()(pump);
}
}
function exit(code) {
if (eventQueue) {
eventQueue.length = 0;
}
if (commandLineFlags['pause_on_exit']) {
waitForKey(function() {
process.exit(code);
});
} else {
process.exit(code);
}
}
function fatal(msg) {
console.error(msg);
exit(1);
}
function exec(cmd, onSuccess, opt_onError, opt_silent) {
var onError = opt_onError || function(e) {
fatal('command failed: ' + cmd + '\n' + e);
};
if (!opt_silent) {
console.log('Running: ' + cmd);
}
childProcess.exec(cmd, function(error, stdout, stderr) {
if (error) {
onError(error);
} else {
onSuccess(stdout.trim(), stderr.trim());
}
});
}
function spawn(cmd, args, onSuccess, opt_onError, opt_silent) {
var onError = opt_onError || function(e) {
fatal('command failed: ' + cmd + '\n' + e);
};
if (!opt_silent) {
console.log('Spawning: ' + cmd + ' ' + args.join(' '));
}
var p = childProcess.spawn(cmd, args);
p.stdout.on('data', function (data) {
process.stdout.write(data);
});
p.stderr.on('data', function (data) {
process.stderr.write(data);
});
process.stdin.resume();
try {
// This fails if the process is a spawned child (likely a node bug);
process.stdin.setRawMode(true);
} catch (e) {
}
process.stdin.setEncoding('utf8');
process.stdin.on('data', forward);
p.on('close', function (code) {
process.stdin.removeListener('data', forward);
process.stdin.pause();
onSuccess();
});
function forward(data) {
p.stdin.write(data);
}
}
function sudo(cmd, onSuccess, opt_onError, silent) {
if (!isWindows) {
cmd = 'sudo ' + cmd;
}
exec(cmd, onSuccess, opt_onError, silent);
}
function chdir(d) {
d = path.resolve(scriptDir, d);
if (process.cwd() != d) {
console.log('Changing directory to: ' + d);
process.chdir(d);
}
}
function copyFile(src, dst, callback) {
var rd = fs.createReadStream(src);
var wr = fs.createWriteStream(dst);
wr.on('error', function(err) {
fatal('Copy file error: ' + err);
});
wr.on('close', callback);
rd.pipe(wr);
}
function copyDirectory(src, dst, callback) {
ncp.ncp(src, dst, function(err) {
if (err) {
fatal('Copy file error: ' + err);
} else {
callback();
}
});
}
function recursiveDelete(dirPath) {
if (fs.existsSync(dirPath)) {
console.log('Deleting: ' + dirPath);
helper(dirPath);
}
function helper(dirPath) {
try {
var files = fs.readdirSync(dirPath);
} catch(e) {
return;
}
for (var i = 0; i < files.length; i++) {
var filePath = path.join(dirPath, files[i]);
fs.chmodSync(filePath, '777');
if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
} else {
helper(filePath);
}
}
fs.rmdirSync(dirPath);
}
}
function waitForKey(opt_prompt, callback) {
if (typeof opt_prompt == 'function') {
callback = opt_prompt;
opt_prompt = 'press a key';
}
console.log(opt_prompt);
function cont(key) {
if (key == '\u0003') {
process.exit(2);
}
process.stdin.removeListener('data', cont);
process.stdin.pause();
callback(key);
}
process.stdin.resume();
try {
// This fails if the process is a spawned child (likely a node bug);
process.stdin.setRawMode(true);
} catch (e) {
}
process.stdin.setEncoding('utf8');
process.stdin.on('data', cont);
}
/******************************************************************************/
/******************************************************************************/
// Tools Check
function parseTargetOutput(targetOutput) {
var targets = [];
var target;
var targetRe = /^id: (\d+) or "([^"]*)"/gm;
while (target = targetRe.exec(targetOutput)) {
targets.push(target[2]);
}
return targets;
}
function toolsCheck() {
console.log('## Checking that tools are installed');
function checkAndroid(callback) {
exec('android list targets', function(targetOutput) {
hasAndroidSdk = true;
console.log('Android SDK detected.');
var targets = parseTargetOutput(targetOutput);
/* This is the android SDK version declared in cordova-android/framework/project.properties */
if (targets.length === 0) {
console.log('No Android Platforms are installed');
} else if (targets.indexOf('Google Inc.:Google APIs:17') > -1 ||
targets.indexOf('android-17') > -1) {
hasAndroidPlatform = true;
console.log('Android 4.2.2 (Google APIs) Platform is installed.');
} else {
console.log('Android 4.2.2 (Google APIs) Platform is not installed.');
}
callback();
}, function() {
console.log('Android SDK not detected on your PATH.');
callback();
}, true);
}
function checkXcode(callback) {
if (process.platform == 'darwin') {
exec('which xcodebuild', function() {
exec('xcodebuild -version', function() {
hasXcode = true;
console.log('Xcode detected.');
callback();
}, function() {
console.log('Xcode appears to be installed, but no version is selected (fix this with xcodeselect).');
callback();
}, true);
}, function() {
console.log('Xcode not detected.');
callback();
}, true);
} else {
callback();
}
}
function checkAtLeastOneTool(callback) {
if (!hasAndroidPlatform && !hasXcode) {
if (process.platform == 'darwin') {
fatal('No usable build environment could be found. Please install either XCode or the\nAndroid SDK (with the Android 4.2.2 platform and Google APIs) and try again.');
} else {
fatal('No usable build environment could be found. Please install the Android SDK (with\nthe Android 4.2.2 platform and Google APIs), make sure that android is on your\npath, and try again.');
}
}
callback();
}
function checkNodeVersion(callback) {
if (!os.tmpdir) {
fatal('Your version of node (' + process.version + ') is too old. Please update your version of node.');
}
callback();
}
eventQueue.push(checkNodeVersion);
eventQueue.push(checkAndroid);
eventQueue.push(checkXcode);
eventQueue.push(checkAtLeastOneTool);
}
/******************************************************************************/
/******************************************************************************/
// Init
function buildCordovaJsStep(callback) {
console.log('## Building cordova-js');
process.chdir(path.join(scriptDir, 'cordova', 'cordova-js'));
var packager = require(path.join(scriptDir, 'cordova', 'cordova-js', 'build', 'packager'));
packager.generate('ios', undefined, function() {
packager.generate('android', undefined, callback);
});
}
function initCommand() {
function checkGit(callback) {
var errMsg = 'git is not installed (or not available on your PATH). Please install it from http://git-scm.com';
exec('git --version', callback, function() {
if (isWindows) {
// See if it's at the default install path.
process.env['PATH'] += ';' + path.join(process.env['ProgramFiles'], 'Git', 'bin');
exec('git --version', callback, function() {
fatal(errMsg);
}, true);
} else {
fatal(errMsg);
}
}, true);
}
function checkOutSelf(callback) {
console.log('## Checking Out mobile-chrome-apps');
function reRunThisScriptWithNewVersionThenExit() {
console.log(scriptName + ' has been updated. Restarting with new version.');
console.log(new Array(80).join('*'));
console.log(new Array(80).join('*'));
process.chdir(origDir);
// TODO: We should quote the args.
spawn(process.argv[0], process.argv.slice(1), function() {
exit(0);
});
}
// If the repo doesn't exist where the script is, then use the CWD as the checkout location.
var requiresClone = true;
// First - try the directory of the script.
if (scriptDir.slice(0, 2) != '\\\\') {
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
// Next - try the CWD, if it is
if (requiresClone && path.basename(origDir) == 'mobile-chrome-apps') {
scriptDir = origDir;
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
// Next - see if it exists within the CWD.
if (requiresClone && fs.existsSync(path.join(origDir, 'mobile-chrome-apps'))) {
scriptDir = path.join(origDir, 'mobile-chrome-apps');
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
if (requiresClone) {
scriptDir = path.join(origDir, 'mobile-chrome-apps');
chdir(origDir);
recursiveDelete('mobile-chrome-apps');
exec('git clone "https://github.com/MobileChromeApps/mobile-chrome-apps.git"', function() {
console.log('Successfully cloned mobile-chrome-apps repo');
chdir(scriptDir);
return;
});
return;
}
function updateAndRerun() {
exec('git pull --rebase', reRunThisScriptWithNewVersionThenExit);
}
function promptForUpdate() {
waitForKey('There are new git repo updates. Would you like to autoupdate? [y/n] ', function(key) {
if (key.toLowerCase() == 'y') {
updateAndRerun();
} else {
callback();
}
});
}
function checkIfNeedsUpdate() {
exec('git pull --rebase --dry-run', function(stdout, stderr) {
var needsUpdate = (!!stdout || !!stderr);
if (needsUpdate)
promptForUpdate();
}, function(error) {
console.log("Could not update repo:");
console.error(error.toString());
console.log("Continuing without update.");
callback();
}, true);
}
exec('git pull --rebase', function() {
if (command === 'init') {
callback();
return;
}
reRunThisScriptWithNewVersionThenExit();
});
}
function checkOutSubModules(callback) {
console.log('## Checking Out SubModules');
process.chdir(scriptDir);
exec('git submodule update --init --recursive --rebase', callback, function(error) {
console.log("Could not update submodules:");
console.error(error.toString());
console.log("Continuing without update.");
callback();
});
}
function cleanup(callback) {
process.chdir(origDir);
callback();
}
eventQueue.push(checkGit);
eventQueue.push(checkOutSelf);
eventQueue.push(checkOutSubModules);
eventQueue.push(buildCordovaJsStep);
eventQueue.push(cleanup);
}
/******************************************************************************/
/******************************************************************************/
// Create App
function createCommand(appId, addAndroidPlatform, addIosPlatform) {
var match = /[a-z]+\.[a-z][a-z0-9]*\.([a-z][a-z0-9]*)/i.exec(appId);
if (!match) {
fatal('App Name must follow the pattern: com.company.id');
}
var appName = match[1];
var appDir = null;
function resolveTilde(string) {
// TODO: implement better
if (string.substr(0,1) === '~')
string = process.env.HOME + string.substr(1)
return path.resolve(string)
}
function validateSourceArgStep(callback) {
var sourceArg = commandLineFlags.source;
if (!sourceArg) {
appDir = path.join(scriptDir, 'mobile-chrome-app-samples', 'helloworld', 'www');
} else {
var dirsToTry = [
sourceArg && path.resolve(origDir, resolveTilde(sourceArg)),
sourceArg === 'spec' && path.join(scriptDir, 'chrome-cordova', 'spec', 'www'),
sourceArg && path.join(scriptDir, 'mobile-chrome-app-samples', sourceArg, 'www')
];
for (var i = 0; appDir = dirsToTry[i]; i++) {
if (appDir) console.log('Searching for Chrome app source in ' + appDir);
if (appDir && fs.existsSync(appDir)) {
if (!fs.existsSync(path.join(appDir, 'manifest.json'))) {
fatal('No manifest.json file found within: ' + appDir);
} else {
break;
}
}
}
if (!appDir) {
fatal('Directory does not exist.');
}
}
callback();
}
function createStep(callback) {
console.log('## Creating Your Application');
chdir(origDir);
var platformSpecified = addAndroidPlatform || addIosPlatform;
var cmds = [];
if ((!platformSpecified && hasXcode) || addIosPlatform) {
cmds.push(['platform', 'add', 'ios']);
}
if ((!platformSpecified && hasAndroidSdk) || addAndroidPlatform) {
cmds.push(['platform', 'add', 'android']);
}
ACTIVE_PLUGINS.forEach(function(pluginPath) {
cmds.push(['plugin', 'add', pluginPath]);
});
function runCmd() {
var curCmd = cmds.shift();
if (curCmd) {
console.log(curCmd.join(' '));
exec(cordovaCmd(curCmd), runCmd, undefined, true);
} else {
// Create a script that runs update.js.
if (isWindows) {
fs.writeFileSync('.cordova/hooks/after_prepare/mca-update.cmd', 'cd "' + process.cwd() + '"\n"' + process.argv[0] + '" "' + path.join(scriptDir, scriptName) + '" update-app');
} else {
fs.writeFileSync('.cordova/hooks/after_prepare/mca-update.sh', '#!/bin/sh\ncd "' + process.cwd() + '"\n"' + process.argv[0] + '" "' + path.join(scriptDir, scriptName) + '" update-app');
fs.chmodSync('.cordova/hooks/after_prepare/mca-update.sh', '777');
}
// Create a convenience link to our version of CLI.
if (isWindows) {
fs.writeFileSync('cordova.cmd', '"' + process.argv[0] + '" "' + path.join(scriptDir, 'cordova', 'cordova-cli', 'bin', 'cordova') + '" %*');
} else {
fs.symlinkSync(path.join(scriptDir, 'cordova', 'cordova-cli', 'bin', 'cordova'), 'cordova')
}
callback();
}
}
var curCmd = ['create', appName, appId, appName];
console.log(curCmd.join(' '));
exec(cordovaCmd(curCmd), function() {
chdir(path.join(origDir, appName));
var cordova = require(path.join(scriptDir, 'cordova', 'cordova-cli', 'cordova'));
cordova.config(path.join('.'), {
lib: {
android: {
uri: path.join(scriptDir, 'cordova', 'cordova-android'),
version: "master",
id: "cordova-master"
},
ios: {
uri: path.join(scriptDir, 'cordova', 'cordova-ios'),
version: "master",
id: "cordova-master"
}
}
});
runCmd();
}, undefined, true);
}
function createDefaultApp(callback) {
console.log('## Creating Default Chrome App');
// TODO: add merges dir
var wwwDir = 'www';
if (!fs.existsSync(wwwDir)) {
return;
}
var wwwConfigPath = path.join(wwwDir, 'config.xml');
var configFileData = fs.readFileSync(wwwConfigPath, 'utf8');
recursiveDelete(wwwDir);
fs.mkdirSync(wwwDir);
copyDirectory(appDir, wwwDir, function() {
if (!fs.existsSync(wwwConfigPath)) {
fs.writeFileSync(wwwConfigPath, configFileData);
}
callback();
});
}
function prepareStep(callback) {
exec(cordovaCmd(['prepare']), callback);
}
eventQueue.push(validateSourceArgStep);
eventQueue.push(buildCordovaJsStep);
eventQueue.push(createStep);
eventQueue.push(createDefaultApp);
eventQueue.push(prepareStep);
}
/******************************************************************************/
/******************************************************************************/
// Update App
function updateAppCommand() {
var hasAndroid = fs.existsSync(path.join('platforms', 'android'));
var hasIos = fs.existsSync(path.join('platforms', 'ios'));
if (!fs.existsSync('platforms')) {
fatal('No platforms directory found. Please run script from the root of your project.');
}
function assetDirForPlatform(platform) {
if (platform === 'android') {
return path.join('platforms', platform, 'assets','www');
}
return path.join('platforms', platform, 'www');
}
function removeVestigalConfigFile(platform) {
return function(callback) {
var badPath = path.join(assetDirForPlatform(platform), 'config.xml');
if (fs.existsSync(badPath)) {
console.log('## Removing unnecessary files for ' + platform);
fs.unlinkSync(badPath);
}
callback();
};
}
function fixIosWhitelist(callback) {
var configPath = path.join('platforms', 'ios', path.basename(process.cwd()), 'config.xml')
var data = fs.readFileSync(configPath, 'utf8');
data = data.replace('<access origin="*" />', '<access origin="*" />\n<access origin="chrome-extension://*" />');
fs.writeFileSync(configPath, data);
callback();
}
function createAddJsStep(platform) {
return function(callback) {
console.log('## Updating cordova.js for ' + platform);
copyFile(path.join(scriptDir, 'cordova', 'cordova-js', 'pkg', 'cordova.' + platform + '.js'), path.join(assetDirForPlatform(platform), 'cordova.js'), callback);
};
}
if (hasAndroid) {
eventQueue.push(removeVestigalConfigFile('android'));
eventQueue.push(createAddJsStep('android'));
}
if (hasIos) {
eventQueue.push(removeVestigalConfigFile('ios'));
eventQueue.push(fixIosWhitelist);
eventQueue.push(createAddJsStep('ios'));
}
}
/******************************************************************************/
/******************************************************************************/
function parseCommandLine() {
commandLineFlags = optimist
.usage('Usage: $0 command [commandArgs]\n' +
'\n' +
'Valid Commands:\n' +
'\n' +
'init - Checks for updates to the mobile-chrome-apps repository and ensures the environment is setup correctly.\n' +
' Examples:\n' +
' mca.js init.\n' +
'\n' +
'create [--android] [--ios] [--source path] - Creates a new project.\n' +
' Flags:\n' +
' --android: Add the Android platform (default if android SDK is detected).\n' +
' --ios: Add the iOS platform (default if Xcode is detected).\n' +
' --source=path/to/chromeapp: Create a project based on the given chrome app.\n' +
' Examples:\n' +
' mca.js create org.chromium.Demo\n' +
' mca.js create org.chromium.Spec --android --source=chrome-cordova/spec/www\n'
).options('h', {
alias: 'help',
desc: 'Show usage message.'
}).argv;
var validCommands = {
'create': 1,
'init': 1,
'update-app': 1 // Secret command used by our prepare hook.
};
if (commandLineFlags.h || !validCommands[commandLineFlags._[0]]) {
if (commandLineFlags._[0]) {
fatal('Invalid command: ' + commandLineFlags._[0] + '. Use --help for usage.');
}
optimist.showHelp();
exit(1);
}
}
function main() {
parseCommandLine();
command = commandLineFlags._[0];
if (command == 'update-app') {
updateAppCommand();
} else if (command == 'init') {
toolsCheck();
initCommand();
} else if (command == 'create') {
var appId = commandLineFlags._[1] || '';
toolsCheck();
if (!fs.existsSync(path.join(scriptDir, 'cordova/cordova-js/pkg/cordova.ios.js'))) {
initCommand();
}
createCommand(appId, commandLineFlags.android, commandLineFlags.ios);
}
pump();
}
if (require.main === module) {
main();
}