| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 'use strict';
- const assert = require('assert');
- const fs = require('fs');
- const os = require('os');
- const path = require('path');
- const {
- BOOTSTRAP_MARKER,
- packageHotUpdate,
- } = require('../lib/packager');
- async function main() {
- const fixtureRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'hot-update-builder-'));
- try {
- const projectPath = path.join(fixtureRoot, 'project');
- const buildRoot = path.join(projectPath, 'build', 'android');
- const dataRoot = path.join(buildRoot, 'data');
- await fs.promises.mkdir(path.join(dataRoot, 'src'), { recursive: true });
- await fs.promises.mkdir(path.join(dataRoot, 'assets', 'main'), { recursive: true });
- await fs.promises.mkdir(path.join(dataRoot, 'jsb-adapter'), { recursive: true });
- await fs.promises.writeFile(
- path.join(projectPath, 'package.json'),
- JSON.stringify({ creator: { version: '3.8.8' } }),
- );
- await fs.promises.writeFile(path.join(dataRoot, 'main.js'), 'require("src/system.bundle.js");\n');
- await fs.promises.writeFile(path.join(dataRoot, 'application.js'), 'application();\n');
- await fs.promises.writeFile(path.join(dataRoot, 'src', 'system.bundle.js'), 'system();\n');
- await fs.promises.writeFile(path.join(dataRoot, 'assets', 'main', 'config.json'), '{}\n');
- await fs.promises.writeFile(path.join(dataRoot, 'jsb-adapter', 'engine-adapter.js'), 'adapter();\n');
- const input = {
- buildOptions: { platform: 'android', md5Cache: false },
- buildResult: { dest: buildRoot },
- remoteUrl: 'https://cdn.example.com/game/android/1.2.3',
- version: '1.2.3',
- projectPath,
- };
- const first = await packageHotUpdate(input);
- const second = await packageHotUpdate(input);
- const manifest = JSON.parse(await fs.promises.readFile(first.localManifestPath, 'utf8'));
- assert.equal(manifest.packageUrl, 'https://cdn.example.com/game/android/1.2.3/');
- assert.equal(manifest.version, '1.2.3');
- assert.equal(manifest.engineVersion, '3.8.8');
- assert.ok(manifest.assets['application.js']);
- assert.ok(manifest.assets['src/system.bundle.js']);
- assert.ok(manifest.assets['assets/main/config.json']);
- assert.ok(manifest.assets['jsb-adapter/engine-adapter.js']);
- assert.equal(first.fileCount, 4);
- assert.equal(second.fileCount, 4);
- const mainScript = await fs.promises.readFile(path.join(dataRoot, 'main.js'), 'utf8');
- assert.equal(mainScript.split(BOOTSTRAP_MARKER).length - 1, 1, 'bootstrap should only be injected once');
- assert.ok(await exists(path.join(first.remotePackagePath, 'project.manifest')));
- assert.ok(await exists(path.join(first.remotePackagePath, 'application.js')));
- assert.ok(await exists(path.join(first.remotePackagePath, 'assets', 'main', 'config.json')));
- console.log('hot-update-builder packager tests passed');
- } finally {
- await fs.promises.rm(fixtureRoot, { recursive: true, force: true });
- }
- }
- async function exists(target) {
- try {
- await fs.promises.access(target);
- return true;
- } catch {
- return false;
- }
- }
- main().catch((error) => {
- console.error(error);
- process.exitCode = 1;
- });
|