refactor: rename formatter utilities

pull/336/head
xingyu4j 2026-03-13 21:56:23 +08:00
parent 6dabb848a5
commit 542ed6c08f
5 changed files with 16 additions and 25 deletions

View File

@ -37,7 +37,6 @@
"find-up": "catalog:", "find-up": "catalog:",
"ora": "catalog:", "ora": "catalog:",
"pkg-types": "catalog:", "pkg-types": "catalog:",
"prettier": "catalog:",
"rimraf": "catalog:" "rimraf": "catalog:"
} }
} }

View File

@ -0,0 +1,13 @@
import fs from 'node:fs/promises';
import { execa } from 'execa';
async function formatFile(filepath: string) {
await execa('oxfmt', [filepath], {
stdio: 'inherit',
});
return await fs.readFile(filepath, 'utf8');
}
export { formatFile };

View File

@ -1,12 +1,12 @@
export * from './constants'; export * from './constants';
export * from './date'; export * from './date';
export { formatFile } from './formatter';
export * from './fs'; export * from './fs';
export * from './git'; export * from './git';
export { getStagedFiles, add as gitAdd } from './git'; export { getStagedFiles, add as gitAdd } from './git';
export { generatorContentHash } from './hash'; export { generatorContentHash } from './hash';
export * from './monorepo'; export * from './monorepo';
export { toPosixPath } from './path'; export { toPosixPath } from './path';
export { prettierFormat } from './prettier';
export * from './spinner'; export * from './spinner';
export type { Package } from '@manypkg/get-packages'; export type { Package } from '@manypkg/get-packages';
export { default as colors } from 'chalk'; export { default as colors } from 'chalk';

View File

@ -1,21 +0,0 @@
import fs from 'node:fs/promises';
import { format, getFileInfo, resolveConfig } from 'prettier';
async function prettierFormat(filepath: string) {
const prettierOptions = await resolveConfig(filepath, {});
const fileInfo = await getFileInfo(filepath);
const input = await fs.readFile(filepath, 'utf8');
const output = await format(input, {
...prettierOptions,
parser: fileInfo.inferredParser as any,
});
if (output !== input) {
await fs.writeFile(filepath, output, 'utf8');
}
return output;
}
export { prettierFormat };

View File

@ -6,10 +6,10 @@ import {
colors, colors,
consola, consola,
findMonorepoRoot, findMonorepoRoot,
formatFile,
getPackages, getPackages,
gitAdd, gitAdd,
outputJSON, outputJSON,
prettierFormat,
toPosixPath, toPosixPath,
} from '@vben/node-utils'; } from '@vben/node-utils';
@ -40,7 +40,7 @@ async function createCodeWorkspace({
const outputPath = join(monorepoRoot, CODE_WORKSPACE_FILE); const outputPath = join(monorepoRoot, CODE_WORKSPACE_FILE);
await outputJSON(outputPath, { folders }, spaces); await outputJSON(outputPath, { folders }, spaces);
await prettierFormat(outputPath); await formatFile(outputPath);
if (autoCommit) { if (autoCommit) {
await gitAdd(CODE_WORKSPACE_FILE, monorepoRoot); await gitAdd(CODE_WORKSPACE_FILE, monorepoRoot);
} }