RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Builds the plugin SDK package into packages/plugin-sdk/dist. See docs/development.md#plugin-sdk.
|
||||
// Usage: node scripts/buildSdk.mjs
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { realpathSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
export const sdkFolder = 'packages/plugin-sdk';
|
||||
|
||||
// Runs a command and rejects when it fails.
|
||||
export function runCommand(command, args, options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(command, args, { stdio: 'inherit', windowsHide: true, ...options });
|
||||
child.on('error', reject);
|
||||
child.on('exit', code => code === 0 ? resolve() : reject(Error(`${path.basename(command)} ${args.join(' ')} failed with exit code ${code}.`)));
|
||||
});
|
||||
}
|
||||
|
||||
// The declaration files keep the `.ts` import paths of the source. Older TypeScript versions need `.js` paths.
|
||||
export function declarationImports(text) {
|
||||
return text.replace(/(\bfrom\s+["'])(\.{1,2}\/[^"']+)\.ts(["'])/g, '$1$2.js$3').replace(/(\bimport\(\s*["'])(\.{1,2}\/[^"']+)\.ts(["']\s*\))/g, '$1$2.js$3');
|
||||
}
|
||||
|
||||
// Removes dist/, compiles the package with the TypeScript compiler of this repository, and returns the dist/ path.
|
||||
export async function buildSdk(root, run = runCommand) {
|
||||
const folder = path.join(root, sdkFolder);
|
||||
const dist = path.join(folder, 'dist');
|
||||
await fs.rm(dist, { recursive: true, force: true });
|
||||
await run(process.execPath, [path.join(root, 'node_modules/typescript/bin/tsc'), '-p', path.join(folder, 'tsconfig.json')], { cwd: root });
|
||||
for (const name of await fs.readdir(dist)) {
|
||||
if (!name.endsWith('.d.ts')) continue;
|
||||
const file = path.join(dist, name);
|
||||
await fs.writeFile(file, declarationImports(await fs.readFile(file, 'utf8')));
|
||||
}
|
||||
await fs.access(path.join(dist, 'index.js'));
|
||||
return dist;
|
||||
}
|
||||
|
||||
if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) {
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
console.log('SDK build: ' + await buildSdk(root));
|
||||
}
|
||||
Reference in New Issue
Block a user