forked from vladikoff/chromeos-apk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromeos-apk.js
118 lines (96 loc) · 3.7 KB
/
chromeos-apk.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
var path = require('path');
var fs = require('fs');
var readline = require('readline');
var program = require('commander');
var ncp = require('ncp').ncp;
var chalk = require('chalk');
var rl = readline.createInterface(process.stdin, process.stdout);
var parseApk = require('./lib/parseApk');
function success(appPath) {
console.log(chalk.green('Directory "', appPath, '" created. Copy that directory onto your Chromebook and use "Load unpacked extension" to load the application.'));
process.exit(0);
}
module.exports = function (callback) {
program
.version('4.0.2')
.option('-t, --tablet', 'Create a tablet version')
.option('-s, --scale', 'Enable application window scaling')
.option('-n, --name [value]', 'Extension display name')
.usage('<path_to_apk_file ...>')
.parse(process.argv);
var apk = program.args[0];
callback = callback || success;
if (!apk) {
throw new Error('Please provide a path to an APK file...');
}
parseApk(apk, function (err, data) {
if (err) {
console.log(chalk.yellow('Failed to load APK'));
}
var packageName = null;
try {
packageName = data.package;
} catch (e) {
console.log(chalk.yellow('Failed to parse package name in the APK.'));
}
if (!packageName) {
console.log(chalk.yellow('Unknown APK package.'));
console.log('Please enter the package name (i.e "com.skype.raider", if you get this wrong your app will NOT work): ');
rl.prompt();
rl.on('line', function (text) {
text = text.trim();
if (/\.apk$/.test(text)) {
console.log(chalk.red('Package names do not end with .apk'));
console.log('They usually look like com.application.developer or com.website.www');
process.exit(0);
} else if (text.indexOf(' ') !== -1) {
console.log(chalk.red('Package names do not contain spaces'));
console.log('They usually look like com.application.developer or com.website.www');
process.exit(0);
}
else {
createExtension(text);
}
})
.on('close', function () {
process.exit(0);
});
} else {
createExtension(packageName);
}
function createExtension(packageName) {
var templatePath = path.join(__dirname, '_template');
var appPath = path.join(packageName + '.android');
// TODO: refactor this if needed in the future
ncp(templatePath, appPath, function (err) {
if (err) {
throw err;
}
fs.writeFileSync(path.join(appPath, 'vendor', 'chromium', 'crx', 'custom-android-release-1400197.apk'), fs.readFileSync(apk));
var manifest = JSON.parse(fs.readFileSync(path.join(templatePath, 'manifest.json')));
var messages = JSON.parse(fs.readFileSync(path.join(templatePath, '_locales', 'en', 'messages.json')));
manifest.arc_metadata.name = packageName;
manifest.arc_metadata.packageName = packageName;
manifest.version = '1337';
if (program.name) {
messages.extName.message = program.name;
} else if (packageName) {
messages.extName.message = packageName;
} else {
messages.extName.message = 'App';
}
if (program.tablet) {
manifest.arc_metadata.formFactor = 'tablet';
manifest.arc_metadata.orientation = 'landscape';
}
if (program.scale) {
manifest.arc_metadata.resize = 'scale';
}
fs.writeFileSync(path.join(appPath, 'manifest.json'), JSON.stringify(manifest, null, 2));
fs.writeFileSync(path.join(appPath, '_locales', 'en', 'messages.json'), JSON.stringify(messages, null, 2));
// done.
callback(appPath);
});
}
});
};