106 lines
4.0 KiB
TypeScript
106 lines
4.0 KiB
TypeScript
import * as path from 'path'
|
|
|
|
import { glob, globSync, GlobOptions, hasMagic } from 'glob'
|
|
import { REGEX_VAR, REGEX_VAR_ALT } from "@polymech/core/constants"
|
|
import { sync as exists } from '@polymech/fs/exists'
|
|
|
|
export const files = (cwd, glob, options?: any) => globSync(glob, { ...{ dot: true, cwd, absolute: true, caseSensitiveMatch: false }, ...options || {} }) as []
|
|
export const filesEx = (cwd, glob, options?: GlobOptions) => globSync(glob, { ...{ dot: true, cwd, absolute: true, caseSensitiveMatch: false }, ...options || {} }) as []
|
|
|
|
import { substitute } from '../variables.js'
|
|
import { isFile, isFolder } from '../fs.js'
|
|
import { PATH_INFO } from '../types_common.js'
|
|
|
|
import { globBase } from './glob-base.js'
|
|
export { globBase } from './glob-base.js'
|
|
export { globParent } from './glob-parent.js'
|
|
|
|
const GLOB_GROUP_PATTERN = /[!*+?@]\(.*\)/
|
|
|
|
export const getExtensions = (glob: string) => {
|
|
//const parseGlob = require('parse-glob')
|
|
//https://github.com/micromatch/parse-glob/blob/master/index.js
|
|
const match = glob.match(GLOB_GROUP_PATTERN);
|
|
if (match) {
|
|
return glob.substring((match.index || 0) + 2, glob.lastIndexOf(')')).split('|')
|
|
} else {
|
|
// return [parseGlob(glob).path.ext]
|
|
}
|
|
}
|
|
|
|
|
|
export const forward_slash = (path) => {
|
|
const isExtendedLengthPath = /^\\\\\?\\/.test(path)
|
|
const hasNonAscii = /[^\u0000-\u0080]+/.test(path)
|
|
if (isExtendedLengthPath || hasNonAscii) {
|
|
return path;
|
|
}
|
|
return path.replace(/\\/g, '/')
|
|
}
|
|
|
|
export const pathInfoEx = (src: string, altToken: boolean = false, globOptions: GlobOptions = {}): PATH_INFO => {
|
|
const srcParts = path.parse(src)
|
|
let variables: PATH_INFO = {
|
|
PATH: src
|
|
} as PATH_INFO
|
|
|
|
variables.DIR = srcParts.dir
|
|
variables.NAME = srcParts.name
|
|
variables.FILE_NAME = srcParts.base
|
|
variables.FILE_EXT = srcParts.ext.replace('.', '')
|
|
variables.PATH = src
|
|
variables.IS_FILE = isFile(src)
|
|
variables.IS_FOLDER = isFolder(src)
|
|
variables.IS_EXPRESSION = src.match(altToken ? REGEX_VAR_ALT : REGEX_VAR) != null
|
|
variables.IS_GLOB = hasMagic(src)
|
|
if (variables.IS_GLOB) {
|
|
const glob_base = globBase(src)
|
|
variables.DIR = path.resolve(glob_base.base)
|
|
variables.FILE_NAME = glob_base.glob
|
|
variables.GLOB = glob_base.glob
|
|
variables.GLOB_EXTENSIONS = getExtensions(glob_base.glob)
|
|
globOptions = {
|
|
...globOptions,
|
|
cwd: globOptions.cwd ? path.join(globOptions.cwd as string, glob_base.base) : glob_base.base
|
|
}
|
|
variables.FILES = glob.sync(glob_base.glob, globOptions) as []
|
|
} else if (variables.IS_FILE && exists(src)) {
|
|
variables.FILES = [src]
|
|
}
|
|
return variables
|
|
}
|
|
export const pathInfo = (src: string, altToken: boolean = false, cwd: string = null): PATH_INFO => {
|
|
const srcParts = path.parse(src)
|
|
let variables: PATH_INFO = {
|
|
PATH: src
|
|
} as PATH_INFO
|
|
variables.DIR = srcParts.dir
|
|
variables.NAME = srcParts.name
|
|
variables.FILE_NAME = srcParts.base
|
|
variables.FILE_EXT = srcParts.ext.replace('.', '')
|
|
variables.PATH = src
|
|
variables.IS_FILE = isFile(src)
|
|
variables.IS_FOLDER = isFolder(src)
|
|
variables.IS_EXPRESSION = src.match(altToken ? REGEX_VAR_ALT : REGEX_VAR) != null
|
|
|
|
if (!variables.IS_FOLDER && !variables.IS_FILE) {
|
|
variables.IS_GLOB = hasMagic(srcParts.base)
|
|
} else {
|
|
variables.IS_GLOB = false
|
|
}
|
|
if (variables.IS_GLOB) {
|
|
const glob_base = globBase(src)
|
|
variables.DIR = path.resolve(glob_base.base)
|
|
variables.FILE_NAME = glob_base.glob
|
|
variables.GLOB = glob_base.glob
|
|
//variables.GLOB_EXTENSIONS = getExtensions(glob_base.glob)
|
|
variables.FILES = globSync(glob_base.glob, {
|
|
dot: true,
|
|
cwd: path.resolve(cwd || variables.DIR),
|
|
absolute: true
|
|
})
|
|
} else if (variables.IS_FILE && exists(src)) {
|
|
variables.FILES = [src]
|
|
}
|
|
return variables
|
|
} |