Skip to content

Commit

Permalink
Fix watch mode+fix not re-running after a change
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jul 3, 2020
1 parent bc7a172 commit 72e4654
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 63 deletions.
9 changes: 6 additions & 3 deletions lib/autofix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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?'
Expand All @@ -32,8 +37,6 @@ function askConfirmationToFixWithOptions(options, app) {
return;
}

appState.fixProposalReceived(data.changedFiles);

const accepted =
options.fixAllWithoutPrompt ||
(
Expand Down
42 changes: 42 additions & 0 deletions lib/run-review.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 1 addition & 34 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
58 changes: 32 additions & 26 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const model = {

const type = Symbol('type');

// eslint-disable-next-line complexity
function update(message) {
switch (message[type]) {
case 'initializedApp': {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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')) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 72e4654

Please sign in to comment.