Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(fs): complete documentation #3897

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions fs/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
: undefined;
}

/**
* Walk entry for {@linkcode walk}, {@linkcode walkSync},
* {@linkcode expandGlob} and {@linkcode expandGlobSync}.
*/
export interface WalkEntry extends Deno.DirEntry {
/** Full path of the entry. */
path: string;
}

/** Create WalkEntry for the `path` synchronously */
/** Create {@linkcode WalkEntry} for the `path` synchronously. */
export function createWalkEntrySync(path: string | URL): WalkEntry {
path = toPathString(path);
path = normalize(path);
Expand All @@ -75,7 +80,7 @@ export function createWalkEntrySync(path: string | URL): WalkEntry {
};
}

/** Create WalkEntry for the `path` asynchronously */
/** Create {@linkcode WalkEntry} for the `path` asynchronously. */
export async function createWalkEntry(path: string | URL): Promise<WalkEntry> {
path = toPathString(path);
path = normalize(path);
Expand Down
1 change: 1 addition & 0 deletions fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { assert } from "../assert/assert.ts";

const isWindows = Deno.build.os === "windows";

/** Options for {@linkcode copy} and {@linkcode copySync}. */
export interface CopyOptions {
/**
* overwrite existing file or directory.
Expand Down
4 changes: 2 additions & 2 deletions fs/ensure_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { getFileInfoType, toPathString } from "./_util.ts";
* ensureFile("./folder/targetFile.dat"); // returns promise
* ```
*/
export async function ensureFile(filePath: string | URL) {
export async function ensureFile(filePath: string | URL): Promise<void> {
try {
// if file exists
const stat = await Deno.lstat(filePath);
Expand Down Expand Up @@ -56,7 +56,7 @@ export async function ensureFile(filePath: string | URL) {
* ensureFileSync("./folder/targetFile.dat"); // void
* ```
*/
export function ensureFileSync(filePath: string | URL) {
export function ensureFileSync(filePath: string | URL): void {
try {
// if file exists
const stat = Deno.lstatSync(filePath);
Expand Down
4 changes: 2 additions & 2 deletions fs/eol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

// End-of-line character for POSIX platforms such as macOS and Linux.
/** End-of-line character for POSIX platforms such as macOS and Linux. */
export const LF = "\n" as const;

/** End-of-line character for Windows platforms. */
Expand All @@ -16,7 +16,7 @@
* EOL; // Returns "\n" on POSIX platforms or "\r\n" on Windows
* ```
*/
export const EOL = Deno?.build.os === "windows" ? CRLF : LF;
export const EOL: "\n" | "\r\n" = Deno?.build.os === "windows" ? CRLF : LF;

Check warning on line 19 in fs/eol.ts

View check run for this annotation

Codecov / codecov/patch

fs/eol.ts#L19

Added line #L19 was not covered by tests

const regDetect = /(?:\r?\n)/g;

Expand Down
1 change: 1 addition & 0 deletions fs/exists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

/** Options for {@linkcode exists} and {@linkcode existsSync.} */
export interface ExistsOptions {
/**
* When `true`, will check if the path is readable by the user as well.
Expand Down
15 changes: 15 additions & 0 deletions fs/expand_glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@ import {
toPathString,
WalkEntry,
} from "./_util.ts";
export type { GlobOptions };

const isWindows = Deno.build.os === "windows";

/** Options for {@linkcode expandGlob} and {@linkcode expandGlobSync}. */
export interface ExpandGlobOptions extends Omit<GlobOptions, "os"> {
/** File path where to expand from. */
root?: string;
/** List of glob patterns to be excluded from the expansion. */
exclude?: string[];
/**
* Whether to include directories in entries.
*
* @default {true}
*/
includeDirs?: boolean;
/**
* Whether to follow symbolic links.
*
* @default {false}
*/
followSymlinks?: boolean;
/**
* Indicates whether the followed symlink's path should be canonicalized.
* This option works only if `followSymlinks` is not `false`.
*
* @default {true}
*/
canonicalize?: boolean;
Expand Down
18 changes: 15 additions & 3 deletions fs/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ import { isSamePath, isSubdir } from "./_util.ts";

const EXISTS_ERROR = new Deno.errors.AlreadyExists("dest already exists.");

/**
* Error thrown in {@linkcode move} or {@linkcode moveSync} when the
* destination is a subdirectory of the source.
*/
export class SubdirectoryMoveError extends Error {
/** Constructs a new instance. */
constructor(src: string | URL, dest: string | URL) {
super(
`Cannot move '${src}' to a subdirectory of itself, '${dest}'.`,
);
}
}

interface MoveOptions {
/** Options for {@linkcode move} and {@linkcode moveSync}. */
export interface MoveOptions {
/**
* Whether the destination file should be overwritten if it already exists.
*
* @default {false}
*/
overwrite?: boolean;
}

Expand All @@ -29,7 +40,7 @@ export async function move(
src: string | URL,
dest: string | URL,
{ overwrite = false }: MoveOptions = {},
) {
): Promise<void> {
const srcStat = await Deno.stat(src);

if (
Expand Down Expand Up @@ -62,6 +73,7 @@ export async function move(

/**
* Moves a file or directory synchronously.
*
* @example
* ```ts
* import { moveSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
Expand All @@ -73,7 +85,7 @@ export function moveSync(
src: string | URL,
dest: string | URL,
{ overwrite = false }: MoveOptions = {},
) {
): void {
const srcStat = Deno.statSync(src);

if (
Expand Down
15 changes: 9 additions & 6 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
WalkEntry,
} from "./_util.ts";

/** Error thrown in {@linkcode walk} or {@linkcode walkSync} during iteration. */
export class WalkError extends Error {
override cause: unknown;
override name = "WalkError";
path: string;
/** File path of the root that's being walked. */
root: string;

constructor(cause: unknown, path: string) {
/** Constructs a new instance. */
constructor(cause: unknown, root: string) {
super(
`${cause instanceof Error ? cause.message : cause} for path "${path}"`,
`${cause instanceof Error ? cause.message : cause} for path "${root}"`,

Check warning on line 22 in fs/walk.ts

View check run for this annotation

Codecov / codecov/patch

fs/walk.ts#L22

Added line #L22 was not covered by tests
);
this.path = path;
this.cause = cause;
this.name = "WalkError";
this.root = root;
}
}

Expand All @@ -48,6 +50,7 @@
return new WalkError(err, root);
}

/** Options for {@linkcode walk} and {@linkcode walkSync}. */
export interface WalkOptions {
/**
* The maximum depth of the file tree to be walked recursively.
Expand Down
1 change: 1 addition & 0 deletions path/_common/glob_to_reg_exp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/** Options for {@linkcode globToRegExp}. */
export interface GlobOptions {
/** Extended glob syntax.
* See https://www.linuxjournal.com/content/bash-extended-globbing.
Expand Down