Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/devkit/src/generators/format-files.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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');
Copy link
Member

Choose a reason for hiding this comment

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

I know we setup the json parser for the files when we generate the swcrc config, but if someone didnt use a generator and had an swcrc file, what would happen? Is there any way for us to set the json parser here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. Should we?

Copy link
Member

Choose a reason for hiding this comment

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

If we can set it at runtime here, I'd think that'd be nicer. Looking at this code vs. the formatFiles utility function, we aren't programmatically calling prettier. We are just using the CLI + child_process. That may make it hard or not feasible in this case.

That said, we probably want these changes in the formatFiles helper too, s.t. the files are formatted if a generator changes them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried it with runtime now. A little ugly but it's better than modifying the prettier config on generation (and modifying approach only works for .prettierrc in JSON format)


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