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

feat: download _alloy_ db #1410

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions Alloy/alloy.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ program.command('purgetss')
.option('--modules', 'Copy or generate the corresponding CommonJS module into `./app/lib/` folder.')
.option('--vendor <arguments>', 'Use any of the following arguments to copy specific vendors: fa = Font Awesome, md = Material Design or f7 = Framework7 Icons');

program.command('db')
.description('Fetches the default alloy database (_alloy_) from the device')
.option('get', 'Downloads the _alloy_ database to your app folder');

program.command('info [type]', { hidden: true });

program.command('debugger', { hidden: true });
Expand Down
68 changes: 68 additions & 0 deletions Alloy/commands/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const {
exec,
execSync
} = require('child_process');
const U = require('../../utils');
const tiapp = require('../../tiapp');
const fs = require('fs');
const os = require('os');
const path = require('path');

module.exports = async function(args, program) {
try {
if (args.length === 0) {
U.die('Missing parameter "get"');
} else {
args.forEach(command => {
switch (command) {
case 'get':
tiapp.init();
var adbPath = 'adb';
if (os.platform() === 'darwin') {
// default path
adbPath = '~/Library/Android/sdk/platform-tools/adb';

// try to get android.sdkPath from ti config
const output = execSync('ti config android.sdkPath --json');
const jsonObject = JSON.parse(output);
if (!Object.prototype.hasOwnProperty.call(jsonObject, 'success')) {
// found string
adbPath = jsonObject;
}

// check if adb is in that folder
const testPath = path.join(adbPath, 'platform-tools/adb');
if (!fs.existsSync(testPath)) {
U.die('adb not found at ' + testPath + '. Please check "ti config android.sdkPath" and point to your SDK folder.');
return;
} else {
// use the new path
adbPath = testPath;
}
}
console.log('Downloading _alloy_ database to: ' + tiapp.getBundleId() + '.db');
execCommand(adbPath + ' shell "run-as ' + tiapp.getBundleId() + ' cat /data/data/' + tiapp.getBundleId() + '/databases/_alloy_" > ' + tiapp.getBundleId() + '.db');
break;
}
});
}
} catch (error) {
console.error('Failed to get database: ' + error);
}
};

function execCommand(currentCommand) {
exec(currentCommand, (error, response) => {
if (error) {
if (error.message) {
console.error(error.message);
} else {
console.error("Couldn't fetch database");
}
}
if (response) {
console.log(response);
}
return true;
});
}
28 changes: 28 additions & 0 deletions Alloy/tiapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ tiapp.getSdkVersion = function() {
}
};

tiapp.getBundleId = function() {
var elems = doc.documentElement.getElementsByTagName('id');
if (elems && elems.length > 0) {
var bundleId = U.XML.getNodeText(elems.item(elems.length - 1));
var isForced = false;
for (var i = 0; i < elems.length; i++) {
if (elems.item(i).getAttribute('platform') === 'android') {
// platform specific ID
isForced = true;
bundleId = U.XML.getNodeText(elems.item(i));
} else if (elems.item(i).getAttribute('platform') === '' && !isForced) {
// normal ID - only if no platform specific was set already
bundleId = U.XML.getNodeText(elems.item(i));
}
}
return bundleId;
}
};

function getSdkSelectVersion() {
var homeDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
file = path.join(homeDir, '.titanium', 'config.json');
if (!fs.existsSync(file)) {
U.die('Titanium configuration file does not exist at "' + file + '"');
}
var ticonfig = JSON.parse(fs.readFileSync(file, {encoding: 'utf8'}));
return ticonfig.sdk.selected;
}

// Get the value of a property from the tiapp.xml
tiapp.getProperty = function(name) {
Expand Down