Skip to content

Commit

Permalink
fix(js): programmatically format .swcrc with json
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed May 14, 2022
1 parent a140401 commit 0da26d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
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
40 changes: 0 additions & 40 deletions packages/js/src/utils/swc/add-swc-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,6 @@ const swcOptionsString = () => `{

export function addSwcConfig(tree: Tree, projectDir: string) {
const swcrcPath = join(projectDir, '.lib.swcrc');
updatePrettierRcForSwcrc(tree);
if (tree.exists(swcrcPath)) return;
tree.write(swcrcPath, swcOptionsString());
}

function updatePrettierRcForSwcrc(tree: Tree) {
const prettierrcPath = '.prettierrc';
const isExist = tree.exists(prettierrcPath);
if (!isExist) {
logger.info(`
For SWC, @nrwl/js:lib attempted to update Prettier Configuration for ".swcrc" file
but the root ".prettierrc" does not exist. Please consider adding parser support for ".lib.swcrc" file
`);
return;
}

try {
const prettierRc = readJson(tree, prettierrcPath);
const hasSwcrcOverride = prettierRc.overrides?.some((override) => {
const files = Array.isArray(override.files)
? override.files
: [override.files];
return files.some((file) => file.includes('.swcrc'));
});

if (hasSwcrcOverride) return;
updateJson(tree, prettierrcPath, (json) => {
json.overrides = [
...(json.overrides ?? []),
{
files: '**/*.swcrc',
options: {
parser: 'json',
},
},
];

return json;
});
} catch (e) {
logger.error(e);
}
}
23 changes: 22 additions & 1 deletion packages/nx/src/command-line/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,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) {
execSync(
`node "${PRETTIER_PATH}" --write --list-different ${swcrcPatterns.join(
' '
)} --parser json`,
{
stdio: [0, 1, 2],
}
);
}
}
}

Expand Down

0 comments on commit 0da26d4

Please sign in to comment.