const fs = require('fs'); const path = require('path'); const { spawnSync } = require('child_process'); const JSON5 = require('json5'); const root = __dirname; const publishDir = path.resolve(root, 'publish'); const artifacts = [ { script: 'pkg-windows', source: path.resolve(root, 'dist/windows/kit.exe'), target: path.resolve(publishDir, 'windows/x86_64/kit.exe'), localPath: 'publish/windows/x86_64/kit.exe', os: 'windows', arch: 'x86_64', filename: 'kit.exe' }, { script: 'pkg-linux', source: path.resolve(root, 'dist/linux/kit'), target: path.resolve(publishDir, 'linux/x86_64/kit'), localPath: 'publish/linux/x86_64/kit', os: 'linux', arch: 'x86_64', filename: 'kit' } ]; function run(command, args) { console.log(`> ${command} ${args.join(' ')}`); const result = spawnSync(command, args, { cwd: root, stdio: 'inherit', shell: process.platform === 'win32' }); if (result.error) { throw result.error; } if (result.status !== 0) { process.exit(result.status || 1); } } function readJson(file) { return JSON5.parse(fs.readFileSync(file, 'utf-8')); } function readPackage() { return readJson(path.resolve(root, 'package.json')); } function readSourceMeta() { const candidates = [ path.resolve(root, 'meta.json'), path.resolve(root, '..', 'meta.json') ]; for (const file of candidates) { if (fs.existsSync(file)) { return readJson(file); } } return {}; } function copyFile(source, target) { if (!fs.existsSync(source)) { throw new Error(`Build artifact not found: ${source}`); } fs.mkdirSync(path.dirname(target), { recursive: true }); fs.copyFileSync(source, target); } function writeMeta() { const pkg = readPackage(); const sourceMeta = readSourceMeta(); const meta = { id: sourceMeta.id || pkg.name || 'kit', name: sourceMeta.name || pkg.name || 'kit', desc: sourceMeta.desc || sourceMeta.description || '', group: sourceMeta.group || '/modules', dist: artifacts.map((artifact) => ({ path: artifact.localPath, os: artifact.os, arch: artifact.arch, filename: artifact.filename, type: 'executable' })) }; fs.mkdirSync(publishDir, { recursive: true }); fs.writeFileSync(path.resolve(publishDir, 'meta.json'), JSON.stringify(meta, null, 4), 'utf-8'); } function preparePublishDir() { fs.rmSync(publishDir, { recursive: true, force: true }); for (const artifact of artifacts) { copyFile(artifact.source, artifact.target); } writeMeta(); } for (const artifact of artifacts) { run('npm', ['run', artifact.script]); } preparePublishDir(); run('kit3', ['publish', './publish']); console.log('Publish completed.');