From 7d1466a0b0645356a2f01a4e94ce0f4572111183 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Wed, 19 Jul 2023 11:37:53 -0700 Subject: [PATCH] chore: script workspaces from package.json on publishing Signed-off-by: Matthew Peveler --- package.json | 3 ++- scripts/publish_helper.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 scripts/publish_helper.js diff --git a/package.json b/package.json index a7cd3ca..a3c5fcb 100644 --- a/package.json +++ b/package.json @@ -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": [], diff --git a/scripts/publish_helper.js b/scripts/publish_helper.js new file mode 100644 index 0000000..3f9a711 --- /dev/null +++ b/scripts/publish_helper.js @@ -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'.`); +}