/
module.js
211 lines (180 loc) Β· 6.18 KB
/
module.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
/**
* @overview
* Logic for creating new Titanium modules.
*
* @copyright
* Copyright (c) 2014-2018 by Appcelerator, Inc. All Rights Reserved.
*
* @license
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
'use strict';
const appc = require('node-appc'),
Creator = require('../creator'),
fs = require('fs'),
path = require('path'),
ti = require('node-titanium-sdk'),
util = require('util'),
uuid = require('node-uuid'),
wrench = require('wrench'),
__ = appc.i18n(__dirname).__;
/**
* Creates module projects.
*
* @module lib/creators/module
*/
module.exports = ModuleCreator;
/**
* Constructs the module creator.
* @class
* @classdesc Creates a module project.
* @constructor
* @param {Object} logger - The logger instance
* @param {Object} config - The CLI config
* @param {Object} cli - The CLI instance
*/
function ModuleCreator(logger, config, cli) { // eslint-disable-line no-unused-vars
Creator.apply(this, arguments);
this.title = __('Titanium Module');
this.titleOrder = 2;
this.type = 'module';
// build list of all valid platforms
const availablePlatforms = {},
validPlatforms = {};
ti.platforms.forEach(function (platform) {
if (/^iphone|ios|ipad$/.test(platform)) {
validPlatforms['iphone'] = 1;
validPlatforms['ipad'] = 1;
validPlatforms['ios'] = availablePlatforms['ios'] = 1;
} else {
validPlatforms[platform] = availablePlatforms[platform] = 1;
}
});
// add "all"
validPlatforms['all'] = 1;
this.availablePlatforms = [ 'all' ].concat(Object.keys(availablePlatforms));
this.validPlatforms = validPlatforms;
}
util.inherits(ModuleCreator, Creator);
/**
* Initializes the module creator.
* @return {object}
*/
ModuleCreator.prototype.init = function init() {
return {
options: {
id: this.configOptionId(150),
name: this.configOptionName(140),
platforms: this.configOptionPlatforms(120),
template: this.configOptionTemplate(110),
'workspace-dir': this.configOptionWorkspaceDir(170)
}
};
};
/**
* Creates the project directory and copies the project files.
* @param {Function} callback - A function to call after the project has been created
*/
ModuleCreator.prototype.run = function run(callback) {
Creator.prototype.run.apply(this, arguments);
const platforms = ti.scrubPlatforms(this.cli.argv.platforms),
projectName = this.cli.argv.name,
projectDir = this.projectDir = appc.fs.resolvePath(this.cli.argv['workspace-dir'], projectName),
id = this.cli.argv.id;
fs.existsSync(projectDir) || wrench.mkdirSyncRecursive(projectDir);
// download/install the project template
this.processTemplate(function (err, templateDir) {
if (err) {
return callback(err);
}
const variables = {
author: this.config.get('user.name', 'Your Name'),
publisher: this.config.get('app.publisher', 'Your Company'),
guid: uuid.v4(),
tisdkVersion: this.sdk.name,
tisdkPath: this.sdk.path,
year: (new Date()).getFullYear(),
// My Module
moduleName: projectName,
// MyModule
moduleNameCamel: projectName.replace(/[^a-zA-Z0-9_]/g, '_').replace(/_+/g, '_').split(/[\W_]/).map(function (s) { return appc.string.capitalize(s); }).join(''),
// mymodule
moduleNameJSSafe: projectName.toLowerCase().replace(/[^a-zA-Z0-9]/g, '_').replace(/_+/g, '_'),
// com.appcelerator.mymodule
moduleId: id,
// ComAppceleratorMymodule
moduleIdAsIdentifier: id.replace(/[\s-]/g, '_').replace(/_+/g, '_').split(/\./).map(function (s) { return s.substring(0, 1).toUpperCase() + s.substring(1); }).join(''),
// com/appcelerator/mymodule
moduleIdAsFolder: id.replace(/\./g, path.sep),
mainEncryptedAsset: '',
allEncryptedAssets: '',
mainEncryptedAssetReturn: 'return nil;',
allEncryptedAssetsReturn: 'return nil;'
},
tasks = [
function (next) {
// copy the template files, if exists
const dir = path.join(templateDir, 'template');
if (!fs.existsSync(dir)) {
next();
} else {
this.logger.info(__('Template directory: %s', templateDir.cyan));
this.copyDir(dir, projectDir, next, variables);
}
}
];
platforms.scrubbed.forEach(function (platform) {
// if we're using the built-in template, load the platform specific template hooks
const usingBuiltinTemplate = templateDir.indexOf(this.sdk.path) === 0,
platformTemplateDir = path.join(this.sdk.path, platform, 'templates', this.projectType, this.cli.argv.template);
if (usingBuiltinTemplate) {
this.cli.scanHooks(path.join(platformTemplateDir, 'hooks'));
}
tasks.push(function (next) {
this.cli.emit([
'create.pre.platform.' + platform,
'create.pre.' + this.projectType + '.platform.' + platform
], this, function (err) {
if (err) {
return next(err);
}
// only copy platform specific files if we're copying from a built-in template
if (usingBuiltinTemplate) {
this.cli.createHook('create.copyFiles.platform.' + platform, this, function (vars, done) {
this.logger.info(__('Copying %s platform resources', platform.cyan));
this.copyDir(path.join(platformTemplateDir, 'template'), projectDir, function () {
this.cli.emit([
'create.post.' + this.projectType + '.platform.' + platform,
'create.post.platform.' + platform
], this, done);
}.bind(this), vars);
}.bind(this))(appc.util.mix({ platform: platform }, variables), next);
return;
}
this.cli.emit([
'create.post.' + this.projectType + '.platform.' + platform,
'create.post.platform.' + platform
], this, next);
}.bind(this));
});
}, this);
tasks.push(function (next) {
// send the analytics
this.cli.addAnalyticsEvent('project.create.module', {
name: variables.moduleName,
author: variables.author,
moduleid: variables.moduleId,
description: '',
guid: variables.guid,
version: '1.0.0',
copyright: 'copyright: Copyright (c) ' + variables.year + ' by ' + variables.publisher,
minsdk: this.sdk.name,
platforms: platforms.original.join(', '),
date: (new Date()).toDateString()
});
next();
});
appc.async.series(this, tasks, callback);
}.bind(this));
};