Files
renovate/tools/static-data/utils.mjs
T
Jamie Tanna 836afde273 chore(tools): pre-format output JSON files (#42941)
* chore(tools): pre-format output JSON files

This also helps when running changes locally, as there's no need to run
`prettier` to check the actual updates between what's committed.

Co-authored-by: gpt-4.1 <jamie.tanna+github-copilot@mend.io>

* refactor: directly use `prettier`

* fixup! refactor: directly use `prettier`

---------

Co-authored-by: gpt-4.1 <jamie.tanna+github-copilot@mend.io>
2026-04-28 09:13:51 +00:00

27 lines
753 B
JavaScript

import { isString } from '@sindresorhus/is';
import fs from 'fs-extra';
import prettier from 'prettier';
/**
* Update given file with new provided data.
* @param {string} file Path to a data file
* @param {string|NodeJS.ArrayBufferView} newData New data to be written
*/
export async function updateJsonFile(file, newData) {
try {
console.log(`Updating ${file}`);
const newString = isString(newData)
? newData
: Buffer.from(
newData.buffer,
newData.byteOffset,
newData.byteLength,
).toString('utf8');
const pretty = await prettier.format(newString, { parser: 'json' });
await fs.writeFile(file, pretty);
} catch (e) {
console.error(e.toString());
process.exit(1);
}
}