93 lines
3.7 KiB
JavaScript
93 lines
3.7 KiB
JavaScript
import * as path from 'path';
|
|
// import * as isGlob from 'is-glob'
|
|
// import * as fg from 'fast-glob'
|
|
import { glob, globSync, hasMagic } from 'glob';
|
|
import { REGEX_VAR, REGEX_VAR_ALT } from "@polymech/core/constants.js";
|
|
import { sync as exists } from '@polymech/fs/exists';
|
|
export const files = (cwd, glob, options) => globSync(glob, { ...{ dot: true, cwd, absolute: true, caseSensitiveMatch: false }, ...options || {} });
|
|
export const filesEx = (cwd, glob, options) => globSync(glob, { ...{ dot: true, cwd, absolute: true, caseSensitiveMatch: false }, ...options || {} });
|
|
import { substitute } from '@/variables.js';
|
|
import { isFile, isFolder, getExtensions } from '@/fs.js';
|
|
const globBase = require('glob-base');
|
|
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, altToken = false, globOptions = {}) => {
|
|
const srcParts = path.parse(src);
|
|
let variables = {
|
|
PATH: src
|
|
};
|
|
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(substitute(altToken, srcParts.base, {}, false));
|
|
}
|
|
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);
|
|
globOptions = {
|
|
...globOptions,
|
|
cwd: globOptions.cwd ? path.join(globOptions.cwd, glob_base.base) : null
|
|
};
|
|
variables.FILES = glob.sync(glob_base.glob, globOptions);
|
|
}
|
|
else if (variables.IS_FILE && exists(src)) {
|
|
variables.FILES = [src];
|
|
}
|
|
return variables;
|
|
};
|
|
export const pathInfo = (src, altToken = false, cwd = null) => {
|
|
const srcParts = path.parse(src);
|
|
let variables = {
|
|
PATH: src
|
|
};
|
|
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;
|
|
};
|
|
//# sourceMappingURL=_glob.js.map
|