Skip to content

Commit

Permalink
refactor: move messages to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Apr 21, 2020
1 parent 6392480 commit 6da7667
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
42 changes: 42 additions & 0 deletions lib/messages.js
@@ -0,0 +1,42 @@
'use strict'

const chalk = require('chalk')
const symbols = require('log-symbols')

const NO_STAGED_FILES = `${symbols.info} No staged files found.`

const skippingBackup = (hasInitialCommit) => {
const reason = hasInitialCommit ? '`--no-stash` was used' : 'there’s no initial commit yet'
return `${symbols.warning} ${chalk.yellow(`Skipping backup because ${reason}.\n`)}`
}

const DEPRECATED_GIT_ADD = `${symbols.warning} ${chalk.yellow(
`Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.`
)}
`

const TASK_ERROR = 'Skipped because of errors from tasks.'

const GIT_ERROR = 'Skipped because of previous git error.'

const PREVENTED_EMPTY_COMMIT = `
${symbols.warning} ${chalk.yellow(`lint-staged prevented an empty git commit.
Use the --allow-empty option to continue, or check your task configuration`)}
`

const RESTORE_STASH_EXAMPLE = ` Any lost modifications can be restored from a git stash:
> git stash list
stash@{0}: automatic lint-staged backup
> git stash apply --index stash@{0}
`

module.exports = {
NO_STAGED_FILES,
skippingBackup,
DEPRECATED_GIT_ADD,
TASK_ERROR,
GIT_ERROR,
PREVENTED_EMPTY_COMMIT,
RESTORE_STASH_EXAMPLE
}
47 changes: 20 additions & 27 deletions lib/runAll.js
Expand Up @@ -12,6 +12,15 @@ const generateTasks = require('./generateTasks')
const getStagedFiles = require('./getStagedFiles')
const GitWorkflow = require('./gitWorkflow')
const makeCmdTasks = require('./makeCmdTasks')
const {
DEPRECATED_GIT_ADD,
GIT_ERROR,
NO_STAGED_FILES,
PREVENTED_EMPTY_COMMIT,
RESTORE_STASH_EXAMPLE,
TASK_ERROR,
skippingBackup
} = require('./messages')
const resolveGitRepo = require('./resolveGitRepo')
const {
ApplyEmptyCommitError,
Expand All @@ -32,19 +41,14 @@ const getRenderer = ({ debug, quiet }) => {
return 'update'
}

const MESSAGES = {
TASK_ERROR: 'Skipped because of errors from tasks.',
GIT_ERROR: 'Skipped because of previous git error.'
}

const shouldSkipApplyModifications = (ctx) => {
// Should be skipped in case of git errors
if (ctx.errors.has(GitError)) {
return MESSAGES.GIT_ERROR
return GIT_ERROR
}
// Should be skipped when tasks fail
if (ctx.errors.has(TaskError)) {
return MESSAGES.TASK_ERROR
return TASK_ERROR
}
}

Expand All @@ -55,7 +59,7 @@ const shouldSkipRevert = (ctx) => {
!ctx.errors.has(ApplyEmptyCommitError) &&
!ctx.errors.has(RestoreUnstagedChangesError)
) {
return MESSAGES.GIT_ERROR
return GIT_ERROR
}
}

Expand All @@ -66,11 +70,11 @@ const shouldSkipCleanup = (ctx) => {
!ctx.errors.has(ApplyEmptyCommitError) &&
!ctx.errors.has(RestoreUnstagedChangesError)
) {
return MESSAGES.GIT_ERROR
return GIT_ERROR
}
// Should be skipped when reverting to original state fails
if (ctx.errors.has(RestoreOriginalStateError)) {
return MESSAGES.GIT_ERROR
return GIT_ERROR
}
}

Expand Down Expand Up @@ -120,8 +124,7 @@ const runAll = async (
// Lint-staged should create a backup stash only when there's an initial commit
const shouldBackup = hasInitialCommit && stash
if (!shouldBackup) {
const reason = hasInitialCommit ? '`--no-stash` was used' : 'there’s no initial commit yet'
logger.warn(`${symbols.warning} ${chalk.yellow(`Skipping backup because ${reason}.\n`)}`)
logger.warn(skippingBackup(hasInitialCommit))
}

const files = await getStagedFiles({ cwd: gitDir })
Expand All @@ -130,7 +133,7 @@ const runAll = async (

// If there are no files avoid executing any lint-staged logic
if (files.length === 0) {
return logger.log(`${symbols.info} No staged files found.`)
return logger.log(NO_STAGED_FILES)
}

const stagedFileChunks = chunkFiles({ baseDir: gitDir, files, maxArgLength, relative })
Expand Down Expand Up @@ -205,7 +208,7 @@ const runAll = async (
task: () => new Listr(chunkListrTasks, { ...listrOptions, concurrent }),
skip: () => {
// Skip if the first step (backup) failed
if (listrCtx.errors.has(GitError)) return MESSAGES.GIT_ERROR
if (listrCtx.errors.has(GitError)) return GIT_ERROR
// Skip chunk when no every task is skipped (due to no matches)
if (chunkListrTasks.every((task) => task.skip())) return 'No tasks to run.'
return false
Expand All @@ -214,10 +217,7 @@ const runAll = async (
}

if (hasDeprecatedGitAdd) {
logger.warn(`${symbols.warning} ${chalk.yellow(
`Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.`
)}
`)
logger.warn(DEPRECATED_GIT_ADD)
}

// If all of the configured tasks should be skipped
Expand Down Expand Up @@ -277,20 +277,13 @@ const runAll = async (

if (context.errors.size > 0) {
if (context.errors.has(ApplyEmptyCommitError)) {
logger.warn(`
${symbols.warning} ${chalk.yellow(`lint-staged prevented an empty git commit.
Use the --allow-empty option to continue, or check your task configuration`)}
`)
logger.warn(PREVENTED_EMPTY_COMMIT)
} else if (context.errors.has(GitError) && !context.errors.has(GetBackupStashError)) {
logger.error(`\n ${symbols.error} ${chalk.red(`lint-staged failed due to a git error.`)}`)

if (shouldBackup) {
// No sense to show this if the backup stash itself is missing.
logger.error(` Any lost modifications can be restored from a git stash:
> git stash list
stash@{0}: automatic lint-staged backup
> git stash apply --index stash@{0}\n`)
logger.error(RESTORE_STASH_EXAMPLE)
}
}
const error = new Error('lint-staged failed')
Expand Down
2 changes: 1 addition & 1 deletion test/runAll.unmocked.spec.js
Expand Up @@ -803,7 +803,7 @@ describe('runAll', () => {
WARN
‼ lint-staged prevented an empty git commit.
Use the --allow-empty option to continue, or check your task configuration
"
"
`)

// Something was wrong so the repo is returned to original state
Expand Down

0 comments on commit 6da7667

Please sign in to comment.