fix naming strategy with relative path(#171)

This commit is contained in:
Kononnable 2019-11-17 19:21:54 +01:00
parent 3496c0647b
commit 9e931025b9
4 changed files with 19 additions and 9 deletions

View File

@ -35,10 +35,9 @@ export default function modelCustomizationPhase(
generationOptions.customNamingStrategyPath !== ""
) {
// TODO: change form of logging
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
const req = require(generationOptions.customNamingStrategyPath) as Partial<
NamingStrategy
>;
const req = TomgUtils.requireLocalFile(
generationOptions.customNamingStrategyPath
) as Partial<NamingStrategy>;
if (req.columnName) {
console.log(
`[${new Date().toLocaleTimeString()}] Using custom naming strategy for column names.`

View File

@ -1,11 +1,8 @@
import { plural } from "pluralize";
import * as changeCase from "change-case";
import { Relation } from "./models/Relation";
import { RelationId } from "./models/RelationId";
import changeCase = require("change-case");
// TODO: Fix naming strategy relative path
export function relationIdName(
relationId: RelationId,
relation: Relation

View File

@ -1,4 +1,5 @@
import * as changeCase from "change-case";
import * as path from "path";
import * as packagejson from "../package.json";
import { Entity } from "./models/Entity";
@ -66,3 +67,16 @@ export function findNameForNewField(
}
return fieldName;
}
export function requireLocalFile(fileName: string): any {
try {
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
return require(fileName);
} catch (err) {
if (!path.isAbsolute(fileName)) {
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
return require(path.resolve(process.cwd(), fileName));
}
throw err;
}
}

View File

@ -570,7 +570,7 @@ describe("Model customization phase", async () => {
clearGenerationDir();
generationOptions.customNamingStrategyPath =
"../test/modelCustomization/testNamingStrategy.ts";
"test/modelCustomization/testNamingStrategy.ts";
// TODO: relationId
const customizedModel = modelCustomizationPhase(