Skip to content

Commit

Permalink
feat: Update to babel 7 ecosystem (#37)
Browse files Browse the repository at this point in the history
- Use babel 7.0.0
- Add quick unit test that polyfilling works.
- Replace wrench usage with fs-extra.
- bump min node to 8.9.0
- bump version to 1.0.0

Implements TIMOB-25650

Fix indentation in test fixtures, check for opts.filename existance in global-plugin, pass @babel/core to babel-plugin-tester to override their babel-core require

* Use avd id rather than name in tests

Fixes tests when emulator name contains spaces

* Fix node 6 compat

* Update dependencies
  • Loading branch information
sgtcoolguy committed Nov 13, 2018
1 parent 00b5334 commit 3cfe434
Show file tree
Hide file tree
Showing 15 changed files with 2,512 additions and 843 deletions.
9 changes: 5 additions & 4 deletions Gruntfile.js
Expand Up @@ -8,7 +8,8 @@ module.exports = function (grunt) {
src: [
'Gruntfile.js',
'lib/**/*.js',
'tests/**/*.js'
'tests/**/*.js',
'!tests/resources/**'
]
},
mocha_istanbul: {
Expand All @@ -18,10 +19,10 @@ module.exports = function (grunt) {
ignoreLeaks: false,
reportFormats: [ 'cobertura' ],
check: {
statements: 55,
branches: 39,
statements: 56,
branches: 40,
functions: 60,
lines: 55
lines: 56
}
},
src: [ 'tests/*_test.js' ]
Expand Down
5 changes: 2 additions & 3 deletions lib/adb.js
Expand Up @@ -19,12 +19,11 @@
const appc = require('node-appc');
const __ = appc.i18n(__dirname).__;
const async = require('async');
const fs = require('fs');
const fs = require('fs-extra');
const net = require('net');
const path = require('path');
const spawn = require('child_process').spawn; // eslint-disable-line security/detect-child-process
const StreamSplitter = require('stream-splitter');
const wrench = require('wrench');

require('colors');

Expand Down Expand Up @@ -756,7 +755,7 @@ ADB.prototype.pull = function pull(deviceId, src, dest, callback) {
var destDir = path.dirname(dest);

try {
fs.existsSync(destDir) || wrench.mkdirSyncRecursive(destDir);
fs.ensureDirSync(destDir);

androidDetect(this.config, function (err, results) {
if (err) {
Expand Down
3 changes: 2 additions & 1 deletion lib/babel-plugins/global-scope.js
Expand Up @@ -12,7 +12,8 @@ module.exports = function (babel) {
visitor: {
Program: function (path, state) {
const logger = state.opts.logger;
if (_path.basename(this.file.opts.filename) !== 'app.js') {
const opts = this.file.opts;
if (!opts.filename || _path.basename(opts.filename) !== 'app.js') {
return;
}
for (const bodyPath of path.get('body')) {
Expand Down
7 changes: 3 additions & 4 deletions lib/builder.js
Expand Up @@ -16,10 +16,9 @@
const
appc = require('node-appc'),
crypto = require('crypto'),
fs = require('fs'),
fs = require('fs-extra'),
path = require('path'),
ti = require('./titanium'),
wrench = require('wrench'),
i18n = appc.i18n(__dirname),
__ = i18n.__,
__n = i18n.__n;
Expand Down Expand Up @@ -226,7 +225,7 @@ Builder.prototype.copyFileSync = function copyFileSync(src, dest, opts) {

opts && typeof opts === 'object' || (opts = {});

fs.existsSync(parent) || wrench.mkdirSyncRecursive(parent);
fs.ensureDirSync(parent);

if (!opts.forceSymlink && (opts.forceCopy || !this.symlinkFilesOnCopy || opts.contents)) {
if (exists) {
Expand Down Expand Up @@ -267,7 +266,7 @@ Builder.prototype.copyDirSync = function copyDirSync(src, dest, opts) {
opts && typeof opts === 'object' || (opts = {});

(function copy(src, dest, isRootDir) {
fs.existsSync(dest) || wrench.mkdirSyncRecursive(dest);
fs.ensureDirSync(dest);

fs.readdirSync(src).forEach(function (name) {
const srcFile = path.join(src, name);
Expand Down
5 changes: 2 additions & 3 deletions lib/emulators/genymotion.js
Expand Up @@ -16,10 +16,9 @@
const appc = require('node-appc'),
__ = appc.i18n(__dirname).__,
async = require('async'),
fs = require('fs'),
fs = require('fs-extra'),
path = require('path'),
spawn = require('child_process').spawn,
wrench = require('wrench'),
ADB = require('../adb'),
EmulatorManager = require('../emulator'),
defaultTitaniumHomeDir = appc.fs.resolvePath('~', '.titanium'),
Expand Down Expand Up @@ -558,7 +557,7 @@ exports.start = function start(config, emu, opts, callback) {
pidDir = path.join(titaniumHomeDir, 'genymotion'),
pidFile = path.join(pidDir, emu.name + '.pid');
fs.existsSync(pidFile) && fs.unlinkSync(pidFile);
fs.existsSync(pidDir) || wrench.mkdirSyncRecursive(pidDir);
fs.ensureDirSync(pidDir);
fs.writeFileSync(pidFile, child.pid);

callback(null, device);
Expand Down
83 changes: 58 additions & 25 deletions lib/jsanalyze.js
Expand Up @@ -13,17 +13,17 @@
*/
'use strict';

const appc = require('node-appc'),
fs = require('fs'),
path = require('path'),
DOMParser = require('xmldom').DOMParser,
babel = require('babel-core'),
babylon = require('babylon'),
types = require('babel-types'),
traverse = require('babel-traverse').default,
minify = require('babel-preset-minify'),
env = require('babel-preset-env'),
__ = appc.i18n(__dirname).__;
const appc = require('node-appc');
const fs = require('fs-extra');
const path = require('path');
const DOMParser = require('xmldom').DOMParser;
const babel = require('@babel/core');
const babylon = require('@babel/parser');
const types = require('@babel/types');
const traverse = require('@babel/traverse').default;
const minify = require('babel-preset-minify');
const env = require('@babel/preset-env');
const __ = appc.i18n(__dirname).__;

let apiUsage = {};

Expand Down Expand Up @@ -100,6 +100,48 @@ function getTitaniumExpression(member) {
return null;
}

/**
* Given an npm module id, this will copy it and it's dependencies to a
* destination "node_modules" folder.
* Note that all of the packages are copied to the top-level of "node_modules",
* not nested!
* Also, to shortcut the logic, if the original package has been copied to the
* destination we will *not* attempt to read it's dependencies and ensure those
* are copied as well! So if the modules version changes or something goes
* haywire and the copies aren't full finished due to a failure, the only way to
* get right is to clean the destination "node_modules" dir before rebuilding.
*
* @param {String} moduleId The npm package/module to copy (along with it's dependencies)
* @param {String} destNodeModulesDir path to the destination "node_mdoules" folder
* @param {Array} [paths=[]] Array of additional paths to pass to require.resolve() (in addition to those from require.resolve.paths(moduleId))
*/
function copyPackageAndDependencies(moduleId, destNodeModulesDir, paths = []) {
const destPackage = path.join(destNodeModulesDir, moduleId);
if (fs.existsSync(destPackage)) {
return; // if the module seems to exist in the destination, just skip it.
}

// copy the dependency's folder over
let pkgJSONPath;
if (require.resolve.paths) {
const thePaths = require.resolve.paths(moduleId);
pkgJSONPath = require.resolve(path.join(moduleId, 'package.json'), { paths: thePaths.concat(paths) });
} else {
pkgJSONPath = require.resolve(path.join(moduleId, 'package.json'));
}
const srcPackage = path.dirname(pkgJSONPath);
const srcPackageNodeModulesDir = path.join(srcPackage, 'node_modules');
fs.copySync(srcPackage, destPackage, {
preserveTimestamps: true,
filter: src => !src.startsWith(srcPackageNodeModulesDir)
});
// Now read it's dependencies and recurse on them
const packageJSON = fs.readJSONSync(pkgJSONPath);
for (const dependency in packageJSON.dependencies) {
copyPackageAndDependencies(dependency, destNodeModulesDir, [ srcPackageNodeModulesDir ]);
}
}

/**
* Analyzes a string containing JavaScript for all Titanium API symbols.
*
Expand Down Expand Up @@ -149,7 +191,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
}

// find all of the titanium symbols
let symbols = {};
const symbols = {};
traverse(ast, {
MemberExpression: {
enter: function (path) {
Expand Down Expand Up @@ -188,8 +230,8 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
// transpile
if (opts.transpile) {
options.plugins.push(require.resolve('./babel-plugins/global-this'));
options.plugins.push(require.resolve('babel-plugin-transform-async-to-generator'));
options.presets.push([ env, { targets: opts.targets, useBuiltIns: true } ]);
options.plugins.push(require.resolve('@babel/plugin-transform-async-to-generator'));
options.presets.push([ env, { targets: opts.targets, useBuiltIns: 'usage' } ]);

// install polyfill
if (opts.resourcesDir) {
Expand All @@ -201,16 +243,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
}

// copy over polyfill and its dependencies
// WARNING: REMEMBER TO UPDATE THIS IF 'babel-polyfill' DEPENDENCIES CHANGE!
[ 'babel-polyfill', 'core-js', 'regenerator-runtime' ].forEach((moduleName) => {
const moduleSrcDir = path.dirname(require.resolve(path.join(moduleName, 'package.json'))),
moduleDstDir = path.join(modulesDir, moduleName);

// copy over module if it does not exist
if (!fs.existsSync(moduleDstDir)) {
appc.fs.copyDirSyncRecursive(moduleSrcDir, moduleDstDir);
}
});
copyPackageAndDependencies('@babel/polyfill', modulesDir);
}
}

Expand All @@ -227,7 +260,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
deadcode: false
} ]);

options.plugins.push(require.resolve('babel-plugin-transform-property-literals'));
options.plugins.push(require.resolve('@babel/plugin-transform-property-literals'));
}

if (opts.plugins.length) {
Expand Down
5 changes: 2 additions & 3 deletions lib/tiappxml.js
Expand Up @@ -8,9 +8,8 @@

const appc = require('node-appc');
const DOMParser = require('xmldom').DOMParser;
const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const wrench = require('wrench');

const plist = appc.plist;
const version = appc.version;
Expand Down Expand Up @@ -891,7 +890,7 @@ function tiapp(filename) {
Object.defineProperty(this, 'save', {
value: function (file) {
if (file) {
wrench.mkdirSyncRecursive(path.dirname(file));
fs.ensureDirSync(path.dirname(file));
fs.writeFileSync(file, this.toString('xml'));
}
return this;
Expand Down

0 comments on commit 3cfe434

Please sign in to comment.