gen-jhipster
Version:
VHipster - Spring Boot + Angular/React/Vue in one handy generator
54 lines (53 loc) • 2.39 kB
JavaScript
import { readdir } from 'node:fs/promises';
import { basename, join } from 'node:path';
import { loadFile } from 'mem-fs';
import { Minimatch } from 'minimatch';
import { transform } from 'p-transform';
import { normalizePath } from "../../../lib/utils/path.js";
import { GENERATOR_JHIPSTER } from "../../generator-constants.js";
export const updateApplicationEntitiesTransform = ({ destinationPath, throwOnMissingConfig = true, }) => {
let yoRcFileInMemory;
const entities = [];
const yoRcFilePath = join(destinationPath, '.yo-rc.json');
const entitiesMatcher = new Minimatch(`${normalizePath(destinationPath)}/.jhipster/*.json`);
return transform(file => {
if (file.path === yoRcFilePath) {
yoRcFileInMemory = file;
return undefined;
}
if (entitiesMatcher.match(normalizePath(file.path))) {
entities.push(basename(file.path).replace('.json', ''));
}
return file;
}, async function () {
try {
entities.push(...(await readdir(join(destinationPath, '.jhipster'))).map(file => file.replace('.json', '')));
}
catch {
// Directory does not exist
}
if (entities.length > 0) {
// The mem-fs instance requires another file instance to emit a change event
const yoRcFile = loadFile(yoRcFilePath);
// Prefer in-memory file if it exists
const yoRcFileContents = yoRcFileInMemory?.contents ?? yoRcFile.contents;
if (yoRcFileContents) {
const contents = JSON.parse(yoRcFileContents.toString());
if (contents[GENERATOR_JHIPSTER]) {
contents[GENERATOR_JHIPSTER].entities = [...new Set([...(contents[GENERATOR_JHIPSTER].entities ?? []), ...entities])];
yoRcFile.contents = Buffer.from(JSON.stringify(contents, null, 2));
yoRcFileInMemory = yoRcFile;
}
else if (throwOnMissingConfig) {
throw new Error(`File ${yoRcFile.path} is not a valid JHipster configuration file`);
}
}
else if (throwOnMissingConfig) {
throw new Error(`File ${yoRcFile.path} has no contents`);
}
}
if (yoRcFileInMemory) {
this.push(yoRcFileInMemory);
}
});
};