From 6a2ac2f063b5e11e865762c3a3dff57ef4e924ec Mon Sep 17 00:00:00 2001 From: kononnable Date: Wed, 30 Dec 2020 21:42:59 +0100 Subject: [PATCH] keep old tsconfig and ormconfig files #154 instead of generated ones --- src/ModelGeneration.ts | 29 +++++++++++++++++++++-------- src/drivers/MssqlDriver.ts | 4 ++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index 1a5ceda..dc7bcf7 100644 --- a/src/ModelGeneration.ts +++ b/src/ModelGeneration.ts @@ -27,8 +27,11 @@ export default function modelGenerationPhase( } let entitiesPath = resultPath; if (!generationOptions.noConfigs) { - createTsConfigFile(resultPath); - createTypeOrmConfig(resultPath, connectionOptions); + const tsconfigPath = path.resolve(resultPath, "tsconfig.json"); + const typeormConfigPath = path.resolve(resultPath, "ormconfig.json"); + + createTsConfigFile(tsconfigPath); + createTypeOrmConfig(typeormConfigPath, connectionOptions); entitiesPath = path.resolve(resultPath, "./entities"); if (!fs.existsSync(entitiesPath)) { fs.mkdirSync(entitiesPath); @@ -258,7 +261,13 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions): void { }); } -function createTsConfigFile(outputPath: string): void { +function createTsConfigFile(tsconfigPath: string): void { + if (fs.existsSync(tsconfigPath)) { + console.warn( + `\x1b[33m[${new Date().toLocaleTimeString()}] WARNING: Skipping generation of tsconfig.json file. File already exists. \x1b[0m` + ); + return; + } const templatePath = path.resolve(__dirname, "templates", "tsconfig.mst"); const template = fs.readFileSync(templatePath, "utf-8"); const compliedTemplate = Handlebars.compile(template, { @@ -266,16 +275,21 @@ function createTsConfigFile(outputPath: string): void { }); const rendered = compliedTemplate({}); const formatted = Prettier.format(rendered, { parser: "json" }); - const resultFilePath = path.resolve(outputPath, "tsconfig.json"); - fs.writeFileSync(resultFilePath, formatted, { + fs.writeFileSync(tsconfigPath, formatted, { encoding: "utf-8", flag: "w", }); } function createTypeOrmConfig( - outputPath: string, + typeormConfigPath: string, connectionOptions: IConnectionOptions ): void { + if (fs.existsSync(typeormConfigPath)) { + console.warn( + `\x1b[33m[${new Date().toLocaleTimeString()}] WARNING: Skipping generation of ormconfig.json file. File already exists. \x1b[0m` + ); + return; + } const templatePath = path.resolve(__dirname, "templates", "ormconfig.mst"); const template = fs.readFileSync(templatePath, "utf-8"); const compiledTemplate = Handlebars.compile(template, { @@ -283,8 +297,7 @@ function createTypeOrmConfig( }); const rendered = compiledTemplate(connectionOptions); const formatted = Prettier.format(rendered, { parser: "json" }); - const resultFilePath = path.resolve(outputPath, "ormconfig.json"); - fs.writeFileSync(resultFilePath, formatted, { + fs.writeFileSync(typeormConfigPath, formatted, { encoding: "utf-8", flag: "w", }); diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 885304a..cdae412 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -101,8 +101,8 @@ export default class MssqlDriver extends AbstractDriver { LEFT JOIN (SELECT tc.TABLE_SCHEMA,tc.TABLE_NAME,cu.COLUMN_NAME,COUNT(1) AS cnt FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu on cu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME where tc.CONSTRAINT_TYPE = 'UNIQUE' GROUP BY tc.TABLE_SCHEMA,tc.TABLE_NAME,cu.COLUMN_NAME) AS tc on tc.TABLE_NAME = c.TABLE_NAME and tc.COLUMN_NAME = c.COLUMN_NAME and tc.TABLE_SCHEMA=c.TABLE_SCHEMA where c.TABLE_SCHEMA in (${MssqlDriver.buildEscapedObjectList( - schemas - )}) AND c.TABLE_CATALOG in (${MssqlDriver.buildEscapedObjectList( + schemas + )}) AND c.TABLE_CATALOG in (${MssqlDriver.buildEscapedObjectList( dbNames )}) order by ordinal_position `)