Skip to content

Commit

Permalink
Merge pull request #153 from harpreetkhalsagtbit/hotfix/minify-assets
Browse files Browse the repository at this point in the history
Add: Script to minify output json files
  • Loading branch information
harpreetkhalsagtbit authored Aug 19, 2023
2 parents dae05fe + 4a2375a commit ce36f4d
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 @@ -8,9 +8,10 @@
"sideEffects": false,
"scripts": {
"test": "jest",
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && npm run minify",
"prepublish": "npm run build",
"lint": "eslint index.ts __test__ --fix",
"minify": "ts-node src/scripts/minify.ts",
"split-data":"ts-node src/scripts/splitLargeDatasetFiles-CSC.ts",
"update-data":"ts-node src/scripts/combineSplitFilesToMakeALargeDatasetFiles-CSC.ts",
"update-data-with-gz":"gz=true ts-node src/scripts/combineSplitFilesToMakeALargeDatasetFiles-CSC.ts"
Expand Down
30 changes: 30 additions & 0 deletions src/scripts/minify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import fs from 'fs';
import path from 'path';

const minifyFile = async (parentDir:string, file: string) => {
try {
const filePath = path.join(parentDir, file);
const content:string = fs.readFileSync(filePath, 'utf-8');

fs.writeFileSync(filePath, JSON.stringify(JSON.parse(content)));
console.log(`Minified ${filePath}`);
} catch (error) {
console.error(`Error minifying ${file}:`, error);
}
};

const minify = () => {
const paths = [
path.resolve(process.cwd(), 'lib/assets'),
path.resolve(process.cwd(), 'lib/cjs/assets')
]
paths.forEach(parentDir => {
fs.readdirSync(parentDir).forEach((file) => {
if (file.endsWith('.json')) {
minifyFile(parentDir, file);
}
});
})
}

minify();

0 comments on commit ce36f4d

Please sign in to comment.