Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/autofix.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const path = require('node:path');
const chalk = require('chalk');
const spawn = require('cross-spawn');
const prompts = require('prompts');
const exit = require('../vendor/exit');
const ErrorMessage = require('./error-message');
const {ExitSignal} = require('./exit-signal');
const FS = require('./fs-wrapper');
const {backwardsCompatiblePath, pathKey} = require('./npx');
const ProjectDependencies = require('./project-dependencies');
Expand Down Expand Up @@ -81,7 +81,7 @@ function askConfirmationToFixWithOptions(options, app, elmVersion) {
if (accepted === undefined) {
// User interrupted the process using Ctrl-C

exit(1);
throw new ExitSignal(1);
}

/** @type {FilesProposedByCurrentFix} */
Expand Down
9 changes: 6 additions & 3 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const chalk = require('chalk');
const {hashElement} = require('folder-hash');
const fs = require('graceful-fs');
const wrap = require('wrap-ansi');
const exit = require('../vendor/exit');
const elmCompiler = require('../vendor/node-elm-compiler');
const Anonymize = require('./anonymize');
const Benchmark = require('./benchmark');
Expand All @@ -27,6 +26,7 @@ const RemoteTemplate = require('./remote-template');
const Spinner = require('./spinner');
const TemplateDependencies = require('./template-dependencies');
const {unique} = require('./utils');
const {ExitSignal} = require('./exit-signal');

const templateSrc = path.join(__dirname, '../template/src');
const parseElmFolder = path.join(__dirname, '../parseElm');
Expand Down Expand Up @@ -119,6 +119,9 @@ Please remove one of them and try re-running.`
async function buildLocalProject(options) {
const userSrc = options.userSrc();
const reviewElmJsonPath = path.join(userSrc, 'elm.json');
console.warn(
`Looking for review configuration in ${chalk.cyan(reviewElmJsonPath)}`
);

const reviewElmJson = /** @type {ApplicationElmJson} */ (
await FS.readJsonFile(reviewElmJsonPath).catch((error) => {
Expand Down Expand Up @@ -462,7 +465,7 @@ async function compileElmProject(
return;
}

exit(1);
throw new ExitSignal(1);
}
);
}
Expand Down Expand Up @@ -502,7 +505,7 @@ function compilationError(options, stderr) {

// TODO(@jfmengels): Handle this better.

exit(1);
throw new ExitSignal(1);
}

return {
Expand Down
3 changes: 3 additions & 0 deletions lib/dependency-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const FS = require('./fs-wrapper');
const ProjectJsonFiles = require('./project-json-files');
const SyncGet = require('./sync-get');
const {intoError} = require('./utils');
const {rethrowIfExitSignal} = require('./exit-signal');

/** @type {boolean} */
let wasmWasInitialized = false;
Expand Down Expand Up @@ -52,6 +53,7 @@ class DependencyProvider {
(pkg) => lister.list(elmVersion, pkg, indirectDeps?.[pkg])
);
} catch (error) {
rethrowIfExitSignal(error);
throw intoError(error);
}
}
Expand Down Expand Up @@ -81,6 +83,7 @@ class DependencyProvider {
(pkg) => lister.list(elmVersion, pkg, indirectDeps?.[pkg])
);
} catch (error) {
rethrowIfExitSignal(error);
throw intoError(error);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/elm-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ ${sourceDirectories.map((directory) => `- ${directory}`).join('\n')}`
)
)
);
Debug.log('terminating workers', options.debug);
elmParser.terminateWorkers();
Debug.log('Workers terminated', options.debug);
Benchmark.end(options, 'parse/fetch parsed files');
Benchmark.end(options, 'get project files');

Expand Down
34 changes: 34 additions & 0 deletions lib/exit-signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class ExitSignal extends Error {
/**
* @param {number} exitCode
*/
constructor(exitCode) {
super(`Exit requested with code ${exitCode}`);
this.name = 'ExitSignal';
this.exitCode = exitCode;
}
}

/**
* @param {unknown} error
* @returns {error is ExitSignal}
*/
function isExitSignal(error) {
return error instanceof ExitSignal;
}

/**
* @param {unknown} error
* @returns {void}
*/
function rethrowIfExitSignal(error) {
if (isExitSignal(error)) {
throw error;
}
}

module.exports = {
ExitSignal,
isExitSignal,
rethrowIfExitSignal
};
26 changes: 22 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
const path = require('node:path');
const process = require('node:process');
const chalk = require('chalk');
const exit = require('../vendor/exit');
const Anonymize = require('./anonymize');
const AppWrapper = require('./app-wrapper');
const Builder = require('./build');
Expand All @@ -23,6 +22,11 @@ const Spinner = require('./spinner');
const AppState = require('./state');
const SuppressedErrors = require('./suppressed-errors');
const Watch = require('./watch');
const {
ExitSignal,
isExitSignal,
rethrowIfExitSignal
} = require('./exit-signal');

/**
* @param {NodeJS.Process} process
Expand Down Expand Up @@ -64,6 +68,7 @@ function setup(process) {
*/
function errorHandlerFactory(options) {
return (err) => {
rethrowIfExitSignal(err);
Spinner.fail(undefined, options.report);

let userSrc = null;
Expand All @@ -81,7 +86,7 @@ function errorHandlerFactory(options) {
: ErrorMessage.unexpectedError(err);
console.log(ErrorMessage.report(options, errorToReport, reviewElmJsonPath));

exit(1);
throw new ExitSignal(1);
};
}

Expand Down Expand Up @@ -202,7 +207,7 @@ You will need to run ${chalk.yellow(
'elm-review prepare-offline'
)} to keep the offline mode working
if either your review configuration or your project's dependencies change.`);
exit(0);
throw new ExitSignal(0);
}

/**
Expand All @@ -211,7 +216,15 @@ if either your review configuration or your project's dependencies change.`);
async function main() {
const {options, errorHandler} = setup(process);

await app(options, errorHandler);
try {
await app(options, errorHandler);
} catch (error) {
if (isExitSignal(error)) {
process.exitCode = error.exitCode;
} else {
errorHandler(error);
}
}
}

/**
Expand All @@ -236,6 +249,7 @@ async function app(options, errorHandler) {
await Init.promptAndCreate(options);
return;
} catch (error) {
rethrowIfExitSignal(error);
errorHandler(error);
}
}
Expand All @@ -250,6 +264,7 @@ async function app(options, errorHandler) {
await NewRule.create(/** @type {ReviewOptions} */ (options));
return;
} catch (error) {
rethrowIfExitSignal(error);
errorHandler(error);
}
}
Expand All @@ -264,6 +279,7 @@ async function app(options, errorHandler) {
await NewPackage.create(/** @type {ReviewOptions} */ options);
return;
} catch (error) {
rethrowIfExitSignal(error);
errorHandler(error);
}
}
Expand Down Expand Up @@ -304,6 +320,7 @@ async function app(options, errorHandler) {
await runElmReviewInWatchMode(options, errorHandler);
return;
} catch (error) {
rethrowIfExitSignal(error);
errorHandler(error);
}
}
Expand All @@ -313,6 +330,7 @@ async function app(options, errorHandler) {
try {
await runElmReview(options);
} catch (error) {
rethrowIfExitSignal(error);
errorHandler(error);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/new-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Init = require('./init');
const MinVersion = require('./min-version');
const NewRule = require('./new-rule');
const Spinner = require('./spinner');
const {rethrowIfExitSignal} = require('./exit-signal');

/**
* @param {Options} options
Expand Down Expand Up @@ -317,6 +318,7 @@ ElmjutsuDumMyM0DuL3.elm
}
);
} catch (error) {
rethrowIfExitSignal(error);
console.log(chalk.red('FAILED adding a license'));
if (options.debug) {
console.log(error);
Expand Down
4 changes: 2 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const findUp = require('find-up');
const minimist = require('minimist');
const wrap = require('wrap-ansi');
const packageJson = require('../package.json');
const exit = require('../vendor/exit');
const ErrorMessage = require('./error-message');
const Flags = require('./flags');
const {ExitSignal} = require('./exit-signal');

/**
* @type {Subcommand[]}
Expand Down Expand Up @@ -686,7 +686,7 @@ function reportErrorAndExit(errorToReport) {
// @ts-expect-error(TS2345): Handle this later
console.log(ErrorMessage.report({}, errorToReport));

exit(1);
throw new ExitSignal(1);
}

module.exports = {
Expand Down
16 changes: 13 additions & 3 deletions lib/project-json-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const os = require('node:os');
const path = require('node:path');
const got = require('got').default;
const FS = require('./fs-wrapper');
const {rethrowIfExitSignal} = require('./exit-signal');

const elmRoot =
process.env.ELM_HOME ??
Expand All @@ -29,7 +30,8 @@ async function getElmJson(options, elmVersion, name, packageVersion) {
try {
// Look for the dependency in ELM_HOME first
return await getElmJsonFromElmHome(elmVersion, name, packageVersion);
} catch {
} catch (error) {
rethrowIfExitSignal(error);
// Then in the dependency cache for elm-review
const cacheLocation = elmReviewDependencyCache(
options,
Expand All @@ -43,6 +45,7 @@ async function getElmJson(options, elmVersion, name, packageVersion) {
await FS.readJsonFile(cacheLocation)
);
} catch (error) {
rethrowIfExitSignal(error);
// Finally, try to download it from the packages website
if (options.offline) {
// Unless we're in offline mode
Expand Down Expand Up @@ -133,7 +136,8 @@ async function getDocsJson(options, elmVersion, name, packageVersion) {
'docs.json'
)
);
} catch {
} catch (error) {
rethrowIfExitSignal(error);
const cacheLocation = elmReviewDependencyCache(
options,
elmVersion,
Expand All @@ -144,6 +148,7 @@ async function getDocsJson(options, elmVersion, name, packageVersion) {
try {
return await FS.readJsonFile(cacheLocation);
} catch (error) {
rethrowIfExitSignal(error);
// Finally, try to download it from the packages website
if (options.offline) {
// Unless we're in offline mode
Expand Down Expand Up @@ -183,7 +188,12 @@ async function readFromPackagesWebsite(
const json = /** @type {PackageElmJson} */ (
/** @type {unknown} */ (JSON.parse(response.body))
);
cachePackage(cacheLocation, json).catch(() => {});
try {
await cachePackage(cacheLocation, json);
} catch (error) {
rethrowIfExitSignal(error);
}

return json;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/promisify-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = promisifyPort;
*
* @template DataIn,DataOut
* @param {PortsToPromise<DataIn, DataOut>} obj
* @returns {PromiseLike<DataOut>}
* @returns {Promise<DataOut>}
*/
function promisifyPort({subscribeTo, sendThrough, data}) {
return new Promise((resolve) => {
async function promisifyPort({subscribeTo, sendThrough, data}) {
return await new Promise((resolve) => {
/**
* @param {DataOut} result
* @returns {void}
Expand Down
3 changes: 3 additions & 0 deletions lib/remote-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ErrorMessage = require('./error-message');
const FS = require('./fs-wrapper');
const MinVersion = require('./min-version');
const TemplateDependencies = require('./template-dependencies');
const {rethrowIfExitSignal} = require('./exit-signal');

// GET LATEST INFORMATION ABOUT REPOSITORY

Expand Down Expand Up @@ -185,6 +186,7 @@ function parseElmJson(body, repoName) {

return /** @type {ElmJson} */ (json);
} catch (error) {
rethrowIfExitSignal(error);
throw new ErrorMessage.CustomError(
// prettier-ignore
'TEMPLATE ELM.JSON PARSING ERROR',
Expand Down Expand Up @@ -385,6 +387,7 @@ async function makeGitHubApiRequest(options, url, handleNotFound) {

return body;
} catch (error) {
rethrowIfExitSignal(error);
Debug.log(
`An error occurred when making a request to the GitHub API:`,
options.debug
Expand Down
Loading
Loading