|
| 1 | +import { promises as fs } from "fs" |
| 2 | +import { isUnknownArray } from "./is-unknown-array" |
| 3 | +import { isUnknownObject } from "./is-unknown-object" |
| 4 | + |
| 5 | +export const modifyJsonFile = async ( |
| 6 | + path: string, |
| 7 | + callback: (oldObject: Record<string, unknown>) => Record<string, unknown> |
| 8 | +): Promise<void> => { |
| 9 | + const oldObject = await readJsonFile(path) |
| 10 | + const newObject = callback(oldObject) |
| 11 | + await writeJsonFile(path, newObject) |
| 12 | +} |
| 13 | + |
| 14 | +export const readJsonFile = async ( |
| 15 | + path: string |
| 16 | +): Promise<Record<string, unknown>> => { |
| 17 | + const jsonString = await fs.readFile(path, "utf8") |
| 18 | + const jsonObject = JSON.parse(jsonString) |
| 19 | + |
| 20 | + if (!isUnknownObject(jsonObject)) { |
| 21 | + throw new TypeError("Expected json object to be an object.") |
| 22 | + } |
| 23 | + |
| 24 | + return jsonObject |
| 25 | +} |
| 26 | + |
| 27 | +export const writeJsonFile = async ( |
| 28 | + fileName: string, |
| 29 | + object: Record<string, unknown> |
| 30 | +): Promise<void> => { |
| 31 | + const objectString = JSON.stringify(object, null, 2) |
| 32 | + await fs.writeFile(fileName, objectString) |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * `toObject` takes an unknown variable, `object` and returns `object` if it's an object, or `{}` otherwise. |
| 37 | + * This makes it very easy to spread several layers of a JSON object, for example in combination with `modifyJsonFile`: |
| 38 | + * |
| 39 | + * ```typescript |
| 40 | + * await modifyJsonFile("tsconfig.json", (tsConfig) => ({ |
| 41 | + * ...tsConfig, |
| 42 | + * compilerOptions: { |
| 43 | + * ...toObject(tsConfig.compilerOptions), |
| 44 | + * jsxImportSource: "@emotion/react", |
| 45 | + * }, |
| 46 | + * })) |
| 47 | + * ``` |
| 48 | + * |
| 49 | + * @param object |
| 50 | + * @returns |
| 51 | + */ |
| 52 | +export const toObject = (object: unknown): Record<string, unknown> => { |
| 53 | + if (isUnknownObject(object)) { |
| 54 | + return object |
| 55 | + } else { |
| 56 | + return {} |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * `toArray` takes an unknown variable, `array` and returns `array` if it's an array, or `[]` otherwise. |
| 62 | + * This makes it very easy to spread several layers of a JSON object, for example in combination with `modifyJsonFile`: |
| 63 | + * |
| 64 | + * ```typescript |
| 65 | + * await modifyJsonFile(".babelrc", (babelConfig) => ({ |
| 66 | + * ...babelConfig, |
| 67 | + * plugins: [ |
| 68 | + * ["babel-plugin-styled-components"], |
| 69 | + * ...toArray(babelConfig.plugins), |
| 70 | + * ], |
| 71 | + * })) |
| 72 | + * ``` |
| 73 | + * |
| 74 | + * @param object |
| 75 | + * @returns |
| 76 | + */ |
| 77 | +export const toArray = (array: unknown): unknown[] => { |
| 78 | + if (isUnknownArray(array)) { |
| 79 | + return array |
| 80 | + } else { |
| 81 | + return [] |
| 82 | + } |
| 83 | +} |
0 commit comments