| 5 | import type { FumaDB } from ".."; |
| 6 | |
| 7 | export function createCli(options: { |
| 8 | db: FumaDB<any>; |
| 9 | |
| 10 | /** |
| 11 | * CLI command name, must be lowercase without whitespaces. |
| 12 | */ |
| 13 | command: string; |
| 14 | description?: string; |
| 15 | |
| 16 | /** |
| 17 | * CLI Version |
| 18 | */ |
| 19 | version: string; |
| 20 | }) { |
| 21 | const db = options.db as FumaDB; |
| 22 | |
| 23 | async function selectVersion(defaultValue?: string) { |
| 24 | const schemas = db.schemas; |
| 25 | const selected = await select({ |
| 26 | message: "Select target schema version:", |
| 27 | options: schemas.map((s, i) => { |
| 28 | let hint: string | undefined; |
| 29 | if (s.version === defaultValue) { |
| 30 | hint = "current"; |
| 31 | } else if (i === schemas.length - 1) { |
| 32 | hint = "latest"; |
| 33 | } |
| 34 | |
| 35 | return { |
| 36 | value: s.version, |
| 37 | label: s.version, |
| 38 | hint, |
| 39 | }; |
| 40 | }), |
| 41 | initialValue: defaultValue, |
| 42 | }); |
| 43 | |
| 44 | if (isCancel(selected)) { |
| 45 | cancel("Migration cancelled."); |
| 46 | process.exit(0); |
| 47 | } |
| 48 | |
| 49 | return selected; |
| 50 | } |
| 51 | |
| 52 | async function inputOutputPath(type: "sql" | "orm", suggestion: string) { |
| 53 | const result = await text({ |
| 54 | message: |
| 55 | type === "sql" |
| 56 | ? "Where to output the SQL migration file?" |
| 57 | : "Where to output the generated schema? (it will override the destination)", |
| 58 | defaultValue: suggestion, |
| 59 | placeholder: suggestion, |
| 60 | }); |
| 61 | |
| 62 | if (isCancel(result)) { |
| 63 | cancel("Migration cancelled."); |
| 64 | process.exit(0); |