Skip to content

Instantly share code, notes, and snippets.

@codebutler
Created December 13, 2023 17:19
Show Gist options
  • Save codebutler/6f6e9618b5aec02ca631d83891cba0a1 to your computer and use it in GitHub Desktop.
Save codebutler/6f6e9618b5aec02ca631d83891cba0a1 to your computer and use it in GitHub Desktop.
// example of how to run prettier from orval.config.ts
import fs from "fs/promises";
import * as prettier from "prettier";
import path from "path";
const walk = async (inputPath: string): Promise<string[]> => {
const stats = await fs.stat(inputPath);
if (!stats.isDirectory()) {
return [inputPath];
}
const entries = await fs.readdir(inputPath, { withFileTypes: true });
const filePaths = await Promise.all(
entries.map(async (dirent) => walk(path.resolve(inputPath, dirent.name))),
);
return filePaths.flat();
};
const afterAllFilesWrite = async (paths: string[]): Promise<void> => {
const allPaths = await Promise.all(paths.map(walk));
for (const path of allPaths.flat()) {
const data = await fs.readFile(path, "utf8");
const formattedData = await prettier.format(data, { parser: "typescript" });
await fs.writeFile(path, formattedData);
}
};
// add as hook in your config
hooks: {
afterAllFilesWrite,
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment