1 | # async-file
|
2 | Adapts the Node.js File System API (fs) for use with TypeScript async/await
|
3 |
|
4 | This package wraps the Node.js [File System API](https://nodejs.org/api/fs.html), replacing any callback functions with an equivalent async function.
|
5 | Alternative definitions of some functions are also introduced to enhance the interface and/or take better advantage of TypeScript async/await.
|
6 | Other than the modified async function signatures and introduced functions, the interface is virtually identical to the original API.
|
7 |
|
8 | ## Examples
|
9 |
|
10 | Read a text file...
|
11 | ```js
|
12 | var result = await File.readFile('readme.txt', 'utf8');
|
13 | ```
|
14 |
|
15 | Read a series of three text files, one at a time...
|
16 | ```js
|
17 | var data1 = await File.readTextFile('data1.txt');
|
18 | var data2 = await File.readTextFile('data2.txt');
|
19 | var data3 = await File.readTextFile('data3.txt');
|
20 | ```
|
21 |
|
22 | Append a line into an arbitrary series of text files...
|
23 | ```js
|
24 | var files = ['data1.log', 'data2.log', 'data3.log'];
|
25 | for (var file of files)
|
26 | await File.writeTextFile(file, '\nPASSED!\n', null, File.OpenFlags.append);
|
27 | ```
|
28 |
|
29 | ## Getting Started
|
30 |
|
31 | Make sure you're running Node v4 and TypeScript 1.7 or higher...
|
32 | ```
|
33 | $ node -v
|
34 | v4.2.6
|
35 | $ npm install -g typescript
|
36 | $ tsc -v
|
37 | Version 1.7.5
|
38 | ```
|
39 |
|
40 | Install package...
|
41 | ```
|
42 | $ npm install async-file
|
43 | ```
|
44 |
|
45 | Write some code...
|
46 | ```js
|
47 | import * as File from 'async-file';
|
48 | (async function () {
|
49 | var list = await File.readdir('.');
|
50 | console.log(list);
|
51 | })();
|
52 | ```
|
53 |
|
54 | Save the above to a file (index.ts), build and run it!
|
55 | ```
|
56 | $ tsc index.ts --target es6 --module commonjs
|
57 | $ node index.js
|
58 | ["index.ts", "index.js", "node_modules"]
|
59 | ```
|