fix: improve helper for cli for commands missing positionals (#10133)

* fix: improve helper for cli for commands missing positionals

Add positionals to:
 - MigrationCreateCommand
 - MigrationGenerateCommand
 - SubscriberCreateCommand
 - EntityCreateCommand

* fix: re-enable migration-create/generate tests
This commit is contained in:
Nicolas ROGER 2023-08-19 17:34:32 +02:00 committed by GitHub
parent ca29c0ff8e
commit 9f8899f56c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 29 deletions

View File

@ -11,6 +11,14 @@ export class EntityCreateCommand implements yargs.CommandModule {
command = "entity:create <path>"
describe = "Generates a new entity."
builder(args: yargs.Argv) {
return args.positional("path", {
type: "string",
describe: "Path of the entity file",
demandOption: true,
})
}
async handler(args: yargs.Arguments) {
try {
const fullPath = (args.path as string).startsWith("/")

View File

@ -14,6 +14,11 @@ export class MigrationCreateCommand implements yargs.CommandModule {
builder(args: yargs.Argv) {
return args
.positional("path", {
type: "string",
describe: "Path of the migration file",
demandOption: true,
})
.option("o", {
alias: "outputJs",
type: "boolean",
@ -29,12 +34,12 @@ export class MigrationCreateCommand implements yargs.CommandModule {
})
}
async handler(args: yargs.Arguments) {
async handler(args: yargs.Arguments<any & { path: string }>) {
try {
const timestamp = CommandUtils.getTimestamp(args.timestamp)
const inputPath = (args.path as string).startsWith("/")
? (args.path as string)
: path.resolve(process.cwd(), args.path as string)
const inputPath = args.path.startsWith("/")
? args.path
: path.resolve(process.cwd(), args.path)
const filename = path.basename(inputPath)
const fullPath =
path.dirname(inputPath) + "/" + timestamp + "-" + filename
@ -69,7 +74,7 @@ export class MigrationCreateCommand implements yargs.CommandModule {
* Gets contents of the migration file.
*/
protected static getTemplate(name: string, timestamp: number): string {
return `import { MigrationInterface, QueryRunner } from "typeorm"
return `import { MigrationInterface, QueryRunner } from "typeorm";
export class ${camelCase(
name,

View File

@ -18,6 +18,11 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
builder(args: yargs.Argv) {
return args
.positional("path", {
type: "string",
describe: "Path of the migration file",
demandOption: true,
})
.option("dataSource", {
alias: "d",
type: "string",
@ -60,12 +65,12 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
})
}
async handler(args: yargs.Arguments) {
async handler(args: yargs.Arguments<any & { path: string }>) {
const timestamp = CommandUtils.getTimestamp(args.timestamp)
const extension = args.outputJs ? ".js" : ".ts"
const fullPath = (args.path as string).startsWith("/")
? (args.path as string)
: path.resolve(process.cwd(), args.path as string)
const fullPath = args.path.startsWith("/")
? args.path
: path.resolve(process.cwd(), args.path)
const filename = timestamp + "-" + path.basename(fullPath) + extension
let dataSource: DataSource | undefined = undefined

View File

@ -11,6 +11,14 @@ export class SubscriberCreateCommand implements yargs.CommandModule {
command = "subscriber:create <path>"
describe = "Generates a new subscriber."
builder(args: yargs.Argv) {
return args.positional("path", {
type: "string",
describe: "Path of the subscriber file",
demandOption: true,
})
}
async handler(args: yargs.Arguments) {
try {
const fullPath = (args.path as string).startsWith("/")

View File

@ -16,8 +16,7 @@ import { MigrationCreateCommand } from "../../../src/commands/MigrationCreateCom
import { Post } from "./entity/Post"
import { resultsTemplates } from "./templates/result-templates-create"
// TODO: broken after 0.3.0 changes, fix later
describe.skip("commands - migration create", () => {
describe("commands - migration create", () => {
let connectionOptions: DataSourceOptions[]
let createFileStub: sinon.SinonStub
let timerStub: sinon.SinonFakeTimers
@ -41,8 +40,7 @@ describe.skip("commands - migration create", () => {
const testHandlerArgs = (options: Record<string, any>) => ({
$0: "test",
_: ["test"],
name: "test-migration",
dir: "test-directory",
path: "test-directory/test-migration",
...options,
})

View File

@ -2,6 +2,7 @@ import sinon from "sinon"
import {
ConnectionOptionsReader,
DatabaseType,
DataSource,
DataSourceOptions,
} from "../../../src"
import {
@ -15,24 +16,32 @@ import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerat
import { Post } from "./entity/Post"
import { resultsTemplates } from "./templates/result-templates-generate"
// TODO: broken after 0.3.0 changes, fix later
describe.skip("commands - migration generate", () => {
describe("commands - migration generate", () => {
let connectionOptions: DataSourceOptions[]
let createFileStub: sinon.SinonStub
let loadDataSourceStub: sinon.SinonStub
let timerStub: sinon.SinonFakeTimers
let getConnectionOptionsStub: sinon.SinonStub
let migrationGenerateCommand: MigrationGenerateCommand
let connectionOptionsReader: ConnectionOptionsReader
let baseConnectionOptions: DataSourceOptions
const enabledDrivers = ["mysql"] as DatabaseType[]
const enabledDrivers = [
"postgres",
"mssql",
"mysql",
"mariadb",
"sqlite",
"better-sqlite3",
"oracle",
"cockroachdb",
] as DatabaseType[]
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory`
const testHandlerArgs = (options: Record<string, any>) => ({
$0: "test",
_: ["test"],
name: "test-migration",
dir: "test-directory",
path: "test-directory/test-migration",
...options,
})
@ -52,6 +61,7 @@ describe.skip("commands - migration generate", () => {
connectionOptionsReader = new ConnectionOptionsReader()
migrationGenerateCommand = new MigrationGenerateCommand()
createFileStub = sinon.stub(CommandUtils, "createFile")
loadDataSourceStub = sinon.stub(CommandUtils, "loadDataSource")
timerStub = sinon.useFakeTimers(1610975184784)
})
@ -59,6 +69,7 @@ describe.skip("commands - migration generate", () => {
after(async () => {
timerStub.restore()
createFileStub.restore()
loadDataSourceStub.restore()
})
it("writes regular migration file when no option is passed", async () => {
@ -75,9 +86,11 @@ describe.skip("commands - migration generate", () => {
entities: [Post],
})
loadDataSourceStub.resolves(new DataSource(connectionOption))
await migrationGenerateCommand.handler(
testHandlerArgs({
connection: connectionOption.name,
dataSource: "dummy-path",
}),
)
@ -106,9 +119,11 @@ describe.skip("commands - migration generate", () => {
entities: [Post],
})
loadDataSourceStub.resolves(new DataSource(connectionOption))
await migrationGenerateCommand.handler(
testHandlerArgs({
connection: connectionOption.name,
dataSource: "dummy-path",
outputJs: true,
}),
)
@ -138,9 +153,11 @@ describe.skip("commands - migration generate", () => {
entities: [Post],
})
loadDataSourceStub.resolves(new DataSource(connectionOption))
await migrationGenerateCommand.handler(
testHandlerArgs({
connection: connectionOption.name,
dataSource: "dummy-path",
timestamp: "1641163894670",
}),
)

View File

@ -1,7 +1,7 @@
export const resultsTemplates: Record<string, any> = {
control: `import {MigrationInterface, QueryRunner} from "typeorm";
control: `import { MigrationInterface, QueryRunner } from "typeorm";
export class testMigration1610975184784 implements MigrationInterface {
export class TestMigration1610975184784 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
@ -9,20 +9,23 @@ export class testMigration1610975184784 implements MigrationInterface {
public async down(queryRunner: QueryRunner): Promise<void> {
}
}`,
}
`,
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class testMigration1610975184784 {
module.exports = class TestMigration1610975184784 {
async up(queryRunner) {
}
async down(queryRunner) {
}
}`,
timestamp: `import {MigrationInterface, QueryRunner} from "typeorm";
export class testMigration1641163894670 implements MigrationInterface {
}
`,
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
export class TestMigration1641163894670 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
@ -30,5 +33,6 @@ export class testMigration1641163894670 implements MigrationInterface {
public async down(queryRunner: QueryRunner): Promise<void> {
}
}`,
}
`,
}