import get from 'lodash-es/get.js'
import getPathParent from './getPathParent.mjs'
//fs寫入類Core之上層資料夾推導, 內部共用不對外(R-FS-PARENT-01)
// 依平台之dirname語意推導(posix僅'/'為分隔字元, 反斜線是合法之檔名字元; Windows之'/'與反斜線皆是, 保留UNC之前置雙分隔字元), 不匯入path(R-FS-DEPS-01)
//isSep, Windows之分隔字元
function isSep(c) {
return c === '/' || c === '\\'
}
//dirnamePosix, 判定同Node之path.posix.dirname
function dirnamePosix(fp) {
if (fp.length === 0) {
return '.'
}
let hasRoot = fp[0] === '/'
let end = -1
let matchedSlash = true
for (let i = fp.length - 1; i >= 1; --i) {
if (fp[i] === '/') {
if (!matchedSlash) {
end = i
break
}
}
else {
matchedSlash = false
}
}
if (end === -1) {
return hasRoot ? '/' : '.'
}
if (hasRoot && end === 1) {
return '//'
}
return fp.slice(0, end)
}
//rootOfWin32, 回傳根之結束位置: UNC(兩個分隔字元起首之server與share)、磁碟代號或單一分隔字元; 無根為-1, 整串即UNC根時回null
function rootOfWin32(fp) {
let len = fp.length
if (isSep(fp[0])) {
if (!isSep(fp[1])) {
return 1
}
//skip, 自位置j起略過符合(分隔字元與否)之字元, 回傳停下之位置
let skip = (j, sep) => {
while (j < len && isSep(fp[j]) === sep) {
j++
}
return j
}
let a = skip(2, false) //server
if (a >= len || a === 2) {
return 1
}
let b = skip(a, true)
if (b >= len || b === a) {
return 1
}
let c = skip(b, false) //share
if (c === len) {
return null //只有UNC根
}
return c === b ? 1 : c + 1
}
if (/^[A-Za-z]:/.test(fp)) {
return len > 2 && isSep(fp[2]) ? 3 : 2
}
return -1
}
//rootWin32, 路徑之根, 判定同Node之path.win32.parse之root: UNC根(\\server\share\)、磁碟代號('C:'、'C:\')、單一分隔字元, 無根為''
function rootWin32(fp) {
if (fp.length === 1) {
return isSep(fp) ? fp : ''
}
let rootEnd = rootOfWin32(fp)
if (rootEnd === null) {
return fp
}
return rootEnd === -1 ? '' : fp.slice(0, rootEnd)
}
//dirnameWin32, 判定同Node之path.win32.dirname
function dirnameWin32(fp) {
let len = fp.length
if (len === 0) {
return '.'
}
if (len === 1) {
return isSep(fp) ? fp : '.'
}
let rootEnd = rootOfWin32(fp)
if (rootEnd === null) {
return fp
}
let offset = rootEnd === -1 ? 0 : rootEnd
let end = -1
let matchedSlash = true
for (let i = len - 1; i >= offset; --i) {
if (isSep(fp[i])) {
if (!matchedSlash) {
end = i
break
}
}
else {
matchedSlash = false
}
}
if (end === -1) {
if (rootEnd === -1) {
return '.'
}
end = rootEnd
}
return fp.slice(0, end)
}
//reUncRoot, UNC之根: \\server、\\server\share(含\\?\C:)及\\?\UNC\server\share, 共用根目錄無從建立
let reUncRoot = /^[\\/]{2}[^\\/]*(?:[\\/]+[^\\/]+)?[\\/]*$/
let reUncExtRoot = /^[\\/]{2}\?[\\/]+unc[\\/]+[^\\/]+[\\/]+[^\\/]+[\\/]*$/i
//reDriveRoot, 磁碟根: 磁碟代號加分隔字元, 或只有分隔字元(目前磁碟之根)
let reDriveRoot = /^(?:[A-Za-z]:)?[\\/]+$/
//isColonTail, 去尾端分隔字元後以冒號結尾之Windows路徑: 檔名位置上不是有效之名稱(替代資料流之語法)或即磁碟本身
function isColonTail(fp) {
let i = fp.length
while (i > 0 && isSep(fp[i - 1])) {
i--
}
return fp[i - 1] === ':'
}
//adjust, 將推得之上層資料夾轉為呼叫端所用之形式: ''代表沒有要建立之資料夾
function adjust(r, fp, win32) {
if (win32 && isColonTail(fp)) {
//沿舊版結果(純字串推導; 以'/'結尾者沒有要建立之資料夾), 使呼叫端於建立任何檔案之前即失敗: 依dirname推導會先建出上層資料夾, 原子寫入之暫存檔且會留下0位元組之檔
return fp.endsWith('/') ? '' : getPathParent(fp)
}
if (r === '.') {
return '' //上層為本進程工作目錄
}
if (!win32 || typeof r !== 'string') {
return r
}
if (reUncRoot.test(r) || reUncExtRoot.test(r)) {
return ''
}
if (reDriveRoot.test(r)) {
return '' //磁碟根('C:\'、'C:/')與目前磁碟之根('\'、'/'): 必然已存在而不可建立, 對其呼叫mkdirSync於Windows回EPERM
}
if (/^[A-Za-z]:$/.test(r) && !/[\\/]/.test(fp)) {
return '' //'C:f': 該磁碟之目前資料夾
}
return r
}
/**
* 推導檔案或資料夾路徑fp之上層資料夾
*
* @param {String} fp 輸入路徑字串(呼叫端已檢核為非空字串)
* @param {Object} [opt={}] 輸入呼叫端Core之設定物件, 只讀其path鍵: 提供dirname函數者以之推導(平台依其sep), 未給、不提供dirname或不可讀者依執行平台推導
* @returns {String} 回傳上層資料夾路徑, 沒有要建立之資料夾(純檔名、Windows之磁碟根與UNC根等)時回傳''
*/
function fsParentDir(fp, opt = {}) {
let { dirname, win32 } = getRule(opt)
return adjust(dirname(fp), fp, win32)
}
//getRule, 依opt.path(提供dirname函數者, 平台依其sep)或執行平台取得dirname與是否Windows語意; opt.path為選配之注入, 非預期(含讀取拋錯)視為未給(K3)
function getRule(opt) {
let path = null
let dirname = null
try {
path = get(opt, 'path')
dirname = get(path, 'dirname')
}
catch (err) {}
if (typeof dirname === 'function') {
return { dirname: (fp) => path.dirname(fp), win32: get(path, 'sep') === '\\' }
}
let win32 = get(globalThis, 'process.platform') === 'win32'
return { dirname: win32 ? dirnameWin32 : dirnamePosix, win32 }
}
//fsSplitEntry, 路徑所指之目錄項: dir為上層資料夾(同一平台之dirname語意, 純檔名者為'.'), name為去尾端分隔字元後之最末段(Windows不分大小寫而轉大寫, 同NTFS之比對; 轉小寫會依詞尾變出不同字母), 供尚不存在之兩路徑比對是否同一目錄項
function fsSplitEntry(fp, opt = {}) {
let { dirname, win32 } = getRule(opt)
let sep = win32 ? isSep : (c) => c === '/'
let i = fp.length
while (i > 0 && sep(fp[i - 1])) {
i--
}
let j = i
while (j > 0 && !sep(fp[j - 1])) {
j--
}
let name = fp.slice(j, i)
if (win32) {
if (j === 0) {
name = name.replace(/^[A-Za-z]:/, '') //'C:f': 磁碟代號不屬名稱
}
name = name.toUpperCase()
}
return { dir: dirname(fp), name }
}
export { dirnamePosix, dirnameWin32, rootWin32, fsSplitEntry }
export default fsParentDir