Skip to content

Commit

Permalink
feat: automatically stage task modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 14, 2019
1 parent 7b144b4 commit 74ed28d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .lintstagedrc.json
@@ -1,5 +1,5 @@
{
"*.{js,json,md}": ["prettier --write", "git add"],
"*.js": ["npm run lint:base --fix", "git add"],
".*{rc, json}": ["jsonlint --in-place", "git add"]
"*.{js,json,md}": ["prettier --write"],
"*.js": ["npm run lint:base --fix"],
".*{rc, json}": ["jsonlint --in-place"]
}
17 changes: 13 additions & 4 deletions lib/gitWorkflow.js
Expand Up @@ -117,16 +117,25 @@ class GitWorkflow {
}

/**
* Applies back unstaged changes that have been hidden in the stash.
* Applies back task modifications, and unstaged changes hidden in the stash.
* In case of a merge-conflict retry with 3-way merge.
*
* @param {Object} [options]
* @returns {Promise<void>}
*/
async restoreUnstagedChanges() {
debug('Restoring unstaged changes...')
async applyModifications() {
let modifiedFiles = await this.execGit(['ls-files', '--modified'])
if (modifiedFiles) {
modifiedFiles = modifiedFiles.split('\n')
debug('Detected files modified by tasks:')
debug(modifiedFiles)
debug('Adding files to index...')
await this.execGit(['add', modifiedFiles])
debug('Done adding files to index!')
}

if (this.unstagedDiff) {
debug('Restoring unstaged changes...')
try {
await this.execGit(gitApplyArgs, { input: `${this.unstagedDiff}\n` })
} catch (error) {
Expand All @@ -143,8 +152,8 @@ class GitWorkflow {
throw new Error('Unstaged changes could not be restored due to a merge conflict!')
}
}
debug('Done restoring unstaged changes!')
}
debug('Done restoring unstaged changes!')

if (this.untrackedFiles) {
debug('Restoring untracked files...')
Expand Down
4 changes: 2 additions & 2 deletions lib/runAll.js
Expand Up @@ -115,9 +115,9 @@ module.exports = async function runAll(
task: () => new Listr(tasks, { ...listrOptions, concurrent: true, exitOnError: false })
},
{
title: 'Restoring unstaged changes...',
title: 'Applying modifications...',
skip: ctx => ctx.hasErrors && 'Skipped because of errors from tasks',
task: () => git.restoreUnstagedChanges()
task: () => git.applyModifications()
},
{
title: 'Reverting to original state...',
Expand Down
2 changes: 1 addition & 1 deletion test/__mocks__/gitWorkflow.js
@@ -1,6 +1,6 @@
const stub = {
stashBackup: jest.fn().mockImplementation(() => Promise.resolve()),
restoreUnstagedChanges: jest.fn().mockImplementation(() => Promise.resolve()),
applyModifications: jest.fn().mockImplementation(() => Promise.resolve()),
restoreOriginalState: jest.fn().mockImplementation(() => Promise.resolve()),
dropBackup: jest.fn().mockImplementation(() => Promise.resolve())
}
Expand Down
16 changes: 8 additions & 8 deletions test/__snapshots__/runAll.spec.js.snap
Expand Up @@ -10,8 +10,8 @@ LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running tasks... [completed]
LOG Restoring unstaged changes... [started]
LOG Restoring unstaged changes... [completed]
LOG Applying modifications... [started]
LOG Applying modifications... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [completed]"
`;
Expand All @@ -33,8 +33,8 @@ LOG β†’
LOG Running tasks for *.js [failed]
LOG β†’
LOG Running tasks... [failed]
LOG Restoring unstaged changes... [started]
LOG Restoring unstaged changes... [skipped]
LOG Applying modifications... [started]
LOG Applying modifications... [skipped]
LOG β†’ Skipped because of errors from tasks
LOG Reverting to original state... [started]
LOG Reverting to original state... [completed]
Expand Down Expand Up @@ -64,8 +64,8 @@ LOG β†’
LOG Running tasks for *.js [failed]
LOG β†’
LOG Running tasks... [failed]
LOG Restoring unstaged changes... [started]
LOG Restoring unstaged changes... [skipped]
LOG Applying modifications... [started]
LOG Applying modifications... [skipped]
LOG β†’ Skipped because of errors from tasks
LOG Reverting to original state... [started]
LOG Reverting to original state... [completed]
Expand Down Expand Up @@ -103,8 +103,8 @@ LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running tasks... [completed]
LOG Restoring unstaged changes... [started]
LOG Restoring unstaged changes... [completed]
LOG Applying modifications... [started]
LOG Applying modifications... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [completed]"
`;
2 changes: 1 addition & 1 deletion test/runAll.unmocked.spec.js
Expand Up @@ -26,7 +26,7 @@ const testJsFileUnfixable = `const obj = {
'foo': 'bar'
`

const fixJsConfig = { config: { '*.js': ['prettier --write', 'git add'] } }
const fixJsConfig = { config: { '*.js': 'prettier --write' } }

const isAppveyor = !!process.env.APPVEYOR
const osTmpDir = isAppveyor ? 'C:\\projects' : fs.realpathSync(os.tmpdir())
Expand Down

0 comments on commit 74ed28d

Please sign in to comment.