From: Ardis Lu Date: Sat, 2 Aug 2025 07:21:13 +0000 (-0700) Subject: Add npm script to create local dev packages X-Git-Tag: v0.1.5~14 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=dc2b5af6f11fc8781c2df45e09e718f11e22a391;p=csvio Add npm script to create local dev packages --- diff --git a/npm/prepack.js b/npm/prepack.js index 054d18a..7f5b176 100644 --- a/npm/prepack.js +++ b/npm/prepack.js @@ -1,8 +1,32 @@ +import { existsSync } from 'node:fs'; import { copyFile } from 'node:fs/promises'; import { execSync } from 'node:child_process'; -execSync('npm run types'); +// If called with `npm run pack:dev`, then this code will be executed TWICE: +// node prepack.js -- --dev +// node prepack.js +// npm pack +// node postpack.js +// If called with `npm pack`, then this code will be executed only ONCE: +// node prepack.js +// npm pack +// node postpack.js +// This is the simplest cross-platform solution without (1) using environment variables (non-trivial +// to make work across platforms) or (2) user-defined npm config variables (deprecated in npm v11 and +// will be removed in npm v12). -await copyFile('./package.json', './package.backup.json'); -execSync('npm pkg delete scripts'); -execSync('npm pkg delete devDependencies'); +// First pass if `npm run pack:dev` +if (process.argv.slice(2).includes('--dev')) { + await copyFile('./package.json', './package.backup.json'); + execSync(`npm pkg set version=0.0.0-DEV-${Date.now()}`); + execSync('npm pkg set private=true'); +} +// Second pass if `npm run pack:dev` OR first pass if `npm pack` +else { + if (!existsSync('./package.backup.json')) { + await copyFile('./package.json', './package.backup.json'); + } + execSync('npm run types'); + execSync('npm pkg delete scripts'); + execSync('npm pkg delete devDependencies'); +} diff --git a/package.json b/package.json index 8f01a9a..c607765 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "test:coverage": "npm run test -- --experimental-test-coverage", "bench": "node ./benchmark/core.bench.js", "prepack": "node ./npm/prepack.js", - "postpack": "node ./npm/postpack.js" + "postpack": "node ./npm/postpack.js", + "pack:dev": "npm run prepack -- --dev && npm pack" }, "devDependencies": { "@types/node": "^24.0.13",