Skip to content

Commit

Permalink
Merge pull request #27 from Sleavely/pre-migration-errors
Browse files Browse the repository at this point in the history
More and robuster CLI spinners
  • Loading branch information
Sleavely authored May 19, 2020
2 parents 0b8f403 + bfc54e8 commit d88cd00
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,35 @@ let action = cli.input[0]
const targetPath = await main.create(name)
console.log(`Created migration in "${targetPath}`)
} else if (action === 'migrate') {
// Wrap *Each() to print each migration.
const config = await main.getConfig()
// Wrap *Each() to print each step.
const spinners = {}
spinners.getConfig = ora().start('Loading configuration')
const config = await main.getConfig()
spinners.getConfig.succeed('Loaded configuration')

const originalContextBuilder = config.context
config.context = async () => {
const stepName = 'context()'
const runForFirstTime = !spinners[stepName] || spinners[stepName].isSpinning
if (runForFirstTime) spinners[stepName] = ora().start('Building context')
const contextReturnValue = await originalContextBuilder()
if (runForFirstTime) spinners[stepName].succeed('Loaded context')
return contextReturnValue
}

const originalBeforeEach = config.beforeEach
config.beforeEach = async (migrationJob, ...additionalArgs) => {
spinners[migrationJob.filename] = ora()
spinners[migrationJob.filename].start(`Running "${migrationJob.filename}"`)
const stepName = `"${migrationJob.filename}"`
spinners[stepName] = ora()
spinners[stepName].start(`Running ${stepName}`)
await originalBeforeEach(migrationJob, ...additionalArgs)
}

const originalAfterEach = config.afterEach
config.afterEach = async (migrationJob, ...additionalArgs) => {
await originalAfterEach(migrationJob, ...additionalArgs)
spinners[migrationJob.filename].succeed(`Ran "${migrationJob.filename}"`)
const stepName = `"${migrationJob.filename}"`
spinners[stepName].succeed(`Ran ${stepName}`)
}
// Now run 'em
try {
Expand All @@ -71,15 +85,15 @@ let action = cli.input[0]
ora().info('No migrations to run.')
}
} catch (err) {
let currentFile
for (let filename in spinners) {
let currentStep
for (let step in spinners) {
// find runnign ones and fail them.
if (spinners[filename].isSpinning) currentFile = filename
if (spinners[step].isSpinning) currentStep = step
}
spinners[currentFile].fail()
if (currentStep) spinners[currentStep].fail()
console.error(err)
ora('').warn()
ora(`"${currentFile}" encountered a problem. The error above might help.`).warn()
ora(`${currentStep || 'exodus'} encountered a problem. The error above might help.`).warn()
ora('Migrations that finished have been saved to history and will not run again.').warn()
ora('').warn()
process.exit(1)
Expand Down

0 comments on commit d88cd00

Please sign in to comment.