"use strict"; 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.ItemHandler = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const Utils_1 = require("./Utils"); const START_VERSION = 1000; class ItemHandler { constructor(projectDir, mode, config) { this.config = config; const buildDir = path_1.default.join(projectDir, 'build'); const outputBaseDir = path_1.default.join(buildDir, 'subpackage'); Utils_1.Utils.log(`-------------------------------------------Make package [${this.config.key}] handler environment---------------------------------------------------`, Utils_1.Utils.LogLevel.Particular); this.assetsOutputDir = path_1.default.join(outputBaseDir, 'assets', config.key); Utils_1.Utils.log(`Make package [${this.config.key}] assets dir: ${this.assetsOutputDir}`, Utils_1.Utils.LogLevel.Particular); Utils_1.Utils.rmdir(this.assetsOutputDir); Utils_1.Utils.mkdir(this.assetsOutputDir); this.zipCacheOutputDir = path_1.default.join(outputBaseDir, 'cache', config.key); Utils_1.Utils.log(`Make package [${this.config.key}] zip cache dir: ${this.zipCacheOutputDir}`, Utils_1.Utils.LogLevel.Particular); Utils_1.Utils.rmdir(this.zipCacheOutputDir); Utils_1.Utils.mkdir(this.zipCacheOutputDir); this.zipOutputDir = path_1.default.join(outputBaseDir, 'build'); Utils_1.Utils.log(`Make package [${this.config.key}] zip dir: ${this.zipOutputDir}`, Utils_1.Utils.LogLevel.Particular); Utils_1.Utils.mkdir(this.zipOutputDir); this.versionCacheDir = path_1.default.join(projectDir, 'tools', 'subpackage', mode, config.key); Utils_1.Utils.log(`Make package [${this.config.key}] info dir: ${this.versionCacheDir}`, Utils_1.Utils.LogLevel.Particular); Utils_1.Utils.mkdir(this.versionCacheDir); this.resourcesDir = path_1.default.join(buildDir, 'jsb-default', 'res'); Utils_1.Utils.log(`-------------------------------------------Make package [${this.config.key}] handler environment finished------------------------------------------\n\n`, Utils_1.Utils.LogLevel.Particular); } readExistVersion() { const versions = []; const versionDirs = fs_1.default.readdirSync(this.versionCacheDir); for (const versionDir of versionDirs) { const version = parseInt(versionDir); versions.push(version); } return versions.sort((a, b) => { return a - b; }); } pickFile(dir, cache) { cache = cache || []; const files = fs_1.default.readdirSync(dir); for (let i = 0; i < files.length; i++) { const subFileName = files[i]; const subFilePath = path_1.default.join(dir, subFileName); const stat = fs_1.default.statSync(subFilePath); if (stat.isDirectory()) { this.pickFile(subFilePath, cache); } else { const fileName = subFileName.substring(0, subFileName.indexOf('.')); const nameIndex = this.config.uuids.indexOf(fileName); if (nameIndex < 0) continue; Utils_1.Utils.log(`Pick package [${this.config.key}] file: ${subFilePath}`, Utils_1.Utils.LogLevel.Particular); const outAssetBundleDir = path_1.default.join(this.assetsOutputDir, dir.replace(this.resourcesDir, 'res')); Utils_1.Utils.mkdir(outAssetBundleDir); fs_1.default.copyFileSync(subFilePath, path_1.default.join(outAssetBundleDir, subFileName)); cache.push(subFilePath); } } return cache; } readInfo(version) { const infoFilePath = path_1.default.join(this.versionCacheDir, version.toString(), `${this.config.key}.json`); if (!fs_1.default.existsSync(infoFilePath)) return null; const subpackageInfoContent = fs_1.default.readFileSync(infoFilePath); const info = JSON.parse(subpackageInfoContent.toString()); return info; } writeInfo(md5, version, size) { const info = { md5, version, size }; const infoContent = JSON.stringify(info); let infoFilePath = path_1.default.join(this.versionCacheDir, version.toString(), `${this.config.key}.json`); Utils_1.Utils.mkdir(path_1.default.dirname(infoFilePath)); fs_1.default.writeFileSync(infoFilePath, infoContent); } handle() { return __awaiter(this, void 0, void 0, function* () { Utils_1.Utils.log(`-----------------------------------------------------Handle package [${this.config.key}] start-----------------------------------------------------`); const pickFiles = []; this.pickFile(this.resourcesDir, pickFiles); const md5 = Utils_1.Utils.md5dir(this.assetsOutputDir); const existVersions = this.readExistVersion(); let lastItemInfo = { md5: [], version: START_VERSION, size: [] }; let lastVersion = START_VERSION; let result; if (existVersions.length) { lastVersion = existVersions[existVersions.length - 1]; Utils_1.Utils.log(`Package [${this.config.key}] last version: ${lastVersion}`, Utils_1.Utils.LogLevel.Particular); lastItemInfo = this.readInfo(lastVersion) || lastItemInfo; lastVersion = lastItemInfo.version; } else { Utils_1.Utils.log(`Package [${this.config.key}] not have version cache`, Utils_1.Utils.LogLevel.Particular); } const lastMD5CompareResult = Utils_1.Utils.fileMD5Compare(lastItemInfo.md5, md5); Utils_1.Utils.log(`Package [${this.config.key}] compare with last: added=${lastMD5CompareResult.added.length}, deleted:${lastMD5CompareResult.deleted.length}, same:${lastMD5CompareResult.same.length}, changed:${lastMD5CompareResult.changed.length}`, Utils_1.Utils.LogLevel.Particular); const isChanged = lastMD5CompareResult.changed.length || lastMD5CompareResult.added.length; if (!isChanged) { Utils_1.Utils.log(`Package [${this.config.key}] no changes`); result = { info: lastItemInfo, pickFiles }; } else { Utils_1.Utils.log(`Package [${this.config.key}] changed`); const newVersion = lastVersion + 1; const size = []; for (const version of existVersions) { Utils_1.Utils.log(`Pick package [${this.config.key}] assets for version ${version} to ${newVersion}`); const versionInfo = this.readInfo(version); if (!versionInfo) continue; const versionCompareResult = Utils_1.Utils.fileMD5Compare(versionInfo.md5, md5); Utils_1.Utils.log(`Package [${this.config.key}] compare with version ${version}: added=${versionCompareResult.added.length}, deleted:${versionCompareResult.deleted.length} , same:${versionCompareResult.same.length}, changed:${versionCompareResult.changed.length}`, Utils_1.Utils.LogLevel.Particular); const files = versionCompareResult.changed.concat(versionCompareResult.added); const zipCacheDir = path_1.default.join(this.zipCacheOutputDir, `${version}_${newVersion}`); Utils_1.Utils.mkdir(zipCacheDir); for (const fileMD5 of files) { const outPath = path_1.default.join(zipCacheDir, fileMD5.path.replace(this.assetsOutputDir, '')); Utils_1.Utils.mkdir(path_1.default.dirname(outPath)); fs_1.default.copyFileSync(fileMD5.path, outPath); } const zipOutFile = path_1.default.join(this.zipOutputDir, `${this.config.key}_${version}_${newVersion}.zip`); yield Utils_1.Utils.zipdir(zipCacheDir, zipOutFile); Utils_1.Utils.rmdir(zipCacheDir); const zipStat = fs_1.default.statSync(zipOutFile); size.push({ version, size: zipStat.size }); } const zipOutFile = path_1.default.join(this.zipOutputDir, `${this.config.key}_${newVersion}.zip`); yield Utils_1.Utils.zipdir(this.assetsOutputDir, zipOutFile); Utils_1.Utils.rmdir(this.assetsOutputDir); const zipStat = fs_1.default.statSync(zipOutFile); size.push({ version: 0, size: zipStat.size }); this.writeInfo(md5, newVersion, size); result = { info: { version: newVersion, size, md5 }, pickFiles }; } Utils_1.Utils.log(`-----------------------------------------------------Handle package [${this.config.key}] end-------------------------------------------------------\n\n`); return result; }); } } exports.ItemHandler = ItemHandler;