forked from typeorm/typeorm
/
MigrationCreateCommand.ts
95 lines (80 loc) · 3.29 KB
/
MigrationCreateCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {ConnectionOptionsReader} from "../connection/ConnectionOptionsReader";
import {CommandUtils} from "./CommandUtils";
import {camelCase} from "../util/StringUtils";
import * as yargs from "yargs";
const chalk = require("chalk");
/**
* Creates a new migration file.
*/
export class MigrationCreateCommand implements yargs.CommandModule {
command = "migration:create";
describe = "Creates a new migration file.";
aliases = "migrations:create";
builder(args: yargs.Argv) {
return args
.option("c", {
alias: "connection",
default: "default",
describe: "Name of the connection on which run a query."
})
.option("n", {
alias: "name",
describe: "Name of the migration class.",
demand: true
})
.option("d", {
alias: "dir",
describe: "Directory where migration should be created."
})
.option("f", {
alias: "config",
default: "ormconfig",
describe: "Name of the file with connection configuration."
});
}
async handler(args: yargs.Arguments) {
if (args._[0] === "migrations:create") {
console.log("'migrations:create' is deprecated, please use 'migration:create' instead");
}
try {
const timestamp = new Date().getTime();
const fileContent = MigrationCreateCommand.getTemplate(args.name as any, timestamp);
const filename = timestamp + "-" + args.name + ".ts";
let directory = args.dir;
// if directory is not set then try to open tsconfig and find default path there
if (!directory) {
try {
const connectionOptionsReader = new ConnectionOptionsReader({
root: process.cwd(),
configName: args.config as any
});
const connectionOptions = await connectionOptionsReader.get(args.connection as any);
directory = connectionOptions.cli ? connectionOptions.cli.migrationsDir : undefined;
} catch (err) { }
}
const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
await CommandUtils.createFile(path, fileContent);
console.log(`Migration ${chalk.blue(path)} has been generated successfully.`);
} catch (err) {
console.log(chalk.black.bgRed("Error during migration creation:"));
console.error(err);
process.exit(1);
}
}
// -------------------------------------------------------------------------
// Protected Static Methods
// -------------------------------------------------------------------------
/**
* Gets contents of the migration file.
*/
protected static getTemplate(name: string, timestamp: number): string {
return `import {MigrationInterface, QueryRunner} from "typeorm";
export class ${camelCase(name, true)}${timestamp} implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}
`;
}
}