Skip to content

Commit

Permalink
build: add build patches script
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed Feb 22, 2022
1 parent 6958d74 commit 5068a6d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"scripts": {
"lint": "eslint ./src/**/*.ts ./e2e/**/*.ts",
"prebuild": "rimraf dist ethers",
"build": "npm run build:contracts && npm run build:ts && copyfiles \"ethers/**/*.d.ts\" \"patches/**/*.patch\" dist",
"build": "npm run build:contracts && npm run build:ts && copyfiles \"ethers/**/*.d.ts\" dist && npm run build:patches",
"build:node": "bili --format cjs",
"build:front_end": "bili --format esm",
"build:patches": "copyfiles \"patches/**/*.patch\" dist && node scripts/patches-build.js",
"build:ts": "npm run build:front_end && npm run build:node",
"build:api_docs": "rm -rf docs/api && typedoc --plugin typedoc-plugin-markdown --hideBreadcrumbs true",
"build:contracts": "npm run build:typechain:ens && npm run build:typechain:did && npm run build:typechain:roles && npm run build:typechain:staking",
Expand Down
46 changes: 46 additions & 0 deletions scripts/patches-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs');

/**
* @description Read the patches from the patches directory
*
* @returns {Promise<Array<string>>}
*/
async function getPatches() {
console.log('Getting patches...');
return new Promise((resolve, reject) => {
fs.readdir('./dist/patches', (err, files) => {
if (err) reject(err);
console.log('Got patches.');
resolve(files);
});
});
}

/**
* @description Updates the patchFile so that `patch-package` can notice them from node_modules directory
*
* @param {string} file - Path to the patch file
* @returns {Promise<void>}
*/
async function updatePatchFile(file) {
console.log(`Updating ${file}...`);
return new Promise((resolve, reject) => {
fs.readFile(`./dist/patches/${file}`, 'utf8', (err, data) => {
if (err) reject(err);
const newData = data.replaceAll('node_modules', '..');
fs.writeFile(`./dist/patches/${file}`, newData, 'utf8', (err) => {
if (err) reject(err);
console.log(`Updated ${file}`);
resolve();
});
});
});
}

async function main() {
console.log('Start building patches...');
const patches = await getPatches();
return Promise.all(patches.map((patchFile) => updatePatchFile(patchFile)));
}

main().then(() => console.log('Done.'));
7 changes: 6 additions & 1 deletion tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"include": [".eslintrc.js", ".prettierrc.js", "scripts/**/*.ts", "bili.config.ts"]
"include": [
".eslintrc.js",
".prettierrc.js",
"scripts/**/*.*s",
"bili.config.ts"
]
}

0 comments on commit 5068a6d

Please sign in to comment.