Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): improve logging - add namespaces, colors and highlighting
Logger now defaults to `console.log()`, and users can specify what namespaces are they interested in via `debug` option. `true/false` will enable/disable all namespaces. Closes #108
- Loading branch information
Showing
28 changed files
with
331 additions
and
229 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
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,74 @@ | ||
import yargs, { Arguments, Argv, CommandModule } from 'yargs'; | ||
import chalk from 'chalk'; | ||
import { CLIHelper } from './CLIHelper'; | ||
|
||
export type Options = { dump: boolean; run: boolean; noFk: boolean }; | ||
|
||
export class SchemaCommandFactory { | ||
|
||
static readonly DESCRIPTIONS = { | ||
create: 'Create database schema based on current metadata', | ||
update: 'Update database schema based on current metadata', | ||
drop: 'Drop database schema based on current metadata', | ||
}; | ||
|
||
static readonly SUCCESS_MESSAGES = { | ||
create: 'Schema successfully created', | ||
update: 'Schema successfully updated', | ||
drop: 'Schema successfully dropped', | ||
}; | ||
|
||
static create<U extends Options = Options>(command: 'create' | 'update' | 'drop'): CommandModule<{}, U> & { builder: (args: Argv) => Argv<U>; handler: (args: Arguments<U>) => Promise<void> } { | ||
const successMessage = SchemaCommandFactory.SUCCESS_MESSAGES[command]; | ||
|
||
return { | ||
command: `schema:${command}`, | ||
describe: SchemaCommandFactory.DESCRIPTIONS[command], | ||
builder: (args: Argv) => SchemaCommandFactory.configureSchemaCommand(args) as Argv<U>, | ||
handler: (args: Arguments<U>) => SchemaCommandFactory.handleSchemaCommand(args, command, successMessage), | ||
}; | ||
} | ||
|
||
static configureSchemaCommand(args: Argv) { | ||
args.option('r', { | ||
alias: 'run', | ||
type: 'boolean', | ||
desc: 'Runs queries', | ||
}); | ||
args.option('d', { | ||
alias: 'dump', | ||
type: 'boolean', | ||
desc: 'Dumps all queries to console', | ||
}); | ||
args.option('no-fk', { | ||
type: 'boolean', | ||
desc: 'Disable foreign key checks if possible', | ||
default: true, | ||
}); | ||
|
||
return args; | ||
} | ||
|
||
static async handleSchemaCommand(args: Arguments<{ dump: boolean; run: boolean; noFk: boolean }>, method: 'create' | 'update' | 'drop', successMessage: string) { | ||
if (!args.run && !args.dump) { | ||
yargs.showHelp(); | ||
return; | ||
} | ||
|
||
const orm = await CLIHelper.getORM(); | ||
const generator = orm.getSchemaGenerator(); | ||
|
||
if (args.dump) { | ||
const m = `get${method.substr(0, 1).toUpperCase()}${method.substr(1)}SchemaSQL`; | ||
const dump = await generator[m](args.noFk); | ||
CLIHelper.dump(dump, orm.config, 'sql'); | ||
} else { | ||
const m = method + 'Schema'; | ||
await generator[m](args.noFk); | ||
CLIHelper.dump(chalk.green(successMessage)); | ||
} | ||
|
||
await orm.close(true); | ||
} | ||
|
||
} |
This file was deleted.
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
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
Oops, something went wrong.