-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): support using config file instead of cli args
- Loading branch information
1 parent
ccb8153
commit 1312bb0
Showing
17 changed files
with
343 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: postgres | ||
dialect: 10.7 | ||
declarations: domain.ts | ||
generates: | ||
sql: | ||
to: generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { DomainObject } from 'domain-objects'; | ||
import Joi from 'joi'; | ||
|
||
export enum DatabaseLanguage { | ||
MYSQL = 'mysql', | ||
POSTGRES = 'postgres', | ||
} | ||
|
||
const schema = Joi.object().keys({ | ||
rootDir: Joi.string().required(), // dir of config file, to which all config paths are relative | ||
language: Joi.string().valid(...Object.values(DatabaseLanguage)), | ||
dialect: Joi.string().required(), | ||
declarationsPath: Joi.string().required(), | ||
targetDirPath: Joi.string().required(), | ||
}); | ||
|
||
export interface GeneratorConfig { | ||
rootDir: string; | ||
language: DatabaseLanguage; | ||
dialect: string; | ||
declarationsPath: string; | ||
targetDirPath: string; | ||
} | ||
export class GeneratorConfig | ||
extends DomainObject<GeneratorConfig> | ||
implements GeneratorConfig | ||
{ | ||
public static schema = schema; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { | ||
DatabaseLanguage, | ||
GeneratorConfig, | ||
} from '../../../domain/objects/GeneratorConfig'; | ||
import { UserInputError } from '../../../utils/errors/UserInputError'; | ||
import { readYmlFile } from '../../../utils/fileio/readYmlFile'; | ||
import { getDirOfPath } from '../../../utils/filepaths/getDirOfPath'; | ||
|
||
/* | ||
1. read the config | ||
2. validate the config | ||
*/ | ||
export const readConfig = async ({ | ||
configPath, | ||
}: { | ||
configPath: string; | ||
}): Promise<GeneratorConfig> => { | ||
const configDir = getDirOfPath(configPath); | ||
const getAbsolutePathFromRelativeToConfigPath = (relpath: string) => | ||
`${configDir}/${relpath}`; | ||
|
||
// get the yml | ||
const contents = await readYmlFile({ filePath: configPath }); | ||
|
||
// get the language and dialect | ||
if (!contents.language) | ||
throw new UserInputError({ reason: 'config.language must be defined' }); | ||
const language = contents.language; | ||
if (contents.language && contents.language !== DatabaseLanguage.POSTGRES) | ||
throw new UserInputError({ | ||
reason: | ||
'dao generator only supports postgres. please update the `language` option in your config to `postgres` to continue', | ||
}); | ||
if (!contents.dialect) | ||
throw new UserInputError({ reason: 'config.dialect must be defined' }); | ||
const dialect = `${contents.dialect}`; // ensure that we read it as a string, as it could be a number | ||
|
||
// validate the output config | ||
if (!contents.declarations) | ||
throw new UserInputError({ | ||
reason: | ||
'config.declarations must specify path to the file containing declarations', | ||
}); | ||
if (!contents.generates.sql?.to) | ||
throw new UserInputError({ | ||
reason: | ||
'config.generates.sql.to must specify where to output the generated sql', | ||
}); | ||
|
||
// return the results | ||
return new GeneratorConfig({ | ||
language, | ||
dialect, | ||
rootDir: configDir, | ||
declarationsPath: getAbsolutePathFromRelativeToConfigPath( | ||
contents.declarations, | ||
), | ||
targetDirPath: getAbsolutePathFromRelativeToConfigPath( | ||
contents.generates.sql.to, | ||
), | ||
}); | ||
}; |
Oops, something went wrong.