Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-23997] Reverted bad CommonJS module logic #8623

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 9 additions & 27 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @module cli/_build
*
* @copyright
* Copyright (c) 2009-2015 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
*
* Copyright (c) 2012-2013 Chris Talkington, contributors.
* {@link https://github.com/ctalkington/node-archiver}
Expand Down Expand Up @@ -1472,24 +1472,12 @@ AndroidBuilder.prototype.validate = function validate(logger, config, cli) {
if (module.platform.indexOf('commonjs') != -1) {
module.native = false;

// Look for legacy module.id.js first
module.libFile = path.join(module.modulePath, module.id + '.js');
if (!fs.existsSync(module.libFile)) {
// then package.json TODO Verify the main property points at reale file under the module!
module.libFile = path.join(module.modulePath, 'package.json');
if (!fs.existsSync(module.libFile)) {
// then index.js
module.libFile = path.join(module.modulePath, 'index.js');
if (!fs.existsSync(module.libFile)) {
// then index.json
module.libFile = path.join(module.modulePath, 'index.json');
if (!fs.existsSync(module.libFile)) {
this.logger.error(__('Module %s version %s is missing module files: %s, package.json, index.js, or index.json', module.id.cyan, (module.manifest.version || 'latest').cyan, path.join(module.modulePath, module.id + '.js').cyan) + '\n');
process.exit(1);
}
}
}
this.logger.error(__('Module %s version %s is missing module file: %s', module.id.cyan, (module.manifest.version || 'latest').cyan, module.libFile.cyan) + '\n');
process.exit(1);
}

this.commonJsModules.push(module);
} else {
module.native = true;
Expand Down Expand Up @@ -2404,7 +2392,7 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {

// we use the destination file name minus the path to the assets dir as the id
// which will eliminate dupes
var id = to.replace(opts.origDest, opts.prefix ? opts.prefix : '').replace(/\\/g, '/').replace(/^\//, '');
var id = to.replace(opts.origDest, '').replace(/\\/g, '/').replace(/^\//, '');

if (!jsFiles[id] || !opts || !opts.onJsConflict || opts.onJsConflict(from, to, id)) {
jsFiles[id] = from;
Expand Down Expand Up @@ -2480,16 +2468,10 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) {
this.commonJsModules.forEach(function (module) {
// copy the main module
tasks.push(function (cb) {
_t.logger.debug(__('Copying %s', module.modulePath.cyan));
_t.logger.debug(__('Copying %s', module.libFile.cyan));
copyDir.call(this, {
src: module.modulePath,
// Copy under subfolder named after module.id
dest: path.join(this.buildBinAssetsResourcesDir, path.basename(module.id)),
// Don't copy files under apidoc, docs, documentation, example or assets (assets is handled below)
ignoreRootDirs: ['apidoc', 'documentation', 'docs', 'example', 'assets'],
// Make note that files are copied relative to the module.id folder at dest
// so that we don't see clashes between module1/index.js and module2/index.js
prefix: module.id,
src: module.libFile,
dest: this.buildBinAssetsResourcesDir,
onJsConflict: function (src, dest, id) {
this.logger.error(__('There is a project resource "%s" that conflicts with a CommonJS module', id));
this.logger.error(__('Please rename the file, then rebuild') + '\n');
Expand Down Expand Up @@ -2743,7 +2725,7 @@ AndroidBuilder.prototype.generateRequireIndex = function generateRequireIndex(ca
if (fs.existsSync(file)) {
if (fs.statSync(file).isDirectory()) {
walk(file);
} else if (/\.js(on)?$/.test(filename)) {
} else if (/\.js$/.test(filename)) {
index[file.replace(/\\/g, '/').replace(binAssetsDir + '/', '')] = 1;
}
}
Expand Down
40 changes: 17 additions & 23 deletions iphone/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1997,24 +1997,12 @@ iOSBuilder.prototype.validate = function (logger, config, cli) {
if (module.platform.indexOf('commonjs') !== -1) {
module.native = false;

// Look for legacy module.id.js first
module.libFile = path.join(module.modulePath, module.id + '.js');
if (!fs.existsSync(module.libFile)) {
// then package.json TODO Verify the main property points at reale file under the module!
module.libFile = path.join(module.modulePath, 'package.json');
if (!fs.existsSync(module.libFile)) {
// then index.js
module.libFile = path.join(module.modulePath, 'index.js');
if (!fs.existsSync(module.libFile)) {
// then index.json
module.libFile = path.join(module.modulePath, 'index.json');
if (!fs.existsSync(module.libFile)) {
this.logger.error(__('Module %s version %s is missing module files: %s, package.json, index.js, or index.json', module.id.cyan, (module.manifest.version || 'latest').cyan, path.join(module.modulePath, module.id + '.js').cyan) + '\n');
process.exit(1);
}
}
}
this.logger.error(__('Module %s version %s is missing module file: %s', module.id.cyan, (module.manifest.version || 'latest').cyan, module.libFile.cyan) + '\n');
process.exit(1);
}

this.commonJsModules.push(module);
} else {
module.native = true;
Expand Down Expand Up @@ -4568,18 +4556,18 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {
launchLogos = {},
imageAssets = {};

function walk(src, dest, ignore, origSrc, prefix) {
function walk(src, dest, ignore, origSrc) {
fs.existsSync(src) && fs.readdirSync(src).forEach(function (name) {
var from = path.join(src, name),
relPath = from.replace((origSrc || src) + '/', prefix ? prefix + '/' : ''),
relPath = from.replace((origSrc || src) + '/', ''),
srcStat = fs.statSync(from),
isDir = srcStat.isDirectory();

if ((!ignore || !ignore.test(name)) && (!ignoreDirs || !isDir || !ignoreDirs.test(name)) && (!ignoreFiles || isDir || !ignoreFiles.test(name)) && fs.existsSync(from)) {
var to = path.join(dest, name);

if (srcStat.isDirectory()) {
return walk(from, to, null, origSrc || src, prefix);
return walk(from, to, null, origSrc || src);
}

var parts = name.match(filenameRegExp),
Expand Down Expand Up @@ -4702,11 +4690,17 @@ iOSBuilder.prototype.copyResources = function copyResources(next) {

this.logger.info(__('Analyzing CommonJS modules'));
this.commonJsModules.forEach(function (module) {
this.logger.info(__('Analyzing CommonJS module: %s', module.id));
var dest = path.join(this.xcodeAppDir, path.basename(module.id));
// Pass in the relative path prefix we should give because we aren't copying direct to the root here.
// Otherwise index.js in one module "overwrites" index.js in another (because they're at same relative path inside module)
walk(module.modulePath, dest, /^(apidoc|docs|documentation|example)$/, null, module.id); // TODO Consult some .moduleignore file in the module or something? .npmignore?
var filename = path.basename(module.libFile);
if (jsFiles[filename]) {
this.logger.error(__('There is a project resource "%s" that conflicts with a CommonJS module', filename));
this.logger.error(__('Please rename the file, then rebuild') + '\n');
process.exit(1);
}
jsFiles[filename] = {
src: module.libFile,
dest: path.join(this.xcodeAppDir, path.basename(module.libFile)),
srcStat: fs.statSync(module.libFile)
};
}, this);

function writeAssetContentsFile(dest, json) {
Expand Down