diff --git a/nodejs/package.json b/nodejs/package.json index e1a5c7d..11a8cf0 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -15,6 +15,7 @@ "scripts": { "build": "wasm-pack build --target nodejs --out-dir pkg", "test": "node test.js", + "publish:local": "node scripts/publish.mjs", "prepublishOnly": "npm run build" }, "keywords": ["svg", "vectorization", "raster", "wasm", "computer-graphics"], diff --git a/nodejs/scripts/publish.mjs b/nodejs/scripts/publish.mjs new file mode 100644 index 0000000..fe1387f --- /dev/null +++ b/nodejs/scripts/publish.mjs @@ -0,0 +1,42 @@ +#!/usr/bin/env node +// Build the wasm package and publish it, by default to a local npm registry +// (e.g. a Verdaccio instance at http://localhost:4873). +// +// node scripts/publish.mjs # publish to the local registry +// node scripts/publish.mjs --dry-run # build + pack, don't publish +// node scripts/publish.mjs --registry=http://... # override the registry +// NPM_REGISTRY=http://... node scripts/publish.mjs +// +// The registry may also be given via the NPM_REGISTRY env var. + +import { execFileSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +import { dirname, resolve } from 'node:path'; + +const pkgDir = resolve(dirname(fileURLToPath(import.meta.url)), '..'); + +const args = process.argv.slice(2); +const dryRun = args.includes('--dry-run'); +const regArg = args.find((a) => a.startsWith('--registry=')); +const registry = + (regArg && regArg.slice('--registry='.length)) || + process.env.NPM_REGISTRY || + 'http://localhost:4873'; + +function run(cmd, cmdArgs) { + console.log(`\n$ ${cmd} ${cmdArgs.join(' ')}`); + execFileSync(cmd, cmdArgs, { stdio: 'inherit', cwd: pkgDir }); +} + +// 1. Fresh wasm build (regenerates pkg/). +run('wasm-pack', ['build', '--target', 'nodejs', '--out-dir', 'pkg']); + +// 2. Sanity check before publishing. +run('node', ['test.js']); + +// 3. Publish (or dry-run) to the chosen registry. +const publishArgs = ['publish', '--registry', registry]; +if (dryRun) publishArgs.push('--dry-run'); +run('npm', publishArgs); + +console.log(`\n✔ ${dryRun ? 'dry-run for' : 'published to'} ${registry}`);