Skip to content

Commit

Permalink
Add option to pad order in fileName with zeros in addMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Nov 5, 2024
1 parent c2dfc79 commit c5180ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
20 changes: 19 additions & 1 deletion src/commands/migrations/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const register: RegisterCommand = yargs =>
.option("migrationsFolder", {
alias: "m",
type: "string",
describe: "Specifies the path to the folder where the migration should be stored.",
describe:
"Specifies the path to the folder where the migration should be stored. Defaults to the current working directory.",
})
.option("type", {
alias: "t",
Expand All @@ -41,6 +42,12 @@ export const register: RegisterCommand = yargs =>
describe: "Sets the order property to the provided value.",
type: "number",
conflicts: ["timestamp"],
})
.option("padWithLeadingZeros", {
describe: "Specifies the number of leading zeros for the order property.",
type: "number",
conflicts: ["timestamp"],
implies: "order",
}),
handler: args => addMigrationCli(args).catch(simplifyErrors),
});
Expand All @@ -51,6 +58,7 @@ type AddMigrationCliParams =
migrationsFolder: string | undefined;
timestamp: boolean | undefined;
order: number | undefined;
padWithLeadingZeros: number | undefined;
type: string;
}>
& LogOptions;
Expand All @@ -69,8 +77,18 @@ const resolveParams = (args: AddMigrationCliParams): AddMigrationParams => {
throw new Error(`Invalid type '${args.type}'. Allowed values are 'ts' (TypeScript) or 'js' (JavaScript).`);
}

const orderParams = {
timestamp: args.timestamp,
order: args.order,
padWithLeadingZeros: args.padWithLeadingZeros,
} as
| { timestamp: true }
| { timestamp: false | undefined; order: number }
| { timestamp: false | undefined; order: number; padWithLeadingZeros: number | undefined };

return {
...args,
type: args.type as "ts" | "js",
...orderParams,
};
};
28 changes: 20 additions & 8 deletions src/modules/migrations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ import * as path from "path";
import { logInfo, LogOptions } from "../../log.js";
import { handleErr } from "./utils/errUtils.js";
import { createFolder, saveFile } from "./utils/fileUtils.js";
import {
formatDateForFileName,
generateJavascriptMigration,
generateTypescriptMigration,
getMigrationName,
} from "./utils/migrationUtils.js";
import { generateJavascriptMigration, generateTypescriptMigration, getMigrationName } from "./utils/migrationUtils.js";

export type AddMigrationParams = Readonly<
& {
name: string;
migrationsFolder?: string;
type: "js" | "ts";
}
& ({ timestamp: true } | { timestamp?: false; order?: number })
& TimestampOrOrderParams
& LogOptions
>;

Expand All @@ -38,7 +33,11 @@ export const addMigration = async (params: AddMigrationParams) => {
const migrationName = getMigrationName(
params.name,
params.type,
params.timestamp ? formatDateForFileName(currentDate) : undefined,
params.timestamp
? currentDate
: params.order === undefined
? undefined
: padWithLeadingZeros(params.order, params.padWithLeadingZeros),
);
const migrationData = params.type === "ts"
? generateTypescriptMigration(params.timestamp ? currentDate : params.order)
Expand All @@ -55,3 +54,16 @@ export const addMigration = async (params: AddMigrationParams) => {
`Migration ${migrationName} has been created sucessfully in ${chalk.blue(saveMigrationPath)}`,
);
};

const padWithLeadingZeros = (order: number, numberOfZeros: number | undefined) =>
numberOfZeros
? order.toString().padStart(numberOfZeros, "0")
: order.toString();

type TimestampOrOrderParams =
| Readonly<{ timestamp: true }>
| Readonly<{ timestamp?: false } & OrderParams>;

type OrderParams =
| Readonly<{ order: number; padWithLeadingZeros?: number }>
| Readonly<{ order?: never }>;
3 changes: 2 additions & 1 deletion src/modules/migrations/utils/migrationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const formatDateForFileName = (date: Date) =>
+ `${("0" + date.getUTCMinutes()).slice(-2)}-`
+ `${("0" + date.getUTCSeconds()).slice(-2)}-`;

export const getMigrationName = (name: string, type: "js" | "ts", prefix?: string) => `${prefix ?? ""}${name}.${type}`;
export const getMigrationName = (name: string, type: "js" | "ts", prefix: Date | string | undefined) =>
`${prefix instanceof Date ? formatDateForFileName(prefix) : prefix ?? ""}${name}.${type}`;

export const generateTypescriptMigration = (order: Date | number | undefined): string =>
`import { MigrationModule } from "@kontent-ai/data-ops";
Expand Down

0 comments on commit c5180ae

Please sign in to comment.