@modyo/cli
Version:
Modyo CLI Command line to expose local development tools
47 lines (46 loc) • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const promises_1 = tslib_1.__importDefault(require("node:fs/promises"));
const node_path_1 = tslib_1.__importDefault(require("node:path"));
const debug_1 = tslib_1.__importDefault(require("debug"));
const get_random_hash_1 = tslib_1.__importDefault(require("../utils/get-random-hash"));
const debug = (0, debug_1.default)('push/prepare-zip-files.ts');
async function prepareZipFiles(buildDirectory, html, zipDir = '.zip') {
const hash = (0, get_random_hash_1.default)();
const zipFileName = `widget-${hash}.zip`;
debug(`zip file name ${zipFileName}`);
const buildDirectoryPath = node_path_1.default.resolve(buildDirectory);
const zipDirPath = node_path_1.default.resolve(zipDir);
try {
await promises_1.default.access(zipDir);
debug(`${zipDir} exists`);
await promises_1.default.rm(zipDir, { recursive: true, force: true });
debug(`${zipDir} deleted`);
}
catch (_a) {
debug(`create ${zipDir}`);
}
await promises_1.default.mkdir(zipDir, { recursive: true });
debug(`${zipDir} created`);
const files = await promises_1.default.readdir(buildDirectoryPath);
for (const file of files) {
if (['.js', '.json', '.css', '.wasm'].includes(node_path_1.default.extname(file).toLowerCase())
&& file !== 'manifest.json') {
const from = node_path_1.default.join(buildDirectoryPath, file);
const to = node_path_1.default.join(zipDirPath, file);
debug(`copy file from ${from} to ${to}`);
// eslint-disable-next-line no-await-in-loop
await promises_1.default.cp(from, to);
}
}
await promises_1.default.writeFile(node_path_1.default.join(zipDirPath, 'index.html'), html, 'utf8');
debug(`copy clean html to zip ${html}`);
const zipFilePath = node_path_1.default.resolve(zipFileName);
debug(`zip file path ${zipFilePath}`);
return {
zipDirPath,
zipFilePath,
};
}
exports.default = prepareZipFiles;