-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
promptForMigrationName.ts
49 lines (43 loc) · 1.32 KB
/
promptForMigrationName.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { isCi, isInteractive } from '@prisma/internals'
import slugify from '@sindresorhus/slugify'
import { prompt } from 'prompts'
type getMigrationNameOutput = {
name?: string
userCancelled?: string
}
export async function getMigrationName(name?: string): Promise<getMigrationNameOutput> {
// Truncate if longer
const maxMigrationNameLength = 200
if (name) {
return {
name: slugify(name, { separator: '_' }).substring(0, maxMigrationNameLength),
}
}
// We use prompts.inject() for testing in our CI
// If not TTY or CI, use default name
else if ((!isInteractive || isCi()) && Boolean(prompt._injected?.length) === false) {
return {
name: '',
}
}
const messageForPrompt = `Enter a name for the new migration:`
// For testing purposes we log the message
// An alternative would be to find a way to capture the prompt message from jest tests
// (attempted without success)
if (Boolean((prompt as any)._injected?.length) === true) {
console.info(messageForPrompt)
}
const response = await prompt({
type: 'text',
name: 'name',
message: messageForPrompt,
})
if (!('name' in response)) {
return {
userCancelled: 'Canceled by user.',
}
}
return {
name: slugify(response.name, { separator: '_' }).substring(0, maxMigrationNameLength) || '',
}
}