From c2dfc79ba6bacdc4dff80f0bc997987bb9903f83 Mon Sep 17 00:00:00 2001 From: Jiri Lojda Date: Mon, 4 Nov 2024 14:11:22 +0100 Subject: [PATCH] Add option to specify order in addMigration --- src/commands/migrations/add/add.ts | 11 +++++++++-- src/modules/migrations/add.ts | 6 +++--- src/modules/migrations/utils/migrationUtils.ts | 11 +++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/commands/migrations/add/add.ts b/src/commands/migrations/add/add.ts index 7771cfa1..43d92c0a 100644 --- a/src/commands/migrations/add/add.ts +++ b/src/commands/migrations/add/add.ts @@ -34,7 +34,13 @@ export const register: RegisterCommand = yargs => alias: "d", describe: "Sets the current DateTime in the order property and prefixes the migration name with it.", type: "boolean", - default: false, + conflicts: ["order"], + }) + .option("order", { + alias: "o", + describe: "Sets the order property to the provided value.", + type: "number", + conflicts: ["timestamp"], }), handler: args => addMigrationCli(args).catch(simplifyErrors), }); @@ -43,7 +49,8 @@ type AddMigrationCliParams = & Readonly<{ name: string; migrationsFolder: string | undefined; - timestamp: boolean; + timestamp: boolean | undefined; + order: number | undefined; type: string; }> & LogOptions; diff --git a/src/modules/migrations/add.ts b/src/modules/migrations/add.ts index f1296311..db60bf4d 100644 --- a/src/modules/migrations/add.ts +++ b/src/modules/migrations/add.ts @@ -16,9 +16,9 @@ export type AddMigrationParams = Readonly< & { name: string; migrationsFolder?: string; - timestamp?: boolean; type: "js" | "ts"; } + & ({ timestamp: true } | { timestamp?: false; order?: number }) & LogOptions >; @@ -41,8 +41,8 @@ export const addMigration = async (params: AddMigrationParams) => { params.timestamp ? formatDateForFileName(currentDate) : undefined, ); const migrationData = params.type === "ts" - ? generateTypescriptMigration(params.timestamp ? currentDate : undefined) - : generateJavascriptMigration(params.timestamp ? currentDate : undefined); + ? generateTypescriptMigration(params.timestamp ? currentDate : params.order) + : generateJavascriptMigration(params.timestamp ? currentDate : params.order); const migrationPath = path.join(folderPath, migrationName); diff --git a/src/modules/migrations/utils/migrationUtils.ts b/src/modules/migrations/utils/migrationUtils.ts index 96479b0d..019df802 100644 --- a/src/modules/migrations/utils/migrationUtils.ts +++ b/src/modules/migrations/utils/migrationUtils.ts @@ -25,11 +25,11 @@ export const formatDateForFileName = (date: Date) => export const getMigrationName = (name: string, type: "js" | "ts", prefix?: string) => `${prefix ?? ""}${name}.${type}`; -export const generateTypescriptMigration = (orderDate?: Date): string => +export const generateTypescriptMigration = (order: Date | number | undefined): string => `import { MigrationModule } from "@kontent-ai/data-ops"; const migration: MigrationModule = { - order: ${orderDate ? `new Date('${orderDate.toISOString()}')` : "1"}, + order: ${order === undefined ? "1" : createOrderPropertyValue(order)}, run: async apiClient => {}, rollback: async apiClient => {}, }; @@ -37,9 +37,9 @@ const migration: MigrationModule = { export default migration; `; -export const generateJavascriptMigration = (orderDate?: Date | null): string => +export const generateJavascriptMigration = (order: Date | number | undefined): string => `const migration = { - order: ${orderDate ? `new Date('${orderDate.toISOString()}')` : "1"}, + order: ${order === undefined ? "1" : createOrderPropertyValue(order)}, run: async apiClient => {}, rollback: asyncapiClient => {}, }; @@ -47,6 +47,9 @@ export const generateJavascriptMigration = (orderDate?: Date | null): string => module.exports = migration; `; +const createOrderPropertyValue = (order: Date | number) => + typeof order === "number" ? order.toString() : `new Date('${order.toISOString()}')`; + export const filterMigrations = ( migrations: ReadonlyArray, params: RunMigrationFilterParams,