Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add basic CLI tool (#102)
Adds `mikro-orm` npm command that allows to clear cache and work with SchemaGenerator and EntityGenerator. First one needs to provide `cli-config.[jt]s` file in the root directory that will export the ORM configuration. Closes #101
- Loading branch information
Showing
23 changed files
with
562 additions
and
13 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 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,14 @@ | ||
import yargs from 'yargs'; | ||
import { CLIHelper } from './cli/CLIHelper'; | ||
|
||
require('yargonaut') | ||
.style('blue') | ||
.style('yellow', 'required') | ||
.helpStyle('green') | ||
.errorsStyle('red'); | ||
|
||
const args = CLIHelper.configure().parse(process.argv.slice(2)) as { _: string[] }; | ||
|
||
if (args._.length === 0) { | ||
yargs.showHelp(); | ||
} |
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,58 @@ | ||
import yargs, { Argv } from 'yargs'; | ||
import { ClearCacheCommand } from './ClearCacheCommand'; | ||
import { GenerateEntitiesCommand } from './GenerateEntitiesCommand'; | ||
import { MikroORM } from '../MikroORM'; | ||
import { Configuration, Utils } from '../utils'; | ||
import { CreateSchemaCommand } from './CreateSchemaCommand'; | ||
import { UpdateSchemaCommand } from './UpdateSchemaCommand'; | ||
import { DropSchemaCommand } from './DropSchemaCommand'; | ||
|
||
export class CLIHelper { | ||
|
||
static getConfiguration(): Configuration { | ||
return new Configuration(require(Utils.normalizePath(process.env.MIKRO_ORM_CLI || './cli-config'))); | ||
} | ||
|
||
static async getORM(): Promise<MikroORM> { | ||
const options = CLIHelper.getConfiguration(); | ||
return MikroORM.init(options); | ||
} | ||
|
||
static configure() { | ||
return yargs | ||
.scriptName('mikro-orm') | ||
.version(require('../../package.json').version) | ||
.usage('Usage: $0 <command> [options]') | ||
.example('$0 schema:update --run', 'Runs schema synchronization') | ||
.alias('v', 'version') | ||
.alias('h', 'help') | ||
.command(new ClearCacheCommand()) | ||
.command(new GenerateEntitiesCommand()) | ||
.command(new CreateSchemaCommand()) | ||
.command(new DropSchemaCommand()) | ||
.command(new UpdateSchemaCommand()) | ||
.recommendCommands() | ||
.strict(); | ||
} | ||
|
||
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; | ||
} | ||
|
||
} |
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,19 @@ | ||
import { Arguments, CommandModule } from 'yargs'; | ||
import chalk from 'chalk'; | ||
import { CLIHelper } from './CLIHelper'; | ||
|
||
export class ClearCacheCommand implements CommandModule { | ||
|
||
command = 'cache:clear'; | ||
describe = 'Clear metadata cache'; | ||
|
||
async handler(args: Arguments) { | ||
const config = CLIHelper.getConfiguration(); | ||
const cache = config.getCacheAdapter(); | ||
await cache.clear(); | ||
|
||
// tslint:disable-next-line:no-console | ||
console.log(chalk.green('Metadata cache was successfully cleared') + '\n'); | ||
} | ||
|
||
} |
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,38 @@ | ||
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 CreateSchemaCommand<U extends Options = Options> implements CommandModule<{}, U> { | ||
|
||
command = 'schema:create'; | ||
describe = 'Create database schema based on current metadata'; | ||
|
||
builder(args: Argv) { | ||
return CLIHelper.configureSchemaCommand(args) as Argv<U>; | ||
} | ||
|
||
async handler(args: Arguments<U>) { | ||
if (!args.run && !args.dump) { | ||
yargs.showHelp(); | ||
return; | ||
} | ||
|
||
const orm = await CLIHelper.getORM(); | ||
const generator = orm.getSchemaGenerator(); | ||
|
||
if (args.dump) { | ||
const dump = await generator.getCreateSchemaSQL(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(dump); | ||
} else { | ||
await generator.createSchema(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(chalk.green('Schema successfully created')); | ||
} | ||
|
||
await orm.close(true); | ||
} | ||
|
||
} |
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,38 @@ | ||
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 DropSchemaCommand<U extends Options = Options> implements CommandModule<{}, U> { | ||
|
||
command = 'schema:drop'; | ||
describe = 'Drop all tables based on current metadata'; | ||
|
||
builder(args: Argv) { | ||
return CLIHelper.configureSchemaCommand(args) as Argv<U>; | ||
} | ||
|
||
async handler(args: Arguments<U>) { | ||
if (!args.run && !args.dump) { | ||
yargs.showHelp(); | ||
return; | ||
} | ||
|
||
const orm = await CLIHelper.getORM(); | ||
const generator = orm.getSchemaGenerator(); | ||
|
||
if (args.dump) { | ||
const dump = await generator.getDropSchemaSQL(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(dump); | ||
} else { | ||
await generator.dropSchema(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(chalk.green('Schema successfully dropped') + '\n'); | ||
} | ||
|
||
await orm.close(true); | ||
} | ||
|
||
} |
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,48 @@ | ||
import yargs, { Arguments, Argv, CommandModule } from 'yargs'; | ||
import { CLIHelper } from './CLIHelper'; | ||
|
||
export type Options = { dump: boolean; save: boolean; path: string }; | ||
|
||
export class GenerateEntitiesCommand<U extends Options = Options> implements CommandModule<{}, U> { | ||
|
||
command = 'generate-entities'; | ||
describe = 'Generate entities based on current database schema'; | ||
|
||
builder(args: Argv) { | ||
args.option('s', { | ||
alias: 'save', | ||
type: 'boolean', | ||
desc: 'Saves entities to directory defined by --path', | ||
}); | ||
args.option('d', { | ||
alias: 'dump', | ||
type: 'boolean', | ||
desc: 'Dumps all entities to console', | ||
}); | ||
args.option('p', { | ||
alias: 'path', | ||
type: 'string', | ||
desc: 'Sets path to directory where to save entities', | ||
}); | ||
|
||
return args as unknown as Argv<U>; | ||
} | ||
|
||
async handler(args: Arguments<U>) { | ||
if (!args.save && !args.dump) { | ||
yargs.showHelp(); | ||
return; | ||
} | ||
|
||
const orm = await CLIHelper.getORM(); | ||
const generator = orm.getEntityGenerator(); | ||
const dump = await generator.generate({ save: args.save, baseDir: args.path }); | ||
|
||
if (args.dump) { | ||
process.stdout.write(dump.join('\n\n')); | ||
} | ||
|
||
await orm.close(true); | ||
} | ||
|
||
} |
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,38 @@ | ||
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 UpdateSchemaCommand<U extends Options = Options> implements CommandModule<{}, U> { | ||
|
||
command = 'schema:update'; | ||
describe = 'Update database schema based on current metadata'; | ||
|
||
builder(args: Argv) { | ||
return CLIHelper.configureSchemaCommand(args) as Argv<U>; | ||
} | ||
|
||
async handler(args: Arguments<U>) { | ||
if (!args.run && !args.dump) { | ||
yargs.showHelp(); | ||
return; | ||
} | ||
|
||
const orm = await CLIHelper.getORM(); | ||
const generator = orm.getSchemaGenerator(); | ||
|
||
if (args.dump) { | ||
const dump = await generator.getUpdateSchemaSQL(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(dump + '\n'); | ||
} else { | ||
await generator.updateSchema(args.noFk); | ||
// tslint:disable-next-line:no-console | ||
console.log(chalk.green('Schema successfully updated') + '\n'); | ||
} | ||
|
||
await orm.close(true); | ||
} | ||
|
||
} |
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 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,13 @@ | ||
import { BASE_DIR } from './bootstrap'; | ||
import { JavaScriptMetadataProvider } from '../lib'; | ||
import { SqliteDriver } from '../lib/drivers/SqliteDriver'; | ||
|
||
const { BaseEntity4, Test3 } = require('./entities-js'); | ||
|
||
export = { | ||
entities: [Test3, BaseEntity4], | ||
dbName: './mikro_orm_test.db', | ||
baseDir: BASE_DIR, | ||
driver: SqliteDriver, | ||
metadataProvider: JavaScriptMetadataProvider, | ||
}; |
Oops, something went wrong.