Skip to content

Commit

Permalink
chore: Strip workspaces from package.json on publishing (#153)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Peveler <matt@popsql.com>
  • Loading branch information
MasterOdin committed Jul 20, 2023
1 parent 9bba88d commit 887fb53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"scripts": {
"build": "npm run lint && tsc",
"test": "npm test --workspaces",
"prepublishOnly": "npm run build",
"prepublishOnly": "npm run build && node scripts/publish_helper.js prepublish",
"postpublish": "node scripts/publish_helper.js postpublish",
"lint": "eslint . --ext .ts && echo 'No lint errors found!'"
},
"keywords": [],
Expand Down
30 changes: 30 additions & 0 deletions scripts/publish_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');

const error = (msg) => {
console.error(msg);
process.exit(1);
}

const packagePath = path.join(__dirname, '..', 'package.json');
const packageBakPath = packagePath + '.bak';

const type = process.argv[2];
if (type === 'prepublish') {
if (fs.existsSync(packageBakPath)) {
fs.copyFileSync(packageBakPath, packagePath);
fs.unlinkSync(packageBakPath);
}
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
delete packageJson.workspaces;
fs.copyFileSync(packagePath, './package.json.bak');
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));
} else if (type === 'postpublish') {
if (!fs.existsSync(packageBakPath)) {
error('package.json.bak not found')
}
fs.copyFileSync(packageBakPath, packagePath);
fs.unlinkSync(packageBakPath);
} else {
error(`invalid type: ${type}. Expected 'prepublish' or 'postpublish'.`);
}

0 comments on commit 887fb53

Please sign in to comment.