@ohos-ports/stdlib-fs 0.0.0-stage → 0.4.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +230 -2
- package/SECURITY.md +5 -0
- package/append-file/README.md +239 -0
- package/append-file/bin/cli +110 -0
- package/append-file/docs/repl.txt +72 -0
- package/append-file/docs/types/index.d.ts +133 -0
- package/append-file/docs/types/test.ts +193 -0
- package/append-file/docs/usage.txt +10 -0
- package/append-file/etc/cli_opts.json +22 -0
- package/append-file/lib/async.js +58 -0
- package/append-file/lib/index.js +61 -0
- package/append-file/lib/sync.js +58 -0
- package/append-file/package.json +69 -0
- package/close/README.md +172 -0
- package/close/docs/repl.txt +45 -0
- package/close/docs/types/index.d.ts +135 -0
- package/close/docs/types/test.ts +89 -0
- package/close/lib/index.js +73 -0
- package/close/lib/main.js +68 -0
- package/close/lib/sync.js +65 -0
- package/close/package.json +64 -0
- package/docs/types/index.d.ts +502 -0
- package/exists/README.md +252 -0
- package/exists/bin/cli +83 -0
- package/exists/docs/repl.txt +42 -0
- package/exists/docs/types/index.d.ts +112 -0
- package/exists/docs/types/test.ts +87 -0
- package/exists/docs/usage.txt +8 -0
- package/exists/etc/cli_opts.json +14 -0
- package/exists/lib/async.js +85 -0
- package/exists/lib/index.js +66 -0
- package/exists/lib/sync.js +62 -0
- package/exists/package.json +69 -0
- package/lib/index.js +186 -0
- package/open/README.md +204 -0
- package/open/docs/repl.txt +65 -0
- package/open/docs/types/index.d.ts +150 -0
- package/open/docs/types/test.ts +129 -0
- package/open/lib/defaults.json +4 -0
- package/open/lib/index.js +63 -0
- package/open/lib/main.js +64 -0
- package/open/lib/sync.js +68 -0
- package/open/package.json +64 -0
- package/package.json +67 -4
- package/read-dir/README.md +232 -0
- package/read-dir/bin/cli +83 -0
- package/read-dir/docs/repl.txt +44 -0
- package/read-dir/docs/types/index.d.ts +99 -0
- package/read-dir/docs/types/test.ts +85 -0
- package/read-dir/docs/usage.txt +8 -0
- package/read-dir/etc/cli_opts.json +14 -0
- package/read-dir/lib/async.js +57 -0
- package/read-dir/lib/index.js +62 -0
- package/read-dir/lib/sync.js +52 -0
- package/read-dir/package.json +69 -0
- package/read-file/README.md +246 -0
- package/read-file/bin/cli +91 -0
- package/read-file/docs/repl.txt +68 -0
- package/read-file/docs/types/index.d.ts +138 -0
- package/read-file/docs/types/test.ts +147 -0
- package/read-file/docs/usage.txt +10 -0
- package/read-file/etc/cli_opts.json +21 -0
- package/read-file/lib/index.js +61 -0
- package/read-file/lib/main.js +59 -0
- package/read-file/lib/sync.js +61 -0
- package/read-file/package.json +68 -0
- package/read-file-list/README.md +197 -0
- package/read-file-list/bin/cli +100 -0
- package/read-file-list/docs/repl.txt +81 -0
- package/read-file-list/docs/types/index.d.ts +166 -0
- package/read-file-list/docs/types/test.ts +166 -0
- package/read-file-list/docs/usage.txt +10 -0
- package/read-file-list/etc/cli_opts.json +21 -0
- package/read-file-list/lib/async.js +202 -0
- package/read-file-list/lib/clear_pending.js +47 -0
- package/read-file-list/lib/clear_timeout.js +23 -0
- package/read-file-list/lib/config.json +4 -0
- package/read-file-list/lib/delay.js +47 -0
- package/read-file-list/lib/index.js +66 -0
- package/read-file-list/lib/sync.js +94 -0
- package/read-file-list/package.json +72 -0
- package/read-json/README.md +176 -0
- package/read-json/docs/repl.txt +72 -0
- package/read-json/docs/types/index.d.ts +170 -0
- package/read-json/docs/types/test.ts +175 -0
- package/read-json/lib/async.js +114 -0
- package/read-json/lib/index.js +65 -0
- package/read-json/lib/sync.js +89 -0
- package/read-json/package.json +66 -0
- package/read-ndjson/README.md +166 -0
- package/read-ndjson/docs/repl.txt +72 -0
- package/read-ndjson/docs/types/index.d.ts +170 -0
- package/read-ndjson/docs/types/test.ts +179 -0
- package/read-ndjson/lib/async.js +114 -0
- package/read-ndjson/lib/index.js +65 -0
- package/read-ndjson/lib/sync.js +89 -0
- package/read-ndjson/package.json +68 -0
- package/read-wasm/README.md +158 -0
- package/read-wasm/docs/repl.txt +58 -0
- package/read-wasm/docs/types/index.d.ts +153 -0
- package/read-wasm/docs/types/test.ts +127 -0
- package/read-wasm/lib/async.js +106 -0
- package/read-wasm/lib/index.js +67 -0
- package/read-wasm/lib/sync.js +86 -0
- package/read-wasm/package.json +67 -0
- package/rename/README.md +259 -0
- package/rename/bin/cli +89 -0
- package/rename/docs/repl.txt +80 -0
- package/rename/docs/types/index.d.ts +109 -0
- package/rename/docs/types/test.ts +111 -0
- package/rename/docs/usage.txt +7 -0
- package/rename/etc/cli_opts.json +15 -0
- package/rename/lib/index.js +60 -0
- package/rename/lib/main.js +51 -0
- package/rename/lib/sync.js +53 -0
- package/rename/package.json +73 -0
- package/resolve-parent-path/README.md +204 -0
- package/resolve-parent-path/bin/cli +85 -0
- package/resolve-parent-path/docs/repl.txt +58 -0
- package/resolve-parent-path/docs/types/index.d.ts +128 -0
- package/resolve-parent-path/docs/types/test.ts +117 -0
- package/resolve-parent-path/docs/usage.txt +9 -0
- package/resolve-parent-path/etc/cli_opts.json +17 -0
- package/resolve-parent-path/lib/async.js +116 -0
- package/resolve-parent-path/lib/index.js +58 -0
- package/resolve-parent-path/lib/sync.js +83 -0
- package/resolve-parent-path/lib/validate.js +67 -0
- package/resolve-parent-path/package.json +78 -0
- package/resolve-parent-path-by/README.md +220 -0
- package/resolve-parent-path-by/docs/repl.txt +105 -0
- package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
- package/resolve-parent-path-by/docs/types/test.ts +225 -0
- package/resolve-parent-path-by/lib/async.js +156 -0
- package/resolve-parent-path-by/lib/index.js +67 -0
- package/resolve-parent-path-by/lib/sync.js +98 -0
- package/resolve-parent-path-by/lib/validate.js +67 -0
- package/resolve-parent-path-by/package.json +76 -0
- package/resolve-parent-paths/README.md +228 -0
- package/resolve-parent-paths/bin/cli +93 -0
- package/resolve-parent-paths/docs/repl.txt +84 -0
- package/resolve-parent-paths/docs/types/index.d.ts +145 -0
- package/resolve-parent-paths/docs/types/test.ts +196 -0
- package/resolve-parent-paths/docs/usage.txt +10 -0
- package/resolve-parent-paths/etc/cli_opts.json +18 -0
- package/resolve-parent-paths/lib/index.js +60 -0
- package/resolve-parent-paths/lib/main.js +415 -0
- package/resolve-parent-paths/lib/sync.js +231 -0
- package/resolve-parent-paths/lib/validate.js +81 -0
- package/resolve-parent-paths/package.json +78 -0
- package/unlink/README.md +233 -0
- package/unlink/bin/cli +81 -0
- package/unlink/docs/repl.txt +62 -0
- package/unlink/docs/types/index.d.ts +105 -0
- package/unlink/docs/types/test.ts +85 -0
- package/unlink/docs/usage.txt +7 -0
- package/unlink/etc/cli_opts.json +15 -0
- package/unlink/lib/index.js +60 -0
- package/unlink/lib/main.js +50 -0
- package/unlink/lib/sync.js +52 -0
- package/unlink/package.json +70 -0
- package/write-file/README.md +250 -0
- package/write-file/bin/cli +110 -0
- package/write-file/docs/repl.txt +74 -0
- package/write-file/docs/types/index.d.ts +133 -0
- package/write-file/docs/types/test.ts +193 -0
- package/write-file/docs/usage.txt +10 -0
- package/write-file/etc/cli_opts.json +22 -0
- package/write-file/lib/index.js +60 -0
- package/write-file/lib/main.js +58 -0
- package/write-file/lib/sync.js +58 -0
- package/write-file/package.json +68 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2018 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
# Write File
|
|
22
|
+
|
|
23
|
+
> Write data to a file.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var writeFile = require( '@stdlib/fs/write-file' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### writeFile( file, data\[, options], clbk )
|
|
34
|
+
|
|
35
|
+
Asynchronously writes `data` to a `file`.
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
var join = require( 'path' ).join;
|
|
39
|
+
|
|
40
|
+
var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
|
|
41
|
+
|
|
42
|
+
writeFile( fpath, 'beep boop\n', onWrite );
|
|
43
|
+
|
|
44
|
+
function onWrite( error ) {
|
|
45
|
+
if ( error ) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The `data` argument may be either a `string` or a [`Buffer`][@stdlib/buffer/ctor].
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
var join = require( 'path' ).join;
|
|
55
|
+
var string2buffer = require( '@stdlib/buffer/from-string' );
|
|
56
|
+
|
|
57
|
+
var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
|
|
58
|
+
|
|
59
|
+
writeFile( fpath, string2buffer( 'beep boop\n' ), onWrite );
|
|
60
|
+
|
|
61
|
+
function onWrite( error ) {
|
|
62
|
+
if ( error ) {
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The function accepts the same `options` and has the same defaults as [`fs.writeFile()`][node-fs].
|
|
69
|
+
|
|
70
|
+
#### writeFile.sync( file, data\[, options] )
|
|
71
|
+
|
|
72
|
+
Synchronously writes `data` to a `file`.
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
var join = require( 'path' ).join;
|
|
76
|
+
|
|
77
|
+
var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
|
|
78
|
+
|
|
79
|
+
var err = writeFile.sync( fpath, 'beep boop\n' );
|
|
80
|
+
if ( err instanceof Error ) {
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The function accepts the same `options` and has the same defaults as [`fs.writeFileSync()`][node-fs].
|
|
86
|
+
|
|
87
|
+
</section>
|
|
88
|
+
|
|
89
|
+
<!-- /.usage -->
|
|
90
|
+
|
|
91
|
+
<section class="notes">
|
|
92
|
+
|
|
93
|
+
## Notes
|
|
94
|
+
|
|
95
|
+
- The difference between this `writeFile.sync` and [`fs.writeFileSync()`][node-fs] is that [`fs.writeFileSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent directory path) and this API will return an `error`. Hence, the following anti-pattern
|
|
96
|
+
|
|
97
|
+
<!-- run-disable -->
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
var fs = require( 'fs' );
|
|
101
|
+
|
|
102
|
+
// Check for directory path existence to prevent an error being thrown...
|
|
103
|
+
if ( fs.existsSync( '/path/to' ) ) {
|
|
104
|
+
fs.writeFileSync( '/path/to/file.txt', 'beep boop\n' );
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
can be replaced by an approach which addresses existence via `error` handling.
|
|
109
|
+
|
|
110
|
+
<!-- run-disable -->
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
var writeFile = require( '@stdlib/fs/write-file' );
|
|
114
|
+
|
|
115
|
+
// Explicitly handle the error...
|
|
116
|
+
var err = writeFile.sync( '/path/to/file.txt', 'beep boop\n' );
|
|
117
|
+
if ( err instanceof Error ) {
|
|
118
|
+
// You choose what to do...
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
</section>
|
|
124
|
+
|
|
125
|
+
<!-- /.notes -->
|
|
126
|
+
|
|
127
|
+
<section class="examples">
|
|
128
|
+
|
|
129
|
+
## Examples
|
|
130
|
+
|
|
131
|
+
<!-- eslint no-undef: "error" -->
|
|
132
|
+
|
|
133
|
+
```javascript
|
|
134
|
+
var join = require( 'path' ).join;
|
|
135
|
+
var writeFile = require( '@stdlib/fs/write-file' );
|
|
136
|
+
|
|
137
|
+
var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
|
|
138
|
+
|
|
139
|
+
// Synchronously write data to a file:
|
|
140
|
+
var err = writeFile.sync( fpath, 'beep boop\n', 'utf8' );
|
|
141
|
+
// returns null
|
|
142
|
+
|
|
143
|
+
console.log( err instanceof Error );
|
|
144
|
+
// => false
|
|
145
|
+
|
|
146
|
+
// Asynchronously write data to a file:
|
|
147
|
+
writeFile( fpath, 'beep boop\n', onWrite );
|
|
148
|
+
|
|
149
|
+
function onWrite( error ) {
|
|
150
|
+
if ( error ) {
|
|
151
|
+
console.error( 'Error: %s', error.message );
|
|
152
|
+
}
|
|
153
|
+
console.log( 'Success!' );
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
</section>
|
|
158
|
+
|
|
159
|
+
<!-- /.examples -->
|
|
160
|
+
|
|
161
|
+
* * *
|
|
162
|
+
|
|
163
|
+
<section class="cli">
|
|
164
|
+
|
|
165
|
+
## CLI
|
|
166
|
+
|
|
167
|
+
<section class="usage">
|
|
168
|
+
|
|
169
|
+
### Usage
|
|
170
|
+
|
|
171
|
+
```text
|
|
172
|
+
Usage: write-file [options] <filepath>
|
|
173
|
+
|
|
174
|
+
Options:
|
|
175
|
+
|
|
176
|
+
-h, --help Print this message.
|
|
177
|
+
-V, --version Print the package version.
|
|
178
|
+
--enc, --encoding encoding Encoding. Default: 'utf8'.
|
|
179
|
+
--flag flag Flag. Default: 'r'.
|
|
180
|
+
--mode mode Mode. Default: 0o666.
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
</section>
|
|
184
|
+
|
|
185
|
+
<!-- /.usage -->
|
|
186
|
+
|
|
187
|
+
<section class="notes">
|
|
188
|
+
|
|
189
|
+
### Notes
|
|
190
|
+
|
|
191
|
+
- Relative output file paths are resolved relative to the current working directory.
|
|
192
|
+
- Errors are written to `stderr`.
|
|
193
|
+
- File contents should be provided over `stdin` as part of a [standard stream][standard-stream] pipeline.
|
|
194
|
+
|
|
195
|
+
</section>
|
|
196
|
+
|
|
197
|
+
<!-- /.notes -->
|
|
198
|
+
|
|
199
|
+
<section class="examples">
|
|
200
|
+
|
|
201
|
+
### Examples
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
$ printf 'beep boop\n' | write-file ./examples/fixtures/file.txt
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
</section>
|
|
208
|
+
|
|
209
|
+
<!-- /.examples -->
|
|
210
|
+
|
|
211
|
+
</section>
|
|
212
|
+
|
|
213
|
+
<!-- /.cli -->
|
|
214
|
+
|
|
215
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
216
|
+
|
|
217
|
+
<section class="related">
|
|
218
|
+
|
|
219
|
+
* * *
|
|
220
|
+
|
|
221
|
+
## See Also
|
|
222
|
+
|
|
223
|
+
- <span class="package-name">[`@stdlib/fs/exists`][@stdlib/fs/exists]</span><span class="delimiter">: </span><span class="description">test whether a path exists on the filesystem.</span>
|
|
224
|
+
- <span class="package-name">[`@stdlib/fs/read-file`][@stdlib/fs/read-file]</span><span class="delimiter">: </span><span class="description">read the entire contents of a file.</span>
|
|
225
|
+
|
|
226
|
+
</section>
|
|
227
|
+
|
|
228
|
+
<!-- /.related -->
|
|
229
|
+
|
|
230
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
231
|
+
|
|
232
|
+
<section class="links">
|
|
233
|
+
|
|
234
|
+
[node-fs]: https://nodejs.org/api/fs.html
|
|
235
|
+
|
|
236
|
+
[@stdlib/buffer/ctor]: https://www.npmjs.com/package/@stdlib/buffer-ctor
|
|
237
|
+
|
|
238
|
+
[standard-stream]: https://en.wikipedia.org/wiki/Pipeline_%28Unix%29
|
|
239
|
+
|
|
240
|
+
<!-- <related-links> -->
|
|
241
|
+
|
|
242
|
+
[@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
|
|
243
|
+
|
|
244
|
+
[@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
|
|
245
|
+
|
|
246
|
+
<!-- </related-links> -->
|
|
247
|
+
|
|
248
|
+
</section>
|
|
249
|
+
|
|
250
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
7
|
+
*
|
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License.
|
|
10
|
+
* You may obtain a copy of the License at
|
|
11
|
+
*
|
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13
|
+
*
|
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17
|
+
* See the License for the specific language governing permissions and
|
|
18
|
+
* limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
'use strict';
|
|
22
|
+
|
|
23
|
+
// MODULES //
|
|
24
|
+
|
|
25
|
+
var resolve = require( 'path' ).resolve;
|
|
26
|
+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
|
|
27
|
+
var CLI = require( '@stdlib/cli/ctor' );
|
|
28
|
+
var stdin = require( '@stdlib/process/read-stdin' );
|
|
29
|
+
var cwd = require( '@stdlib/process/cwd' );
|
|
30
|
+
var writeFile = require( './../lib' );
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// MAIN //
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Main execution sequence.
|
|
37
|
+
*
|
|
38
|
+
* @private
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
function main() {
|
|
42
|
+
var flags;
|
|
43
|
+
var fpath;
|
|
44
|
+
var args;
|
|
45
|
+
var opts;
|
|
46
|
+
var cli;
|
|
47
|
+
|
|
48
|
+
// Create a command-line interface:
|
|
49
|
+
cli = new CLI({
|
|
50
|
+
'pkg': require( './../package.json' ),
|
|
51
|
+
'options': require( './../etc/cli_opts.json' ),
|
|
52
|
+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
|
|
53
|
+
'encoding': 'utf8'
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Get any provided command-line options:
|
|
58
|
+
flags = cli.flags();
|
|
59
|
+
if ( flags.help || flags.version ) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Get any provided command-line arguments:
|
|
64
|
+
args = cli.args();
|
|
65
|
+
|
|
66
|
+
opts = {};
|
|
67
|
+
if ( flags.encoding ) {
|
|
68
|
+
opts.encoding = flags.encoding;
|
|
69
|
+
}
|
|
70
|
+
if ( flags.flag ) {
|
|
71
|
+
opts.flag = flags.flag;
|
|
72
|
+
}
|
|
73
|
+
if ( flags.mode ) {
|
|
74
|
+
opts.mode = parseInt( flags.mode, 8 );
|
|
75
|
+
}
|
|
76
|
+
fpath = resolve( cwd(), args[ 0 ] );
|
|
77
|
+
|
|
78
|
+
// Gather data from `stdin`...
|
|
79
|
+
return stdin( onRead );
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Callback invoked upon reading from `stdin`.
|
|
83
|
+
*
|
|
84
|
+
* @private
|
|
85
|
+
* @param {(Error|null)} error - error object
|
|
86
|
+
* @param {Buffer} data - data
|
|
87
|
+
* @returns {void}
|
|
88
|
+
*/
|
|
89
|
+
function onRead( error, data ) {
|
|
90
|
+
if ( error ) {
|
|
91
|
+
return cli.error( error );
|
|
92
|
+
}
|
|
93
|
+
writeFile( fpath, data, opts, onWrite );
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Callback invoked upon writing a file.
|
|
98
|
+
*
|
|
99
|
+
* @private
|
|
100
|
+
* @param {Error} [error] - error object
|
|
101
|
+
* @returns {void}
|
|
102
|
+
*/
|
|
103
|
+
function onWrite( error ) {
|
|
104
|
+
if ( error ) {
|
|
105
|
+
return cli.error( error );
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
main();
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( file, data[, options], clbk )
|
|
3
|
+
Asynchronously writes data to a file.
|
|
4
|
+
|
|
5
|
+
Parameters
|
|
6
|
+
----------
|
|
7
|
+
file: string|Buffer|integer
|
|
8
|
+
Filename or file descriptor.
|
|
9
|
+
|
|
10
|
+
data: string|Buffer
|
|
11
|
+
Data to write.
|
|
12
|
+
|
|
13
|
+
options: Object|string (optional)
|
|
14
|
+
Options. If a string, the value is the encoding.
|
|
15
|
+
|
|
16
|
+
options.encoding: string|null (optional)
|
|
17
|
+
Encoding. The encoding option is ignored if the data argument is a
|
|
18
|
+
buffer. Default: 'utf8'.
|
|
19
|
+
|
|
20
|
+
options.flag: string (optional)
|
|
21
|
+
Flag. Default: 'w'.
|
|
22
|
+
|
|
23
|
+
options.mode: integer (optional)
|
|
24
|
+
Mode. Default: 0o666.
|
|
25
|
+
|
|
26
|
+
clbk: Function
|
|
27
|
+
Callback to invoke upon writing data to a file.
|
|
28
|
+
|
|
29
|
+
Examples
|
|
30
|
+
--------
|
|
31
|
+
> function onWrite( error ) {
|
|
32
|
+
... if ( error ) {
|
|
33
|
+
... console.error( error.message );
|
|
34
|
+
... }
|
|
35
|
+
... };
|
|
36
|
+
> {{alias}}( './beep/boop.txt', 'beep boop', onWrite );
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
{{alias}}.sync( file, data[, options] )
|
|
40
|
+
Synchronously writes data to a file.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
file: string|Buffer|integer
|
|
45
|
+
Filename or file descriptor.
|
|
46
|
+
|
|
47
|
+
data: string|Buffer
|
|
48
|
+
Data to write.
|
|
49
|
+
|
|
50
|
+
options: Object|string (optional)
|
|
51
|
+
Options. If a string, the value is the encoding.
|
|
52
|
+
|
|
53
|
+
options.encoding: string|null (optional)
|
|
54
|
+
Encoding. The encoding option is ignored if the data argument is a
|
|
55
|
+
buffer. Default: 'utf8'.
|
|
56
|
+
|
|
57
|
+
options.flag: string (optional)
|
|
58
|
+
Flag. Default: 'w'.
|
|
59
|
+
|
|
60
|
+
options.mode: integer (optional)
|
|
61
|
+
Mode. Default: 0o666.
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
err: Error|null
|
|
66
|
+
Error object or null.
|
|
67
|
+
|
|
68
|
+
Examples
|
|
69
|
+
--------
|
|
70
|
+
> var err = {{alias}}.sync( './beep/boop.txt', 'beep boop' );
|
|
71
|
+
|
|
72
|
+
See Also
|
|
73
|
+
--------
|
|
74
|
+
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="node"/>
|
|
22
|
+
|
|
23
|
+
import { Buffer } from 'buffer';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Interface defining function options.
|
|
27
|
+
*/
|
|
28
|
+
interface Options {
|
|
29
|
+
/**
|
|
30
|
+
* Encoding; ignored if the data argument is a buffer (default: null).
|
|
31
|
+
*/
|
|
32
|
+
encoding?: string | null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Flag (default: 'r').
|
|
36
|
+
*/
|
|
37
|
+
flag?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Mode (default: 0o666)
|
|
41
|
+
*/
|
|
42
|
+
mode?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Callback invoked upon writing data to a file.
|
|
47
|
+
*
|
|
48
|
+
* @param err - error object
|
|
49
|
+
*/
|
|
50
|
+
type Callback = ( err: Error | null ) => void;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Interface for writing a file.
|
|
54
|
+
*/
|
|
55
|
+
interface WriteFile {
|
|
56
|
+
/**
|
|
57
|
+
* Asynchronously writes data to a file.
|
|
58
|
+
*
|
|
59
|
+
* @param file - file path or file descriptor
|
|
60
|
+
* @param data - data to write
|
|
61
|
+
* @param options - options; if a string, the value is the encoding
|
|
62
|
+
* @param clbk - callback to invoke after writing data to a file
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* function onWrite( error ) {
|
|
66
|
+
* if ( error ) {
|
|
67
|
+
* throw error;
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
*
|
|
71
|
+
* var opts = { 'flag': 'r' };
|
|
72
|
+
* writeFile( './beep/boop.txt', 'beep boop\n', opts, onWrite );
|
|
73
|
+
*/
|
|
74
|
+
( file: string | Buffer | number, data: string | Buffer, options: Options | string, clbk: Callback ): void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Asynchronously writes data to a file.
|
|
78
|
+
*
|
|
79
|
+
* @param file - file path or file descriptor
|
|
80
|
+
* @param data - data to write
|
|
81
|
+
* @param clbk - callback to invoke after writing data to a file
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* function onWrite( error ) {
|
|
85
|
+
* if ( error ) {
|
|
86
|
+
* throw error;
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* writeFile( './beep/boop.txt', 'beep boop\n', onWrite );
|
|
91
|
+
*/
|
|
92
|
+
( file: string | Buffer | number, data: string | Buffer, clbk: Callback ): void;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Synchronously writes data to a file.
|
|
96
|
+
*
|
|
97
|
+
* @param file - file path or file descriptor
|
|
98
|
+
* @param data - data to write
|
|
99
|
+
* @param options - options; if a string, the value is the encoding
|
|
100
|
+
* @returns error object or null
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* var err = writeFile.sync( './beep/boop.txt', 'beep boop\n' );
|
|
104
|
+
* if ( err instanceof Error ) {
|
|
105
|
+
* throw err;
|
|
106
|
+
* }
|
|
107
|
+
*/
|
|
108
|
+
sync( file: string | Buffer | number, data: string | Buffer, options?: Options | string ): Error | null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Asynchronously writes data to a file.
|
|
113
|
+
*
|
|
114
|
+
* @param file - file path or file descriptor
|
|
115
|
+
* @param data - data to write
|
|
116
|
+
* @param options - options; if a string, the value is the encoding
|
|
117
|
+
* @param clbk - callback to invoke after writing data to a file
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* function onWrite( error ) {
|
|
121
|
+
* if ( error ) {
|
|
122
|
+
* throw error;
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* writeFile( './beep/boop.txt', 'beep boop\n', onWrite );
|
|
127
|
+
*/
|
|
128
|
+
declare var writeFile: WriteFile;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
// EXPORTS //
|
|
132
|
+
|
|
133
|
+
export = writeFile;
|