-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcreator.js
More file actions
624 lines (548 loc) · 18.5 KB
/
creator.js
File metadata and controls
624 lines (548 loc) · 18.5 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
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
/**
* @overview
* The base class for platform specific build commands. This ensures some
* commonality between build commands so that hooks can consistently
* access build properties.
*
* @copyright
* Copyright TiDev, Inc. 04/07/2022-Present
*
* @license
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
import appc from 'node-appc';
import async from 'async';
import ejs from 'ejs';
import fields from 'fields';
import fs from 'fs-extra';
import http from 'node:http';
import path from 'node:path';
import request from 'request';
import temp from 'temp';
import ti from 'node-titanium-sdk';
/**
* The base class for project creators (i.e. apps, modules).
*
* General usage is to extend the Creator class and override the run(), methods.
*
* @module lib/creator
*/
export class Creator {
/**
* Constructs the creator state. This needs to be explicitly called from the
* derived creator's constructor.
* @class
* @classdesc Base class for all project creators.
* @constructor
* @param {Object} logger - The logger instance
* @param {Object} config - The CLI config
* @param {Object} cli - The CLI instance
*/
constructor(logger, config, cli) {
this.logger = logger;
this.config = config;
this.cli = cli;
this.availablePlatforms = [];
this.validPlatforms = {};
}
/**
* Init stub function. Meant to be overwritten. Function must be synchronous.
*/
init() {
// stub
}
/**
* Common tasks to run.
*
* @param {Function} callback - A function to call after the function finishes
*/
run() {
if (this.cli.argv.type === 'alloy') {
// alloy app - reset type to normal app
this.cli.argv.type = 'app';
}
this.projectType = this.cli.argv.type;
this.sdk = this.cli.env.getSDK(this.cli.argv.sdk);
}
/**
* Recursively copies files and directories to the destintion. When an .ejs file
* is encountered, the contents is substituted and the .ejs extension is removed.
*
* @param {String} srcDir - The directory to copy
* @param {String} destDir - The directory to copy the files to
* @param {Function} callback - A function to call after all of the files have been copied
* @param {Object} variables - An object to resolve filename substitutions and .ejs templates
* @return {undefined}
*/
copyDir(srcDir, destDir, callback, variables) {
if (!fs.existsSync(srcDir)) {
return callback();
}
variables || (variables = {});
fs.ensureDirSync(destDir);
const _t = this,
ejsRegExp = /\.ejs$/,
nameRegExp = /\{\{(\w+?)\}\}/g,
ignoreDirs = new RegExp(this.config.get('cli.ignoreDirs')), // eslint-disable-line security/detect-non-literal-regexp
ignoreFiles = new RegExp(this.config.get('cli.ignoreFiles')); // eslint-disable-line security/detect-non-literal-regexp
async.eachSeries(fs.readdirSync(srcDir), function (filename, next) {
const src = path.join(srcDir, filename);
if (!fs.existsSync(src)) {
return next();
}
const destName = filename.replace(nameRegExp, function (match, name) {
return variables[name] || variables[name.substring(0, 1).toLowerCase() + name.substring(1)] || match;
});
let dest = path.join(destDir, destName);
if (fs.statSync(src).isDirectory() && !ignoreDirs.test(filename)) {
_t.copyDir(src, dest, next, variables);
} else if (!ignoreFiles.test(filename)) {
if (ejsRegExp.test(filename)) {
dest = dest.replace(ejsRegExp, '');
_t.logger.debug(`Copying ${src.cyan} => ${dest.cyan}`);
// strip the .ejs extension and render the template
fs.writeFileSync(dest, ejs.render(fs.readFileSync(src).toString(), variables));
} else {
_t.logger.debug(`Copying ${src.cyan} => ${dest.cyan}`);
fs.writeFileSync(dest, fs.readFileSync(src));
}
fs.chmodSync(dest, fs.statSync(src).mode & 0o777);
next();
} else {
// ignore
next();
}
}, callback);
}
/**
* Defines the --id option.
*
* @param {Integer} order - The order to apply to this option.
*
* @returns {Object}
*/
configOptionId(order) {
const cli = this.cli,
config = this.config,
logger = this.logger,
idPrefix = config.get('app.idprefix');
function validate(value, callback) {
if (!value) {
logger.error('Please specify an App ID\n');
return callback(true);
}
// general app id validation
if (!/^([a-zA-Z_]{1}[a-zA-Z0-9_-]*(\.[a-zA-Z0-9_-]*)*)$/.test(value)) {
logger.error(`Invalid App ID "${value}"`);
logger.error('The App ID must consist of letters, numbers, dashes, and underscores.');
logger.error('Note: Android does not allow dashes and iOS does not allow underscores.');
logger.error('The first character must be a letter or underscore.');
logger.error('Usually the App ID is your company\'s reversed Internet domain name. (e.g. com.example.myapp)\n');
return callback(true);
}
if (cli.argv.type !== 'app' || cli.argv.platforms.indexOf('android') !== -1) {
if (value.indexOf('-') !== -1) {
logger.error(`Invalid App ID "${value}"`);
logger.error(`Dashes are not allowed in the App ID when targeting ${'Android'.cyan}.\n`);
return callback(true);
}
if (!/^([a-zA-Z_]{1}[a-zA-Z0-9_]*(\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*)$/.test(value)) {
logger.error(`Invalid App ID "${value}"`);
logger.error(`Numbers are not allowed directly after periods when targeting ${'Android'.cyan}.\n`);
return callback(true);
}
if (!ti.validAppId(value)) {
logger.error(`Invalid App ID "${value}"`);
logger.error(`The app must not contain Java reserved words when targeting ${'Android'.cyan}.\n`);
return callback(true);
}
} else {
// android is not in the list of platforms
let counter = 0;
if (value.indexOf('-') !== -1) {
logger.warn('The specified App ID is not compatible with the Android platform.');
logger.warn('Android does not allow dashes in the App ID.');
counter++;
}
if (!/^([a-zA-Z_]{1}[a-zA-Z0-9_]*(\.[a-zA-Z_]{1}[a-zA-Z0-9_]*)*)$/.test(value)) {
counter || logger.warn('The specified App ID is not compatible with the Android platform.');
logger.warn('Android does not allow numbers directly following periods in the App ID.');
counter++;
}
if (!ti.validAppId(value)) {
counter || logger.warn('The specified App ID is not compatible with the Android platform.');
logger.warn('Android does not allow Java reserved words in the App ID.');
counter++;
}
counter && logger.warn('If you wish to add Android support, you will need to fix the <id> in the tiapp.xml.\n');
}
if (value.indexOf('_') !== -1) {
if (cli.argv.type !== 'app' && (cli.argv.platforms.indexOf('ios') !== -1 || cli.argv.platforms.indexOf('iphone') !== -1 || cli.argv.platforms.indexOf('ipad') !== -1)) {
logger.error(`Invalid App ID "${value}"`);
logger.error(`Underscores are not allowed in the App ID when targeting ${'iOS'.cyan}.\n`);
return callback(true);
} else {
logger.warn('The specified App ID is not compatible with the iOS platform.');
logger.warn('iOS does not allow underscores in the App ID.');
logger.warn('If you wish to add iOS support, you will need to fix the <id> in the tiapp.xml.\n');
}
}
callback(null, value);
}
return {
desc: 'the App ID in the format \'com.companyname.appname\'',
order: order,
prompt: function (callback) {
let defaultValue;
const name = cli.argv.name.replace(/[^a-zA-Z0-9]/g, '');
if (idPrefix) {
defaultValue = idPrefix.replace(/\.$/, '') + '.' + (/^[a-zA-Z]/.test(name) || (cli.argv.type === 'app' && cli.argv.platforms.indexOf('android') === -1) ? '' : 'my') + name;
}
callback(fields.text({
default: defaultValue,
promptLabel: 'App ID',
validate: validate
}));
},
required: true,
validate: validate
};
}
/**
* Defines the -c option to select the code base (Swift or Obj-C).
* Kept for backwards compatibility, remove in SDK 10
*
* @param {Integer} order - The order to apply to this option.
*
* @returns {Object}
*/
configOptionCodeBase(order) {
const cli = this.cli;
const validTypes = [ 'swift', 'objc' ];
const logger = this.logger;
function validate(value, callback) {
if (!value || !validTypes.includes(value)) {
logger.error('Please specify a valid code base\n');
return callback(true);
}
callback(null, value);
}
return {
abbr: 'c',
desc: 'the code base of the iOS project',
order: order,
default: !cli.argv.prompt ? 'objc' : undefined, // if we're prompting, then force the platforms to be prompted for, otherwise force 'all'
required: false,
validate: validate,
values: validTypes,
hidden: true
};
}
/**
* Defines the --name option.
*
* @param {Integer} order - The order to apply to this option.
*
* @returns {Object}
*/
configOptionName(order) {
const cli = this.cli,
config = this.config,
logger = this.logger;
function validate(value, callback) {
if (!value) {
logger.error('Please specify a project name\n');
return callback(true);
}
if ((cli.argv.type !== 'app' || cli.argv.platforms.indexOf('android') !== -1) && value.indexOf('&') !== -1) {
if (config.get('android.allowAppNameAmpersands', false)) {
logger.warn('The project name contains an ampersand (&) which will most likely cause problems.');
logger.warn('It is recommended that you change the app name in the tiapp.xml or define the app name using i18n strings.');
logger.warn(`Refer to ${'https://titaniumsdk.com/guide/Titanium_SDK/Titanium_SDK_How-tos/Cross-Platform_Mobile_Development_In_Titanium/Internationalization.html'.cyan} for more information.`);
} else {
logger.error('The project name contains an ampersand (&) which will most likely cause problems.');
logger.error('It is recommended that you change the app name in the tiapp.xml or define the app name using i18n strings.');
logger.error(`Refer to ${'https://titaniumsdk.com/guide/Titanium_SDK/Titanium_SDK_How-tos/Cross-Platform_Mobile_Development_In_Titanium/Internationalization.html'} for more information.`);
logger.error('To allow ampersands in the app name, run:');
logger.error(' ti config android.allowAppNameAmpersands true\n');
return callback(true);
}
}
callback(null, value);
}
return {
abbr: 'n',
desc: 'the name of the project',
order: order,
prompt: function (callback) {
callback(fields.text({
promptLabel: 'Project name',
validate: validate
}));
},
required: true,
validate: validate
};
}
/**
* Defines the --platforms option.
*
* @param {Integer} order - The order to apply to this option.
*
* @returns {Object}
*/
configOptionPlatforms(order) {
const cli = this.cli,
logger = this.logger,
availablePlatforms = this.availablePlatforms,
validPlatforms = this.validPlatforms;
function validate(value, callback) {
// just in case they set -p or --platforms without a value
if (value === true || value === '') {
logger.error(`Invalid platforms value "${value}"\n`);
return callback(true);
}
let goodValues = {};
const badValues = {};
value.trim().toLowerCase().split(',').forEach(function (s) {
if (s = s.trim()) {
if (validPlatforms[s]) {
goodValues[s] = 1;
} else {
badValues[s] = 1;
}
}
}, this);
const badLen = Object.keys(badValues).length;
if (badLen) {
if (badLen === 1) {
logger.error(`Invalid platform: ${Object.keys(badValues).join(', ')}\n`);
} else {
logger.error(`Invalid platforms: ${Object.keys(badValues).join(', ')}\n`);
}
return callback(true);
}
if (goodValues.ios) {
goodValues.iphone = 1;
goodValues.ipad = 1;
delete goodValues.ios;
}
if (goodValues.all) {
goodValues = {};
availablePlatforms.forEach(function (p) {
if (p !== 'all') {
goodValues[p] = 1;
}
});
}
callback(null, Object.keys(goodValues).join(','));
}
return {
abbr: 'p',
default: !cli.argv.prompt ? 'all' : undefined, // if we're prompting, then force the platforms to be prompted for, otherwise force 'all'
desc: 'one or more target platforms.',
order: order,
prompt: function (callback) {
callback(fields.text({
promptLabel: `Target platform (${availablePlatforms.join('|')})`,
default: 'all',
validate: validate
}));
},
required: true,
skipValueCheck: true,
validate: validate,
values: availablePlatforms
};
}
/**
* Defines the --template option.
*
* @param {Integer} order - The order to apply to this option.
* @param {string} defaultValue the default value to use
*
* @returns {Object}
*/
configOptionTemplate(order, defaultValue) {
return {
desc: 'the name of the project template, path to template dir, path to zip file, or URL to zip file',
default: defaultValue || 'default',
order: order,
required: true
};
}
/**
* Defines the --workspace-dir option.
*
* @param {Integer} order - The order to apply to this option.
*
* @returns {Object}
*/
configOptionWorkspaceDir(order) {
const cli = this.cli,
config = this.config,
logger = this.logger;
let workspaceDir = config.app.workspace ? appc.fs.resolvePath(config.app.workspace) : null;
workspaceDir && !fs.existsSync(workspaceDir) && (workspaceDir = null);
function validate(dir, callback) {
if (!dir) {
logger.error('Please specify the workspace directory\n');
return callback(true);
}
dir = appc.fs.resolvePath(dir);
// check if the directory is writable
let prev = null,
curr = dir;
while (curr != prev) { // eslint-disable-line eqeqeq
if (fs.existsSync(curr)) {
if (appc.fs.isDirWritable(curr)) {
break;
} else {
logger.error('Directory "curr" is not writable\n');
return callback(true);
}
}
prev = curr;
curr = path.dirname(curr);
}
// check if the project already exists
if (cli.argv.name && !cli.argv.force && dir) {
const projectDir = path.join(dir, cli.argv.name);
if (fs.existsSync(projectDir)) {
logger.error(`Project already exists: ${projectDir}`);
logger.error('Either change the project name, workspace directory, or re-run this command with the --force flag.\n');
process.exit(1);
}
}
callback(null, dir);
}
return {
abbr: 'd',
default: !cli.argv.prompt && workspaceDir || undefined,
desc: 'the directory to place the project in',
order: order,
prompt: function (callback) {
callback(fields.file({
complete: true,
default: workspaceDir || '.',
ignoreDirs: new RegExp(config.get('cli.ignoreDirs')), // eslint-disable-line security/detect-non-literal-regexp
ignoreFiles: new RegExp(config.get('cli.ignoreFiles')), // eslint-disable-line security/detect-non-literal-regexp
promptLabel: 'Directory to place project',
showHidden: true,
validate: validate
}));
},
required: true,
validate: validate
};
}
/**
* Defines the --workspace-dir option.
*
* @param {Function} next - Callback function
*
* @returns {Object}
*/
processTemplate(next) {
// try to resolve the template dir
const template = this.cli.argv.template = this.cli.argv.template || 'default';
const additionalPaths = this.config.get('paths.templates');
const builtinTemplateDir = appc.fs.resolvePath(this.sdk.path, 'templates', this.cli.argv.type, template);
const searchPaths = [];
// first check if the specified template is a built-in template name
if (fs.existsSync(builtinTemplateDir)) {
this.cli.scanHooks(path.join(builtinTemplateDir, 'hooks'));
return next(null, builtinTemplateDir);
}
if (/^https?:\/\/.+/.test(template)) {
return this.downloadFile(template, next);
}
if (/\.zip$/.test(template) && fs.existsSync(template)) {
return this.unzipFile(template, next);
}
// could be the name of a template in one of the template paths
this.cli.env.os.sdkPaths.forEach(function (dir) {
if (fs.existsSync(dir = appc.fs.resolvePath(dir, 'templates')) && searchPaths.indexOf(dir) === -1) {
searchPaths.push(dir);
}
});
(Array.isArray(additionalPaths) ? additionalPaths : [ additionalPaths ]).forEach(function (p) {
if (p && fs.existsSync(p = appc.fs.resolvePath(p)) && searchPaths.indexOf(p) === -1) {
searchPaths.push(p);
}
});
let dir;
while (dir = searchPaths.shift()) {
if (fs.existsSync(dir = path.join(dir, template))) {
return next(null, dir);
}
}
// last possibility is it's a local directory
if (fs.existsSync(template) && fs.statSync(template).isDirectory()) {
return next(template);
}
next(new Error(`Unable to find template "${template}"`));
}
/**
* Downloads the specified file and unzips it.
*
* @param {String} url - The URL of a zip file to download
* @param {Function} callback - The function to call after the file has been downloaded and unzipped
*/
downloadFile(url, callback) {
const tempName = temp.path({ suffix: '.zip' }),
tempDir = path.dirname(tempName);
fs.ensureDirSync(tempDir);
this.logger.info(`Downloading ${url.cyan}`);
const tempStream = fs.createWriteStream(tempName),
req = request({
url: url,
proxy: this.config.get('cli.httpProxyServer'),
rejectUnauthorized: this.config.get('cli.rejectUnauthorized', true)
});
req.pipe(tempStream);
req.on('error', function () {
fs.existsSync(tempName) && fs.unlinkSync(tempName);
this.logger.log();
this.logger.error(`Failed to download template: ${url}\n`);
callback(true);
}.bind(this));
req.on('response', function (req) {
if (req.statusCode >= 400) {
// something went wrong, abort
this.logger.error(`Request failed with HTTP status code ${req.statusCode} ${http.STATUS_CODES[req.statusCode] || ''}`);
callback(true);
return;
}
tempStream.on('close', function () {
this.unzipFile(tempName, function (err, dir) {
fs.unlinkSync(tempName);
callback(err, dir);
});
}.bind(this));
}.bind(this));
}
/**
* Unzips the specified file.
*
* @param {String} zipFile - A local path to a zip file to unzip
* @param {Function} callback - The function to call after the file has been unzipped
*/
unzipFile(zipFile, callback) {
const dir = temp.mkdirSync({ prefix: 'titanium-' });
fs.ensureDirSync(dir);
this.logger.info(`Extracting ${zipFile.cyan}`);
const logger = this.logger;
this.cli.on('create.finalize', function () {
// clean up the temp dir
if (fs.existsSync(dir)) {
logger.debug(`Removing temp unzip dir: ${dir.cyan}`);
fs.rmdirSync(dir);
}
});
appc.zip.unzip(zipFile, dir, null, function () {
callback(null, dir);
});
}
}