Skip to content

Commit bc0c45b

Browse files
committed
chore: cwd refactor
I do think it'd be good to audit usages of cwd at some point. A few of these feel like they don't actually need the cwd. This is good bandaid for now though.
1 parent e1cfd62 commit bc0c45b

File tree

13 files changed

+39
-37
lines changed

13 files changed

+39
-37
lines changed

lib/anonymize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function pathsAndVersions(options, string) {
3737
*/
3838
function anonymizePath(options, filePath) {
3939
if (options.forTests) {
40-
return replaceVersion(path.relative(process.cwd(), filePath));
40+
return replaceVersion(path.relative(options.cwd, filePath));
4141
}
4242

4343
return filePath;

lib/autofix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ function formatFileContent(options, file, filePath) {
196196
input: file.source,
197197
env: {
198198
...process.env,
199-
[pathKey]: backwardsCompatiblePath(options.elmFormatPath)
199+
[pathKey]: backwardsCompatiblePath(options.elmFormatPath, options.cwd)
200200
}
201201
}
202202
);

lib/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ function updateSourceDirectories(options, userSrc, elmJson) {
366366
if (options.localElmReviewSrc) {
367367
sourceDirectories = unique([
368368
...sourceDirectories,
369-
path.resolve(process.cwd(), options.localElmReviewSrc)
369+
path.resolve(options.cwd, options.localElmReviewSrc)
370370
]);
371371
}
372372

lib/elm-binary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {backwardsCompatiblePath} = require('./npx');
1919
async function getElmBinary(options) {
2020
try {
2121
const elmBinaryPath = await which(options.compiler ?? 'elm', {
22-
path: backwardsCompatiblePath(options.compiler)
22+
path: backwardsCompatiblePath(options.compiler, options.cwd)
2323
});
2424
return path.resolve(elmBinaryPath);
2525
} catch {

lib/elm-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function getSourceDirectories(options, elmJson) {
244244
isFromCliArguments: true,
245245
sourceDirectories: unique(
246246
options.directoriesToAnalyze.map((directory) =>
247-
path.resolve(process.cwd(), directory)
247+
path.resolve(options.cwd, directory)
248248
)
249249
)
250250
};

lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function setup(process) {
3939
}
4040
}
4141

42-
const options = Options_.compute(process.argv);
42+
const options = Options_.compute(process.argv, process.cwd());
4343

4444
const errorHandler = errorHandlerFactory(options);
4545

@@ -63,7 +63,7 @@ function errorHandlerFactory(options) {
6363
try {
6464
userSrc = options.userSrc();
6565
} catch {
66-
userSrc = process.cwd();
66+
userSrc = options.cwd;
6767
}
6868

6969
const reviewElmJsonPath = path.join(userSrc, 'elm.json');

lib/new-package.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ async function createProject(
203203
ruleType,
204204
license
205205
) {
206-
const dir = path.join(process.cwd(), packageName);
206+
const dir = path.join(options.cwd, packageName);
207207
const ruleNameFolder = ruleName.split('.').slice(0, -1).join('/');
208208

209209
try {
@@ -236,7 +236,7 @@ async function createProject(
236236
await writeFile(dir, 'README.md', readme(authorName, packageName, ruleName));
237237

238238
Spinner.succeedAndNowDo('Adding preview folder', options.report);
239-
const pathToPreview = path.join(process.cwd(), packageName, 'preview');
239+
const pathToPreview = path.join(options.cwd, packageName, 'preview');
240240
await Init.create(options, pathToPreview, 'ReviewConfigForExample.elm');
241241

242242
// Adding package to the example's elm.json
@@ -333,11 +333,11 @@ ElmjutsuDumMyM0DuL3.elm
333333
pathToFolder: 'new-package/review-config-templates/2.3.0',
334334
reference: null
335335
},
336-
path.join(process.cwd(), packageName, 'review')
336+
path.join(options.cwd, packageName, 'review')
337337
);
338338

339339
Spinner.succeedAndNowDo('Adding maintenance scripts', options.report);
340-
const maintenancePath = path.join(process.cwd(), packageName, 'maintenance');
340+
const maintenancePath = path.join(options.cwd, packageName, 'maintenance');
341341
await FS.mkdirp(maintenancePath);
342342
await FS.copyFiles(
343343
path.join(__dirname, '../new-package/maintenance/'),
@@ -346,7 +346,7 @@ ElmjutsuDumMyM0DuL3.elm
346346
);
347347

348348
const packageTests = path.join(
349-
process.cwd(),
349+
options.cwd,
350350
packageName,
351351
'elm-review-package-tests'
352352
);

lib/npx.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ const pathKey = getPathKey();
6262
* This can be removed in a major version.
6363
*
6464
* @param {Path | undefined} providedBinaryPath
65+
* @param {string} cwd
6566
* @returns {Path}
6667
*/
67-
function backwardsCompatiblePath(providedBinaryPath) {
68+
function backwardsCompatiblePath(providedBinaryPath, cwd) {
6869
return [
69-
...process
70-
.cwd()
70+
...cwd
7171
.split(path.sep)
7272
.map((_, index, parts) =>
7373
[...parts.slice(0, index + 1), 'node_modules', '.bin'].join(path.sep)

lib/options.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ let containsHelp = false;
4242
/**
4343
* Compute the options for this run.
4444
*
45-
* @param {string[]} processArgv
45+
* @param {string[]} processArgv - An argument array (typically `process.argv`)
46+
* @param {Path} cwd - The current working directory (typically `process.cwd()`)
4647
* @returns {Options | never}
4748
*/
48-
function compute(processArgv) {
49+
function compute(processArgv, cwd) {
4950
containsHelp =
5051
processArgv.slice(2).includes('--help') ||
5152
processArgv.slice(2).includes('-h');
@@ -79,7 +80,7 @@ function compute(processArgv) {
7980
}
8081

8182
/** @type {Path | null} */
82-
const elmJsonPath = findElmJsonPath(args, subcommand);
83+
const elmJsonPath = findElmJsonPath(args, subcommand, cwd);
8384
/** @type {Path | null} */
8485
const readmePath =
8586
elmJsonPath && path.join(path.dirname(elmJsonPath), 'README.md');
@@ -89,13 +90,13 @@ function compute(processArgv) {
8990
*/
9091
function initPath() {
9192
if (args.config) {
92-
return path.resolve(process.cwd(), args.config);
93+
return path.resolve(cwd, args.config);
9394
}
9495

9596
try {
9697
return path.join(projectToReview(), 'review');
9798
} catch {
98-
return path.join(process.cwd(), 'review');
99+
return path.join(cwd, 'review');
99100
}
100101
}
101102

@@ -111,7 +112,7 @@ function compute(processArgv) {
111112
112113
If you wish to run elm-review from outside your project,
113114
try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
114-
path.relative(process.cwd(), 'elm.json')
115+
path.relative(cwd, 'elm.json')
115116
);
116117
}
117118

@@ -163,7 +164,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
163164
*/
164165
function userSrc() {
165166
return args.config
166-
? path.resolve(process.cwd(), args.config)
167+
? path.resolve(cwd, args.config)
167168
: path.join(projectToReview(), 'review');
168169
}
169170

@@ -258,7 +259,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
258259
: null,
259260

260261
// PATHS - REVIEW APPLICATION
261-
262+
cwd,
262263
userSrc,
263264
usedConfig: Boolean(args.config),
264265
template,
@@ -271,7 +272,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
271272
path.join(elmStuffFolder(), 'review-applications', `${appHash}.js`),
272273
elmParserPath: (elmSyntaxVersion) =>
273274
path.resolve(
274-
process.cwd(),
275+
cwd,
275276
elmStuffFolder(),
276277
'elm-parser',
277278
`elm-syntax-v${elmSyntaxVersion}${args.debug ? '-debug' : ''}.js`
@@ -391,9 +392,10 @@ ${Flags.buildFlag(subcommand, Flags.templateFlag)}`
391392
/**
392393
* @param {ParsedArgs} args
393394
* @param {Subcommand | null} subcommand
395+
* @param {Path} cwd
394396
* @returns {Path | null}
395397
*/
396-
function findElmJsonPath(args, subcommand) {
398+
function findElmJsonPath(args, subcommand, cwd) {
397399
// eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Casting is ugly.
398400
if (args.elmjson) return args.elmjson;
399401
// Shortcutting the search for elm.json when `--help` since we won't need it
@@ -409,7 +411,7 @@ function findElmJsonPath(args, subcommand) {
409411
return null;
410412
}
411413

412-
return findUp.sync('elm.json') ?? null;
414+
return findUp.sync('elm.json', {cwd}) ?? null;
413415
}
414416

415417
/**

lib/run-review.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function runReview(options, app) {
3636
const orange = chalk.keyword('orange');
3737
console.log(
3838
`I created suppressions files in ${orange(
39-
path.relative(process.cwd(), options.suppressedErrorsFolder())
39+
path.relative(options.cwd, options.suppressedErrorsFolder())
4040
)}`
4141
);
4242
}

0 commit comments

Comments
 (0)