<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
99 lines
2.2 KiB
TypeScript
99 lines
2.2 KiB
TypeScript
import {
|
|
addDependenciesToPackageJson,
|
|
stripIndents,
|
|
updateJson,
|
|
writeJson,
|
|
type GeneratorCallback,
|
|
type Tree,
|
|
} from '@nx/devkit';
|
|
import type { Options } from 'prettier';
|
|
import { prettierVersion } from './versions';
|
|
|
|
export interface ExistingPrettierConfig {
|
|
sourceFilepath: string;
|
|
config: Options;
|
|
}
|
|
|
|
export async function resolveUserExistingPrettierConfig(): Promise<ExistingPrettierConfig | null> {
|
|
let prettier: typeof import('prettier');
|
|
try {
|
|
prettier = require('prettier');
|
|
} catch {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const filepath = await prettier.resolveConfigFile();
|
|
if (!filepath) {
|
|
return null;
|
|
}
|
|
|
|
const config = await prettier.resolveConfig(process.cwd(), {
|
|
useCache: false,
|
|
config: filepath,
|
|
});
|
|
if (!config) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
sourceFilepath: filepath,
|
|
config: config,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function generatePrettierSetup(
|
|
tree: Tree,
|
|
options: { skipPackageJson?: boolean }
|
|
): GeneratorCallback {
|
|
// https://prettier.io/docs/en/configuration.html
|
|
const prettierrcNameOptions = [
|
|
'.prettierrc',
|
|
'.prettierrc.json',
|
|
'.prettierrc.yml',
|
|
'.prettierrc.yaml',
|
|
'.prettierrc.json5',
|
|
'.prettierrc.js',
|
|
'.prettierrc.cjs',
|
|
'.prettierrc.mjs',
|
|
'.prettierrc.toml',
|
|
'prettier.config.js',
|
|
'prettier.config.cjs',
|
|
'prettier.config.mjs',
|
|
];
|
|
|
|
if (prettierrcNameOptions.every((name) => !tree.exists(name))) {
|
|
writeJson(tree, '.prettierrc', { singleQuote: true });
|
|
}
|
|
|
|
if (!tree.exists('.prettierignore')) {
|
|
tree.write(
|
|
'.prettierignore',
|
|
stripIndents`# Add files here to ignore them from prettier formatting
|
|
/dist
|
|
/coverage
|
|
/.nx/cache
|
|
/.nx/workspace-data
|
|
`
|
|
);
|
|
}
|
|
|
|
if (tree.exists('.vscode/extensions.json')) {
|
|
updateJson(tree, '.vscode/extensions.json', (json) => {
|
|
json.recommendations ??= [];
|
|
const extension = 'esbenp.prettier-vscode';
|
|
if (!json.recommendations.includes(extension)) {
|
|
json.recommendations.push(extension);
|
|
}
|
|
return json;
|
|
});
|
|
}
|
|
|
|
return options.skipPackageJson
|
|
? () => {}
|
|
: addDependenciesToPackageJson(tree, {}, { prettier: prettierVersion });
|
|
}
|