Skip to content

Commit

Permalink
fix: improve helper for cli for commands missing positionals (typeorm…
Browse files Browse the repository at this point in the history
…#10133)

* fix: improve helper for cli for commands missing positionals

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

* fix: re-enable migration-create/generate tests
  • Loading branch information
nicolasroger17 authored Aug 19, 2023
1 parent ca29c0f commit 9f8899f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/commands/EntityCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand Down
15 changes: 10 additions & 5 deletions src/commands/MigrationCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 9 additions & 4 deletions src/commands/MigrationGenerateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/commands/SubscriberCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand Down
6 changes: 2 additions & 4 deletions test/functional/commands/migration-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
})

Expand Down
33 changes: 25 additions & 8 deletions test/functional/commands/migration-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import sinon from "sinon"
import {
ConnectionOptionsReader,
DatabaseType,
DataSource,
DataSourceOptions,
} from "../../../src"
import {
Expand All @@ -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,
})

Expand All @@ -52,13 +61,15 @@ 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)
})

after(async () => {
timerStub.restore()
createFileStub.restore()
loadDataSourceStub.restore()
})

it("writes regular migration file when no option is passed", async () => {
Expand All @@ -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",
}),
)

Expand Down Expand Up @@ -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,
}),
)
Expand Down Expand Up @@ -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",
}),
)
Expand Down
20 changes: 12 additions & 8 deletions test/functional/commands/templates/result-templates-create.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
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> {
}
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> {
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}`,
}
`,
}

0 comments on commit 9f8899f

Please sign in to comment.