Skip to content

Commit

Permalink
offload prettier to thread
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 8, 2024
1 parent 8b4f0cb commit f322214
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 53 deletions.
88 changes: 35 additions & 53 deletions generators/bootstrap/support/prettier-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
*/
import { passthrough } from 'p-transform';
import { isFileStateModified } from 'mem-fs-editor/state';
import prettier from 'prettier';
import prettierPluginJava from 'prettier-plugin-java';
import prettierPluginProperties from 'prettier-plugin-properties';
import prettierPluginPackagejson from 'prettier-plugin-packagejson';
import type prettier from 'prettier';
import { Minimatch } from 'minimatch';
import type { MemFsEditorFile, VinylMemFsEditorFile } from 'mem-fs-editor';
import Tinypool from 'tinypool';
import type CoreGenerator from '../../base-core/index.js';

const minimatch = new Minimatch('**/{.prettierrc**,.prettierignore}');
Expand All @@ -41,62 +39,46 @@ export const createPrettierTransform = async function (
prettierOptions?: prettier.Options;
} = {},
) {
// prettier cache is global, generators may execute more than one commit.
// In case prettier config is committed to disk at later commits, the cache may be outdated.
await prettier.clearConfigCache();
const pool = new Tinypool({
runtime: 'child_process',
maxThreads: 1,
filename: new URL('./prettier-worker.js', import.meta.url).href,
});

const { ignoreErrors = false, extensions = '*', prettierPackageJson, prettierJava, prettierProperties, prettierOptions } = options;
const globExpression = extensions.includes(',') ? `**/*.{${extensions}}` : `**/*.${extensions}`;
const minimatch = new Minimatch(globExpression, { dot: true });

return passthrough(async (file: VinylMemFsEditorFile) => {
if (!minimatch.match(file.path) || !isFileStateModified(file)) {
return;
}
if (!file.contents) {
throw new Error(`File content doesn't exist for ${file.relative}`);
}
/* resolve from the projects config */
let fileContent;
try {
const resolvedDestinationFileOptions = await prettier.resolveConfig(file.relative);
const fileOptions: prettier.Options = {
// Config from disk
...resolvedDestinationFileOptions,
plugins: [],
// for better errors
filepath: file.relative,
...prettierOptions,
};
if (prettierPackageJson && file.path.endsWith('package.json')) {
fileOptions.plugins!.push(prettierPluginPackagejson);
}
if (prettierJava && file.path.endsWith('.java')) {
fileOptions.plugins!.push(prettierPluginJava);
return passthrough(
async (file: VinylMemFsEditorFile) => {
if (!minimatch.match(file.path) || !isFileStateModified(file)) {
return;
}
if (prettierProperties) {
fileOptions.plugins!.push(prettierPluginProperties);
if (!file.contents) {
throw new Error(`File content doesn't exist for ${file.relative}`);
}
fileContent = file.contents.toString('utf8');
const data = await prettier.format(fileContent, fileOptions);
file.contents = Buffer.from(data);
} catch (error) {
let errorMessage;
if (fileContent) {
errorMessage = `Error parsing file ${file.relative}: ${error}
At: ${fileContent
.split('\n')
.map((value, idx) => `${idx + 1}: ${value}`)
.join('\n')}`;
} else {
errorMessage = `Unknown prettier error: ${error}`;
const result = await pool.run({
relativeFilePath: file.relative,
filePath: file.path,
fileContents: file.contents.toString('utf8'),
prettierOptions,
prettierPackageJson,
prettierJava,
prettierProperties,
ignoreErrors,
});
if (result.errorMessage) {
if (!ignoreErrors) {
throw new Error(result.errorMessage);
}
this?.log?.warn?.(result.errorMessage);
}
if (ignoreErrors) {
this?.log?.warn?.(errorMessage);
return;
if (result.fileContents) {
file.contents = Buffer.from(result.fileContents);
}
throw new Error(errorMessage);
}
});
},
() => {
pool.destroy();
},
);
};
50 changes: 50 additions & 0 deletions generators/bootstrap/support/prettier-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import prettier from 'prettier';
import prettierPluginJava from 'prettier-plugin-java';
import prettierPluginProperties from 'prettier-plugin-properties';
import prettierPluginPackagejson from 'prettier-plugin-packagejson';

export default async ({
relativeFilePath,
filePath,
fileContents,
prettierOptions,
prettierPackageJson,
prettierJava,
prettierProperties,
}) => {
try {
const resolvedDestinationFileOptions = await prettier.resolveConfig(relativeFilePath);
const fileOptions = {
// Config from disk
...resolvedDestinationFileOptions,
plugins: [],
// for better errors
filepath: relativeFilePath,
...prettierOptions,
};
if (prettierPackageJson && filePath.endsWith('package.json')) {
fileOptions.plugins.push(prettierPluginPackagejson);
}
if (prettierJava && filePath.endsWith('.java')) {
fileOptions.plugins.push(prettierPluginJava);
}
if (prettierProperties) {
fileOptions.plugins.push(prettierPluginProperties);
}
const data = await prettier.format(fileContents, fileOptions);
return { fileContents: data };
} catch (error) {
let errorMessage;
if (fileContents) {
errorMessage = `Error parsing file ${relativeFilePath}: ${error}
At: ${fileContents
.split('\n')
.map((value, idx) => `${idx + 1}: ${value}`)
.join('\n')}`;
} else {
errorMessage = `Unknown prettier error: ${error}`;
}
return { errorMessage };
}
};

0 comments on commit f322214

Please sign in to comment.