1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00
denoland-deno/std/fs
Akshat Agarwal b8a5c29bf8
BREAKING CHANGE Rename Deno.run's args to cmd (#4444)
This is to avoid confusion with Deno.args which does not include the 
executable to be run.
2020-03-21 17:44:18 -04:00
..
testdata feat: Deno.args now does not include script (#3628) 2020-01-09 11:37:01 -07:00
copy.ts Rename readDir -> readdir (#4225) 2020-03-06 08:34:02 -05:00
copy_test.ts Add require-await lint rule (#4401) 2020-03-20 09:38:34 -04:00
empty_dir.ts Rename readDir -> readdir (#4225) 2020-03-06 08:34:02 -05:00
empty_dir_test.ts BREAKING CHANGE Rename Deno.run's args to cmd (#4444) 2020-03-21 17:44:18 -04:00
ensure_dir.ts rename Deno.Err -> Deno.errors (#4093) 2020-02-24 15:48:35 -05:00
ensure_dir_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
ensure_file.ts rename Deno.Err -> Deno.errors (#4093) 2020-02-24 15:48:35 -05:00
ensure_file_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
ensure_link.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
ensure_link_test.ts refactor: remove unneeded ErrorKinds (#3936) 2020-02-21 10:36:13 -05:00
ensure_symlink.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
ensure_symlink_test.ts use prebuilt "not implemented" error (#4442) 2020-03-20 12:10:02 -04:00
eol.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
eol_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
exists.ts Add require-await lint rule (#4401) 2020-03-20 09:38:34 -04:00
exists_test.ts BREAKING CHANGE Rename Deno.run's args to cmd (#4444) 2020-03-21 17:44:18 -04:00
expand_glob.ts rename Deno.Err -> Deno.errors (#4093) 2020-02-24 15:48:35 -05:00
expand_glob_test.ts BREAKING CHANGE Rename Deno.run's args to cmd (#4444) 2020-03-21 17:44:18 -04:00
mod.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
move.ts refactor: Cleanup options object parameters (#4296) 2020-03-10 12:08:58 -04:00
move_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
read_file_str.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
read_file_str_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
read_json.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
read_json_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
README.md Run deno_std tests in github actions 2019-10-09 17:22:22 -04:00
utils.ts std: Move fs/path to the top-level (#3100) 2019-10-16 14:39:33 -04:00
utils_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
walk.ts Rename readDir -> readdir (#4225) 2020-03-06 08:34:02 -05:00
walk_test.ts Add require-await lint rule (#4401) 2020-03-20 09:38:34 -04:00
write_file_str.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
write_file_str_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
write_json.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
write_json_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00

fs

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

Usage

All the following modules are exposed in mod.ts

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",
  "file"
); // returns promise
ensureSymlinkSync(
  "./folder/targetFile.dat",
  "./folder/targetFile.link.dat",
  "file"
); // 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

globToRegExp

Generate a regex based on glob pattern and options This was meant to be using the the fs.walk function but can be used anywhere else.

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

globToRegExp("foo/**/*.json", {
  flags: "g",
  extended: true,
  globstar: true
}); // returns the regex to find all .json files in the folder foo

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");

walk

Iterate all files in a directory recursively.

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

for (const fileInfo of walkSync(".")) {
  console.log(fileInfo.filename);
}

// Async
async function printFilesNames() {
  for await (const fileInfo of walk()) {
    console.log(fileInfo.filename);
  }
}

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

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.
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

readFileStr

Read file and output it as a string.

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" }); // void

writeFileStr

Write the string to file.

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