From aacece72dd6427588daaa65ec2894f31b1ad063a Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 26 Dec 2021 17:51:21 -0600 Subject: [PATCH] Add Read In The Contents Of A File as a Deno til --- README.md | 7 ++++++- deno/read-in-the-contents-of-a-file.md | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 deno/read-in-the-contents-of-a-file.md diff --git a/README.md b/README.md index c0e0e90..9ca9278 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1174 TILs and counting..._ +_1175 TILs and counting..._ --- @@ -21,6 +21,7 @@ _1174 TILs and counting..._ * [Chrome](#chrome) * [Clojure](#clojure) * [CSS](#css) +* [Deno](#deno) * [Devops](#devops) * [Elixir](#elixir) * [Gatsby](#gatsby) @@ -142,6 +143,10 @@ _1174 TILs and counting..._ - [Style A Background With A Linear Gradient](css/style-a-background-with-a-linear-gradient.md) - [Using Maps In SCSS](css/using-maps-in-scss.md) +### Deno + +- [Read In The Contents Of A File](deno/read-in-the-contents-of-a-file.md) + ### Devops - [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md) diff --git a/deno/read-in-the-contents-of-a-file.md b/deno/read-in-the-contents-of-a-file.md new file mode 100644 index 0000000..5a6dd5b --- /dev/null +++ b/deno/read-in-the-contents-of-a-file.md @@ -0,0 +1,23 @@ +# Read In The Contents Of A File + +Deno offers some nice utilities out of the box like reading in the contents of +a file from the filesystem. The `readTextFile` function is available on the +`Deno` object. + +```typescript +// Read a file using Deno +const text: string = await Deno.readTextFile("./first_input.txt"); +``` + +You use a top-level await with the function call and, assuming the file exists, +it will read the contents in. In this case, I assign them to the `text` +variable. + +For the file reading to work when the program is executed, you must use the +`--allow-read` flag. + +```bash +$ deno run --allow-read program.ts +``` + +[source](https://deno.land/manual@v1.14.0/examples/read_write_files)