| 8 | } |
| 9 | |
| 10 | export async function getMigrationName(name?: string): Promise<getMigrationNameOutput> { |
| 11 | // Truncate if longer |
| 12 | const maxMigrationNameLength = 200 |
| 13 | |
| 14 | if (name) { |
| 15 | return { |
| 16 | name: slugify(name, { separator: '_' }).substring(0, maxMigrationNameLength), |
| 17 | } |
| 18 | } |
| 19 | // We use prompts.inject() for testing in our CI |
| 20 | // If not TTY or CI, use default name |
| 21 | else if ((!isInteractive || isCi()) && Boolean(prompt._injected?.length) === false) { |
| 22 | return { |
| 23 | name: '', |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | const messageForPrompt = `Enter a name for the new migration:` |
| 28 | // For testing purposes we log the message |
| 29 | // An alternative would be to find a way to capture the prompt message from jest tests |
| 30 | // (attempted without success) |
| 31 | if (Boolean((prompt as any)._injected?.length) === true) { |
| 32 | process.stdout.write(messageForPrompt + '\n') |
| 33 | } |
| 34 | const response = await prompt({ |
| 35 | type: 'text', |
| 36 | name: 'name', |
| 37 | message: messageForPrompt, |
| 38 | }) |
| 39 | |
| 40 | if (!('name' in response)) { |
| 41 | return { |
| 42 | userCancelled: 'Canceled by user.', |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return { |
| 47 | name: slugify(response.name, { separator: '_' }).substring(0, maxMigrationNameLength) || '', |
| 48 | } |
| 49 | } |