1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 05:14:21 -05:00
denoland-deno/std/fs
Behnam Mohammadi a59f5eadd8
refactor(std): add std/_util/os.ts module (#8370)
This commit adds "std/_util/os.ts" module which contains
common os detection logic and is browser compatible.

Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
2020-11-14 00:21:11 +01:00
..
testdata Update to Prettier 2 and use ES Private Fields (#4498) 2020-03-28 13:03:49 -04:00
_util.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
_util_test.ts feat(fmt): Sort named import and export specifiers (#7711) 2020-09-27 12:22:32 +02:00
copy.ts refactor(std): add std/_util/os.ts module (#8370) 2020-11-14 00:21:11 +01:00
copy_test.ts feat(fmt): Sort named import and export specifiers (#7711) 2020-09-27 12:22:32 +02:00
empty_dir.ts refactor: Don't destructure the Deno namespace (#6268) 2020-06-12 15:23:38 -04:00
empty_dir_test.ts rename(std/testing): rename assert*Contains to assert*Includes (#7951) 2020-10-26 16:03:30 +01:00
ensure_dir.ts Use dprint for internal formatting (#6682) 2020-07-14 15:24:17 -04:00
ensure_dir_test.ts test(std/fs): make tests runnable from any directory (#7388) 2020-09-08 11:43:43 +02:00
ensure_file.ts Use dprint for internal formatting (#6682) 2020-07-14 15:24:17 -04:00
ensure_file_test.ts test(std/fs): make tests runnable from any directory (#7388) 2020-09-08 11:43:43 +02:00
ensure_link.ts Use dprint for internal formatting (#6682) 2020-07-14 15:24:17 -04:00
ensure_link_test.ts test(std/fs): make tests runnable from any directory (#7388) 2020-09-08 11:43:43 +02:00
ensure_symlink.ts refactor(std): add std/_util/os.ts module (#8370) 2020-11-14 00:21:11 +01:00
ensure_symlink_test.ts test(std/fs): make tests runnable from any directory (#7388) 2020-09-08 11:43:43 +02:00
eol.ts refactor(std/fs): improve performance by using some instead filter method (#8322) 2020-11-10 09:43:44 +11:00
eol_test.ts feat(fmt): Sort named import and export specifiers (#7711) 2020-09-27 12:22:32 +02:00
exists.ts refactor: Don't destructure the Deno namespace (#6268) 2020-06-12 15:23:38 -04:00
exists_test.ts rename(std/testing): rename assert*Contains to assert*Includes (#7951) 2020-10-26 16:03:30 +01:00
expand_glob.ts refactor(std): add std/_util/os.ts module (#8370) 2020-11-14 00:21:11 +01:00
expand_glob_test.ts rename(std/testing): rename assert*Contains to assert*Includes (#7951) 2020-10-26 16:03:30 +01:00
mod.ts BREAKING(std/fs): remove writeJson and writeJsonSync (#7256) 2020-09-07 19:12:24 +02:00
move.ts refactor(std): remove redundant code in move.ts (#8269) 2020-11-09 18:21:33 +01:00
move_test.ts test(std/fs): make tests runnable from any directory (#7388) 2020-09-08 11:43:43 +02:00
README.md docs: fix typo in std/fs README.md (#8104) 2020-11-02 18:33:13 +01:00
test.ts fix(std/fs): remove unexisted imports (#6850) 2020-07-23 00:09:20 -04:00
walk.ts fix(std/fs): mark createWalkEntry(Sync) as internal (#7643) 2020-09-23 14:09:09 -04:00
walk_test.ts feat(fmt): Sort named import and export specifiers (#7711) 2020-09-27 12:22:32 +02: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 { format, detect, EOL } 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"); // 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@$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/mod.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);
}