83 lines
2.7 KiB
TypeScript
83 lines
2.7 KiB
TypeScript
import * as path from 'path'
|
|
import * as isGlob from 'is-glob'
|
|
import * as fg from 'fast-glob'
|
|
|
|
import { REGEX_VAR, REGEX_VAR_ALT } from "@plastichub/core/constants"
|
|
|
|
import { PATH_INFO } from "@plastichub/osr-cli-commons"
|
|
import { sync as exists } from '@plastichub/fs/exists'
|
|
import { isFile, isFolder, getExtensions } from "@plastichub/osr-commons"
|
|
import { IOptions } from '../'
|
|
|
|
export const files = (dir, glob, options?: any) => fg.sync(glob, { ...{ dot: true, cwd: dir, absolute: true, caseSensitiveMatch: false }, ...options || {} }) as []
|
|
|
|
const globBase = require('glob-base')
|
|
|
|
|
|
import { forward_slash } from "@plastichub/osr-cli-commons/"
|
|
import { resolve } from "@plastichub/osr-commons"
|
|
import { readOSRConfig, write_components } from '../lib'
|
|
|
|
export const pathInfo = (src: string, altToken: boolean = false): 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 = isGlob(src)
|
|
} else {
|
|
variables.IS_GLOB = false
|
|
}
|
|
|
|
if (variables.IS_GLOB) {
|
|
//important: use the forward slash since path.resolve will return backslashes on Windows
|
|
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 = fg.sync(glob_base.glob, {
|
|
dot: true,
|
|
cwd: variables.DIR,
|
|
absolute: true,
|
|
caseSensitiveMatch: false
|
|
})
|
|
} else if (variables.IS_FILE && exists(src)) {
|
|
variables.FILES = [src]
|
|
}
|
|
return variables
|
|
}
|
|
|
|
export const _find = (options: IOptions) => {
|
|
let nodes = options.srcInfo.FILES || []
|
|
if (options.filter) {
|
|
return nodes.filter(options.filter)
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
export const find_items = (options) => {
|
|
let ret = _find(options).map((c) => {
|
|
const root = resolve(options.root, false, {})
|
|
return {
|
|
path: forward_slash(`${options.root}/${path.relative(root, c)}`),
|
|
config: readOSRConfig(c)
|
|
}
|
|
})
|
|
options.dst && write_components(ret, options.dst)
|
|
return ret
|
|
}
|
|
|