@lcap/builder
Version:
lcap builder utils
92 lines (91 loc) • 3.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genFilesHashMap = genFilesHashMap;
exports.default = genManifestConfig;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const loader_utils_1 = require("loader-utils");
const dayjs_1 = __importDefault(require("dayjs"));
const getManifest = (type, outDir, modulesOutDir = 'es') => {
return {
nasl: [
...type === 'extension'
? ['nasl.extension.json', 'nasl.extension.d.ts']
: [`${outDir}/nasl.ui.json`, `${outDir}/nasl.ui.d.ts`],
],
runtime: [
`${outDir}/index.js`,
`${outDir}/index.css`,
],
theme: [
`${outDir}/theme.config.json`,
`${outDir}/theme.json`,
`${outDir}/theme/index.html`,
],
style: [
`${outDir}/index.css-info-map.json`,
],
package: ['source.zip'],
i18n: [`${outDir}/i18n.json`],
modules: [
`${modulesOutDir}/modules.json`,
`${modulesOutDir}/index.d.ts`,
],
ide: [
`${outDir}/ide/index.js`,
`${outDir}/ide/index.css`,
`${outDir}/setters.json`,
],
};
};
function genFilesHashMap(rootPath, files) {
return __awaiter(this, void 0, void 0, function* () {
const hashMap = {};
yield Promise.all(files.map((fileName) => __awaiter(this, void 0, void 0, function* () {
const filePath = path_1.default.join(rootPath, fileName);
const content = yield fs_extra_1.default.readFile(filePath);
const contentHash = (0, loader_utils_1.getHashDigest)(content, 'md5', 'hex', 16);
hashMap[fileName] = contentHash;
})));
return hashMap;
});
}
function needGenHash(key, filePath) {
if ([
'modules',
'package',
].includes(key) || filePath.endsWith('.html') || filePath.endsWith('.zip') || filePath.endsWith('.tgz')) {
return false;
}
return true;
}
function genManifestConfig(options) {
return __awaiter(this, void 0, void 0, function* () {
const manifest = getManifest(options.type, options.destDir);
const hashFiles = [];
Object.keys(manifest).forEach((key) => {
manifest[key] = manifest[key].filter((filePath) => fs_extra_1.default.existsSync(path_1.default.join(options.rootPath, filePath)));
manifest[key].forEach((filePath) => {
if (needGenHash(key, filePath)) {
hashFiles.push(filePath);
}
});
});
manifest.package.push('zip.tgz');
manifest.hashMap = yield genFilesHashMap(options.rootPath, hashFiles);
manifest.hashMap['zip.tgz'] = (0, dayjs_1.default)().format('YYYYMMDDHHmmssSSS');
return manifest;
});
}