-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaplus-babel.js
225 lines (207 loc) · 11.3 KB
/
aplus-babel.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
"use strict";
/***
* __ _ __ __
* ____ ___ ____ / /_ (_)/ /___ / /_ ___ _____ ____
* / __ `__ \ / __ \ / __ \ / // // _ \ / __ \ / _ \ / ___// __ \
* / / / / / // /_/ // /_/ // // // __// / / // __// / / /_/ /
* /_/ /_/ /_/ \____//_.___//_//_/ \___//_/ /_/ \___//_/ \____/
*
* mobile solutions for everyday heroes
*
* @file
* Alloy+ plugin for running babel transformations on your Appcelerator code
*
* @module
* @aplus/babel
*
* @author
* Brenton House <brenton.house@gmail.com>
*
* @copyright
* Copyright (c) 2016 by Superhero Studios Incorporated. All Rights Reserved.
*
* @license
* Licensed under the terms of the MIT License (MIT)
* Please see the license.md included with this distribution for details.
*
*/
var path = require('path');
var fs = require('fs');
var wrench = require('wrench');
var _ = require('lodash');
var logger;
var babel = require('babel-core');
var minimatch = require('minimatch');
/**
* Run babel tranformations on Alloy source code
*
* @param {Object} params - parameters available for executing of Alloy+ plugin.
* @param {Object} params.event - Provides a set of objects and values which may be useful for building tasks:
* @param {Object} params.event.alloyConfig - Contains Alloy compiler configuration information.
* @param {string} params.event.alloyConfig.platform - either android, ios, mobileweb or windows.
* @param {string} [params.event.alloyConfig.file] - file to target for selective compilation.
* @param {string} params.event.alloyConfig.deploytype - compilation environment type: either development, test or production.
* @param {string} params.event.alloyConfig.beautify - if set to true, the output from UglifyJS will be beautified.
* @param {string} params.event.autoStyle - If set to true, autostyle is enabled for the entire project.
* @param {Object} params.event.dependencies - If set to true, autostyle is enabled for the entire project.
* @param {Object} params.event.dir - Contains directory paths to various resources.
* @param {string} params.event.dir.home - absolute path to the Alloy project's app directory.
* @param {string} params.event.dir.project - absolute path to the Alloy project's root directory.
* @param {string} params.event.dir.resources - absolute path to the Alloy project's Resource directory.
* @param {string} params.event.dir.resourcesAlloy - absolute path to the Alloy project's Resource/alloy directory.
* @param {string} params.event.dir.resourcesPlatform - absolute path to the Alloy project's Resource/{platform} directory. (i.e. /Resources/iphone)
* @param {string} params.event.dir.assets - absolute path to the Alloy project's assets.
* @param {string} params.event.dir.config - absolute path to the Alloy project's config.
* @param {string} params.event.dir.controllers - absolute path to the Alloy project's controllers.
* @param {string} params.event.dir.migrations - absolute path to the Alloy project's migrations.
* @param {string} params.event.dir.models - absolute path to the Alloy project's models.
* @param {string} params.event.dir.styles - absolute path to the Alloy project's styles.
* @param {string} params.event.dir.themes - absolute path to the Alloy project's themes.
* @param {string} params.event.dir.views - absolute path to the Alloy project's views.
* @param {string} params.event.dir.widgets - absolute path to the Alloy project's widgets.
* @param {string} params.event.dir.builtins - absolute path to the Alloy project's builtins.
* @param {string} params.event.dir.template - absolute path to the Alloy project's template.
* @param {string} params.event.sourcemap - If true, generates the source mapping files for use with the Studio debugger and other functions.
These files maps the generated Titanium files in the Resources directory to the ones in the app directory.
* @param {string} params.event.theme - Name of the theme being used.
* @param {string} [params.event.code] - Only present for the appjs build hook. Contains the contents of the app.js file.
* @param {string} [params.event.appJSFile] - Only present for the appjs build hook. Contains the the absolute path to the app.js file.
* @param {Object} params.logger - Alloy logger object
* @param {string[]} [params.includes] - Array of glob patterns to match files to be included in transformation
* @param {string} [params.code]
* @param {Object} params.options - babel configuration object (see http://babeljs.io/docs/usage/options/)
* @param {string[]} [params.options.presets=[]] - List of presets (a set of plugins) to load and use..
* @param {string[]} [options.plugins=[]] - List of plugins to load and use.
* @param {boolean} [params.options.babelrc=true] - Specify whether or not to use .babelrc and .babelignore files.
* @param {boolean} [params.options.ast=true] - Include the AST in the returned object
* @param {boolean} [params.options.minified=true] - Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping () from new when safe)
* @param {boolean} [params.options.comments=true] - Output comments in generated output.
* @param {Object} [params.options.env={}] - This is an object of keys that represent different environments. For example, you may have: { env: { production: { someOption: true } } } which will use those options when the enviroment variable BABEL_ENV is set to "production". If BABEL_ENV isn’t set then NODE_ENV will be used, if it’s not set then it defaults to "development"
* @param {string} [params.options.extends=null] - A path to an .babelrc file to extend
*/
function plugin(params) {
logger = params.logger;
params.dirname = params.dirname ? _.template(params.dirname)(params) : params.event.dir.resourcesPlatform;
logger.trace("running babel in directory: " + params.dirname);
_.defaults(params, {
options: {},
includes: ["**/*.js", "!backbone.js"]
});
if (params.code) {
params.code = transformCode(params.code, params.options);
} else {
var files = findFiles(params.dirname, params.includes);
_.forEach(files, function(file) {
transformFile(path.join(params.dirname, file), params.options);
});
}
}
/**
* Replace backslashes for cross-platform usage
* Adapted from https://github.com/sindresorhus/slash
*
* @param {string} intput - value needing to have backslashes replaced in.
* @returns {string}
*/
function replaceBackSlashes(input) {
var isExtendedLengthPath = /^\\\\\?\\/.test(input);
var hasNonAscii = /[^\x00-\x80]+/.test(input);
if (isExtendedLengthPath || hasNonAscii) {
return input;
}
return input.replace(/\\/g, '/');
};
/**
* Find all files that match extension criteria
*
* @param {string} rootpath - Absolute path of the directory from which file search will begin
* @param {string[]|string} [patterns="**"] - Pattern(s) to be used when attempting to match files found
* @returns {string[]} - Matched file paths
*/
function findFiles(rootpath, patterns) {
logger.trace("inside findFiles()");
var patterns = patterns || ['**'];
if (_.isString(patterns)) {
patterns = [patterns];
}
var files = _.map(wrench.readdirSyncRecursive(rootpath), function(filename) {
return path.posix.sep + replaceBackSlashes(filename);
});
var matchedFiles = match(files, patterns, {
nocase: true,
matchBase: true,
dot: true,
});
return _.filter(matchedFiles, function(file) {
return !fs.statSync(path.join(rootpath, file)).isDirectory();
}) || [];
};
/**
* Find items in array that match a set of patterns
* Adapted from https://github.com/sindresorhus/multimatch
*
* @param {string[]} list
* @param {string[]|string} patterns
* @param {Object} options
* @returns {string[]}
*/
function match(list, patterns, options) {
list = list || [];
patterns = patterns || [];
if (_.isString(patterns)) {
patterns = [patterns];
}
if (list.length === 0 || patterns.length === 0) {
return [];
}
options = options || {};
return patterns.reduce(function(ret, pattern) {
var process = _.union
if (pattern[0] === '!') {
pattern = pattern.slice(1);
process = _.difference;
}
return process(ret, minimatch.match(list, pattern, options));
}, []);
};
/**
* Transform a file with babeljs using babel config
*
* @param {string} filepath - absolute path of the file to be transformed
* @param {Object} options - babel configuration object (see http://babeljs.io/docs/usage/options/)
* @param {string[]} [options.presets=[]] - List of presets (a set of plugins) to load and use..
* @param {string[]} [options.plugins=[]] - List of plugins to load and use.
* @param {boolean} [options.babelrc=true] - Specify whether or not to use .babelrc and .babelignore files.
* @param {boolean} [options.ast=true] - Include the AST in the returned object
* @param {boolean} [options.minified=true] - Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping () from new when safe)
* @param {boolean} [options.comments=true] - Output comments in generated output.
* @param {Object} [options.env={}] - This is an object of keys that represent different environments. For example, you may have: { env: { production: { someOption: true } } } which will use those options when the enviroment variable BABEL_ENV is set to "production". If BABEL_ENV isn’t set then NODE_ENV will be used, if it’s not set then it defaults to "development"
* @param {string} [options.extends=null] - A path to an .babelrc file to extend
*/
function transformFile(filepath, options) {
logger.trace("transforming file - " + filepath);
var content = fs.readFileSync(filepath, 'utf8');
var result = transformCode(content, options);
fs.writeFileSync(filepath, result);
}
/**
* Transform the code with bablejs using babel config.
*
* @param {string} code - code to transform using babeljs
* @param {Object} options - babel configuration object (see http://babeljs.io/docs/usage/options/)
* @param {string[]} [options.presets=[]] - List of presets (a set of plugins) to load and use..
* @param {string[]} [options.plugins=[]] - List of plugins to load and use.
* @param {boolean} [options.babelrc=true] - Specify whether or not to use .babelrc and .babelignore files.
* @param {boolean} [options.ast=true] - Include the AST in the returned object
* @param {boolean} [options.minified=true] - Should the output be minified (not printing last semicolons in blocks, printing literal string values instead of escaped ones, stripping () from new when safe)
* @param {boolean} [options.comments=true] - Output comments in generated output.
* @param {Object} [options.env={}] - This is an object of keys that represent different environments. For example, you may have: { env: { production: { someOption: true } } } which will use those options when the enviroment variable BABEL_ENV is set to "production". If BABEL_ENV isn’t set then NODE_ENV will be used, if it’s not set then it defaults to "development"
* @param {string} [options.extends=null] - A path to an .babelrc file to extend
*/
function transformCode(code, options) {
var result = babel.transform(code, options);
var modified = result.code;
return modified;
}
module.exports.execute = plugin;
module.exports.tasks = [];