2019-10-16 14:39:33 -04:00
|
|
|
import {
|
|
|
|
GlobOptions,
|
|
|
|
SEP_PATTERN,
|
|
|
|
globToRegExp,
|
|
|
|
isAbsolute,
|
|
|
|
isGlob,
|
|
|
|
isWindows,
|
|
|
|
joinGlobs,
|
2020-03-28 13:03:49 -04:00
|
|
|
normalize,
|
2019-10-16 14:39:33 -04:00
|
|
|
} from "../path/mod.ts";
|
2020-04-29 16:00:31 -04:00
|
|
|
import {
|
|
|
|
WalkEntry,
|
|
|
|
createWalkEntry,
|
|
|
|
createWalkEntrySync,
|
|
|
|
walk,
|
|
|
|
walkSync,
|
|
|
|
} from "./walk.ts";
|
2020-02-08 15:15:59 -05:00
|
|
|
import { assert } from "../testing/asserts.ts";
|
2020-04-29 16:00:31 -04:00
|
|
|
const { cwd } = Deno;
|
2019-10-02 13:59:27 -04:00
|
|
|
type FileInfo = Deno.FileInfo;
|
2019-03-02 14:56:19 -05:00
|
|
|
|
2019-09-28 09:33:17 -04:00
|
|
|
export interface ExpandGlobOptions extends GlobOptions {
|
|
|
|
root?: string;
|
2019-10-02 13:59:27 -04:00
|
|
|
exclude?: string[];
|
2019-09-28 09:33:17 -04:00
|
|
|
includeDirs?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
interface SplitPath {
|
|
|
|
segments: string[];
|
|
|
|
isAbsolute: boolean;
|
|
|
|
hasTrailingSep: boolean;
|
|
|
|
// Defined for any absolute Windows path.
|
|
|
|
winRoot?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Maybe make this public somewhere.
|
|
|
|
function split(path: string): SplitPath {
|
|
|
|
const s = SEP_PATTERN.source;
|
|
|
|
const segments = path
|
|
|
|
.replace(new RegExp(`^${s}|${s}$`, "g"), "")
|
|
|
|
.split(SEP_PATTERN);
|
|
|
|
const isAbsolute_ = isAbsolute(path);
|
|
|
|
return {
|
|
|
|
segments,
|
|
|
|
isAbsolute: isAbsolute_,
|
|
|
|
hasTrailingSep: !!path.match(new RegExp(`${s}$`)),
|
2020-03-28 13:03:49 -04:00
|
|
|
winRoot: isWindows && isAbsolute_ ? segments.shift() : undefined,
|
2019-10-02 13:59:27 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-11 19:42:21 -05:00
|
|
|
function throwUnlessNotFound(error: Error): void {
|
2020-02-24 15:48:35 -05:00
|
|
|
if (!(error instanceof Deno.errors.NotFound)) {
|
2019-12-11 19:42:21 -05:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
function comparePath(a: WalkEntry, b: WalkEntry): number {
|
|
|
|
if (a.path < b.path) return -1;
|
|
|
|
if (a.path > b.path) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:33:17 -04:00
|
|
|
/**
|
|
|
|
* Expand the glob string from the specified `root` directory and yield each
|
2020-04-16 01:40:30 -04:00
|
|
|
* result as a `WalkEntry` object.
|
2019-09-28 09:33:17 -04:00
|
|
|
*/
|
|
|
|
export async function* expandGlob(
|
2019-10-02 13:59:27 -04:00
|
|
|
glob: string,
|
2019-09-28 09:33:17 -04:00
|
|
|
{
|
|
|
|
root = cwd(),
|
2019-10-02 13:59:27 -04:00
|
|
|
exclude = [],
|
2019-09-28 09:33:17 -04:00
|
|
|
includeDirs = true,
|
|
|
|
extended = false,
|
2020-03-28 13:03:49 -04:00
|
|
|
globstar = false,
|
2019-09-28 09:33:17 -04:00
|
|
|
}: ExpandGlobOptions = {}
|
2020-04-16 01:40:30 -04:00
|
|
|
): AsyncIterableIterator<WalkEntry> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const globOptions: GlobOptions = { extended, globstar };
|
|
|
|
const absRoot = isAbsolute(root)
|
|
|
|
? normalize(root)
|
|
|
|
: joinGlobs([cwd(), root], globOptions);
|
|
|
|
const resolveFromRoot = (path: string): string =>
|
|
|
|
isAbsolute(path)
|
|
|
|
? normalize(path)
|
|
|
|
: joinGlobs([absRoot, path], globOptions);
|
|
|
|
const excludePatterns = exclude
|
|
|
|
.map(resolveFromRoot)
|
|
|
|
.map((s: string): RegExp => globToRegExp(s, globOptions));
|
2020-04-29 16:00:31 -04:00
|
|
|
const shouldInclude = (path: string): boolean =>
|
|
|
|
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
|
2019-10-02 13:59:27 -04:00
|
|
|
const { segments, hasTrailingSep, winRoot } = split(resolveFromRoot(glob));
|
|
|
|
|
|
|
|
let fixedRoot = winRoot != undefined ? winRoot : "/";
|
|
|
|
while (segments.length > 0 && !isGlob(segments[0])) {
|
2020-02-07 02:23:38 -05:00
|
|
|
const seg = segments.shift();
|
|
|
|
assert(seg != null);
|
|
|
|
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
let fixedRootInfo: WalkEntry;
|
2019-10-02 13:59:27 -04:00
|
|
|
try {
|
2020-04-29 16:00:31 -04:00
|
|
|
fixedRootInfo = await createWalkEntry(fixedRoot);
|
2019-12-11 19:42:21 -05:00
|
|
|
} catch (error) {
|
|
|
|
return throwUnlessNotFound(error);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function* advanceMatch(
|
2020-04-16 01:40:30 -04:00
|
|
|
walkInfo: WalkEntry,
|
2019-10-02 13:59:27 -04:00
|
|
|
globSegment: string
|
2020-04-16 01:40:30 -04:00
|
|
|
): AsyncIterableIterator<WalkEntry> {
|
2020-04-29 16:00:31 -04:00
|
|
|
if (!walkInfo.isDirectory) {
|
2019-10-02 13:59:27 -04:00
|
|
|
return;
|
|
|
|
} else if (globSegment == "..") {
|
2020-04-29 16:00:31 -04:00
|
|
|
const parentPath = joinGlobs([walkInfo.path, ".."], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
try {
|
2019-12-11 19:42:21 -05:00
|
|
|
if (shouldInclude(parentPath)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
return yield await createWalkEntry(parentPath);
|
2019-12-11 19:42:21 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throwUnlessNotFound(error);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
2019-12-11 19:42:21 -05:00
|
|
|
return;
|
2019-10-02 13:59:27 -04:00
|
|
|
} else if (globSegment == "**") {
|
2020-04-29 16:00:31 -04:00
|
|
|
return yield* walk(walkInfo.path, {
|
2019-10-02 13:59:27 -04:00
|
|
|
includeFiles: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip: excludePatterns,
|
2019-10-02 13:59:27 -04:00
|
|
|
});
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
yield* walk(walkInfo.path, {
|
2019-10-02 13:59:27 -04:00
|
|
|
maxDepth: 1,
|
|
|
|
match: [
|
|
|
|
globToRegExp(
|
2020-04-29 16:00:31 -04:00
|
|
|
joinGlobs([walkInfo.path, globSegment], globOptions),
|
2019-10-02 13:59:27 -04:00
|
|
|
globOptions
|
2020-03-28 13:03:49 -04:00
|
|
|
),
|
2019-10-02 13:59:27 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
skip: excludePatterns,
|
2019-10-02 13:59:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
let currentMatches: WalkEntry[] = [fixedRootInfo];
|
2019-10-02 13:59:27 -04:00
|
|
|
for (const segment of segments) {
|
|
|
|
// Advancing the list of current matches may introduce duplicates, so we
|
|
|
|
// pass everything through this Map.
|
2020-04-29 16:00:31 -04:00
|
|
|
const nextMatchMap: Map<string, WalkEntry> = new Map();
|
2019-10-02 13:59:27 -04:00
|
|
|
for (const currentMatch of currentMatches) {
|
|
|
|
for await (const nextMatch of advanceMatch(currentMatch, segment)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
nextMatchMap.set(nextMatch.path, nextMatch);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
currentMatches = [...nextMatchMap.values()].sort(comparePath);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
if (hasTrailingSep) {
|
2020-04-16 01:40:30 -04:00
|
|
|
currentMatches = currentMatches.filter(
|
2020-04-29 16:00:31 -04:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-04-29 16:00:31 -04:00
|
|
|
(entry: WalkEntry): boolean => !entry.isDirectory
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
yield* currentMatches;
|
2019-09-28 09:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Synchronous version of `expandGlob()`. */
|
|
|
|
export function* expandGlobSync(
|
2019-10-02 13:59:27 -04:00
|
|
|
glob: string,
|
2019-09-28 09:33:17 -04:00
|
|
|
{
|
|
|
|
root = cwd(),
|
2019-10-02 13:59:27 -04:00
|
|
|
exclude = [],
|
2019-09-28 09:33:17 -04:00
|
|
|
includeDirs = true,
|
|
|
|
extended = false,
|
2020-03-28 13:03:49 -04:00
|
|
|
globstar = false,
|
2019-09-28 09:33:17 -04:00
|
|
|
}: ExpandGlobOptions = {}
|
2020-04-16 01:40:30 -04:00
|
|
|
): IterableIterator<WalkEntry> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const globOptions: GlobOptions = { extended, globstar };
|
|
|
|
const absRoot = isAbsolute(root)
|
|
|
|
? normalize(root)
|
|
|
|
: joinGlobs([cwd(), root], globOptions);
|
|
|
|
const resolveFromRoot = (path: string): string =>
|
|
|
|
isAbsolute(path)
|
|
|
|
? normalize(path)
|
|
|
|
: joinGlobs([absRoot, path], globOptions);
|
|
|
|
const excludePatterns = exclude
|
|
|
|
.map(resolveFromRoot)
|
|
|
|
.map((s: string): RegExp => globToRegExp(s, globOptions));
|
2020-04-29 16:00:31 -04:00
|
|
|
const shouldInclude = (path: string): boolean =>
|
|
|
|
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
|
2019-10-02 13:59:27 -04:00
|
|
|
const { segments, hasTrailingSep, winRoot } = split(resolveFromRoot(glob));
|
|
|
|
|
|
|
|
let fixedRoot = winRoot != undefined ? winRoot : "/";
|
|
|
|
while (segments.length > 0 && !isGlob(segments[0])) {
|
2020-02-07 02:23:38 -05:00
|
|
|
const seg = segments.shift();
|
|
|
|
assert(seg != null);
|
|
|
|
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
let fixedRootInfo: WalkEntry;
|
2019-10-02 13:59:27 -04:00
|
|
|
try {
|
2020-04-29 16:00:31 -04:00
|
|
|
fixedRootInfo = createWalkEntrySync(fixedRoot);
|
2019-12-11 19:42:21 -05:00
|
|
|
} catch (error) {
|
|
|
|
return throwUnlessNotFound(error);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function* advanceMatch(
|
2020-04-16 01:40:30 -04:00
|
|
|
walkInfo: WalkEntry,
|
2019-10-02 13:59:27 -04:00
|
|
|
globSegment: string
|
2020-04-16 01:40:30 -04:00
|
|
|
): IterableIterator<WalkEntry> {
|
2020-04-29 16:00:31 -04:00
|
|
|
if (!walkInfo.isDirectory) {
|
2019-10-02 13:59:27 -04:00
|
|
|
return;
|
|
|
|
} else if (globSegment == "..") {
|
2020-04-29 16:00:31 -04:00
|
|
|
const parentPath = joinGlobs([walkInfo.path, ".."], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
try {
|
2019-12-11 19:42:21 -05:00
|
|
|
if (shouldInclude(parentPath)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
return yield createWalkEntrySync(parentPath);
|
2019-12-11 19:42:21 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throwUnlessNotFound(error);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
2019-12-11 19:42:21 -05:00
|
|
|
return;
|
2019-10-02 13:59:27 -04:00
|
|
|
} else if (globSegment == "**") {
|
2020-04-29 16:00:31 -04:00
|
|
|
return yield* walkSync(walkInfo.path, {
|
2019-10-02 13:59:27 -04:00
|
|
|
includeFiles: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip: excludePatterns,
|
2019-10-02 13:59:27 -04:00
|
|
|
});
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
yield* walkSync(walkInfo.path, {
|
2019-10-02 13:59:27 -04:00
|
|
|
maxDepth: 1,
|
|
|
|
match: [
|
|
|
|
globToRegExp(
|
2020-04-29 16:00:31 -04:00
|
|
|
joinGlobs([walkInfo.path, globSegment], globOptions),
|
2019-10-02 13:59:27 -04:00
|
|
|
globOptions
|
2020-03-28 13:03:49 -04:00
|
|
|
),
|
2019-10-02 13:59:27 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
skip: excludePatterns,
|
2019-10-02 13:59:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-16 01:40:30 -04:00
|
|
|
let currentMatches: WalkEntry[] = [fixedRootInfo];
|
2019-10-02 13:59:27 -04:00
|
|
|
for (const segment of segments) {
|
|
|
|
// Advancing the list of current matches may introduce duplicates, so we
|
|
|
|
// pass everything through this Map.
|
2020-04-29 16:00:31 -04:00
|
|
|
const nextMatchMap: Map<string, WalkEntry> = new Map();
|
2019-10-02 13:59:27 -04:00
|
|
|
for (const currentMatch of currentMatches) {
|
|
|
|
for (const nextMatch of advanceMatch(currentMatch, segment)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
nextMatchMap.set(nextMatch.path, nextMatch);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
currentMatches = [...nextMatchMap.values()].sort(comparePath);
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
|
|
|
if (hasTrailingSep) {
|
2020-04-16 01:40:30 -04:00
|
|
|
currentMatches = currentMatches.filter(
|
2020-04-29 16:00:31 -04:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-04-29 16:00:31 -04:00
|
|
|
(entry: WalkEntry): boolean => !entry.isDirectory
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
yield* currentMatches;
|
2019-09-28 09:33:17 -04:00
|
|
|
}
|