Skip to content

Commit 409d510

Browse files
authored
Merge pull request #37 from andreban/npx
Enables running llama-pack using `npx`
2 parents 484acd6 + 9b5d6a6 commit 409d510

File tree

10 files changed

+89
-105
lines changed

10 files changed

+89
-105
lines changed

src/bin/llama-pack.js bin/llama-pack.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818

1919
'use strict';
2020

21-
require('..')();
21+
require('../dist')();

package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"name": "llama-pack",
33
"version": "0.1.0",
44
"description": "CLI tool to Generate TWA projects from a Web Manifest",
5-
"bin": {
6-
"outside": "dist/bin/llama-pack"
7-
},
5+
"bin": "bin/llama-pack.js",
86
"scripts": {
9-
"llama-pack": "node dist/bin/llama-pack",
7+
"postinstall": "tsc",
108
"build": "tsc",
119
"lint": "eslint .",
12-
"test": "jasmine --config=jasmine.json"
10+
"test": "tsc && jasmine --config=jasmine.json"
1311
},
1412
"main": "dist/index.js",
1513
"keywords": [
@@ -19,6 +17,10 @@
1917
"progressive-web-apps",
2018
"android"
2119
],
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/GoogleChromeLabs/llama-pack.git"
23+
},
2224
"author": "",
2325
"license": "Apache-2.0",
2426
"dependencies": {
@@ -31,13 +33,13 @@
3133
"node-fetch": "^2.6.0",
3234
"prompt": "^1.0.0",
3335
"tar": "^5.0.5",
34-
"valid-url": "^1.0.9"
36+
"valid-url": "^1.0.9",
37+
"@types/node": "^12.12.12",
38+
"typescript": "^3.7.2"
3539
},
3640
"devDependencies": {
37-
"@types/node": "^12.12.12",
3841
"eslint": "^6.6.0",
3942
"eslint-config-google": "^0.14.0",
40-
"jasmine": "^3.5.0",
41-
"typescript": "^3.7.2"
43+
"jasmine": "^3.5.0"
4244
}
4345
}

src/cli/cmds/build.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
'use strict';
1818

19-
const {androidSdkTools} = require('../../lib/androidSdk');
19+
const AndroidSdkTools = require('../../lib/androidSdk/AndroidSdkTools');
20+
const JdkHelper = require('../../lib/jdk/JdkHelper');
2021
const GradleWraper = require('../../lib/GradleWrapper');
2122
const TwaManifest = require('../../lib/TwaManifest');
2223

@@ -25,7 +26,10 @@ const prompt = require('prompt');
2526
const colors = require('colors/safe');
2627
prompt.get = promisify(prompt.get);
2728

28-
async function build() {
29+
async function build(_, config) {
30+
const jdkHelper = new JdkHelper(process, config);
31+
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper);
32+
2933
if (!await androidSdkTools.checkBuildTools()) {
3034
console.log('Installing Android Build Tools. Please, read and accept the license agreement');
3135
await androidSdkTools.installBuildTools();

src/cli/cmds/init.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
const colorString = require('color-string');
2020
const TwaGenerator = require('../../lib/TwaGenerator');
2121
const TwaManifest = require('../../lib/TwaManifest');
22-
const {keytool} = require('../../lib/jdk');
22+
const KeyTool = require('../../lib/jdk/KeyTool');
23+
const JdkHelper = require('../../lib/jdk/JdkHelper');
2324
const {promisify} = require('util');
2425
const colors = require('colors/safe');
2526
const prompt = require('prompt');
@@ -117,7 +118,7 @@ async function confirmTwaConfig(twaManifest) {
117118
return twaManifest;
118119
}
119120

120-
async function init(args) {
121+
async function init(args, config) {
121122
console.log('Fetching Manifest: ', args.manifest);
122123
try {
123124
let twaManifest = await TwaManifest.fromWebManifest(args.manifest);
@@ -126,20 +127,23 @@ async function init(args) {
126127
const targetDirectory = args.directory || process.cwd();
127128
await twaManifest.saveToFile('./twa-manifest.json');
128129
await twaGenerator.createTwaProject(targetDirectory, twaManifest);
129-
await createSigningKey(twaManifest);
130+
await createSigningKey(twaManifest, config);
130131
return true;
131132
} catch (e) {
132133
console.error('Error Genearating TWA', e);
133134
return false;
134135
}
135136
}
136137

137-
async function createSigningKey(twaManifest) {
138+
async function createSigningKey(twaManifest, config) {
138139
// Signing Key already exists. Skip creation.
139140
if (fs.existsSync(twaManifest.signingKey.path)) {
140141
return;
141142
}
142143

144+
const jdkHelper = new JdkHelper(process, config);
145+
const keytool = new KeyTool(jdkHelper);
146+
143147
prompt.start();
144148

145149
// Ask user if they want to create a signing key now.
@@ -157,7 +161,7 @@ async function createSigningKey(twaManifest) {
157161
return;
158162
}
159163

160-
await keytool.createSigningKey(
164+
await keytool.createSigningKeyIfNeeded(
161165
twaManifest.signingKey.path,
162166
twaManifest.signingKey.alias,
163167
);

src/cli/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717
'use strict';
1818

1919
const minimist = require('minimist');
20-
const config = require('../lib/Config');
20+
const Config = require('../lib/Config');
2121

2222
class Cli {
2323
async run(args) {
24-
await config.check();
24+
const config = await Config.loadOrCreate();
25+
2526
args = minimist(args);
2627
const command = args._[0] || 'help';
2728
switch (command) {
2829
case 'help':
29-
return await require('./cmds/help')(args);
30+
return await require('./cmds/help')(args, config);
3031
case 'init':
31-
return await require('./cmds/init')(args);
32+
return await require('./cmds/init')(args, config);
3233
case 'update':
33-
return await require('./cmds/update')(args);
34+
return await require('./cmds/update')(args, config);
3435
case 'build':
35-
return await require('./cmds/build')(args);
36+
return await require('./cmds/build')(args, config);
3637
default:
3738
throw new Error(`"${command}" is not a valid command!`);
3839
}

src/lib/Config.js

+52-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,50 @@
1818

1919
const {promisify} = require('util');
2020
const fs = require('fs');
21+
const homedir = require('os').homedir();
22+
const path = require('path');
2123
const fileExists = promisify(fs.exists);
24+
const prompt = require('prompt');
25+
prompt.get = promisify(prompt.get);
26+
27+
const CONFIG_FILE_NAME = path.join(homedir, '/.llama-pack/llama-pack-config.json');
2228

2329
class Config {
30+
async loadConfig() {
31+
if (!await fileExists(CONFIG_FILE_NAME)) {
32+
return false;
33+
}
34+
const config = JSON.parse(await fs.promises.readFile(CONFIG_FILE_NAME));
35+
Object.assign(this, config);
36+
return true;
37+
}
38+
39+
async saveConfig() {
40+
await fs.promises.mkdir(path.join(homedir, '/.llama-pack'));
41+
await fs.promises.writeFile(CONFIG_FILE_NAME, JSON.stringify(this));
42+
}
43+
44+
async createConfig() {
45+
const schema = {
46+
properties: {
47+
jdkPath: {
48+
name: 'jdkPath',
49+
description: 'Path to the JDK:',
50+
required: true,
51+
conform: fs.existsSync,
52+
},
53+
androidSdkPath: {
54+
name: 'androidSdkPath',
55+
description: 'Path to the Android SDK:',
56+
required: true,
57+
conform: fs.existsSync,
58+
},
59+
},
60+
};
61+
const result = await prompt.get(schema);
62+
Object.assign(this, result);
63+
}
64+
2465
async check() {
2566
if (!await fileExists(this.jdkPath)) {
2667
throw new Error(`${this.jdkPath} does not exist. Check the jdkPath on your config`);
@@ -30,10 +71,16 @@ class Config {
3071
`${this.androidSdkPath} does not exist. Check the androidSdkPath on your config`);
3172
};
3273
}
74+
75+
static async loadOrCreate() {
76+
const config = new Config();
77+
const configExists = await config.loadConfig();
78+
if (!configExists) {
79+
await config.createConfig();
80+
await config.saveConfig();
81+
}
82+
return config;
83+
}
3384
}
3485

35-
module.exports = (() => {
36-
const config = new Config();
37-
Object.assign(config, require('../llama-pack-config'));
38-
return config;
39-
})();
86+
module.exports = Config;

src/lib/TwaGenerator.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,16 @@ class TwaGenerator {
158158
};
159159

160160
console.log('Generating Android Project files:');
161-
const templateDirectory = path.join(__dirname, '../template_project');
161+
console.log(__dirname);
162+
const templateDirectory = path.join(__dirname, '../../template_project');
162163

163164
const copyFileList = new Set(COPY_FILE_LIST);
164165
if (!args.maskableIconUrl) {
165166
DELETE_FILE_LIST.forEach((file) => copyFileList.delete(file));
166167
}
167168

168169
// Copy Project Files
169-
await this._copyStaticFiles(templateDirectory, targetDirectory, [...copyFileList]);
170+
await this._copyStaticFiles(templateDirectory, targetDirectory, Array.from(copyFileList));
170171

171172
// Generate templated files
172173
await this._applyTemplates(templateDirectory, targetDirectory, TEMPLATE_FILE_LIST, args);

src/lib/androidSdk/index.js

-25
This file was deleted.

src/lib/jdk/index.js

-28
This file was deleted.

src/llama-pack-config.js

-22
This file was deleted.

0 commit comments

Comments
 (0)