Skip to content

Commit 1156b91

Browse files
committed
refactor(nx-plugin): improve file formatting utilities
1 parent a4e0c78 commit 1156b91

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

packages/nx-plugin/src/executors/update-api/updateApi.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ import {
99
import { join } from 'node:path';
1010

1111
import type { PromiseExecutor } from '@nx/devkit';
12-
import { logger, names, workspaceRoot } from '@nx/devkit';
13-
import {
14-
format,
15-
type Options as PrettierOptions,
16-
resolveConfig,
17-
} from 'prettier';
12+
import { logger, names } from '@nx/devkit';
1813

1914
import {
2015
bundleAndDereferenceSpecFile,
2116
compareSpecs,
2217
formatFiles,
18+
formatStringFromFilePath,
2319
generateClientCode,
2420
isUrl,
2521
makeDir,
@@ -241,24 +237,17 @@ const runExecutor: PromiseExecutor<UpdateApiExecutorSchema> = async (
241237
const apiDirectoryExists = existsSync(absoluteApiDirectory);
242238
const existingSpecFileExists = existsSync(absoluteExistingSpecPath);
243239

244-
const prettierConfig = await resolveConfig(workspaceRoot, {
245-
editorconfig: true,
246-
});
247-
const prettierOptions: PrettierOptions = {
248-
...prettierConfig,
249-
};
250-
251240
// Copy new spec to project
252241
if (apiDirectoryExists) {
253242
if (existingSpecFileExists) {
254243
logger.debug('Existing spec file found. Updating...');
255244
} else {
256245
logger.debug('No existing spec file found. Creating...');
257246
}
258-
const formattedSpec = await format(newSpecString, {
259-
...prettierOptions,
260-
filepath: absoluteTempSpecPath,
261-
});
247+
const formattedSpec = await formatStringFromFilePath(
248+
newSpecString,
249+
absoluteTempSpecPath,
250+
);
262251

263252
await writeFile(absoluteExistingSpecPath, formattedSpec);
264253
logger.debug(`Spec file updated successfully`);
@@ -302,7 +291,7 @@ const runExecutor: PromiseExecutor<UpdateApiExecutorSchema> = async (
302291
});
303292

304293
logger.debug('Formatting generated directory...');
305-
await formatFiles(absoluteProjectGeneratedDir, prettierOptions);
294+
await formatFiles(absoluteProjectGeneratedDir);
306295

307296
logger.info('Successfully updated API client and spec files.');
308297
await cleanup(absoluteTempFolder);

packages/nx-plugin/src/utils.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@hey-api/openapi-ts/internal';
1212
import { logger, workspaceRoot } from '@nx/devkit';
1313
import { compareOpenApi } from 'api-smart-diff';
14-
import { format, type Options as PrettierOptions } from 'prettier';
14+
import { format, resolveConfig } from 'prettier';
1515
import { convert } from 'swagger2openapi';
1616

1717
import { CONSTANTS } from './vars';
@@ -335,22 +335,17 @@ export async function makeDir(path: string) {
335335
await mkdir(path, { recursive: true });
336336
}
337337

338-
export async function formatFiles(
339-
dir: string,
340-
prettierOptions?: PrettierOptions,
341-
) {
338+
/**
339+
* Formats all files in a directory
340+
*/
341+
export async function formatFiles(dir: string) {
342342
const files = await readdir(dir, { withFileTypes: true });
343343
const tasks = files.map(async (file) => {
344344
const filePath = join(dir, file.name);
345345
if (file.isDirectory()) {
346-
await formatFiles(filePath, prettierOptions);
347-
} else if (file.name.endsWith('.ts')) {
348-
const content = await readFile(filePath, 'utf-8');
349-
const formatted = await format(content, {
350-
...prettierOptions,
351-
filepath: filePath,
352-
});
353-
await writeFile(filePath, formatted);
346+
await formatFiles(filePath);
347+
} else if (file.isFile()) {
348+
await formatFile(filePath);
354349
}
355350
});
356351
await Promise.all(tasks);
@@ -434,3 +429,21 @@ export async function getBaseTsConfigPath({
434429
logger.error(message);
435430
throw new Error(message);
436431
}
432+
433+
export async function formatFile(filePath: string) {
434+
const content = await readFile(filePath, 'utf-8');
435+
const formatted = await formatStringFromFilePath(content, filePath);
436+
await writeFile(filePath, formatted);
437+
}
438+
439+
export async function formatStringFromFilePath(
440+
content: string,
441+
filePath: string,
442+
) {
443+
const prettierOptions = await resolveConfig(filePath);
444+
const formatted = await format(content, {
445+
...prettierOptions,
446+
filepath: filePath,
447+
});
448+
return formatted;
449+
}

0 commit comments

Comments
 (0)