Skip to content

Commit

Permalink
fix(misc): avoid writing unformatted json if prettier is available
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 4, 2023
1 parent b59a0a1 commit 8102a7f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Tree } from '../tree';
import * as path from 'path';
import type * as Prettier from 'prettier';
import { formatFileContentsWithPrettierIfAvailable } from '../../utils/prettier';
import { readJson } from '../utils/json';

/**
* Formats all the created or updated files using Prettier
Expand All @@ -9,47 +11,39 @@ import type * as Prettier from 'prettier';
export async function formatChangedFilesWithPrettierIfAvailable(
tree: Tree
): Promise<void> {
let prettier: typeof Prettier;
try {
prettier = await import('prettier');
} catch {}

if (!prettier) return;

const files = new Set(
tree.listChanges().filter((file) => file.type !== 'DELETE')
);

const changedPrettierInTree = getChangedPrettierConfigInTree(tree);

await Promise.all(
Array.from(files).map(async (file) => {
const systemPath = path.join(tree.root, file.path);
let options: any = {
filepath: systemPath,
};

const resolvedOptions = await prettier.resolveConfig(systemPath, {
editorconfig: true,
});
if (!resolvedOptions) {
return;
}
options = {
...options,
...resolvedOptions,
};

const support = await prettier.getFileInfo(systemPath, options);
if (support.ignored || !support.inferredParser) {
return;
}

try {
tree.write(
file.path,
prettier.format(file.content.toString('utf-8'), options)
await formatFileContentsWithPrettierIfAvailable(
systemPath,
tree.read(file.path),
changedPrettierInTree
)
);
} catch (e) {
console.warn(`Could not format ${file.path}. Error: "${e.message}"`);
}
})
);
}

function getChangedPrettierConfigInTree(tree: Tree): Prettier.Options | null {
if (tree.listChanges().find((file) => file.path === '.prettierrc')) {
try {
return readJson(tree, '.prettierrc');
} catch {
return null;
}
} else {
return null;
}
}
11 changes: 8 additions & 3 deletions packages/nx/src/utils/fileutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
import { dirname } from 'path';
import * as tar from 'tar-stream';
import { createGunzip } from 'zlib';
import {
formatFileContentsWithPrettierIfAvailable,
formatFileContentsWithPrettierIfAvailableSync,
} from './prettier';

export interface JsonReadOptions extends JsonParseOptions {
/**
Expand Down Expand Up @@ -67,9 +71,10 @@ export function writeJsonFile<T extends object = object>(
): void {
mkdirSync(dirname(path), { recursive: true });
const serializedJson = serializeJson(data, options);
const content = options?.appendNewLine
? `${serializedJson}\n`
: serializedJson;
const content = formatFileContentsWithPrettierIfAvailableSync(
path,
options?.appendNewLine ? `${serializedJson}\n` : serializedJson
);
writeFileSync(path, content, { encoding: 'utf-8' });
}

Expand Down
67 changes: 67 additions & 0 deletions packages/nx/src/utils/prettier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type * as Prettier from 'prettier';

let prettier: typeof Prettier | null;
export function getPrettierOrNull() {
if (prettier === undefined) {
try {
prettier = require('prettier');
} catch (e) {
prettier = null;
}
}
return prettier;
}

export function formatFileContentsWithPrettierIfAvailableSync<
T extends string | Buffer
>(path: string, contents: T, extraOptions?: Prettier.Options): string {
const prettier = getPrettierOrNull();
const contentsAsString =
typeof contents === 'string' ? contents : contents.toString('utf-8');
if (!prettier) {
return contentsAsString;
}

const resolvedOptions = prettier.resolveConfig.sync(path, {
editorconfig: true,
});
const options: Prettier.Options = {
...resolvedOptions,
...extraOptions,
filepath: path,
};

const support = prettier.getFileInfo.sync(path, options as any);
if (support.ignored || !support.inferredParser) {
return contentsAsString;
}

return prettier.format(contentsAsString, options);
}

export async function formatFileContentsWithPrettierIfAvailable<
T extends string | Buffer
>(path: string, contents: T, extraOptions?: Prettier.Options): Promise<string> {
const prettier = getPrettierOrNull();
const contentsAsString =
typeof contents === 'string' ? contents : contents.toString('utf-8');
if (!prettier) {
return contentsAsString;
}

const resolvedOptions = await prettier.resolveConfig(path, {
editorconfig: true,
});
const options: Prettier.Options = {
...resolvedOptions,
...extraOptions,
filepath: path,
};

const support = await prettier.getFileInfo(path, options as any);
if (support.ignored || !support.inferredParser) {
return contentsAsString;
}

return prettier.format(contentsAsString, options);
}

0 comments on commit 8102a7f

Please sign in to comment.