41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import * as fs from 'fs'
|
|
|
|
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#namespaces
|
|
// https://github.com/isaacs/node-glob/blob/main/src/pattern.ts
|
|
|
|
export const UNC_REGEX = /^[\\\/]{2,}[^\\\/]+[\\\/]+[^\\\/]+/
|
|
export const WIN32_PATH_REGEX = /^([a-z]:)?[\\\/]/i
|
|
|
|
import { is_windows } from './os.js'
|
|
|
|
|
|
export const isFile = (src: string) => {
|
|
let srcIsFile = false;
|
|
try {
|
|
srcIsFile = fs.lstatSync(src).isFile()
|
|
} catch (e) { }
|
|
return srcIsFile
|
|
}
|
|
|
|
export const isFolder = (src: string) => {
|
|
let srcIsFolder = false;
|
|
try {
|
|
srcIsFolder = fs.lstatSync(src).isDirectory()
|
|
} catch (e) { }
|
|
return srcIsFolder;
|
|
}
|
|
|
|
const is_relative_win32 = (fp) => !fp.test(UNC_REGEX) && !WIN32_PATH_REGEX.test(fp)
|
|
const is_absolute_posix = (fp) => fp.charAt(0) === '/'
|
|
const is_absolute_win32 = (fp) => {
|
|
if (/[a-z]/i.test(fp.charAt(0)) && fp.charAt(1) === ':' && fp.charAt(2) === '\\') {
|
|
return true
|
|
}
|
|
// Microsoft Azure absolute filepath
|
|
if (fp.slice(0, 2) === '\\\\') {
|
|
return true;
|
|
}
|
|
return !is_relative_win32(fp)
|
|
}
|
|
export const is_absolute = (fp) => is_windows() ? is_absolute_win32(fp) : is_absolute_posix(fp)
|