Skip to content

Commit

Permalink
cleanup(core): reuse existing util
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jul 24, 2024
1 parent 59db7fe commit e29bcc9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
48 changes: 10 additions & 38 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {
createProjectGraphAsync,
readProjectsConfigurationFromProjectGraph,
} from '../../project-graph/project-graph';
import type * as Prettier from 'prettier';
import { formatFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';

export interface ResolvedMigrationConfiguration extends MigrationsJson {
packageGroup?: ArrayPackageGroup;
Expand Down Expand Up @@ -1325,45 +1325,17 @@ async function writeFormattedJsonFile(
content: any,
options?: JsonWriteOptions
): Promise<void> {
let prettier: typeof Prettier;
try {
prettier = await import('prettier');
} catch {}

if (!prettier) {
// no prettier, write the json file as is
writeJsonFile(filePath, content, options);
return;
}
const formattedContent = await formatFilesWithPrettierIfAvailable(
[{ path: filePath, content: JSON.stringify(content) }],
workspaceRoot,
{ silent: true }
);

try {
const resolvedOptions = await prettier.resolveConfig(filePath, {
editorconfig: true,
if (formattedContent.has(filePath)) {
writeFileSync(filePath, formattedContent.get(filePath)!, {
encoding: 'utf-8',
});

const prettierOptions: Prettier.Options = {
...resolvedOptions,
filepath: filePath,
};

const support = await prettier.getFileInfo(
filePath,
prettierOptions as any
);
if (support.ignored || !support.inferredParser) {
// it's ignored or the parser could not be inferred, write the json file as is
writeJsonFile(filePath, content, options);
return;
}

// format and write the file
const formattedContent = await (prettier.format(
JSON.stringify(content),
prettierOptions
) as Promise<string> | string);
writeFileSync(filePath, formattedContent, { encoding: 'utf-8' });
} catch {
// prettier failed, ignore and write the json file as is
} else {
writeJsonFile(filePath, content, options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,43 @@ export async function formatChangedFilesWithPrettierIfAvailable(
silent?: boolean;
}
): Promise<void> {
const files = new Set(
tree.listChanges().filter((file) => file.type !== 'DELETE')
);

const results = await formatFilesWithPrettierIfAvailable(
Array.from(files),
tree.root,
options
);

for (const [path, content] of results) {
tree.write(path, content);
}
}

export async function formatFilesWithPrettierIfAvailable(
files: { path: string; content: string | Buffer }[],
root: string,
options?: {
silent?: boolean;
}
): Promise<Map<string, string>> {
const results = new Map<string, string>();

let prettier: typeof Prettier;
try {
prettier = await import('prettier');
} catch {}

if (!prettier) return;
if (!prettier) {
return results;
}

const files = new Set(
tree.listChanges().filter((file) => file.type !== 'DELETE')
);
await Promise.all(
Array.from(files).map(async (file) => {
try {
const systemPath = path.join(tree.root, file.path);
const systemPath = path.join(root, file.path);
let options: any = {
filepath: systemPath,
};
Expand All @@ -46,7 +69,7 @@ export async function formatChangedFilesWithPrettierIfAvailable(
return;
}

tree.write(
results.set(
file.path,
// In prettier v3 the format result is a promise
await (prettier.format(file.content.toString('utf-8'), options) as
Expand All @@ -60,4 +83,6 @@ export async function formatChangedFilesWithPrettierIfAvailable(
}
})
);

return results;
}

0 comments on commit e29bcc9

Please sign in to comment.