Skip to content

Commit

Permalink
feat: JavaScript file migrations output (#7253)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxor committed Jan 11, 2021
1 parent 37698fe commit ce9cb87
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
29 changes: 27 additions & 2 deletions src/commands/MigrationCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export class MigrationCreateCommand implements yargs.CommandModule {
alias: "config",
default: "ormconfig",
describe: "Name of the file with connection configuration."
})
.option("o", {
alias: "outputJs",
type: "boolean",
default: false,
describe: "Generate a migration file on Javascript instead of Typescript",
});
}

Expand All @@ -43,8 +49,11 @@ export class MigrationCreateCommand implements yargs.CommandModule {

try {
const timestamp = new Date().getTime();
const fileContent = MigrationCreateCommand.getTemplate(args.name as any, timestamp);
const filename = timestamp + "-" + args.name + ".ts";
const fileContent = args.outputJs ?
MigrationCreateCommand.getJavascriptTemplate(args.name as any, timestamp)
: MigrationCreateCommand.getTemplate(args.name as any, timestamp);
const extension = args.outputJs ? ".js" : ".ts";
const filename = timestamp + "-" + args.name + extension;
let directory = args.dir as string | undefined;

// if directory is not set then try to open tsconfig and find default path there
Expand Down Expand Up @@ -95,4 +104,20 @@ export class ${camelCase(name, true)}${timestamp} implements MigrationInterface
`;
}

/**
* Gets contents of the migration file in Javascript.
*/
protected static getJavascriptTemplate(name: string, timestamp: number): string {
return `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class ${camelCase(name, true)}${timestamp} {
async up(queryRunner) {
}
async down(queryRunner) {
}
}
`;
}
}
37 changes: 35 additions & 2 deletions src/commands/MigrationGenerateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
alias: "config",
default: "ormconfig",
describe: "Name of the file with connection configuration."
})
.option("o", {
alias: "outputJs",
type: "boolean",
default: false,
describe: "Generate a migration file on Javascript instead of Typescript",
});
}

Expand All @@ -53,7 +59,8 @@ export class MigrationGenerateCommand implements yargs.CommandModule {
}

const timestamp = new Date().getTime();
const filename = timestamp + "-" + args.name + ".ts";
const extension = args.outputJs ? ".js" : ".ts";
const filename = timestamp + "-" + args.name + extension;
let directory = args.dir;

// if directory is not set then try to open tsconfig and find default path there
Expand Down Expand Up @@ -119,7 +126,9 @@ export class MigrationGenerateCommand implements yargs.CommandModule {

if (upSqls.length) {
if (args.name) {
const fileContent = MigrationGenerateCommand.getTemplate(args.name as any, timestamp, upSqls, downSqls.reverse());
const fileContent = args.outputJs ?
MigrationGenerateCommand.getJavascriptTemplate(args.name as any, timestamp, upSqls, downSqls.reverse()) :
MigrationGenerateCommand.getTemplate(args.name as any, timestamp, upSqls, downSqls.reverse());
const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
await CommandUtils.createFile(path, fileContent);

Expand Down Expand Up @@ -178,6 +187,30 @@ ${downSqls.join(`
`;
}

/**
* Gets contents of the migration file in Javascript.
*/
protected static getJavascriptTemplate(name: string, timestamp: number, upSqls: string[], downSqls: string[]): string {
const migrationName = `${camelCase(name, true)}${timestamp}`;

return `const { MigrationInterface, QueryRunner } = require("typeorm");
module.exports = class ${migrationName} {
name = '${migrationName}'
async up(queryRunner) {
${upSqls.join(`
`)}
}
async down(queryRunner) {
${downSqls.join(`
`)}
}
}
`;
}

/**
*
*/
Expand Down

0 comments on commit ce9cb87

Please sign in to comment.