Skip to content

Commit 653ce91

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 653ce91

File tree

10 files changed

+29
-27
lines changed

10 files changed

+29
-27
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/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-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/options.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let containsHelp = false;
4545
* @param {string[]} processArgv
4646
* @returns {Options | never}
4747
*/
48-
function compute(processArgv) {
48+
function compute(processArgv, pcwd = () => process.cwd()) {
4949
containsHelp =
5050
processArgv.slice(2).includes('--help') ||
5151
processArgv.slice(2).includes('-h');
@@ -84,18 +84,20 @@ function compute(processArgv) {
8484
const readmePath =
8585
elmJsonPath && path.join(path.dirname(elmJsonPath), 'README.md');
8686

87+
const cwd = pcwd();
88+
8789
/**
8890
* @returns {Path}
8991
*/
9092
function initPath() {
9193
if (args.config) {
92-
return path.resolve(process.cwd(), args.config);
94+
return path.resolve(cwd, args.config);
9395
}
9496

9597
try {
9698
return path.join(projectToReview(), 'review');
9799
} catch {
98-
return path.join(process.cwd(), 'review');
100+
return path.join(cwd, 'review');
99101
}
100102
}
101103

@@ -111,7 +113,7 @@ function compute(processArgv) {
111113
112114
If you wish to run elm-review from outside your project,
113115
try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
114-
path.relative(process.cwd(), 'elm.json')
116+
path.relative(cwd, 'elm.json')
115117
);
116118
}
117119

@@ -163,7 +165,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
163165
*/
164166
function userSrc() {
165167
return args.config
166-
? path.resolve(process.cwd(), args.config)
168+
? path.resolve(cwd, args.config)
167169
: path.join(projectToReview(), 'review');
168170
}
169171

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

260262
// PATHS - REVIEW APPLICATION
261-
263+
cwd,
262264
userSrc,
263265
usedConfig: Boolean(args.config),
264266
template,
@@ -271,7 +273,7 @@ try re-running it with ${chalk.cyan('--elmjson <path-to-elm.json>')}.`,
271273
path.join(elmStuffFolder(), 'review-applications', `${appHash}.js`),
272274
elmParserPath: (elmSyntaxVersion) =>
273275
path.resolve(
274-
process.cwd(),
276+
cwd,
275277
elmStuffFolder(),
276278
'elm-parser',
277279
`elm-syntax-v${elmSyntaxVersion}${args.debug ? '-debug' : ''}.js`

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
}

lib/suppressed-errors.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ async function write(options, suppressionFiles) {
5252
const error = intoError(err);
5353

5454
const relativeFolderPath =
55-
path.relative(process.cwd(), options.suppressedErrorsFolder()) +
56-
'/';
57-
const relativeFilePath = path.relative(process.cwd(), filePath);
55+
path.relative(options.cwd, options.suppressedErrorsFolder()) + '/';
56+
const relativeFilePath = path.relative(options.cwd, filePath);
5857
throw new ErrorMessage.CustomError(
5958
'FAILED TO UPDATE SUPPRESSION FILE',
6059
// prettier-ignore
@@ -181,7 +180,7 @@ Try updating ${chalk.greenBright('elm-review')} to a version that supports this
181180
*/
182181
function checkForUncommittedSuppressions(options) {
183182
const pathToSuppressedFolder = path.relative(
184-
process.cwd(),
183+
options.cwd,
185184
options.suppressedErrorsFolder()
186185
);
187186

lib/types/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type Options = OptionsBase & {
4343
newRuleName: string | null;
4444
ruleType: RuleType | undefined;
4545

46+
cwd: Path;
4647
userSrc: () => Path;
4748
usedConfig: boolean;
4849
template: Template | null;

lib/watch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function createFileWatcher(
297297
)
298298
.on('add', async (absolutePath) => {
299299
const relativePath = OsHelpers.makePathOsAgnostic(
300-
path.relative(process.cwd(), absolutePath)
300+
path.relative(options.cwd, absolutePath)
301301
);
302302

303303
Debug.log(
@@ -334,7 +334,7 @@ function createFileWatcher(
334334
})
335335
.on('change', async (absolutePath) => {
336336
const relativePath = OsHelpers.makePathOsAgnostic(
337-
path.relative(process.cwd(), absolutePath)
337+
path.relative(options.cwd, absolutePath)
338338
);
339339

340340
const elmFile = AppState.getFileFromMemoryCache(relativePath) ?? {
@@ -364,7 +364,7 @@ function createFileWatcher(
364364
})
365365
.on('unlink', (absolutePath) => {
366366
const relativePath = OsHelpers.makePathOsAgnostic(
367-
path.relative(process.cwd(), absolutePath)
367+
path.relative(options.cwd, absolutePath)
368368
);
369369
Debug.log(
370370
`File ${Anonymize.path(options, relativePath)} has been removed`,
@@ -401,7 +401,7 @@ function createExtraFilesWatcher(options, app, runReview, onError, request) {
401401
)
402402
.on('add', async (absolutePath) => {
403403
const relativePath = OsHelpers.makePathOsAgnostic(
404-
path.relative(process.cwd(), absolutePath)
404+
path.relative(options.cwd, absolutePath)
405405
);
406406

407407
const newSource = await FS.readFile(relativePath);
@@ -415,7 +415,7 @@ function createExtraFilesWatcher(options, app, runReview, onError, request) {
415415
})
416416
.on('change', async (absolutePath) => {
417417
const relativePath = OsHelpers.makePathOsAgnostic(
418-
path.relative(process.cwd(), absolutePath)
418+
path.relative(options.cwd, absolutePath)
419419
);
420420

421421
const newSource = await FS.readFile(relativePath);
@@ -429,7 +429,7 @@ function createExtraFilesWatcher(options, app, runReview, onError, request) {
429429
})
430430
.on('unlink', (absolutePath) => {
431431
const relativePath = OsHelpers.makePathOsAgnostic(
432-
path.relative(process.cwd(), absolutePath)
432+
path.relative(options.cwd, absolutePath)
433433
);
434434
Debug.log(
435435
`Extra file ${Anonymize.path(options, relativePath)} has been removed`,

0 commit comments

Comments
 (0)