Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
lib: refactor platform utility methods
  • Loading branch information
danielbayley committed Jun 23, 2024
commit 7169bd45da94efab1a9a65a1e33c80169dfc4a98
7 changes: 3 additions & 4 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const { join, resolve, basename, isAbsolute } = require('path');

const {
kEmptyObject,
isWindows,
isMacOS,
} = require('internal/util');
const {
validateFunction,
Expand All @@ -35,9 +37,6 @@ function lazyMicromatch() {
return micromatch;
}

const isWindows = process.platform === 'win32';
const isOSX = process.platform === 'darwin';

async function getDirent(path) {
return new DirentFromStats(basename(path), await lstat(path), path);
}
Expand Down Expand Up @@ -203,7 +202,7 @@ class Glob {
patterns = [pattern];
}
this.matchers = ArrayPrototypeMap(patterns, (pattern) => lazyMicromatch()(pattern, {
nocase: isWindows || isOSX,
nocase: isWindows || isMacOS,
nonegate: true,
}));

Expand Down
3 changes: 3 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const kWriteFileMaxChunkSize = 512 * 1024;
const kMaxUserId = 2 ** 32 - 1;

const isWindows = process.platform === 'win32';
const isMacOS = process.platform === 'darwin';

let fs;
function lazyLoadFs() {
Expand Down Expand Up @@ -970,6 +971,8 @@ module.exports = {
getValidatedFd,
getValidatedPath,
handleErrorFromBinding,
isMacOS,
isWindows,
preprocessSymlinkDestination,
realpathCacheKey: Symbol('realpathCacheKey'),
getStatFsFromBinding,
Expand Down
10 changes: 5 additions & 5 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ const {
const {
getLazy,
emitExperimentalWarning,
isWindows,
isMacOS,
} = require('internal/util');

const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index'));

const platformIsWin32 = (process.platform === 'win32');
const platformIsOSX = (process.platform === 'darwin');

function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
Expand Down Expand Up @@ -167,7 +167,7 @@ function glob(path, pattern, windows) {
validateString(pattern, 'pattern');
return lazyMinimatch().minimatch(path, pattern, {
__proto__: null,
nocase: platformIsOSX || platformIsWin32,
nocase: isWindows || isMacOS,
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
Expand Down Expand Up @@ -1100,7 +1100,7 @@ const win32 = {
};

const posixCwd = (() => {
if (platformIsWin32) {
if (isWindows) {
// Converts Windows' backslash path separators to POSIX forward slashes
// and truncates any drive indicator
const regexp = /\\/g;
Expand Down Expand Up @@ -1575,4 +1575,4 @@ posix.posix = win32.posix = posix;
win32._makeLong = win32.toNamespacedPath;
posix._makeLong = posix.toNamespacedPath;

module.exports = platformIsWin32 ? win32 : posix;
module.exports = isWindows ? win32 : posix;