1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
denoland-deno/fs
Ryan Dahl 3e9c7918c9 v0.18.0 (denoland/deno_std#600)
Several tests were disabled in order to land this update.
Original: a8f6cf7b4f
2019-09-18 13:17:03 -04:00
..
path chore: Implement strict mode (denoland/deno_std#453) 2019-05-30 08:59:30 -04:00
testdata feat(fs): add copy/copySync (denoland/deno_std#278) 2019-05-16 12:19:17 -04:00
copy.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
copy_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
empty_dir.ts fix emptydir on windows (denoland/deno_std#531) 2019-07-08 11:09:23 -04:00
empty_dir_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
ensure_dir.ts fix: ensure exists file/dir must be the same type or it will throw error (denoland/deno_std#294) 2019-04-07 04:01:23 +03:00
ensure_dir_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
ensure_file.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
ensure_file_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
ensure_link.ts fs: fix ensureLink broken (denoland/deno_std#360) 2019-04-27 10:14:56 -07:00
ensure_link_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
ensure_symlink.ts fs: Remove completed todo item (denoland/deno_std#406) 2019-05-14 15:11:57 -04:00
ensure_symlink_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
eol.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
eol_test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
exists.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
exists_test.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
glob.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
glob_test.ts Added isGlob function (denoland/deno_std#433) 2019-05-23 10:23:31 +03:00
globrex.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
globrex_test.ts v0.18.0 (denoland/deno_std#600) 2019-09-18 13:17:03 -04:00
mod.ts feat(fs): add copy/copySync (denoland/deno_std#278) 2019-05-16 12:19:17 -04:00
move.ts remove unnecessary path.resolve in move/readJson/writeJson (denoland/deno_std#292) 2019-03-19 13:23:22 -04:00
move_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
path.ts Add missiong copyright headers (denoland/deno_std#177) 2019-02-07 11:45:47 -05:00
read_file_str.ts Added read file str (denoland/deno_std#276) 2019-04-13 15:26:09 -04:00
read_file_str_test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
read_json.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
read_json_test.ts bump prettier to 1.18.2 (denoland/deno_std#592) 2019-09-11 23:31:23 -04:00
README.md fix wrong field name in example code (denoland/deno_std#490) 2019-06-14 07:57:30 -07:00
utils.ts lint: add max line length rules (denoland/deno_std#507) 2019-06-18 21:22:01 -07:00
utils_test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
walk.ts Add includeDirs to WalkOptions (denoland/deno_std#601) 2019-09-18 11:37:37 -04:00
walk_test.ts Add includeDirs to WalkOptions (denoland/deno_std#601) 2019-09-18 11:37:37 -04:00
write_file_str.ts add writeFileStr and update documentation (denoland/deno_std#340) 2019-04-14 16:14:57 -04:00
write_file_str_test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04:00
write_json.ts remove unnecessary path.resolve in move/readJson/writeJson (denoland/deno_std#292) 2019-03-19 13:23:22 -04:00
write_json_test.ts Eslint fixes (denoland/deno_std#356) 2019-04-24 07:41:22 -04: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

glob

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

glob("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