var path = require('path');
var fs = require('fs');

var program = require('commander');
var parseApk = require('apk-parser2');
var ncp = require('ncp').ncp;
var chalk = require('chalk');

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.'));
}

module.exports = function() {

  program
    .version('1.0.0')
    .option('-t, --tablet', 'Create a tablet version')
    .usage('<path_to_apk_file ...>')
    .parse(process.argv);

  var apk = program.args[0];

  if (!apk) {
    throw new Error('Please provide a path to an APK file...');
  }

  parseApk(apk, function (err, data) {
    if (err) {
      console.log('>> Failed to load APK');
      throw err;
    }

    var packageName = null;

    try {
      packageName = data.manifest[0]['@package'];
    } catch (e) {
      throw new Error('Failed to parse package name in the APK.');
    }

    if (!packageName) {
      throw new Error('Unknown APK package.');
    }

    var templatePath = path.join(__dirname, '_template');
    var appPath = path.join(packageName);

    // 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;
      messages.extName.message = packageName;

      if (program.tablet) {
        manifest.arc_metadata.formFactor = 'tablet';
        manifest.arc_metadata.orientation = 'landscape';
      }

      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.
      success(appPath);
    });
  });

};

@@ -180,16 +180,118 @@
 chalk');
+%0Avar readline = require('readline');%0Avar rl = readline.createInterface(process.stdin, process.stdout);
 %0A%0Afuncti
@@ -478,16 +478,37 @@
 on.'));%0A
+    process.exit(0);%0A
 %7D%0A%0Amodul
@@ -873,12 +873,22 @@
 log(
-'%3E%3E 
+chalk.yellow('
 Fail
@@ -907,25 +907,9 @@
 PK')
-;%0A      throw err
+)
 ;%0A  
@@ -1026,31 +1026,40 @@
 %7B%0A      
-throw new Error
+console.log(chalk.yellow
 ('Failed
@@ -1094,16 +1094,17 @@
 e APK.')
+)
 ;%0A    %7D%0A
@@ -1134,31 +1134,40 @@
 %7B%0A      
-throw new Error
+console.log(chalk.yellow
 ('Unknow
@@ -1182,25 +1182,303 @@
 ckage.')
+)
 ;%0A    
-%7D%0A%0A
+  console.log('Please enter the package name: ');%0A      rl.prompt();%0A      rl.on('line', createExtension)%0A        .on('close',function()%7B%0A          process.exit(0);%0A        %7D);%0A    %7D else %7B%0A      createExtension(packageName);%0A    %7D%0A%0A    function createExtension (packageName) %7B%0A  
     var 
@@ -1527,24 +1527,26 @@
 late');%0A    
+  
 var appPath 
@@ -1568,24 +1568,26 @@
 kageName);%0A%0A
+  
     // TODO:
@@ -1625,16 +1625,18 @@
  future%0A
+  
     ncp(
@@ -1677,24 +1677,26 @@
 rr) %7B%0A      
+  
 if (err) %7B%0A 
@@ -1686,32 +1686,34 @@
      if (err) %7B%0A
+  
         throw er
@@ -1715,16 +1715,18 @@
 ow err;%0A
+  
       %7D%0A
@@ -1724,32 +1724,34 @@
        %7D%0A%0A      
+  
 fs.writeFileSync
@@ -1864,24 +1864,26 @@
 k));%0A%0A      
+  
 var manifest
@@ -1952,24 +1952,26 @@
 t.json')));%0A
+  
       var me
@@ -2066,32 +2066,34 @@
 json')));%0A      
+  
 manifest.arc_met
@@ -2114,24 +2114,26 @@
 ackageName;%0A
+  
       manife
@@ -2171,24 +2171,26 @@
 ackageName;%0A
+  
       messag
@@ -2226,24 +2226,26 @@
 ame;%0A%0A      
+  
 if (program.
@@ -2250,24 +2250,26 @@
 m.tablet) %7B%0A
+  
         mani
@@ -2309,16 +2309,18 @@
 ablet';%0A
+  
         
@@ -2370,27 +2370,31 @@
 ape';%0A      
+  
 %7D%0A%0A
+  
       fs.wri
@@ -2473,24 +2473,26 @@
  null, 2));%0A
+  
       fs.wri
@@ -2600,16 +2600,18 @@
 %0A%0A      
+  
 // done.
@@ -2611,16 +2611,18 @@
 / done.%0A
+  
       su
@@ -2641,20 +2641,28 @@
 h);%0A    
+  
 %7D);%0A
+    %7D%0A
   %7D);%0A%0A%7D

Add custom package input