'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const NPM_PREFIX = process.env.NPM_PREFIX || `${process.env.HOME}/.local`;
const CORE_PATH = path.join(
NPM_PREFIX,
'lib/node_modules/@bubblewrap/cli/node_modules/@bubblewrap/core',
);
if (!fs.existsSync(CORE_PATH)) {
console.error(
`[twa-init] @bubblewrap/core not found at ${CORE_PATH}.\n` +
`Run bin/setup-twa first.`,
);
process.exit(1);
}
const { TwaManifest, TwaGenerator, ConsoleLog } = require(CORE_PATH);
const manifestPath = path.resolve(__dirname, 'twa-manifest.json');
const targetDir = path.resolve(__dirname, 'app');
if (!fs.existsSync(manifestPath)) {
console.error(`[twa-init] Missing ${manifestPath}. Render it from the template first.`);
process.exit(1);
}
const json = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
const twaManifest = new TwaManifest(json);
const log = new ConsoleLog('twa-init');
(async () => {
fs.mkdirSync(targetDir, { recursive: true });
const generator = new TwaGenerator();
await generator.createTwaProject(targetDir, twaManifest, log);
const savedManifestPath = path.join(targetDir, 'twa-manifest.json');
const body = JSON.stringify(json, null, 2) + '\n';
fs.writeFileSync(savedManifestPath, body);
fs.writeFileSync(
path.join(targetDir, 'manifest-checksum.txt'),
crypto.createHash('sha1').update(fs.readFileSync(savedManifestPath)).digest('hex'),
);
log.info(`TWA project created at ${targetDir}`);
})().catch((err) => {
console.error(err);
process.exit(1);
});