Files
makelore/scripts/sync-learning-player.mjs
brother7 f7171a471a
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled
merge: integrate remote learning module safely
2026-08-17 01:05:49 +08:00

70 lines
3.1 KiB
JavaScript

import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { spawn } from 'node:child_process';
import { resolve } from 'node:path';
import { validateLearningPlayerArtifact } from './learning-player-artifact.mjs';
const makeloreRoot = resolve(import.meta.dirname, '..');
const outputRoot = resolve(makeloreRoot, 'build', 'learning-player');
const buildManifest = JSON.parse(await readFile(
resolve(import.meta.dirname, 'learning-player-build-manifest.json'),
'utf8',
));
function run(command, args, cwd) {
return new Promise((resolveRun, rejectRun) => {
const child = spawn(command, args, { cwd, stdio: 'inherit', shell: process.platform === 'win32' });
child.once('error', rejectRun);
child.once('exit', (code, signal) => {
if (code === 0) resolveRun();
else rejectRun(new Error(`${command} ${args.join(' ')} failed (${signal || code})`));
});
});
}
async function buildLocalSource() {
if (process.env.CI || process.env.MAKELORE_RELEASE_BUILD === '1') {
throw new Error(
'Release builds require MAKELORE_LEARNING_PLAYER_ARTIFACT. Download and verify the versioned OpenMAIC player before packaging.',
);
}
const explicitSource = process.env.MAKELORE_LEARNING_SOURCE?.trim();
if (!explicitSource) {
throw new Error(
'Learning player input is required. Set MAKELORE_LEARNING_PLAYER_ARTIFACT to a verified artifact, or explicitly opt in to a local source build with MAKELORE_LEARNING_SOURCE.',
);
}
const learningRoot = resolve(explicitSource);
const packageJson = JSON.parse(await readFile(resolve(learningRoot, 'package.json'), 'utf8').catch(() => {
throw new Error(`OpenMAIC source checkout is unavailable at ${learningRoot}`);
}));
for (const script of buildManifest.localSource.requiredScripts) {
if (typeof packageJson.scripts?.[script] !== 'string') {
throw new Error(`OpenMAIC source is missing required script: ${script}`);
}
}
const temporaryOutput = resolve(makeloreRoot, 'build', '.learning-player-local');
await rm(temporaryOutput, { recursive: true, force: true });
await run('pnpm', ['run', 'build'], learningRoot);
await run('pnpm', ['run', 'build:makelore-player', '--', temporaryOutput], learningRoot);
return temporaryOutput;
}
const explicitArtifact = process.env.MAKELORE_LEARNING_PLAYER_ARTIFACT?.trim();
const sourceRoot = explicitArtifact ? resolve(explicitArtifact) : await buildLocalSource();
if (sourceRoot === outputRoot) {
throw new Error('MAKELORE_LEARNING_PLAYER_ARTIFACT must not point at build/learning-player');
}
const verified = await validateLearningPlayerArtifact(sourceRoot);
await rm(outputRoot, { recursive: true, force: true });
await mkdir(resolve(outputRoot, '..'), { recursive: true });
await cp(verified.root, outputRoot, { recursive: true });
await writeFile(resolve(outputRoot, 'makelore-build.json'), JSON.stringify({
schemaVersion: 1,
sourceKind: explicitArtifact ? 'versioned-artifact' : 'local-source-build',
htmlSha256: verified.htmlSha256,
}, null, 2));
await validateLearningPlayerArtifact(outputRoot);
process.stdout.write(`Synced verified OpenMAIC player to ${outputRoot}\n`);