generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
37 lines (36 loc) • 1.42 kB
JavaScript
import { extname } from 'path';
import { passthrough } from 'p-transform';
import { isFileStateModified } from 'mem-fs-editor/state';
import { Piscina } from 'piscina';
import { addLineNumbers } from '../internal/transform-utils.js';
export const createRemoveUnusedImportsTransform = function (options = {}) {
const { ignoreErrors } = options;
const pool = new Piscina({
maxThreads: 1,
filename: new URL('./java-lint-worker.js', import.meta.url).href,
});
return passthrough(async (file) => {
if (extname(file.path) === '.java' && isFileStateModified(file)) {
if (file.contents) {
const fileContents = file.contents.toString('utf8');
const { result, error } = await pool.run({
fileContents,
fileRelativePath: file.relative,
});
if (result) {
file.contents = Buffer.from(result);
}
if (error) {
const errorMessage = `Error parsing file ${file.relative}: ${error} at ${addLineNumbers(fileContents)}`;
if (ignoreErrors) {
this?.log?.warn?.(errorMessage);
return;
}
throw new Error(errorMessage);
}
}
}
}, () => {
pool.destroy();
});
};