Skip to content

Commit 9f8899f

Browse files
fix: improve helper for cli for commands missing positionals (typeorm#10133)
* fix: improve helper for cli for commands missing positionals Add positionals to: - MigrationCreateCommand - MigrationGenerateCommand - SubscriberCreateCommand - EntityCreateCommand * fix: re-enable migration-create/generate tests
1 parent ca29c0f commit 9f8899f

File tree

7 files changed

+74
-29
lines changed

7 files changed

+74
-29
lines changed

src/commands/EntityCreateCommand.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export class EntityCreateCommand implements yargs.CommandModule {
1111
command = "entity:create <path>"
1212
describe = "Generates a new entity."
1313

14+
builder(args: yargs.Argv) {
15+
return args.positional("path", {
16+
type: "string",
17+
describe: "Path of the entity file",
18+
demandOption: true,
19+
})
20+
}
21+
1422
async handler(args: yargs.Arguments) {
1523
try {
1624
const fullPath = (args.path as string).startsWith("/")

src/commands/MigrationCreateCommand.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ export class MigrationCreateCommand implements yargs.CommandModule {
1414

1515
builder(args: yargs.Argv) {
1616
return args
17+
.positional("path", {
18+
type: "string",
19+
describe: "Path of the migration file",
20+
demandOption: true,
21+
})
1722
.option("o", {
1823
alias: "outputJs",
1924
type: "boolean",
@@ -29,12 +34,12 @@ export class MigrationCreateCommand implements yargs.CommandModule {
2934
})
3035
}
3136

32-
async handler(args: yargs.Arguments) {
37+
async handler(args: yargs.Arguments<any & { path: string }>) {
3338
try {
3439
const timestamp = CommandUtils.getTimestamp(args.timestamp)
35-
const inputPath = (args.path as string).startsWith("/")
36-
? (args.path as string)
37-
: path.resolve(process.cwd(), args.path as string)
40+
const inputPath = args.path.startsWith("/")
41+
? args.path
42+
: path.resolve(process.cwd(), args.path)
3843
const filename = path.basename(inputPath)
3944
const fullPath =
4045
path.dirname(inputPath) + "/" + timestamp + "-" + filename
@@ -69,7 +74,7 @@ export class MigrationCreateCommand implements yargs.CommandModule {
6974
* Gets contents of the migration file.
7075
*/
7176
protected static getTemplate(name: string, timestamp: number): string {
72-
return `import { MigrationInterface, QueryRunner } from "typeorm"
77+
return `import { MigrationInterface, QueryRunner } from "typeorm";
7378
7479
export class ${camelCase(
7580
name,

src/commands/MigrationGenerateCommand.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
1818

1919
builder(args: yargs.Argv) {
2020
return args
21+
.positional("path", {
22+
type: "string",
23+
describe: "Path of the migration file",
24+
demandOption: true,
25+
})
2126
.option("dataSource", {
2227
alias: "d",
2328
type: "string",
@@ -60,12 +65,12 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
6065
})
6166
}
6267

63-
async handler(args: yargs.Arguments) {
68+
async handler(args: yargs.Arguments<any & { path: string }>) {
6469
const timestamp = CommandUtils.getTimestamp(args.timestamp)
6570
const extension = args.outputJs ? ".js" : ".ts"
66-
const fullPath = (args.path as string).startsWith("/")
67-
? (args.path as string)
68-
: path.resolve(process.cwd(), args.path as string)
71+
const fullPath = args.path.startsWith("/")
72+
? args.path
73+
: path.resolve(process.cwd(), args.path)
6974
const filename = timestamp + "-" + path.basename(fullPath) + extension
7075

7176
let dataSource: DataSource | undefined = undefined

src/commands/SubscriberCreateCommand.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ export class SubscriberCreateCommand implements yargs.CommandModule {
1111
command = "subscriber:create <path>"
1212
describe = "Generates a new subscriber."
1313

14+
builder(args: yargs.Argv) {
15+
return args.positional("path", {
16+
type: "string",
17+
describe: "Path of the subscriber file",
18+
demandOption: true,
19+
})
20+
}
21+
1422
async handler(args: yargs.Arguments) {
1523
try {
1624
const fullPath = (args.path as string).startsWith("/")

test/functional/commands/migration-create.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import { MigrationCreateCommand } from "../../../src/commands/MigrationCreateCom
1616
import { Post } from "./entity/Post"
1717
import { resultsTemplates } from "./templates/result-templates-create"
1818

19-
// TODO: broken after 0.3.0 changes, fix later
20-
describe.skip("commands - migration create", () => {
19+
describe("commands - migration create", () => {
2120
let connectionOptions: DataSourceOptions[]
2221
let createFileStub: sinon.SinonStub
2322
let timerStub: sinon.SinonFakeTimers
@@ -41,8 +40,7 @@ describe.skip("commands - migration create", () => {
4140
const testHandlerArgs = (options: Record<string, any>) => ({
4241
$0: "test",
4342
_: ["test"],
44-
name: "test-migration",
45-
dir: "test-directory",
43+
path: "test-directory/test-migration",
4644
...options,
4745
})
4846

test/functional/commands/migration-generate.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import sinon from "sinon"
22
import {
33
ConnectionOptionsReader,
44
DatabaseType,
5+
DataSource,
56
DataSourceOptions,
67
} from "../../../src"
78
import {
@@ -15,24 +16,32 @@ import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerat
1516
import { Post } from "./entity/Post"
1617
import { resultsTemplates } from "./templates/result-templates-generate"
1718

18-
// TODO: broken after 0.3.0 changes, fix later
19-
describe.skip("commands - migration generate", () => {
19+
describe("commands - migration generate", () => {
2020
let connectionOptions: DataSourceOptions[]
2121
let createFileStub: sinon.SinonStub
22+
let loadDataSourceStub: sinon.SinonStub
2223
let timerStub: sinon.SinonFakeTimers
2324
let getConnectionOptionsStub: sinon.SinonStub
2425
let migrationGenerateCommand: MigrationGenerateCommand
2526
let connectionOptionsReader: ConnectionOptionsReader
2627
let baseConnectionOptions: DataSourceOptions
2728

28-
const enabledDrivers = ["mysql"] as DatabaseType[]
29+
const enabledDrivers = [
30+
"postgres",
31+
"mssql",
32+
"mysql",
33+
"mariadb",
34+
"sqlite",
35+
"better-sqlite3",
36+
"oracle",
37+
"cockroachdb",
38+
] as DatabaseType[]
2939

3040
// simulate args: `npm run typeorm migration:run -- -n test-migration -d test-directory`
3141
const testHandlerArgs = (options: Record<string, any>) => ({
3242
$0: "test",
3343
_: ["test"],
34-
name: "test-migration",
35-
dir: "test-directory",
44+
path: "test-directory/test-migration",
3645
...options,
3746
})
3847

@@ -52,13 +61,15 @@ describe.skip("commands - migration generate", () => {
5261
connectionOptionsReader = new ConnectionOptionsReader()
5362
migrationGenerateCommand = new MigrationGenerateCommand()
5463
createFileStub = sinon.stub(CommandUtils, "createFile")
64+
loadDataSourceStub = sinon.stub(CommandUtils, "loadDataSource")
5565

5666
timerStub = sinon.useFakeTimers(1610975184784)
5767
})
5868

5969
after(async () => {
6070
timerStub.restore()
6171
createFileStub.restore()
72+
loadDataSourceStub.restore()
6273
})
6374

6475
it("writes regular migration file when no option is passed", async () => {
@@ -75,9 +86,11 @@ describe.skip("commands - migration generate", () => {
7586
entities: [Post],
7687
})
7788

89+
loadDataSourceStub.resolves(new DataSource(connectionOption))
90+
7891
await migrationGenerateCommand.handler(
7992
testHandlerArgs({
80-
connection: connectionOption.name,
93+
dataSource: "dummy-path",
8194
}),
8295
)
8396

@@ -106,9 +119,11 @@ describe.skip("commands - migration generate", () => {
106119
entities: [Post],
107120
})
108121

122+
loadDataSourceStub.resolves(new DataSource(connectionOption))
123+
109124
await migrationGenerateCommand.handler(
110125
testHandlerArgs({
111-
connection: connectionOption.name,
126+
dataSource: "dummy-path",
112127
outputJs: true,
113128
}),
114129
)
@@ -138,9 +153,11 @@ describe.skip("commands - migration generate", () => {
138153
entities: [Post],
139154
})
140155

156+
loadDataSourceStub.resolves(new DataSource(connectionOption))
157+
141158
await migrationGenerateCommand.handler(
142159
testHandlerArgs({
143-
connection: connectionOption.name,
160+
dataSource: "dummy-path",
144161
timestamp: "1641163894670",
145162
}),
146163
)
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
export const resultsTemplates: Record<string, any> = {
2-
control: `import {MigrationInterface, QueryRunner} from "typeorm";
2+
control: `import { MigrationInterface, QueryRunner } from "typeorm";
33
4-
export class testMigration1610975184784 implements MigrationInterface {
4+
export class TestMigration1610975184784 implements MigrationInterface {
55
66
public async up(queryRunner: QueryRunner): Promise<void> {
77
}
88
99
public async down(queryRunner: QueryRunner): Promise<void> {
1010
}
1111
12-
}`,
12+
}
13+
`,
1314
javascript: `const { MigrationInterface, QueryRunner } = require("typeorm");
1415
15-
module.exports = class testMigration1610975184784 {
16+
module.exports = class TestMigration1610975184784 {
1617
1718
async up(queryRunner) {
1819
}
1920
2021
async down(queryRunner) {
2122
}
22-
}`,
23-
timestamp: `import {MigrationInterface, QueryRunner} from "typeorm";
2423
25-
export class testMigration1641163894670 implements MigrationInterface {
24+
}
25+
`,
26+
timestamp: `import { MigrationInterface, QueryRunner } from "typeorm";
27+
28+
export class TestMigration1641163894670 implements MigrationInterface {
2629
2730
public async up(queryRunner: QueryRunner): Promise<void> {
2831
}
2932
3033
public async down(queryRunner: QueryRunner): Promise<void> {
3134
}
3235
33-
}`,
36+
}
37+
`,
3438
}

0 commit comments

Comments
 (0)