]> git.ardis.lu - csvio/commitdiff
Add npm script to create local dev packages
authorArdis Lu <ardisglu@gmail.com>
Sat, 2 Aug 2025 07:21:13 +0000 (00:21 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sat, 2 Aug 2025 07:21:13 +0000 (00:21 -0700)
npm/prepack.js
package.json

index 054d18a48239d0dd5a8293a67f5b8fec11fb0c82..7f5b176a382d7aca3b042422cd146c347bbdbb5a 100644 (file)
@@ -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');
+}
index 8f01a9af4c24b2f8bfc84224ba85b4d7ff490aad..c607765dae3fa3fcfefa1a4bb8220c2d3e471db4 100644 (file)
@@ -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",
This page took 0.041566 seconds and 4 git commands to generate.