Skip to content

Commit

Permalink
fix(sanity): improve error handling on 'sanity migration run' without id
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Jan 30, 2024
1 parent 8290bae commit 5d9a556
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ const createMigrationCommand: CliCommandDefinition<CreateMigrationFlags> = {

let [title] = args.argsWithoutOptions

while (!title.trim()) {
while (!title?.trim()) {
title = await prompt.single({
type: 'input',
suffix: ' (e.g. "Rename field from location to address")',
message: 'Title of migration',
})
if (!title.trim()) {
output.print(chalk.red('Name cannot be empty'))
output.error(chalk.red('Name cannot be empty'))
}
}
const types = await prompt.single({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import {readdir} from 'node:fs/promises'
import type {CliCommandDefinition} from '@sanity/cli'
import type {CliCommandContext, CliCommandDefinition} from '@sanity/cli'
import {register} from 'esbuild-register/dist/node'
import {Migration} from '@sanity/migrate'
import {Table} from 'console-table-printer'
Expand All @@ -21,45 +21,48 @@ const createMigrationCommand: CliCommandDefinition<CreateFlags> = {
helpText,
description: 'List available migrations',
action: async (args, context) => {
const {output, workDir} = context

if (!__DEV__) {
register({
target: `node${process.version.slice(1)}`,
})
}

const directories = (
await readdir(path.join(workDir, MIGRATIONS_DIRECTORY), {withFileTypes: true})
).filter((ent) => ent.isDirectory())

const migrationModules = directories
.map((ent) => {
const candidates = resolveMigrationScript(workDir, ent.name)
const found = candidates.find((candidate) => candidate.mod?.default)
if (!found) {
return null
}
return {
dirname: ent.name,
migration: found.mod.default as Migration,
}
})
.filter(Boolean) as {dirname: string; migration: Migration}[]
const {workDir, output} = context
const migrations = await resolveMigrations(workDir)
const table = new Table({
title: `Found ${migrationModules.length} migrations in project`,
title: `Found ${migrations.length} migrations in project`,
columns: [
{name: 'id', title: 'ID', alignment: 'left'},
{name: 'title', title: 'Title', alignment: 'left'},
],
})

migrationModules.forEach((definedMigration) => {
migrations.forEach((definedMigration) => {
table.addRow({id: definedMigration.dirname, title: definedMigration.migration.title})
})
table.printTable()
output.print(`\nRun "sanity migration run <MIGRATION ID>" to run a migration`)
output.print('\nRun `sanity migration run <ID>` to run a migration')
},
}

export async function resolveMigrations(workDir: string) {
if (!__DEV__) {
register({
target: `node${process.version.slice(1)}`,
})
}

const directories = (
await readdir(path.join(workDir, MIGRATIONS_DIRECTORY), {withFileTypes: true})
).filter((ent) => ent.isDirectory())

return directories
.map((ent) => {
const candidates = resolveMigrationScript(workDir, ent.name)
const found = candidates.find((candidate) => candidate.mod?.default)
if (!found) {
return null
}
return {
dirname: ent.name,
migration: found.mod.default as Migration,
}
})
.filter(Boolean) as {dirname: string; migration: Migration}[]
}

export default createMigrationCommand
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
runFromArchive,
} from '@sanity/migrate'

import {Table} from 'console-table-printer'
import {debug} from '../../debug'
import {formatTransaction} from './utils/mutationFormatter'
import {resolveMigrations} from './listMigrationsCommand'

const helpText = `
Options
Expand Down Expand Up @@ -91,9 +93,23 @@ const runMigrationCommand: CliCommandDefinition<CreateFlags> = {
}

if (!id) {
throw new Error(
"Migration ID must be provided. `sanity migration run <ID>`. To see a list of available migrations and their ID's, run `sanity migration list`",
)
output.error(chalk.red('Error: Migration ID must be provided'))
const migrations = await resolveMigrations(workDir)
const table = new Table({
title: `Migrations found in project`,
columns: [
{name: 'id', title: 'ID', alignment: 'left'},
{name: 'title', title: 'Title', alignment: 'left'},
],
})

migrations.forEach((definedMigration) => {
table.addRow({id: definedMigration.dirname, title: definedMigration.migration.title})
})
table.printTable()
output.print('\nRun `sanity migration run <ID>` to run a migration')

return
}

if (!__DEV__) {
Expand Down

0 comments on commit 5d9a556

Please sign in to comment.