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

add ESLintPool and PrettierPool classes #26953

Merged
merged 3 commits into from
Aug 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
let argument;
if (hasPositionalArguments || argumentDef.type !== Array) {
// Positional arguments already parsed or a single argument.
argument = positionalArguments.shift();
argument = Array.isArray(positionalArguments) ? positionalArguments.shift() : positionalArguments;
} else {
// Varags argument.
argument = positionalArguments;
Expand Down
29 changes: 22 additions & 7 deletions generators/bootstrap/support/eslint-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,44 @@ import { Piscina } from 'piscina';
import BaseGenerator from '../../base-core/index.js';
import { addLineNumbers } from '../internal/transform-utils.js';

type PoolOptions = Exclude<ConstructorParameters<typeof Piscina>[0], undefined>;
type ESLintWorkerOptions = { cwd?: string; extensions: string; recreateEslint?: boolean };

export class ESLintPool extends Piscina {
constructor(options?: PoolOptions) {
super({
maxThreads: 1,
filename: new URL('./eslint-worker.js', import.meta.url).href,
...options,
});
}

apply(data: ESLintWorkerOptions & { filePath: string; fileContents: string }): Promise<{ result: string; error: string }> {
return this.run(data);
}
}

export const createESLintTransform = function (
this: BaseGenerator | void,
transformOptions: { ignoreErrors?: boolean; extensions?: string; cwd?: string } = {},
transformOptions: { ignoreErrors?: boolean; poolOptions?: PoolOptions } & Partial<ESLintWorkerOptions> = {},
) {
const { extensions = 'js,cjs,mjs,ts,cts,mts,jsx,tsx', ignoreErrors, cwd } = transformOptions;
const { extensions = 'js,cjs,mjs,ts,cts,mts,jsx,tsx', ignoreErrors, cwd, poolOptions, recreateEslint } = transformOptions;
const minimatch = new Minimatch(`**/*.{${extensions}}`, { dot: true });

const pool = new Piscina({
maxThreads: 1,
filename: new URL('./eslint-worker.js', import.meta.url).href,
});
const pool = new ESLintPool(poolOptions);

return passthrough(
async file => {
if (!minimatch.match(file.path) || !isFileStateModified(file)) {
return;
}
const fileContents = file.contents.toString();
const { result, error } = await pool.run({
const { result, error } = await pool.apply({
cwd,
filePath: file.path,
fileContents,
extensions,
recreateEslint,
});
if (result) {
file.contents = Buffer.from(result);
Expand Down
11 changes: 8 additions & 3 deletions generators/bootstrap/support/eslint-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import jhipster from '../../../lib/eslint/index.js';

let eslintInstance;

export default async ({ cwd, filePath, fileContents, extensions }) => {
if (!eslintInstance) {
export default async ({ cwd, filePath, fileContents, extensions, config, additionalConfig = [], recreateEslint }) => {
if (recreateEslint || !eslintInstance) {
eslintInstance = new eslint.ESLint({
fix: true,
overrideConfigFile: true,
allowInlineConfig: false,
cache: false,
cwd,
baseConfig: ts.config({ files: [`**/*.{${extensions}}`] }, ts.configs.base, jhipster.base),
baseConfig: ts.config(
{ files: [`**/*.{${extensions}}`] },
ts.configs.base,
...additionalConfig,
config ? JSON.parse(config) : jhipster.base,
),
});
}

Expand Down
47 changes: 28 additions & 19 deletions generators/bootstrap/support/prettier-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,36 @@ const minimatch = new Minimatch('**/{.prettierrc**,.prettierignore}');
export const isPrettierConfigFilePath = (filePath: string) => minimatch.match(filePath);
export const isPrettierConfigFile = (file: MemFsEditorFile) => isPrettierConfigFilePath(file.path);

type PrettierWorkerOptions = {
prettierPackageJson?: boolean;
prettierJava?: boolean;
prettierProperties?: boolean;
prettierOptions?: PrettierOptions;
};

export class PrettierPool extends Piscina {
constructor(options = {}) {
super({
maxThreads: 1,
filename: new URL('./prettier-worker.js', import.meta.url).href,
...options,
});
}

apply(
data: PrettierWorkerOptions & { relativeFilePath: string; filePath: string; fileContents: string },
): Promise<{ result?: string; errorMessage?: string }> {
return this.run(data);
}
}

export const createPrettierTransform = async function (
this: CoreGenerator,
options: {
ignoreErrors?: boolean;
extensions?: string;
prettierPackageJson?: boolean;
prettierJava?: boolean;
prettierProperties?: boolean;
prettierOptions?: PrettierOptions;
} = {},
options: PrettierWorkerOptions & { ignoreErrors?: boolean; extensions?: string } = {},
) {
const pool = new Piscina({
maxThreads: 1,
filename: new URL('./prettier-worker.js', import.meta.url).href,
});
const pool = new PrettierPool();

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

Expand All @@ -56,15 +69,11 @@ export const createPrettierTransform = async function (
if (!file.contents) {
throw new Error(`File content doesn't exist for ${file.relative}`);
}
const { result, errorMessage } = await pool.run({
const { result, errorMessage } = await pool.apply({
relativeFilePath: file.relative,
filePath: file.path,
fileContents: file.contents.toString('utf8'),
prettierOptions,
prettierPackageJson,
prettierJava,
prettierProperties,
ignoreErrors,
...workerOptions,
});
if (result) {
file.contents = Buffer.from(result);
Expand Down
Loading