From bd75400878a4cd8910f4ff88e9793ec3ad0dec2f Mon Sep 17 00:00:00 2001 From: Mac Date: Fri, 13 May 2022 15:13:01 +0800 Subject: [PATCH] chore: :ambulance: add local scripts --- .gitignore | 1 + scripts/clean-package.js | 38 ++++++++++++++++++++++++++++++++++++++ scripts/restore-package.js | 20 ++++++++++++++++++++ scripts/variables.js | 4 ++++ 4 files changed, 63 insertions(+) create mode 100644 scripts/clean-package.js create mode 100644 scripts/restore-package.js create mode 100644 scripts/variables.js diff --git a/.gitignore b/.gitignore index 1ba2544..91bc351 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ dist **.js **.d.ts !jest.config.js +!scripts/** #gulp !gulpfile.js diff --git a/scripts/clean-package.js b/scripts/clean-package.js new file mode 100644 index 0000000..7b3978d --- /dev/null +++ b/scripts/clean-package.js @@ -0,0 +1,38 @@ +const fs = require("fs"); +const path = require("path"); +const env = require("./variables"); + +// Define absolute paths for original pkg and temporary pkg. +const ORIG_PATH = path.resolve(__dirname, env.ORIG_PATH); +const TEMP_PATH = path.resolve(__dirname, env.TEMP_PATH); + +// Obtain original `package.json` contents. +const packageData = require(ORIG_PATH); + +// Write/cache the original `package.json` data to `cached-package.json` file. +fs.writeFile(TEMP_PATH, JSON.stringify(packageData, null, 2), function (err) { + if (err) throw err; +}); + +// Define properties to remove in `package.json`. +const removableProps = ["devDependencies"]; + +// Remove the specified properties from `package.json`. +removableProps.forEach((prop) => delete packageData[prop]); + +// Define new scripts +const scripts = { + clean: "rimraf interface && rimraf index.js && rimraf index.d.ts && rimraf utils && rimraf router && rimraf lib", + postpack: "npm run clean && node ./scripts/restore-package.js", +}; + +// Replace scripts value +packageData.scripts = scripts; + +// Replace main value +packageData.main = "index.js"; + +// Overwrite original `package.json` with new data (i.e. minus the specific data). +fs.writeFile(ORIG_PATH, JSON.stringify(packageData, null, 2), function (err) { + if (err) throw err; +}); diff --git a/scripts/restore-package.js b/scripts/restore-package.js new file mode 100644 index 0000000..1b9d3d9 --- /dev/null +++ b/scripts/restore-package.js @@ -0,0 +1,20 @@ +const fs = require("fs"); +const path = require("path"); +const env = require("./variables"); + +// Define absolute paths for original pkg and temporary pkg. +const ORIG_PATH = path.resolve(__dirname, env.ORIG_PATH); +const TEMP_PATH = path.resolve(__dirname, env.TEMP_PATH); + +// Obtain original/cached contents from `cached-package.json`. +const packageData = JSON.stringify(require(TEMP_PATH), null, 2) + "\n"; + +// Write data from `cached-package.json` back to original `package.json`. +fs.writeFile(ORIG_PATH, packageData, function (err) { + if (err) throw err; +}); + +// Delete the temporary `cached-package.json` file. +fs.unlink(TEMP_PATH, function (err) { + if (err) throw err; +}); diff --git a/scripts/variables.js b/scripts/variables.js new file mode 100644 index 0000000..a42df8a --- /dev/null +++ b/scripts/variables.js @@ -0,0 +1,4 @@ +module.exports = { + ORIG_PATH: "../package.json", + TEMP_PATH: "./temp-package.json", +};