|
| 1 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +// eslint-disable-next-line unicorn/import-style |
| 3 | +import { join } from 'node:path'; |
| 4 | + |
| 5 | +import { findFiles } from '@codemod-utils/files'; |
| 6 | +import { assertFixture, loadFixture } from '@codemod-utils/tests'; |
| 7 | + |
| 8 | +import type { PluginOptions } from '../../src/options.js'; |
| 9 | +import { formatFile } from './format-file.js'; |
| 10 | + |
| 11 | +type DirectoryJSON = Parameters<typeof loadFixture>[0]; |
| 12 | + |
| 13 | +type Options = { |
| 14 | + fixturePath: string; |
| 15 | + pluginOptions?: Partial<PluginOptions>; |
| 16 | +}; |
| 17 | + |
| 18 | +export async function runPrettier(options: Options): Promise<void> { |
| 19 | + const { fixturePath, pluginOptions } = options; |
| 20 | + |
| 21 | + const { inputProject, outputProject } = (await import( |
| 22 | + join('../fixtures', fixturePath, 'index.js') |
| 23 | + )) as { |
| 24 | + inputProject: DirectoryJSON; |
| 25 | + outputProject: DirectoryJSON; |
| 26 | + }; |
| 27 | + |
| 28 | + const projectRoot = join('tmp', fixturePath); |
| 29 | + |
| 30 | + loadFixture(inputProject, { projectRoot }); |
| 31 | + |
| 32 | + const filePaths = findFiles('**/*.{gjs,gts}', { |
| 33 | + projectRoot, |
| 34 | + }); |
| 35 | + |
| 36 | + await Promise.all( |
| 37 | + filePaths.map(async (filePath) => { |
| 38 | + const oldFile = readFileSync(join(projectRoot, filePath), 'utf8'); |
| 39 | + const newFile = await formatFile(oldFile, pluginOptions); |
| 40 | + |
| 41 | + writeFileSync(join(projectRoot, filePath), newFile, 'utf8'); |
| 42 | + }), |
| 43 | + ); |
| 44 | + |
| 45 | + assertFixture(outputProject, { projectRoot }); |
| 46 | +} |
0 commit comments