95 lines
3.7 KiB
JavaScript
95 lines
3.7 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Packs bare-discord-js into a tarball, installs examples/pear-discord-bot deps,
|
|
* applies runtime patches, writes bare-imports.json for Pear, and syncs key patched files.
|
|
*
|
|
* Run from repo root: node scripts/pear-prepare-release.mjs
|
|
*/
|
|
|
|
import { spawnSync } from 'node:child_process';
|
|
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const repoRoot = path.join(__dirname, '..');
|
|
const pearDir = path.join(repoRoot, 'examples', 'pear-discord-bot');
|
|
if (!existsSync(pearDir)) {
|
|
console.error('Missing examples/pear-discord-bot');
|
|
process.exit(1);
|
|
}
|
|
|
|
const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
|
|
console.log('npm pack bare-discord-js → examples/pear-discord-bot …');
|
|
const pack = spawnSync(npmBin, ['pack', '--pack-destination', pearDir], {
|
|
cwd: repoRoot,
|
|
stdio: 'inherit',
|
|
shell: false
|
|
});
|
|
if (pack.status !== 0) process.exit(pack.status ?? 1);
|
|
|
|
console.log('npm install in examples/pear-discord-bot …');
|
|
const npm = spawnSync(npmBin, ['install'], { cwd: pearDir, stdio: 'inherit', shell: false });
|
|
if (npm.status !== 0) process.exit(npm.status ?? 1);
|
|
|
|
const libPkg = JSON.parse(readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
|
|
const libInstalled = path.join(pearDir, 'node_modules', 'bare-discord-js');
|
|
console.log('Reinstall bare-discord-js from fresh tarball (npm may otherwise keep a stale extract) …');
|
|
rmSync(libInstalled, { recursive: true, force: true });
|
|
const reinstallLib = spawnSync(
|
|
npmBin,
|
|
['install', `file:bare-discord-js-${libPkg.version}.tgz`, '--no-save'],
|
|
{ cwd: pearDir, stdio: 'inherit', shell: false }
|
|
);
|
|
if (reinstallLib.status !== 0) process.exit(reinstallLib.status ?? 1);
|
|
|
|
console.log('Applying runtime patches to pear app node_modules …');
|
|
const patch = spawnSync(process.execPath, ['scripts/patch-runtime-deps.mjs'], {
|
|
cwd: repoRoot,
|
|
stdio: 'inherit',
|
|
env: {
|
|
...process.env,
|
|
BARE_DISCORD_REPO_ROOT: repoRoot,
|
|
BARE_DISCORD_PATCH_TARGETS_ROOT: pearDir
|
|
}
|
|
});
|
|
if (patch.status !== 0) process.exit(patch.status ?? 1);
|
|
|
|
const bareImportsSrc = path.join(pearDir, 'node_modules', 'bare-node-runtime', 'imports.json');
|
|
const bareImportsDst = path.join(pearDir, 'bare-imports.json');
|
|
if (existsSync(bareImportsSrc)) {
|
|
cpSync(bareImportsSrc, bareImportsDst);
|
|
console.log('Wrote bare-imports.json (Pear reads import map via bare-fs, not pear:// JSON).');
|
|
}
|
|
|
|
/** Pear often omits deep node_modules trees; ship discord.js as app-owned files. */
|
|
const discordSrc = path.join(pearDir, 'node_modules', 'discord.js');
|
|
const discordVendor = path.join(pearDir, 'vendor', 'discord.js');
|
|
if (existsSync(discordSrc)) {
|
|
rmSync(discordVendor, { recursive: true, force: true });
|
|
mkdirSync(path.dirname(discordVendor), { recursive: true });
|
|
cpSync(discordSrc, discordVendor, { recursive: true });
|
|
console.log('Vendored discord.js → examples/pear-discord-bot/vendor/discord.js (Pear staging).');
|
|
}
|
|
|
|
/** Align patched artifacts with the repo root tree (nested semver/layout can miss regex rules). */
|
|
const vendoredPatches = [
|
|
'node_modules/undici/lib/dispatcher/client.js',
|
|
'node_modules/@discordjs/rest/package.json',
|
|
'node_modules/@discordjs/ws/dist/index.js',
|
|
'node_modules/@discordjs/ws/dist/index.mjs'
|
|
];
|
|
for (const rel of vendoredPatches) {
|
|
const from = path.join(repoRoot, rel);
|
|
const to = path.join(pearDir, rel);
|
|
if (existsSync(from)) {
|
|
mkdirSync(path.dirname(to), { recursive: true });
|
|
cpSync(from, to);
|
|
}
|
|
}
|
|
|
|
console.log(
|
|
'Pear release prep done. Next: cd examples/pear-discord-bot && npm run pear:ship -- <channel-or-link>'
|
|
);
|