2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-16 14:39:33 -04:00
|
|
|
import {
|
|
|
|
GlobOptions,
|
|
|
|
globToRegExp,
|
|
|
|
isAbsolute,
|
|
|
|
isGlob,
|
|
|
|
joinGlobs,
|
2020-03-28 13:03:49 -04:00
|
|
|
normalize,
|
2020-09-27 06:22:32 -04:00
|
|
|
SEP_PATTERN,
|
2019-10-16 14:39:33 -04:00
|
|
|
} from "../path/mod.ts";
|
2020-04-29 16:00:31 -04:00
|
|
|
import {
|
2020-09-23 14:09:09 -04:00
|
|
|
_createWalkEntry,
|
|
|
|
_createWalkEntrySync,
|
2020-04-29 16:00:31 -04:00
|
|
|
walk,
|
2020-09-27 06:22:32 -04:00
|
|
|
WalkEntry,
|
2020-04-29 16:00:31 -04:00
|
|
|
walkSync,
|
|
|
|
} from "./walk.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2020-11-13 18:21:11 -05:00
|
|
|
import { isWindows } from "../_util/os.ts";
|
2020-05-09 08:34:47 -04:00
|
|
|
|
2020-08-06 08:46:31 -04:00
|
|
|
export interface ExpandGlobOptions extends Omit<GlobOptions, "os"> {
|
2019-09-28 09:33:17 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:37:03 -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.
|
2020-06-20 14:34:28 -04:00
|
|
|
*
|
2020-10-01 05:37:03 -04:00
|
|
|
* See [`globToRegExp()`](../path/glob.ts#globToRegExp) for details on supported
|
|
|
|
* syntax.
|
2020-06-20 14:34:28 -04:00
|
|
|
*
|
2020-10-01 05:37:03 -04:00
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* for await (const file of expandGlob("**\/*.ts")) {
|
|
|
|
* console.log(file);
|
|
|
|
* }
|
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
|
|
|
{
|
2020-06-12 15:23:38 -04:00
|
|
|
root = Deno.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,
|
2020-07-14 15:24: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)
|
2020-06-12 15:23:38 -04:00
|
|
|
: joinGlobs([Deno.cwd(), root], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
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-09-23 14:09:09 -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,
|
2020-07-14 15:24:17 -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-09-23 14:09:09 -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),
|
2020-07-14 15:24:17 -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-07-14 15:24:17 -04:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory,
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 15:24:17 -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
|
|
|
}
|
|
|
|
|
2020-10-01 05:37:03 -04:00
|
|
|
/** Synchronous version of `expandGlob()`.
|
2020-06-20 14:34:28 -04:00
|
|
|
*
|
2020-10-01 05:37:03 -04:00
|
|
|
* Example:
|
2020-06-20 14:34:28 -04:00
|
|
|
*
|
2020-10-01 05:37:03 -04:00
|
|
|
* for (const file of expandGlobSync("**\/*.ts")) {
|
|
|
|
* console.log(file);
|
|
|
|
* }
|
2020-06-20 14:34:28 -04:00
|
|
|
*/
|
2019-09-28 09:33:17 -04:00
|
|
|
export function* expandGlobSync(
|
2019-10-02 13:59:27 -04:00
|
|
|
glob: string,
|
2019-09-28 09:33:17 -04:00
|
|
|
{
|
2020-06-12 15:23:38 -04:00
|
|
|
root = Deno.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,
|
2020-07-14 15:24: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)
|
2020-06-12 15:23:38 -04:00
|
|
|
: joinGlobs([Deno.cwd(), root], globOptions);
|
2019-10-02 13:59:27 -04:00
|
|
|
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-09-23 14:09:09 -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,
|
2020-07-14 15:24:17 -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-09-23 14:09:09 -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),
|
2020-07-14 15:24:17 -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-07-14 15:24:17 -04:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory,
|
2019-10-02 13:59:27 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 15:24:17 -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
|
|
|
}
|