Skip to content

Commit 11d15ce

Browse files
authoredMar 13, 2024
feat(cli): improve migration runner output (#5904)
* feat(cli): make output messages directory agnostic when running migrations * feat(cli): use consistent list formatting when displaying multiple migration script candidates * fix(cli): print relative paths for migration script candidates * feat(cli): simplify message
1 parent 21fb367 commit 11d15ce

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 

‎packages/sanity/src/_internal/cli/commands/migration/runMigrationCommand.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path'
2+
13
import {type CliCommandDefinition} from '@sanity/cli'
24
import {
35
DEFAULT_MUTATION_CONCURRENCY,
@@ -13,6 +15,7 @@ import {hideBin} from 'yargs/helpers'
1315
import yargs from 'yargs/yargs'
1416

1517
import {debug} from '../../debug'
18+
import {MIGRATIONS_DIRECTORY} from './constants'
1619
import {resolveMigrations} from './listMigrationsCommand'
1720
import {prettyFormat} from './prettyMutationFormatter'
1821
import {isLoadableMigrationScript, resolveMigrationScript} from './utils/resolveMigrationScript'
@@ -70,6 +73,7 @@ const runMigrationCommand: CliCommandDefinition<CreateFlags> = {
7073
action: async (args, context) => {
7174
const {apiClient, output, prompt, chalk, workDir} = context
7275
const [id] = args.argsWithoutOptions
76+
const migrationsDirectoryPath = path.join(workDir, MIGRATIONS_DIRECTORY)
7377

7478
const flags = await parseCliFlags(args)
7579

@@ -114,18 +118,18 @@ const runMigrationCommand: CliCommandDefinition<CreateFlags> = {
114118
if (resolvedScripts.length > 1) {
115119
// todo: consider prompt user about which one to run? note: it's likely a mistake if multiple files resolve to the same name
116120
throw new Error(
117-
`Found multiple migrations for "${id}" in current directory ${candidates
118-
.map((candidate) => candidate!.relativePath)
119-
.join(', ')}`,
121+
`Found multiple migrations for "${id}" in ${chalk.cyan(migrationsDirectoryPath)}: \n - ${candidates
122+
.map((candidate) => path.relative(migrationsDirectoryPath, candidate.absolutePath))
123+
.join('\n - ')}`,
120124
)
121125
}
122126

123127
const script = resolvedScripts[0]
124128
if (!script) {
125129
throw new Error(
126-
`No migration found for "${id}" in current directory. Make sure that the migration file exists and exports a valid migration as its default export.\n
130+
`No migration found for "${id}" in ${chalk.cyan(chalk.cyan(migrationsDirectoryPath))}. Make sure that the migration file exists and exports a valid migration as its default export.\n
127131
Tried the following files:\n - ${candidates
128-
.map((candidate) => candidate.relativePath)
132+
.map((candidate) => path.relative(migrationsDirectoryPath, candidate.absolutePath))
129133
.join('\n - ')}`,
130134
)
131135
}

0 commit comments

Comments
 (0)
Please sign in to comment.