From 72e4654ad5f4a46f4819f54ad0295862e0dc28c0 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Fri, 3 Jul 2020 22:56:31 +0200 Subject: [PATCH] Fix watch mode+fix not re-running after a change --- lib/autofix.js | 9 +++++--- lib/run-review.js | 42 ++++++++++++++++++++++++++++++++++ lib/runner.js | 35 +--------------------------- lib/state.js | 58 ++++++++++++++++++++++++++--------------------- 4 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 lib/run-review.js diff --git a/lib/autofix.js b/lib/autofix.js index c892ea336..9e81dfb12 100644 --- a/lib/autofix.js +++ b/lib/autofix.js @@ -6,6 +6,7 @@ const appState = require('./state'); const errorMessage = require('./error-message'); const promisifyPort = require('./promisify-port'); const styledMessage = require('./styled-message'); +const {startReview} = require('./run-review'); module.exports = { subscribe: (options, app) => { @@ -22,8 +23,12 @@ function askConfirmationToFixWithOptions(options, app) { styledMessage.log(options, data.confirmationMessage); } + const shouldReReview = appState.fixProposalReceived(data.changedFiles); + if (shouldReReview) { + return startReview(options, app); + } + if (appState.filesProposedByCurrentFix().length > 0) { - appState.fixProposalReceived(data.changedFiles); console.log( `${chalk.cyan('?')} ${chalk.bold( 'Do you wish to apply this fix?' @@ -32,8 +37,6 @@ function askConfirmationToFixWithOptions(options, app) { return; } - appState.fixProposalReceived(data.changedFiles); - const accepted = options.fixAllWithoutPrompt || ( diff --git a/lib/run-review.js b/lib/run-review.js new file mode 100644 index 000000000..45d472e7c --- /dev/null +++ b/lib/run-review.js @@ -0,0 +1,42 @@ +const report = require('./report'); +const appState = require('./state'); +const promisifyPort = require('./promisify-port'); + +module.exports = { + runReview, + startReview, + requestReview +}; + +async function runReview(options, app) { + if (options.watch) { + startReview(options, app); + return undefined; + } + + const result = await promisifyPort({ + subscribeTo: app.ports.reviewReport, + sendThrough: app.ports.startReview, + data: null + }); + + report(options, result); + + return result.success; +} + +let isVeryFirstRun = true; +function startReview(options, app) { + if (options.report !== 'json' && !isVeryFirstRun) { + console.log('Running...'); + } + + isVeryFirstRun = false; + return app.ports.startReview.send(null); +} + +function requestReview(options, app) { + if (appState.requestReview()) { + startReview(options, app); + } +} diff --git a/lib/runner.js b/lib/runner.js index 7291f695d..09b5fc994 100755 --- a/lib/runner.js +++ b/lib/runner.js @@ -7,9 +7,9 @@ const autofix = require('./autofix'); const appWrapper = require('./app-wrapper'); const errorMessage = require('./error-message'); const {getProjectFiles} = require('./elm-files'); -const promisifyPort = require('./promisify-port'); const projectDependencies = require('./project-dependencies'); const {getElmBinary, getElmVersion} = require('./elm-binary'); +const {runReview, startReview, requestReview} = require('./run-review'); function sendProjectContent( options, @@ -57,39 +57,6 @@ function sendProjectContent( }); } -function requestReview(options, app) { - if (appState.requestReview()) { - startReview(options, app); - } -} - -let isVeryFirstRun = true; -function startReview(options, app) { - if (options.report !== 'json' && !isVeryFirstRun) { - console.log('Running...'); - } - - isVeryFirstRun = false; - return app.ports.startReview.send(null); -} - -async function runReview(options, app) { - if (options.watch) { - startReview(options, app); - return undefined; - } - - const result = await promisifyPort({ - subscribeTo: app.ports.reviewReport, - sendThrough: app.ports.startReview, - data: null - }); - - report(options, result); - - return result.success; -} - async function initializeApp(options, elmModulePath, reviewElmJson) { const app = appWrapper.init(options, elmModulePath, { fixMode: fixMode(options), diff --git a/lib/state.js b/lib/state.js index 9a3c54d19..c684d9be9 100644 --- a/lib/state.js +++ b/lib/state.js @@ -48,7 +48,6 @@ const model = { const type = Symbol('type'); -// eslint-disable-next-line complexity function update(message) { switch (message[type]) { case 'initializedApp': { @@ -138,35 +137,13 @@ function update(message) { } case 'reviewFinished': { - switch (model.reviewState.type) { - case 'waiting-for-first-report': { - model.reviewState = {type: 'idle'}; - break; - } - - case 'ongoing': { - if (model.reviewState.shouldRunAfterNewReview) { - model.reviewState = { - type: 'ongoing', - shouldRunAfterNewReview: false - }; - } else { - model.reviewState = {type: 'idle'}; - } - - break; - } - - default: { - break; - } - } - + markReviewAsComplete(); return model; } case 'fixProposalReceived': { model.filesProposedByCurrentFix = message.changedFiles; + markReviewAsComplete(); return model; } @@ -187,6 +164,32 @@ function update(message) { } } +function markReviewAsComplete() { + switch (model.reviewState.type) { + case 'waiting-for-first-report': { + model.reviewState = {type: 'idle'}; + break; + } + + case 'ongoing': { + if (model.reviewState.shouldRunAfterNewReview) { + model.reviewState = { + type: 'ongoing', + shouldRunAfterNewReview: false + }; + } else { + model.reviewState = {type: 'idle'}; + } + + break; + } + + default: { + break; + } + } +} + function updateFilesInCache(files) { files.forEach((file) => { if (file.path.endsWith('.elm')) { @@ -316,10 +319,13 @@ function writingToFileSystemCacheFinished(hash) { } function fixProposalReceived(changedFiles) { - return update({ + update({ [type]: 'fixProposalReceived', changedFiles }); + + const shouldReReview = model.reviewState.type === 'ongoing'; + return shouldReReview; } function fixWasAccepted(files) {