UNPKG

gen-jhipster

Version:

VHipster - Spring Boot + Angular/React/Vue in one handy generator

44 lines (43 loc) 1.77 kB
import { isFileStateModified } from 'mem-fs-editor/state'; import { Minimatch } from 'minimatch'; import { passthrough } from 'p-transform'; import { Piscina } from 'piscina'; import { isDistFolder } from "../../../lib/index.js"; const minimatch = new Minimatch('**/{.prettierrc**,.prettierignore}'); export const isPrettierConfigFilePath = (filePath) => minimatch.match(filePath); const useTsFile = !isDistFolder(); export const createPrettierTransform = async function (options = {}) { const { ignoreErrors = false, extensions = '*', ...workerOptions } = options; const globExpression = extensions.includes(',') ? `**/*.{${extensions}}` : `**/*.${extensions}`; const minimatch = new Minimatch(globExpression, { dot: true }); const pool = new Piscina({ maxThreads: 1, filename: new URL(`./prettier-worker.${useTsFile ? 'ts' : 'js'}`, import.meta.url).href, ...options, }); return passthrough(async (file) => { if (!minimatch.match(file.path) || !isFileStateModified(file)) { return; } if (!file.contents) { throw new Error(`File content doesn't exist for ${file.relative}`); } const result = await pool.run({ relativeFilePath: file.relative, filePath: file.path, fileContents: file.contents.toString('utf8'), ...workerOptions, }); if ('result' in result) { file.contents = Buffer.from(result.result); } if ('errorMessage' in result) { if (!ignoreErrors) { throw new Error(result.errorMessage); } this?.log?.warn?.(result.errorMessage); } }, async () => { await pool?.destroy(); }); };