Skip to content

Commit

Permalink
chore: 🚑 add local scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mackignacio committed May 13, 2022
1 parent 1382a8c commit bd75400
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dist
**.js
**.d.ts
!jest.config.js
!scripts/**

#gulp
!gulpfile.js
Expand Down
38 changes: 38 additions & 0 deletions scripts/clean-package.js
Original file line number Diff line number Diff line change
@@ -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;
});
20 changes: 20 additions & 0 deletions scripts/restore-package.js
Original file line number Diff line number Diff line change
@@ -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;
});
4 changes: 4 additions & 0 deletions scripts/variables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
ORIG_PATH: "../package.json",
TEMP_PATH: "./temp-package.json",
};

0 comments on commit bd75400

Please sign in to comment.