The following script was written in order to sort JSON files by key on pre-commit hook:
/*
* Will reorder all files in given path.
*/
const sortJson = require("sort-json");
const fs = require("fs");
const chalk = require("chalk");
const log = console.log;
const translationsPath = process.argv.slice(2).join(" ");
function readFiles(dirname) {
try {
return fs.readdirSync(dirname);
} catch (e) {
log(chalk.red(`Failed reading files from path; ${e}`));
}
}
log(
chalk.bgGreen(
`Running json sort pre-commit hook on path: ${translationsPath}`
)
);
const files = readFiles(translationsPath);
files.forEach((file) => {
log(chalk.yellow(`Sorting ${file}...`));
try {
sortJson.overwrite(`${translationsPath}\\${file}`);
log(chalk.green(`Finished sorting ${file}`));
} catch (e) {
log(chalk.red(`Failed sorting file ${file}; ${e}`));
}
});
log(
chalk.bgGreen(
`Finished sorting files`
)
);
I'm attaching the script to my package.json
with husky
precommit hook:
"scripts": {
"sort-translations": "node ./scripts/husky/json-sort src/assets/translations",
...
},
"husky": {
"hooks": {
"pre-commit": "npm run sort-translations"
}
},
The result is that the commit is finished, and only then the script finishes with the created unstage changes.
The script itself runs synchronously with the Finished sorting files
message printed last.
My question is, how can I make it synchronous; first finish running node ./scripts/husky/json-sort src/assets/translations
, then git commit
.
Thanks!
Thanks to @adelriosantiago comment, I figured it out.
readdirSync()
instead of readdir()
. Then I was able to verify (through logging) that the script indeed ends only when the files are edited. Unfortunately that wasn't enough - the hook still ended with uncommitted files. && git add src/assets/translations
to pre-commit
hook.Now everything works as desired.