Skip to content
Myr

cwd.js

This file

Path

lib/arguments/cwd.js

43 lines · 1.2 KiB · JavaScript

Last changed by

TweaksSindre Sorhusbefore adoption698a1b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 1why, one step back
1import {statSync} from 'node:fs';

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 2why, one step back
2import path from 'node:path';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–8why, one step back
3import process from 'node:process';
4import {safeNormalizeFileUrl} from './file-url.js';56// Normalize `cwd` option7export const normalizeCwd = (cwd = getDefaultCwd()) => {8 const cwdString = safeNormalizeFileUrl(cwd, 'The "cwd" option');

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 9why, one step back
9 return path.resolve(cwdString);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 10–20why, one step back
10};
1112const getDefaultCwd = () => {13 try {14 return process.cwd();15 } catch (error) {16 error.message = `The current directory does not exist.\n${error.message}`;17 throw error;18 }19};20

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

lines 21–28why, one step back
21// `process.cwd()` throws if the current directory has been deleted.
22// This is only used to improve an error message, so it must not throw itself, which would hide that error.23const getCurrentCwd = () => {24 try {25 return process.cwd();26 } catch {}27};28

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 29–30why, one step back
29// When `cwd` option has an invalid value, provide with a better error message
30export const fixCwdError = (originalMessage, cwd) => {

Fix the subprocess error being hidden when the current directory was deletedSindre Sorhusbefore adoption91d9ded

line 31why, one step back
31 if (cwd === getCurrentCwd()) {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–41why, one step back
32 return originalMessage;
33 }3435 let cwdStat;36 try {37 cwdStat = statSync(cwd);38 } catch (error) {39 return `The "cwd" option is invalid: ${cwd}.\n${error.message}\n${originalMessage}`;40 }41

TweaksSindre Sorhusbefore adoption698a1b8

line 42why, one step back
42 return cwdStat.isDirectory() ? originalMessage : `The "cwd" option is not a directory: ${cwd}.\n${originalMessage}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 43why, one step back
43};