2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-12-21 07:39:51 +00:00
|
|
|
import { GlobOptions } from "../path/glob.ts";
|
|
|
|
import { globToRegExp } from "../path/glob_to_regexp.ts";
|
|
|
|
import { joinGlobs } from "../path/join_globs.ts";
|
|
|
|
import { isGlob } from "../path/is_glob.ts";
|
2023-08-14 07:08:42 +00:00
|
|
|
import { isAbsolute } from "../path/is_absolute.ts";
|
|
|
|
import { resolve } from "../path/resolve.ts";
|
|
|
|
import { SEP_PATTERN } from "../path/separator.ts";
|
2022-06-17 14:18:53 +00:00
|
|
|
import { walk, walkSync } from "./walk.ts";
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assert } from "../assert/assert.ts";
|
2023-12-21 07:39:51 +00:00
|
|
|
import { toPathString } from "./_to_path_string.ts";
|
2022-08-30 03:58:48 +00:00
|
|
|
import {
|
|
|
|
createWalkEntry,
|
|
|
|
createWalkEntrySync,
|
2023-12-21 07:39:51 +00:00
|
|
|
type WalkEntry,
|
|
|
|
} from "./_create_walk_entry.ts";
|
|
|
|
|
2023-12-05 08:52:56 +00:00
|
|
|
export type { GlobOptions };
|
2020-05-09 12:34:47 +00:00
|
|
|
|
2023-08-25 09:07:43 +00:00
|
|
|
const isWindows = Deno.build.os === "windows";
|
|
|
|
|
2023-12-05 08:52:56 +00:00
|
|
|
/** Options for {@linkcode expandGlob} and {@linkcode expandGlobSync}. */
|
2020-08-06 12:46:31 +00:00
|
|
|
export interface ExpandGlobOptions extends Omit<GlobOptions, "os"> {
|
2023-12-05 08:52:56 +00:00
|
|
|
/** File path where to expand from. */
|
2019-09-28 13:33:17 +00:00
|
|
|
root?: string;
|
2023-12-05 08:52:56 +00:00
|
|
|
/** List of glob patterns to be excluded from the expansion. */
|
2019-10-02 17:59:27 +00:00
|
|
|
exclude?: string[];
|
2023-12-05 08:52:56 +00:00
|
|
|
/**
|
|
|
|
* Whether to include directories in entries.
|
|
|
|
*
|
|
|
|
* @default {true}
|
|
|
|
*/
|
2019-09-28 13:33:17 +00:00
|
|
|
includeDirs?: boolean;
|
2023-12-05 08:52:56 +00:00
|
|
|
/**
|
|
|
|
* Whether to follow symbolic links.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks?: boolean;
|
2023-10-16 13:32:15 +00:00
|
|
|
/**
|
|
|
|
* Indicates whether the followed symlink's path should be canonicalized.
|
|
|
|
* This option works only if `followSymlinks` is not `false`.
|
2023-12-05 08:52:56 +00:00
|
|
|
*
|
2023-10-16 13:32:15 +00:00
|
|
|
* @default {true}
|
|
|
|
*/
|
|
|
|
canonicalize?: boolean;
|
2019-09-28 13:33:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:59:27 +00:00
|
|
|
interface SplitPath {
|
|
|
|
segments: string[];
|
|
|
|
isAbsolute: boolean;
|
|
|
|
hasTrailingSep: boolean;
|
|
|
|
// Defined for any absolute Windows path.
|
|
|
|
winRoot?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 17:03:49 +00:00
|
|
|
winRoot: isWindows && isAbsolute_ ? segments.shift() : undefined,
|
2019-10-02 17:59:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-24 01:21:57 +00:00
|
|
|
function throwUnlessNotFound(error: unknown) {
|
2020-02-24 20:48:35 +00:00
|
|
|
if (!(error instanceof Deno.errors.NotFound)) {
|
2019-12-12 00:42:21 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:00:31 +00:00
|
|
|
function comparePath(a: WalkEntry, b: WalkEntry): number {
|
|
|
|
if (a.path < b.path) return -1;
|
|
|
|
if (a.path > b.path) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
|
|
|
* Expand the glob string from the specified `root` directory and yield each
|
2020-04-16 05:40:30 +00:00
|
|
|
* result as a `WalkEntry` object.
|
2020-06-20 18:34:28 +00:00
|
|
|
*
|
2020-10-01 09:37:03 +00:00
|
|
|
* See [`globToRegExp()`](../path/glob.ts#globToRegExp) for details on supported
|
|
|
|
* syntax.
|
2020-06-20 18:34:28 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* @example
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```ts
|
2022-11-25 11:40:23 +00:00
|
|
|
* import { expandGlob } from "https://deno.land/std@$STD_VERSION/fs/expand_glob.ts";
|
|
|
|
* for await (const file of expandGlob("**\/*.ts")) {
|
|
|
|
* console.log(file);
|
|
|
|
* }
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```
|
2019-09-28 13:33:17 +00:00
|
|
|
*/
|
|
|
|
export async function* expandGlob(
|
2022-08-30 03:58:48 +00:00
|
|
|
glob: string | URL,
|
2019-09-28 13:33:17 +00:00
|
|
|
{
|
2023-10-24 07:51:33 +00:00
|
|
|
root,
|
2019-10-02 17:59:27 +00:00
|
|
|
exclude = [],
|
2019-09-28 13:33:17 +00:00
|
|
|
includeDirs = true,
|
2021-12-23 05:32:12 +00:00
|
|
|
extended = true,
|
2023-01-16 15:26:27 +00:00
|
|
|
globstar = true,
|
2021-04-22 03:34:10 +00:00
|
|
|
caseInsensitive,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2023-10-16 13:32:15 +00:00
|
|
|
canonicalize,
|
2020-07-14 19:24:17 +00:00
|
|
|
}: ExpandGlobOptions = {},
|
2020-04-16 05:40:30 +00:00
|
|
|
): AsyncIterableIterator<WalkEntry> {
|
2023-10-24 07:51:33 +00:00
|
|
|
const {
|
|
|
|
segments,
|
|
|
|
isAbsolute: isGlobAbsolute,
|
|
|
|
hasTrailingSep,
|
|
|
|
winRoot,
|
|
|
|
} = split(toPathString(glob));
|
|
|
|
root ??= isGlobAbsolute ? winRoot ?? "/" : Deno.cwd();
|
|
|
|
|
2021-04-22 03:34:10 +00:00
|
|
|
const globOptions: GlobOptions = { extended, globstar, caseInsensitive };
|
2023-10-24 07:51:33 +00:00
|
|
|
const absRoot = isGlobAbsolute ? root : resolve(root!); // root is always string here
|
2021-10-17 18:04:24 +00:00
|
|
|
const resolveFromRoot = (path: string): string => resolve(absRoot, path);
|
2019-10-02 17:59:27 +00:00
|
|
|
const excludePatterns = exclude
|
|
|
|
.map(resolveFromRoot)
|
|
|
|
.map((s: string): RegExp => globToRegExp(s, globOptions));
|
2020-04-29 20:00:31 +00:00
|
|
|
const shouldInclude = (path: string): boolean =>
|
|
|
|
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
|
2019-10-02 17:59:27 +00:00
|
|
|
|
2021-10-17 18:04:24 +00:00
|
|
|
let fixedRoot = isGlobAbsolute
|
2023-08-25 09:04:43 +00:00
|
|
|
? winRoot !== undefined ? winRoot : "/"
|
2021-10-17 18:04:24 +00:00
|
|
|
: absRoot;
|
2019-10-02 17:59:27 +00:00
|
|
|
while (segments.length > 0 && !isGlob(segments[0])) {
|
2020-02-07 07:23:38 +00:00
|
|
|
const seg = segments.shift();
|
2023-08-25 09:04:43 +00:00
|
|
|
assert(seg !== undefined);
|
2020-02-07 07:23:38 +00:00
|
|
|
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
let fixedRootInfo: WalkEntry;
|
2019-10-02 17:59:27 +00:00
|
|
|
try {
|
2022-06-17 14:18:53 +00:00
|
|
|
fixedRootInfo = await createWalkEntry(fixedRoot);
|
2019-12-12 00:42:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
return throwUnlessNotFound(error);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function* advanceMatch(
|
2020-04-16 05:40:30 +00:00
|
|
|
walkInfo: WalkEntry,
|
2020-07-14 19:24:17 +00:00
|
|
|
globSegment: string,
|
2020-04-16 05:40:30 +00:00
|
|
|
): AsyncIterableIterator<WalkEntry> {
|
2020-04-29 20:00:31 +00:00
|
|
|
if (!walkInfo.isDirectory) {
|
2019-10-02 17:59:27 +00:00
|
|
|
return;
|
2023-08-25 09:04:43 +00:00
|
|
|
} else if (globSegment === "..") {
|
2020-04-29 20:00:31 +00:00
|
|
|
const parentPath = joinGlobs([walkInfo.path, ".."], globOptions);
|
2019-10-02 17:59:27 +00:00
|
|
|
try {
|
2019-12-12 00:42:21 +00:00
|
|
|
if (shouldInclude(parentPath)) {
|
2022-06-17 14:18:53 +00:00
|
|
|
return yield await createWalkEntry(parentPath);
|
2019-12-12 00:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throwUnlessNotFound(error);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
2019-12-12 00:42:21 +00:00
|
|
|
return;
|
2023-08-25 09:04:43 +00:00
|
|
|
} else if (globSegment === "**") {
|
2022-10-18 14:40:00 +00:00
|
|
|
return yield* walk(walkInfo.path, {
|
|
|
|
skip: excludePatterns,
|
|
|
|
maxDepth: globstar ? Infinity : 1,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2023-10-16 13:32:15 +00:00
|
|
|
canonicalize,
|
2022-10-18 14:40:00 +00:00
|
|
|
});
|
2021-10-17 18:04:24 +00:00
|
|
|
}
|
|
|
|
const globPattern = globToRegExp(globSegment, globOptions);
|
|
|
|
for await (
|
|
|
|
const walkEntry of walk(walkInfo.path, {
|
|
|
|
maxDepth: 1,
|
2020-03-28 17:03:49 +00:00
|
|
|
skip: excludePatterns,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2021-10-17 18:04:24 +00:00
|
|
|
})
|
|
|
|
) {
|
|
|
|
if (
|
2023-08-25 09:04:43 +00:00
|
|
|
walkEntry.path !== walkInfo.path &&
|
2022-10-18 14:40:00 +00:00
|
|
|
walkEntry.name.match(globPattern)
|
2021-10-17 18:04:24 +00:00
|
|
|
) {
|
|
|
|
yield walkEntry;
|
|
|
|
}
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
let currentMatches: WalkEntry[] = [fixedRootInfo];
|
2019-10-02 17:59:27 +00: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 20:00:31 +00:00
|
|
|
const nextMatchMap: Map<string, WalkEntry> = new Map();
|
2022-10-18 14:40:00 +00:00
|
|
|
await Promise.all(
|
|
|
|
currentMatches.map(async (currentMatch) => {
|
|
|
|
for await (const nextMatch of advanceMatch(currentMatch, segment)) {
|
|
|
|
nextMatchMap.set(nextMatch.path, nextMatch);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2020-04-29 20:00:31 +00:00
|
|
|
currentMatches = [...nextMatchMap.values()].sort(comparePath);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
2022-10-18 14:40:00 +00:00
|
|
|
|
2019-10-02 17:59:27 +00:00
|
|
|
if (hasTrailingSep) {
|
2020-04-16 05:40:30 +00:00
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 19:24:17 +00:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory,
|
2019-10-02 17:59:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 19:24:17 +00:00
|
|
|
(entry: WalkEntry): boolean => !entry.isDirectory,
|
2019-10-02 17:59:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
yield* currentMatches;
|
2019-09-28 13:33:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 11:40:23 +00:00
|
|
|
/**
|
|
|
|
* Synchronous version of `expandGlob()`.
|
2020-06-20 18:34:28 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* @example
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```ts
|
2022-11-25 11:40:23 +00:00
|
|
|
* import { expandGlobSync } from "https://deno.land/std@$STD_VERSION/fs/expand_glob.ts";
|
|
|
|
* for (const file of expandGlobSync("**\/*.ts")) {
|
|
|
|
* console.log(file);
|
|
|
|
* }
|
2021-09-12 15:14:54 +00:00
|
|
|
* ```
|
2020-06-20 18:34:28 +00:00
|
|
|
*/
|
2019-09-28 13:33:17 +00:00
|
|
|
export function* expandGlobSync(
|
2022-08-30 03:58:48 +00:00
|
|
|
glob: string | URL,
|
2019-09-28 13:33:17 +00:00
|
|
|
{
|
2023-10-24 07:51:33 +00:00
|
|
|
root,
|
2019-10-02 17:59:27 +00:00
|
|
|
exclude = [],
|
2019-09-28 13:33:17 +00:00
|
|
|
includeDirs = true,
|
2021-12-23 05:32:12 +00:00
|
|
|
extended = true,
|
2023-01-16 15:26:27 +00:00
|
|
|
globstar = true,
|
2021-04-22 03:34:10 +00:00
|
|
|
caseInsensitive,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2023-10-16 13:32:15 +00:00
|
|
|
canonicalize,
|
2020-07-14 19:24:17 +00:00
|
|
|
}: ExpandGlobOptions = {},
|
2020-04-16 05:40:30 +00:00
|
|
|
): IterableIterator<WalkEntry> {
|
2023-10-24 07:51:33 +00:00
|
|
|
const {
|
|
|
|
segments,
|
|
|
|
isAbsolute: isGlobAbsolute,
|
|
|
|
hasTrailingSep,
|
|
|
|
winRoot,
|
|
|
|
} = split(toPathString(glob));
|
|
|
|
root ??= isGlobAbsolute ? winRoot ?? "/" : Deno.cwd();
|
|
|
|
|
2021-04-22 03:34:10 +00:00
|
|
|
const globOptions: GlobOptions = { extended, globstar, caseInsensitive };
|
2023-10-24 07:51:33 +00:00
|
|
|
const absRoot = isGlobAbsolute ? root : resolve(root!); // root is always string here
|
2021-10-17 18:04:24 +00:00
|
|
|
const resolveFromRoot = (path: string): string => resolve(absRoot, path);
|
2019-10-02 17:59:27 +00:00
|
|
|
const excludePatterns = exclude
|
|
|
|
.map(resolveFromRoot)
|
|
|
|
.map((s: string): RegExp => globToRegExp(s, globOptions));
|
2020-04-29 20:00:31 +00:00
|
|
|
const shouldInclude = (path: string): boolean =>
|
|
|
|
!excludePatterns.some((p: RegExp): boolean => !!path.match(p));
|
2019-10-02 17:59:27 +00:00
|
|
|
|
2021-10-17 18:04:24 +00:00
|
|
|
let fixedRoot = isGlobAbsolute
|
2023-08-25 09:04:43 +00:00
|
|
|
? winRoot !== undefined ? winRoot : "/"
|
2021-10-17 18:04:24 +00:00
|
|
|
: absRoot;
|
2019-10-02 17:59:27 +00:00
|
|
|
while (segments.length > 0 && !isGlob(segments[0])) {
|
2020-02-07 07:23:38 +00:00
|
|
|
const seg = segments.shift();
|
2023-08-25 09:04:43 +00:00
|
|
|
assert(seg !== undefined);
|
2020-02-07 07:23:38 +00:00
|
|
|
fixedRoot = joinGlobs([fixedRoot, seg], globOptions);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
let fixedRootInfo: WalkEntry;
|
2019-10-02 17:59:27 +00:00
|
|
|
try {
|
2022-06-17 14:18:53 +00:00
|
|
|
fixedRootInfo = createWalkEntrySync(fixedRoot);
|
2019-12-12 00:42:21 +00:00
|
|
|
} catch (error) {
|
|
|
|
return throwUnlessNotFound(error);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function* advanceMatch(
|
2020-04-16 05:40:30 +00:00
|
|
|
walkInfo: WalkEntry,
|
2020-07-14 19:24:17 +00:00
|
|
|
globSegment: string,
|
2020-04-16 05:40:30 +00:00
|
|
|
): IterableIterator<WalkEntry> {
|
2020-04-29 20:00:31 +00:00
|
|
|
if (!walkInfo.isDirectory) {
|
2019-10-02 17:59:27 +00:00
|
|
|
return;
|
2023-08-25 09:04:43 +00:00
|
|
|
} else if (globSegment === "..") {
|
2020-04-29 20:00:31 +00:00
|
|
|
const parentPath = joinGlobs([walkInfo.path, ".."], globOptions);
|
2019-10-02 17:59:27 +00:00
|
|
|
try {
|
2019-12-12 00:42:21 +00:00
|
|
|
if (shouldInclude(parentPath)) {
|
2022-06-17 14:18:53 +00:00
|
|
|
return yield createWalkEntrySync(parentPath);
|
2019-12-12 00:42:21 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throwUnlessNotFound(error);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
2019-12-12 00:42:21 +00:00
|
|
|
return;
|
2023-08-25 09:04:43 +00:00
|
|
|
} else if (globSegment === "**") {
|
2022-10-18 14:40:00 +00:00
|
|
|
return yield* walkSync(walkInfo.path, {
|
|
|
|
skip: excludePatterns,
|
|
|
|
maxDepth: globstar ? Infinity : 1,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2023-10-16 13:32:15 +00:00
|
|
|
canonicalize,
|
2022-10-18 14:40:00 +00:00
|
|
|
});
|
2021-10-17 18:04:24 +00:00
|
|
|
}
|
|
|
|
const globPattern = globToRegExp(globSegment, globOptions);
|
|
|
|
for (
|
|
|
|
const walkEntry of walkSync(walkInfo.path, {
|
|
|
|
maxDepth: 1,
|
2020-03-28 17:03:49 +00:00
|
|
|
skip: excludePatterns,
|
2023-03-06 07:35:44 +00:00
|
|
|
followSymlinks,
|
2021-10-17 18:04:24 +00:00
|
|
|
})
|
|
|
|
) {
|
|
|
|
if (
|
2023-08-25 09:04:43 +00:00
|
|
|
walkEntry.path !== walkInfo.path &&
|
2022-10-18 14:40:00 +00:00
|
|
|
walkEntry.name.match(globPattern)
|
2021-10-17 18:04:24 +00:00
|
|
|
) {
|
|
|
|
yield walkEntry;
|
|
|
|
}
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 05:40:30 +00:00
|
|
|
let currentMatches: WalkEntry[] = [fixedRootInfo];
|
2019-10-02 17:59:27 +00: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 20:00:31 +00:00
|
|
|
const nextMatchMap: Map<string, WalkEntry> = new Map();
|
2019-10-02 17:59:27 +00:00
|
|
|
for (const currentMatch of currentMatches) {
|
|
|
|
for (const nextMatch of advanceMatch(currentMatch, segment)) {
|
2020-04-29 20:00:31 +00:00
|
|
|
nextMatchMap.set(nextMatch.path, nextMatch);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 20:00:31 +00:00
|
|
|
currentMatches = [...nextMatchMap.values()].sort(comparePath);
|
2019-10-02 17:59:27 +00:00
|
|
|
}
|
2022-10-18 14:40:00 +00:00
|
|
|
|
2019-10-02 17:59:27 +00:00
|
|
|
if (hasTrailingSep) {
|
2020-04-16 05:40:30 +00:00
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 19:24:17 +00:00
|
|
|
(entry: WalkEntry): boolean => entry.isDirectory,
|
2019-10-02 17:59:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!includeDirs) {
|
|
|
|
currentMatches = currentMatches.filter(
|
2020-07-14 19:24:17 +00:00
|
|
|
(entry: WalkEntry): boolean => !entry.isDirectory,
|
2019-10-02 17:59:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
yield* currentMatches;
|
2019-09-28 13:33:17 +00:00
|
|
|
}
|