2019-04-13 15:30:56 -04:00
|
|
|
# fs
|
|
|
|
|
|
|
|
fs module is made to provide helpers to manipulate the filesystem.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2020-11-02 12:33:13 -05:00
|
|
|
Most of the following modules are exposed in `mod.ts`. This feature is currently
|
2020-10-03 16:19:11 -04:00
|
|
|
<b>unstable</b>. To enable it use `deno run --unstable`.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
### emptyDir
|
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
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.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
emptyDir,
|
|
|
|
emptyDirSync,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
emptyDir("./foo"); // returns a promise
|
|
|
|
emptyDirSync("./foo"); // void
|
|
|
|
```
|
|
|
|
|
|
|
|
### ensureDir
|
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
Ensures that the directory exists. If the directory structure does not exist, it
|
2020-10-03 16:19:11 -04:00
|
|
|
is created. Like `mkdir -p`.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
ensureDir,
|
|
|
|
ensureDirSync,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
ensureDir("./bar"); // returns a promise
|
|
|
|
ensureDirSync("./ensureDirSync"); // void
|
|
|
|
```
|
|
|
|
|
2019-04-14 10:53:19 -04:00
|
|
|
### ensureFile
|
2019-04-13 15:30:56 -04:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
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**.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
ensureFile,
|
|
|
|
ensureFileSync,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
ensureFile("./folder/targetFile.dat"); // returns promise
|
|
|
|
ensureFileSync("./folder/targetFile.dat"); // void
|
|
|
|
```
|
|
|
|
|
2019-04-21 15:23:44 -04:00
|
|
|
### ensureSymlink
|
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
Ensures that the link exists. If the directory structure does not exist, it is
|
|
|
|
created.
|
2019-04-21 15:23:44 -04:00
|
|
|
|
|
|
|
```ts
|
|
|
|
import {
|
|
|
|
ensureSymlink,
|
2020-03-28 13:03:49 -04:00
|
|
|
ensureSymlinkSync,
|
2020-10-04 08:18:36 -04:00
|
|
|
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-21 15:23:44 -04:00
|
|
|
|
2020-04-27 07:03:13 -04:00
|
|
|
ensureSymlink("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // returns promise
|
|
|
|
ensureSymlinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // void
|
2019-04-21 15:23:44 -04:00
|
|
|
```
|
|
|
|
|
2020-05-17 13:24:39 -04:00
|
|
|
### EOL
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
Detects and format the passed string for the targeted End Of Line character.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { format, detect, EOL } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
Test whether or not the given path exists by checking with the file system.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import {
|
|
|
|
exists,
|
|
|
|
existsSync,
|
|
|
|
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
exists("./foo"); // returns a Promise<boolean>
|
|
|
|
existsSync("./foo"); // returns boolean
|
|
|
|
```
|
|
|
|
|
|
|
|
### move
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
Moves a file or directory. Overwrites it if option provided.
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { move, moveSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
|
|
|
move("./foo", "./bar"); // returns a promise
|
|
|
|
moveSync("./foo", "./bar"); // void
|
|
|
|
moveSync("./foo", "./existingFolder", { overwrite: true });
|
|
|
|
// Will overwrite existingFolder
|
|
|
|
```
|
|
|
|
|
2019-05-16 12:19:17 -04:00
|
|
|
### copy
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
copy a file or directory. Overwrites it if option provided.
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { copy, copySync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-05-16 12:19:17 -04:00
|
|
|
|
|
|
|
copy("./foo", "./bar"); // returns a promise
|
|
|
|
copySync("./foo", "./bar"); // void
|
|
|
|
copySync("./foo", "./existingFolder", { overwrite: true });
|
|
|
|
// Will overwrite existingFolder
|
|
|
|
```
|
|
|
|
|
2019-04-13 15:30:56 -04:00
|
|
|
### walk
|
|
|
|
|
|
|
|
Iterate all files in a directory recursively.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { walk, walkSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2019-04-13 15:30:56 -04:00
|
|
|
|
2020-06-08 17:09:38 -04:00
|
|
|
for (const entry of walkSync(".")) {
|
|
|
|
console.log(entry.path);
|
2019-04-13 15:30:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Async
|
2019-06-14 10:57:30 -04:00
|
|
|
async function printFilesNames() {
|
2020-05-20 12:24:02 -04:00
|
|
|
for await (const entry of walk(".")) {
|
2020-04-29 16:00:31 -04:00
|
|
|
console.log(entry.path);
|
2019-05-01 17:45:30 -04:00
|
|
|
}
|
2019-04-13 15:30:56 -04:00
|
|
|
}
|
2019-05-01 17:45:30 -04:00
|
|
|
|
2019-06-14 10:57:30 -04:00
|
|
|
printFilesNames().then(() => console.log("Done!"));
|
2019-04-13 15:30:56 -04:00
|
|
|
```
|
|
|
|
|
2020-06-20 14:34:28 -04:00
|
|
|
### expandGlob
|
|
|
|
|
|
|
|
Expand the glob string from the specified `root` directory and yield each result
|
|
|
|
as a `WalkEntry` object.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { expandGlob } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2020-06-20 14:34:28 -04:00
|
|
|
|
|
|
|
for await (const file of expandGlob("**/*.ts")) {
|
|
|
|
console.log(file);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### expandGlobSync
|
|
|
|
|
|
|
|
Synchronous version of `expandGlob()`.
|
|
|
|
|
|
|
|
```ts
|
2020-10-04 08:18:36 -04:00
|
|
|
import { expandGlobSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
2020-06-20 14:34:28 -04:00
|
|
|
|
|
|
|
for (const file of expandGlobSync("**/*.ts")) {
|
|
|
|
console.log(file);
|
|
|
|
}
|
|
|
|
```
|