std/fs
2022-11-15 15:00:59 +09:00
..
testdata fix(fs/expand_glob): don't parse root as glob (#1417) 2021-10-18 03:04:24 +09:00
_util_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
_util.ts feat(fs): support URL params (#2573) 2022-08-30 12:58:48 +09:00
copy_test.ts refactor(fs): remove exists and existsSync usage in std/fs (#2546) 2022-08-24 13:28:54 +09:00
copy.ts refactor: use _util/asserts.ts for non-test code (#2857) 2022-11-10 16:28:20 +09:00
empty_dir_test.ts refactor: replace Deno.spawn with Deno.Command (#2886) 2022-11-15 15:00:59 +09:00
empty_dir.ts feat(fs): support URL params (#2573) 2022-08-30 12:58:48 +09:00
ensure_dir_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
ensure_dir.ts feat(fs): support URL params (#2573) 2022-08-30 12:58:48 +09:00
ensure_file_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
ensure_file.ts feat(fs): support URL params (#2573) 2022-08-30 12:58:48 +09:00
ensure_link_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
ensure_link.ts feat(fs): support URL params (#2573) 2022-08-30 12:58:48 +09:00
ensure_symlink_test.ts fix(fs): ensureSymlink works when symlink already exists (#2642) 2022-09-14 19:44:51 +09:00
ensure_symlink.ts fix(fs): ensureSymlink works when symlink already exists (#2642) 2022-09-14 19:44:51 +09:00
eol_test.ts chore: remove void and Promise<void> return types (#2550) 2022-08-24 10:21:57 +09:00
eol.ts docs: add/improve JSDoc comments for various modules (#2764) 2022-10-11 11:29:29 -04:00
exists_test.ts refactor: replace Deno.spawn with Deno.Command (#2886) 2022-11-15 15:00:59 +09:00
exists.ts docs(fs): document the deprecation of exists and existsSync (#2730) 2022-10-03 17:52:55 +09:00
expand_glob_test.ts refactor: replace Deno.spawn with Deno.Command (#2886) 2022-11-15 15:00:59 +09:00
expand_glob.ts refactor: use _util/asserts.ts for non-test code (#2857) 2022-11-10 16:28:20 +09:00
mod.ts docs: improve module documentation (#2511) 2022-08-11 21:51:20 +10:00
move_test.ts refactor(fs): remove exists and existsSync usage in std/fs (#2546) 2022-08-24 13:28:54 +09:00
move.ts fix(fs): use Deno.errors.AlreadyExists where appropriate (#2547) 2022-08-31 16:04:53 +09:00
README.md docs(fs): document the deprecation of exists and existsSync (#2730) 2022-10-03 17:52:55 +09:00
test.ts chore: update copyright header (#1871) 2022-02-02 23:21:39 +09:00
walk_test.ts refactor: replace Deno.spawn with Deno.Command (#2886) 2022-11-15 15:00:59 +09:00
walk.ts refactor: use _util/asserts.ts for non-test code (#2857) 2022-11-10 16:28:20 +09:00

fs

fs module is made to provide helpers to manipulate the filesystem.

Usage

Most of the following modules are exposed in mod.ts. This feature is currently unstable. To enable it use deno run --unstable.

emptyDir

Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted.

import {
  emptyDir,
  emptyDirSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

emptyDir("./foo"); // returns a promise
emptyDirSync("./foo"); // void

ensureDir

Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p.

import {
  ensureDir,
  ensureDirSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

ensureDir("./bar"); // returns a promise
ensureDirSync("./ensureDirSync"); // void

ensureFile

Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED.

import {
  ensureFile,
  ensureFileSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

ensureFile("./folder/targetFile.dat"); // returns promise
ensureFileSync("./folder/targetFile.dat"); // void

Ensures that the link exists. If the directory structure does not exist, it is created.

import {
  ensureSymlink,
  ensureSymlinkSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

ensureSymlink("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // returns promise
ensureSymlinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // void

EOL

Detects and format the passed string for the targeted End Of Line character.

import {
  detect,
  EOL,
  format,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

const CRLFinput = "deno\r\nis not\r\nnode";
const Mixedinput = "deno\nis not\r\nnode";
const LFinput = "deno\nis not\nnode";
const NoNLinput = "deno is not node";

detect(LFinput); // output EOL.LF
detect(CRLFinput); // output EOL.CRLF
detect(Mixedinput); // output EOL.CRLF
detect(NoNLinput); // output null

format(CRLFinput, EOL.LF); // output "deno\nis not\nnode"

exists

Test whether or not the given path exists by checking with the file system.

import {
  exists,
  existsSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

exists("./foo.txt"); // resolves a boolean
existsSync("./foo.txt"); // returns a boolean

Note: do not use this function if performing a check before another operation on that file. Doing so causes a race condition. Instead, perform the actual file operation directly.

Bad:

import {
  exists,
  existsSync,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

if (await exists("./foo.txt")) {
  await Deno.remove("./foo.txt");
}

// OR

if (existsSync("./foo.txt")) {
  Deno.removeSync("./foo.txt");
}

Good:

// Notice no use of exists or existsSync
try {
  await Deno.remove("./foo.txt");
} catch (error) {
  if (!(error instanceof Deno.errors.NotFound)) {
    throw error;
  }
  // Do nothing...
}

// OR

try {
  Deno.removeSync("./foo.txt");
} catch (error) {
  if (!(error instanceof Deno.errors.NotFound)) {
    throw error;
  }
  // Do nothing...
}

move

Moves a file or directory. Overwrites it if option provided.

import { move, moveSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

move("./foo", "./bar"); // returns a promise
moveSync("./foo", "./bar"); // void
moveSync("./foo", "./existingFolder", { overwrite: true });
// Will overwrite existingFolder

copy

copy a file or directory. Overwrites it if option provided.

import { copy, copySync } from "https://deno.land/std@$STD_VERSION/fs/copy.ts";

copy("./foo", "./bar"); // returns a promise
copySync("./foo", "./bar"); // void
copySync("./foo", "./existingFolder", { overwrite: true });
// Will overwrite existingFolder

walk

Iterate all files in a directory recursively.

import { walk, walkSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

for (const entry of walkSync(".")) {
  console.log(entry.path);
}

// Async
async function printFilesNames() {
  for await (const entry of walk(".")) {
    console.log(entry.path);
  }
}

printFilesNames().then(() => console.log("Done!"));

expandGlob

Expand the glob string from the specified root directory and yield each result as a WalkEntry object.

import { expandGlob } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

for await (const file of expandGlob("**/*.ts")) {
  console.log(file);
}

expandGlobSync

Synchronous version of expandGlob().

import { expandGlobSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

for (const file of expandGlobSync("**/*.ts")) {
  console.log(file);
}