Skip to content

Commit c90da54

Browse files
committed
refactor: create that one setup function i always talk about
1 parent 4b51f30 commit c90da54

File tree

1 file changed

+68
-43
lines changed

1 file changed

+68
-43
lines changed

lib/main.js

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
// TODO(@lishaduck): Create a `setup` function.
2-
process.title = 'elm-review';
3-
4-
if (!process.argv.includes('--no-color')) {
5-
if (process.env.NO_COLOR !== undefined && process.env.NO_COLOR !== '') {
6-
process.argv.push('--no-color');
7-
} else {
8-
// Force `chalk` to add colors by default.
9-
process.argv.push('--color');
10-
}
11-
}
12-
131
/**
142
* @import {Options, ReviewOptions} from './types/options';
153
*/
164
const path = require('node:path');
5+
const process = require('node:process');
176
const chalk = require('chalk');
187
const exit = require('../vendor/exit');
198
const Anonymize = require('./anonymize');
@@ -34,44 +23,66 @@ const SuppressedErrors = require('./suppressed-errors');
3423
const Watch = require('./watch');
3524

3625
/**
37-
* @type {Options}
26+
* @param {NodeJS.Process} process
27+
* @typedef {(err: Error) => never} ErrorHandler
3828
*/
39-
const options = Options_.compute(process.argv);
4029

41-
// TODO(@lishaduck): Move to aforementioned `setup` function.
42-
process.on('uncaughtException', errorHandler);
43-
process.on('unhandledRejection', errorHandler);
30+
function setup(process) {
31+
process.title = 'elm-review';
32+
33+
if (!process.argv.includes('--no-color')) {
34+
if (process.env.NO_COLOR !== undefined && process.env.NO_COLOR !== '') {
35+
process.argv.push('--no-color');
36+
} else {
37+
// Force `chalk` to add colors by default.
38+
process.argv.push('--color');
39+
}
40+
}
41+
42+
const options = Options_.compute(process.argv);
43+
44+
const errorHandler = errorHandlerFactory(options);
45+
46+
process.on('uncaughtException', errorHandler);
47+
process.on('unhandledRejection', errorHandler);
48+
49+
return {options, errorHandler};
50+
}
4451

4552
/**
4653
* Exit the process with status code 1 after printing an error message.
4754
*
48-
* @param {Error} err - An error object.
49-
* @returns {never}
55+
* @param {Options} options - An error object.
56+
* @returns {ErrorHandler}
5057
*/
51-
function errorHandler(err) {
52-
Spinner.fail(undefined, options.report);
53-
let userSrc = null;
54-
try {
55-
userSrc = options.userSrc();
56-
} catch {
57-
userSrc = process.cwd();
58-
}
58+
function errorHandlerFactory(options) {
59+
return (err) => {
60+
Spinner.fail(undefined, options.report);
61+
62+
let userSrc = null;
63+
try {
64+
userSrc = options.userSrc();
65+
} catch {
66+
userSrc = process.cwd();
67+
}
5968

60-
const reviewElmJsonPath = path.join(userSrc, 'elm.json');
69+
const reviewElmJsonPath = path.join(userSrc, 'elm.json');
6170

62-
const errorToReport =
63-
err instanceof ErrorMessage.CustomError
64-
? err
65-
: ErrorMessage.unexpectedError(err);
66-
console.log(ErrorMessage.report(options, errorToReport, reviewElmJsonPath));
71+
const errorToReport =
72+
err instanceof ErrorMessage.CustomError
73+
? err
74+
: ErrorMessage.unexpectedError(err);
75+
console.log(ErrorMessage.report(options, errorToReport, reviewElmJsonPath));
6776

68-
exit(1);
77+
exit(1);
78+
};
6979
}
7080

7181
/**
82+
* @param {Options} options
7283
* @returns {Promise<void>}
7384
*/
74-
async function runElmReview() {
85+
async function runElmReview(options) {
7586
const {elmModulePath, reviewElmJson, appHash} = await Builder.build(options);
7687

7788
if (!elmModulePath) {
@@ -102,9 +113,11 @@ async function runElmReview() {
102113
}
103114

104115
/**
116+
* @param {Options} options
117+
* @param {ErrorHandler} errorHandler
105118
* @returns {Promise<void | never>}
106119
*/
107-
async function runElmReviewInWatchMode() {
120+
async function runElmReviewInWatchMode(options, errorHandler) {
108121
AppWrapper.stop();
109122
const {elmModulePath, reviewElmJson, reviewElmJsonPath, appHash} =
110123
await Builder.build(options);
@@ -118,7 +131,7 @@ async function runElmReviewInWatchMode() {
118131
reviewElmJsonPath,
119132
() => {
120133
AppState.buildRestarted();
121-
void runElmReviewInWatchMode();
134+
void runElmReviewInWatchMode(options, errorHandler);
122135
}
123136
);
124137
return;
@@ -135,7 +148,7 @@ async function runElmReviewInWatchMode() {
135148
{...initialization, reviewElmJson, reviewElmJsonPath},
136149
() => {
137150
AppState.buildRestarted();
138-
void runElmReviewInWatchMode();
151+
void runElmReviewInWatchMode(options, errorHandler);
139152
},
140153
errorHandler
141154
);
@@ -148,9 +161,10 @@ async function runElmReviewInWatchMode() {
148161
}
149162

150163
/**
164+
* @param {Options} options
151165
* @returns {Promise<void>}
152166
*/
153-
async function prepareOffline() {
167+
async function prepareOffline(options) {
154168
const elmBinary = await ElmBinary.getElmBinary(options);
155169
ElmBinary.downloadDependenciesOfElmJson(
156170
elmBinary,
@@ -178,9 +192,20 @@ if either your review configuration or your project's dependencies change.`);
178192
}
179193

180194
/**
181-
* @returns {Promise<void | never>}
195+
* @returns {Promise<void>}
182196
*/
183197
async function main() {
198+
const {options, errorHandler} = setup(process);
199+
200+
await app(options, errorHandler);
201+
}
202+
203+
/**
204+
* @param {Options} options
205+
* @param {ErrorHandler} errorHandler
206+
* @returns {Promise<void>}
207+
*/
208+
async function app(options, errorHandler) {
184209
if (options.version) {
185210
console.log(Anonymize.version(options));
186211
return;
@@ -249,7 +274,7 @@ async function main() {
249274
return;
250275
}
251276

252-
await prepareOffline();
277+
await prepareOffline(options);
253278
return;
254279
}
255280

@@ -261,7 +286,7 @@ async function main() {
261286

262287
if (options.watch) {
263288
try {
264-
await runElmReviewInWatchMode();
289+
await runElmReviewInWatchMode(options, errorHandler);
265290
return;
266291
} catch (error) {
267292
errorHandler(error);
@@ -271,7 +296,7 @@ async function main() {
271296
}
272297

273298
try {
274-
await runElmReview();
299+
await runElmReview(options);
275300
} catch (error) {
276301
errorHandler(error);
277302
}

0 commit comments

Comments
 (0)