Skip to content

Commit 2a1cba9

Browse files
committed
fix: strip package fields w/releasing on npm
1 parent fe1b24f commit 2a1cba9

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"preview:copy": "node dist/cli.js test-copyin test-copyout --flat --stat",
4848
"preview:release": "release-it --only-version --dry-run",
4949
"release": "release-it --only-version",
50+
"prepack": "node scripts/prepack.js",
51+
"postpack": "node scripts/postpack.js",
5052
"test": "vitest --watch --config ./vitest.config.mts",
5153
"test:coverage": "vitest --coverage --config ./vitest.config.mts"
5254
},
@@ -69,4 +71,4 @@
6971
"engines": {
7072
"node": "^20.0.0 || >=22.0.0"
7173
}
72-
}
74+
}

scripts/postpack.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { copyFile, unlink } from 'node:fs/promises';
2+
import path from 'node:path';
3+
4+
const packageJsonPath = path.join(process.cwd(), 'package.json');
5+
const backupPath = path.join(process.cwd(), 'package.json.bak');
6+
7+
async function main() {
8+
// Restore the original package.json (with new version)
9+
await copyFile(backupPath, packageJsonPath);
10+
await unlink(backupPath);
11+
console.log('postpack: Restored original package.json');
12+
}
13+
14+
main();

scripts/prepack.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { copyFile, readFile, writeFile } from 'node:fs/promises';
2+
import path from 'node:path';
3+
4+
const fieldsToRemove = ['devDependencies', 'scripts'];
5+
const packageJsonPath = path.join(process.cwd(), 'package.json');
6+
const backupPath = path.join(process.cwd(), 'package.json.bak');
7+
8+
async function main() {
9+
// Backup the current package.json (with new version)
10+
await copyFile(packageJsonPath, backupPath);
11+
12+
const content = await readFile(packageJsonPath, 'utf8');
13+
const pkg = JSON.parse(content);
14+
15+
for (const field of fieldsToRemove) {
16+
delete pkg[field];
17+
}
18+
19+
await writeFile(packageJsonPath, JSON.stringify(pkg, null, 2));
20+
console.log('prepack: Stripped dev fields from package.json for npm publish');
21+
}
22+
23+
main();

0 commit comments

Comments
 (0)