std/fs
2021-02-01 10:46:58 +00:00
..
testdata Update to Prettier 2 and use ES Private Fields (denoland/deno#4498) 2021-02-01 10:46:57 +00:00
_util_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
_util.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
copy_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
copy.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
empty_dir_test.ts test: remove unstable from multiple tests (denoland/deno#6882) 2021-02-01 10:46:58 +00:00
empty_dir.ts refactor: Don't destructure the Deno namespace (denoland/deno#6268) 2021-02-01 10:46:58 +00:00
ensure_dir_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_dir.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_file_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_file.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_link_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_link.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_symlink_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
ensure_symlink.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
eol_test.ts Update to Prettier 2 and use ES Private Fields (denoland/deno#4498) 2021-02-01 10:46:57 +00:00
eol.ts Update to Prettier 2 and use ES Private Fields (denoland/deno#4498) 2021-02-01 10:46:57 +00:00
exists_test.ts test: remove unstable from multiple tests (denoland/deno#6882) 2021-02-01 10:46:58 +00:00
exists.ts refactor: Don't destructure the Deno namespace (denoland/deno#6268) 2021-02-01 10:46:58 +00:00
expand_glob_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
expand_glob.ts refactor: Rewrite globToRegExp() (denoland/deno#6963) 2021-02-01 10:46:58 +00:00
mod.ts fix(fs): remove unexisted imports (denoland/deno#6850) 2021-02-01 10:46:58 +00:00
move_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
move.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
read_json_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
read_json.ts Happy new year! (denoland/deno#3578) 2021-02-01 10:46:56 +00:00
README.md docs(fs): provide more context on unstable perm (denoland/deno#6748) 2021-02-01 10:46:58 +00:00
test.ts fix(fs): remove unexisted imports (denoland/deno#6850) 2021-02-01 10:46:58 +00:00
walk_test.ts Use dprint for internal formatting (denoland/deno#6682) 2021-02-01 10:46:58 +00:00
walk.ts docs(fs/walk): fix code example (denoland/deno#7166) 2021-02-01 10:46:58 +00:00
write_json_test.ts test: fs/writeJson add test for append option (denoland/deno#6889) 2021-02-01 10:46:58 +00:00
write_json.ts fix(json): Add newline at the end of json files (denoland/deno#6885) 2021-02-01 10:46:58 +00:00

fs

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

Usage

Most 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/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/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/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/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 { format, detect, EOL } from "https://deno.land/std/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/fs/mod.ts";

exists("./foo"); // returns a Promise<boolean>
existsSync("./foo"); // returns boolean

move

Moves a file or directory. Overwrites it if option provided

import { move, moveSync } from "https://deno.land/std/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/fs/mod.ts";

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

readJson

Reads a JSON file and then parses it into an object

import { readJson, readJsonSync } from "https://deno.land/std/fs/mod.ts";

const f = await readJson("./foo.json");
const foo = readJsonSync("./foo.json");

writeJson

Writes an object to a JSON file.

WriteJsonOptions

  • replacer : An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
  • space : Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

You can also specify options from Deno.WriteFileOptions to configure how the file is written.

import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts";

writeJson("./target.dat", { foo: "bar" }, { spaces: 2 }); // returns a promise
writeJsonSync("./target.dat", { foo: "bar" }, { replacer: ["foo"] }); // void

// appends to the file instead of rewriting
writeJsonSync("./target.dat", { foo: "bar" }, { append: true });

walk

Iterate all files in a directory recursively.

import { walk, walkSync } from "https://deno.land/std/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!"));

readFileStr

Read file and output it as a string. Note: this module does not require the --unstable flag.

ReadOptions

  • encoding : The encoding to read file. lowercased.
import { readFileStr, readFileStrSync } from "https://deno.land/std/fs/mod.ts";

readFileStr("./target.dat", { encoding: "utf8" }); // returns a promise
readFileStrSync("./target.dat", { encoding: "utf8" }); // string

writeFileStr

Write the string to file. Note: this module does not require the --unstable flag.

import {
  writeFileStr,
  writeFileStrSync,
} from "https://deno.land/std/fs/mod.ts";

writeFileStr("./target.dat", "file content"); // returns a promise
writeFileStrSync("./target.dat", "file content"); // void

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/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/fs/mod.ts";

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