UNPKG

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