appc-cli-android
Version:
create an android native project
95 lines (84 loc) • 2.35 kB
JavaScript
var fs = require('fs-extra'),
path = require('path'),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
wrench = require('wrench');
var TYPE = require('../appc').TYPE,
optsMap = require('../appc').OPTS_MAP;
module.exports = {
type: TYPE,
subtype: 'app',
name: 'Android Mobile App',
fields: function(appc) {
return [
appc.fields.id()
]
},
execute: _new
};
function _new(appc, args, opts, callback) {
var workspace = process.cwd();
// translate opts back to arguments since titanium can only be
// invoked vi CLI, there's no module interface
Object.keys(opts).forEach(function(opt) {
if (opt === 'workspace') {
workspace = path.resolve(opts[opt]);
return;
}
});
var appDir = path.join(workspace, 'app');
callback();
}
var filterable = /\.(js|html|json|txt|md|yml|xml|ini|swift|plist|pbxproj|xcworkspacedata|xib|storyboard|xcdatamodel|xccurrentversion|h|c|cpp|m|mm)$/;
var excludeable = /(\.xcworkspace|xcuserdata)/;
function copyDir (opts, fromDir, to, callback) {
wrench.readdirRecursive(fromDir, function readdirCallback(error, curFiles) {
if (error) {
return callback(error);
}
if (curFiles) {
curFiles.forEach(function copyFileCallback(f) {
var fromPath = path.join(fromDir, f),
toPath = path.join(to, f).replace(/__APPNAME__/g,opts.__APPNAME__);
if (excludeable.test(fromPath)) {
return;
}
if (fs.statSync(fromPath).isDirectory()) {
wrench.mkdirSyncRecursive(toPath);
}
else {
copyFile(fromPath, toPath, opts);
}
});
}
else {
callback();
}
});
}
function copyFile(from, to, opts) {
var buf = fs.readFileSync(from);
if (filterable.test(path.basename(to))) {
buf = buf.toString();
Object.keys(opts).forEach(function(key){
var value = opts[key];
buf = buf.replace(new RegExp(key,'g'), value);
});
}
fs.writeFileSync(to, buf);
}
function generateProject(lang, dir, name, id, author, callback) {
fs.ensureDirSync(dir);
var templateDir = path.join(__dirname,'..','templates',lang);
var opts = {
'__APPNAME__': name,
'__ORGID__': id,
'__ORGNAME__': author,
'__AUTHOR__': author
};
copyDir(opts, templateDir, dir, function(){
// write this project to the workspace manifest
//callback && appc.core.writeManifest(path.dirname(dir), { 'android': dir });
callback();
});
}