Skip to content

Commit

Permalink
fix(js): format .lib.swcrc file with nx format (#10254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed May 16, 2022
1 parent 7c2f945 commit 17c4022
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/devkit/src/generators/format-files.ts
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions packages/js/src/utils/swc/add-swc-config.ts
@@ -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 = [
Expand Down Expand Up @@ -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());
}
31 changes: 29 additions & 2 deletions packages/nx/src/command-line/format.ts
Expand Up @@ -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))
);
Expand Down Expand Up @@ -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],
}
);
}
}
}

Expand Down

1 comment on commit 17c4022

@vercel
Copy link

@vercel vercel bot commented on 17c4022 May 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.