fix(js): format .lib.swcrc file with nx format (#10254)

This commit is contained in:
Chau Tran 2022-05-16 09:19:49 -05:00 committed by GitHub
parent 7c2f9452fa
commit 17c40229a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -47,6 +47,10 @@ export async function formatFiles(tree: Tree): Promise<void> {
...resolvedOptions,
};
if (file.path.endsWith('.swcrc')) {
options.parser = 'json';
}
const support = await prettier.getFileInfo(systemPath, options);
if (support.ignored || !support.inferredParser) {
return;

View File

@ -1,6 +1,6 @@
// TODO(chau): change back to 2015 when https://github.com/swc-project/swc/issues/1108 is solved
// target: 'es2015'
import { Tree } from '@nrwl/devkit';
import { logger, readJson, Tree, updateJson } from '@nrwl/devkit';
import { join } from 'path';
export const defaultExclude = [
@ -40,6 +40,5 @@ const swcOptionsString = () => `{
export function addSwcConfig(tree: Tree, projectDir: string) {
const swcrcPath = join(projectDir, '.lib.swcrc');
if (tree.exists(swcrcPath)) return;
tree.write(swcrcPath, swcOptionsString());
}

View File

@ -83,10 +83,16 @@ async function getPatterns(
}
const p = parseFiles(args);
const supportedExtensions = prettier
.getSupportInfo()
.languages.flatMap((language) => language.extensions)
.filter((extension) => !!extension);
.filter((extension) => !!extension)
// Prettier supports ".swcrc" as a file instead of an extension
// So we add ".swcrc" as a supported extension manually
// which allows it to be considered for calculating "patterns"
.concat('.swcrc');
const patterns = p.files.filter(
(f) => fileExists(f) && supportedExtensions.includes(path.extname(f))
);
@ -151,12 +157,33 @@ function chunkify(target: string[], size: number): string[][] {
function write(patterns: string[]) {
if (patterns.length > 0) {
const [swcrcPatterns, regularPatterns] = patterns.reduce(
(result, pattern) => {
result[pattern.includes('.swcrc') ? 0 : 1].push(pattern);
return result;
},
[[], []] as [swcrcPatterns: string[], regularPatterns: string[]]
);
execSync(
`node "${PRETTIER_PATH}" --write --list-different ${patterns.join(' ')}`,
`node "${PRETTIER_PATH}" --write --list-different ${regularPatterns.join(
' '
)}`,
{
stdio: [0, 1, 2],
}
);
if (swcrcPatterns.length > 0) {
execSync(
`node "${PRETTIER_PATH}" --write --list-different ${swcrcPatterns.join(
' '
)} --parser json`,
{
stdio: [0, 1, 2],
}
);
}
}
}