@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
74 lines (69 loc) • 2.38 kB
JavaScript
import path from "node:path";
import * as readline from "node:readline";
import { CommandUtils } from "typeorm/commands/CommandUtils";
import chalk from "chalk";
import { camelCase } from "typeorm/util/StringUtils";
import { PlatformTools } from "typeorm/platform/PlatformTools";
//#region src/consoles/typeorm-create.ts
/**
* Gets contents of the migration file.
*/
function getTemplate(name, timestamp) {
const migrationName = `${camelCase(name, true)}${timestamp}`;
return `import { MigrationInterface, QueryRunner } from "typeorm";
export class ${migrationName} implements MigrationInterface {
name = '${migrationName}'
public async up(queryRunner: QueryRunner): Promise<void> {
// TODO: Add migration queries here
}
public async down(queryRunner: QueryRunner): Promise<void> {
// TODO: Add rollback queries here
}
}
`;
}
async function confirm(question, hint) {
const line = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
line.question(question, (response) => {
line.close();
resolve(response);
});
line.write(hint);
});
}
async function main() {
const timestamp = CommandUtils.getTimestamp(Date.now());
const extension = ".ts";
try {
console.log(chalk.green(`Creating empty migration file...`));
let name;
while (!name) {
name = (await confirm("Please provide a name for the migration: ", "NewMigration"))?.trim();
if (name) break;
console.log(chalk.red(`Name is required, try again`));
process.exit(1);
}
const fullPath = path.resolve(process.cwd(), `src/migrations/${name}`);
const filename = timestamp + "-" + path.basename(fullPath) + extension;
const fileContent = getTemplate(path.basename(fullPath), timestamp);
const migrationFileName = path.dirname(fullPath) + "/" + filename;
await CommandUtils.createFile(migrationFileName, fileContent);
console.log(chalk.green(`Migration ${chalk.blue(migrationFileName)} has been generated successfully.`));
} catch (err) {
PlatformTools.logCmdErr("Error during migration creation:", err);
process.exit(1);
}
}
let start = (/* @__PURE__ */ new Date()).getTime();
main().then(() => {
let end = (/* @__PURE__ */ new Date()).getTime();
console.log("done", end - start, "ms");
process.exit(0);
});
//#endregion
export {};
//# sourceMappingURL=typeorm-create.js.map