@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.
Files changed (173) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +230 -2
  4. package/SECURITY.md +5 -0
  5. package/append-file/README.md +239 -0
  6. package/append-file/bin/cli +110 -0
  7. package/append-file/docs/repl.txt +72 -0
  8. package/append-file/docs/types/index.d.ts +133 -0
  9. package/append-file/docs/types/test.ts +193 -0
  10. package/append-file/docs/usage.txt +10 -0
  11. package/append-file/etc/cli_opts.json +22 -0
  12. package/append-file/lib/async.js +58 -0
  13. package/append-file/lib/index.js +61 -0
  14. package/append-file/lib/sync.js +58 -0
  15. package/append-file/package.json +69 -0
  16. package/close/README.md +172 -0
  17. package/close/docs/repl.txt +45 -0
  18. package/close/docs/types/index.d.ts +135 -0
  19. package/close/docs/types/test.ts +89 -0
  20. package/close/lib/index.js +73 -0
  21. package/close/lib/main.js +68 -0
  22. package/close/lib/sync.js +65 -0
  23. package/close/package.json +64 -0
  24. package/docs/types/index.d.ts +502 -0
  25. package/exists/README.md +252 -0
  26. package/exists/bin/cli +83 -0
  27. package/exists/docs/repl.txt +42 -0
  28. package/exists/docs/types/index.d.ts +112 -0
  29. package/exists/docs/types/test.ts +87 -0
  30. package/exists/docs/usage.txt +8 -0
  31. package/exists/etc/cli_opts.json +14 -0
  32. package/exists/lib/async.js +85 -0
  33. package/exists/lib/index.js +66 -0
  34. package/exists/lib/sync.js +62 -0
  35. package/exists/package.json +69 -0
  36. package/lib/index.js +186 -0
  37. package/open/README.md +204 -0
  38. package/open/docs/repl.txt +65 -0
  39. package/open/docs/types/index.d.ts +150 -0
  40. package/open/docs/types/test.ts +129 -0
  41. package/open/lib/defaults.json +4 -0
  42. package/open/lib/index.js +63 -0
  43. package/open/lib/main.js +64 -0
  44. package/open/lib/sync.js +68 -0
  45. package/open/package.json +64 -0
  46. package/package.json +67 -4
  47. package/read-dir/README.md +232 -0
  48. package/read-dir/bin/cli +83 -0
  49. package/read-dir/docs/repl.txt +44 -0
  50. package/read-dir/docs/types/index.d.ts +99 -0
  51. package/read-dir/docs/types/test.ts +85 -0
  52. package/read-dir/docs/usage.txt +8 -0
  53. package/read-dir/etc/cli_opts.json +14 -0
  54. package/read-dir/lib/async.js +57 -0
  55. package/read-dir/lib/index.js +62 -0
  56. package/read-dir/lib/sync.js +52 -0
  57. package/read-dir/package.json +69 -0
  58. package/read-file/README.md +246 -0
  59. package/read-file/bin/cli +91 -0
  60. package/read-file/docs/repl.txt +68 -0
  61. package/read-file/docs/types/index.d.ts +138 -0
  62. package/read-file/docs/types/test.ts +147 -0
  63. package/read-file/docs/usage.txt +10 -0
  64. package/read-file/etc/cli_opts.json +21 -0
  65. package/read-file/lib/index.js +61 -0
  66. package/read-file/lib/main.js +59 -0
  67. package/read-file/lib/sync.js +61 -0
  68. package/read-file/package.json +68 -0
  69. package/read-file-list/README.md +197 -0
  70. package/read-file-list/bin/cli +100 -0
  71. package/read-file-list/docs/repl.txt +81 -0
  72. package/read-file-list/docs/types/index.d.ts +166 -0
  73. package/read-file-list/docs/types/test.ts +166 -0
  74. package/read-file-list/docs/usage.txt +10 -0
  75. package/read-file-list/etc/cli_opts.json +21 -0
  76. package/read-file-list/lib/async.js +202 -0
  77. package/read-file-list/lib/clear_pending.js +47 -0
  78. package/read-file-list/lib/clear_timeout.js +23 -0
  79. package/read-file-list/lib/config.json +4 -0
  80. package/read-file-list/lib/delay.js +47 -0
  81. package/read-file-list/lib/index.js +66 -0
  82. package/read-file-list/lib/sync.js +94 -0
  83. package/read-file-list/package.json +72 -0
  84. package/read-json/README.md +176 -0
  85. package/read-json/docs/repl.txt +72 -0
  86. package/read-json/docs/types/index.d.ts +170 -0
  87. package/read-json/docs/types/test.ts +175 -0
  88. package/read-json/lib/async.js +114 -0
  89. package/read-json/lib/index.js +65 -0
  90. package/read-json/lib/sync.js +89 -0
  91. package/read-json/package.json +66 -0
  92. package/read-ndjson/README.md +166 -0
  93. package/read-ndjson/docs/repl.txt +72 -0
  94. package/read-ndjson/docs/types/index.d.ts +170 -0
  95. package/read-ndjson/docs/types/test.ts +179 -0
  96. package/read-ndjson/lib/async.js +114 -0
  97. package/read-ndjson/lib/index.js +65 -0
  98. package/read-ndjson/lib/sync.js +89 -0
  99. package/read-ndjson/package.json +68 -0
  100. package/read-wasm/README.md +158 -0
  101. package/read-wasm/docs/repl.txt +58 -0
  102. package/read-wasm/docs/types/index.d.ts +153 -0
  103. package/read-wasm/docs/types/test.ts +127 -0
  104. package/read-wasm/lib/async.js +106 -0
  105. package/read-wasm/lib/index.js +67 -0
  106. package/read-wasm/lib/sync.js +86 -0
  107. package/read-wasm/package.json +67 -0
  108. package/rename/README.md +259 -0
  109. package/rename/bin/cli +89 -0
  110. package/rename/docs/repl.txt +80 -0
  111. package/rename/docs/types/index.d.ts +109 -0
  112. package/rename/docs/types/test.ts +111 -0
  113. package/rename/docs/usage.txt +7 -0
  114. package/rename/etc/cli_opts.json +15 -0
  115. package/rename/lib/index.js +60 -0
  116. package/rename/lib/main.js +51 -0
  117. package/rename/lib/sync.js +53 -0
  118. package/rename/package.json +73 -0
  119. package/resolve-parent-path/README.md +204 -0
  120. package/resolve-parent-path/bin/cli +85 -0
  121. package/resolve-parent-path/docs/repl.txt +58 -0
  122. package/resolve-parent-path/docs/types/index.d.ts +128 -0
  123. package/resolve-parent-path/docs/types/test.ts +117 -0
  124. package/resolve-parent-path/docs/usage.txt +9 -0
  125. package/resolve-parent-path/etc/cli_opts.json +17 -0
  126. package/resolve-parent-path/lib/async.js +116 -0
  127. package/resolve-parent-path/lib/index.js +58 -0
  128. package/resolve-parent-path/lib/sync.js +83 -0
  129. package/resolve-parent-path/lib/validate.js +67 -0
  130. package/resolve-parent-path/package.json +78 -0
  131. package/resolve-parent-path-by/README.md +220 -0
  132. package/resolve-parent-path-by/docs/repl.txt +105 -0
  133. package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
  134. package/resolve-parent-path-by/docs/types/test.ts +225 -0
  135. package/resolve-parent-path-by/lib/async.js +156 -0
  136. package/resolve-parent-path-by/lib/index.js +67 -0
  137. package/resolve-parent-path-by/lib/sync.js +98 -0
  138. package/resolve-parent-path-by/lib/validate.js +67 -0
  139. package/resolve-parent-path-by/package.json +76 -0
  140. package/resolve-parent-paths/README.md +228 -0
  141. package/resolve-parent-paths/bin/cli +93 -0
  142. package/resolve-parent-paths/docs/repl.txt +84 -0
  143. package/resolve-parent-paths/docs/types/index.d.ts +145 -0
  144. package/resolve-parent-paths/docs/types/test.ts +196 -0
  145. package/resolve-parent-paths/docs/usage.txt +10 -0
  146. package/resolve-parent-paths/etc/cli_opts.json +18 -0
  147. package/resolve-parent-paths/lib/index.js +60 -0
  148. package/resolve-parent-paths/lib/main.js +415 -0
  149. package/resolve-parent-paths/lib/sync.js +231 -0
  150. package/resolve-parent-paths/lib/validate.js +81 -0
  151. package/resolve-parent-paths/package.json +78 -0
  152. package/unlink/README.md +233 -0
  153. package/unlink/bin/cli +81 -0
  154. package/unlink/docs/repl.txt +62 -0
  155. package/unlink/docs/types/index.d.ts +105 -0
  156. package/unlink/docs/types/test.ts +85 -0
  157. package/unlink/docs/usage.txt +7 -0
  158. package/unlink/etc/cli_opts.json +15 -0
  159. package/unlink/lib/index.js +60 -0
  160. package/unlink/lib/main.js +50 -0
  161. package/unlink/lib/sync.js +52 -0
  162. package/unlink/package.json +70 -0
  163. package/write-file/README.md +250 -0
  164. package/write-file/bin/cli +110 -0
  165. package/write-file/docs/repl.txt +74 -0
  166. package/write-file/docs/types/index.d.ts +133 -0
  167. package/write-file/docs/types/test.ts +193 -0
  168. package/write-file/docs/usage.txt +10 -0
  169. package/write-file/etc/cli_opts.json +22 -0
  170. package/write-file/lib/index.js +60 -0
  171. package/write-file/lib/main.js +58 -0
  172. package/write-file/lib/sync.js +58 -0
  173. package/write-file/package.json +68 -0
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var appendFile = require( 'fs' ).appendFileSync;
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Synchronously append data to a file, creating the file if it does not yet exist.
30
+ *
31
+ * @param {(string|Buffer|integer)} path - file path or file descriptor
32
+ * @param {(string|Buffer)} data - data to append
33
+ * @param {(Object|string)} [options] - options
34
+ * @returns {(Error|null)} error object or null
35
+ *
36
+ * @example
37
+ * var err = appendFileSync( './beep/boop.txt', 'data to append\n' );
38
+ * if ( err instanceof Error ) {
39
+ * throw err;
40
+ * }
41
+ */
42
+ function appendFileSync( path, data, options ) {
43
+ try {
44
+ if ( arguments.length > 2 ) {
45
+ appendFile( path, data, options );
46
+ } else {
47
+ appendFile( path, data );
48
+ }
49
+ } catch ( err ) {
50
+ return err;
51
+ }
52
+ return null;
53
+ }
54
+
55
+
56
+ // EXPORTS //
57
+
58
+ module.exports = appendFileSync;
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@stdlib/fs/append-file",
3
+ "version": "0.0.0",
4
+ "description": "Append data to a file.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "bin": {
17
+ "append-file": "./bin/cli"
18
+ },
19
+ "main": "./lib",
20
+ "directories": {
21
+ "benchmark": "./benchmark",
22
+ "doc": "./docs",
23
+ "example": "./examples",
24
+ "lib": "./lib",
25
+ "test": "./test"
26
+ },
27
+ "types": "./docs/types",
28
+ "scripts": {},
29
+ "homepage": "https://github.com/stdlib-js/stdlib",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git://github.com/stdlib-js/stdlib.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/stdlib-js/stdlib/issues"
36
+ },
37
+ "dependencies": {},
38
+ "devDependencies": {},
39
+ "engines": {
40
+ "node": ">=0.10.0",
41
+ "npm": ">2.7.0"
42
+ },
43
+ "os": [
44
+ "aix",
45
+ "darwin",
46
+ "freebsd",
47
+ "linux",
48
+ "macos",
49
+ "openbsd",
50
+ "sunos",
51
+ "win32",
52
+ "windows"
53
+ ],
54
+ "keywords": [
55
+ "stdlib",
56
+ "stdfs",
57
+ "fs",
58
+ "appendfile",
59
+ "appendfilesync",
60
+ "path",
61
+ "async",
62
+ "sync",
63
+ "file",
64
+ "append",
65
+ "appends",
66
+ "open",
67
+ "filesystem"
68
+ ]
69
+ }
@@ -0,0 +1,172 @@
1
+ <!--
2
+
3
+ @license Apache-2.0
4
+
5
+ Copyright (c) 2019 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
+ # Close
22
+
23
+ > Close a file descriptor.
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ <!-- eslint-disable stdlib/no-redeclare -->
30
+
31
+ ```javascript
32
+ var close = require( '@stdlib/fs/close' );
33
+ ```
34
+
35
+ #### close( fd, clbk )
36
+
37
+ Asynchronously closes a file descriptor, so that the file descriptor no longer refers to any file and may be reused.
38
+
39
+ <!-- eslint-disable stdlib/no-redeclare -->
40
+
41
+ ```javascript
42
+ var openSync = require( '@stdlib/fs/open' ).sync;
43
+
44
+ var fd = openSync( __filename );
45
+ close( fd, done );
46
+
47
+ function done( error ) {
48
+ if ( error ) {
49
+ throw error;
50
+ }
51
+ }
52
+ ```
53
+
54
+ #### close.sync( fd )
55
+
56
+ Synchronously closes a file descriptor.
57
+
58
+ <!-- eslint-disable stdlib/no-redeclare -->
59
+
60
+ ```javascript
61
+ var openSync = require( '@stdlib/fs/open' ).sync;
62
+
63
+ var fd = openSync( __filename );
64
+
65
+ var err = close.sync( fd );
66
+ if ( err instanceof Error ) {
67
+ throw err;
68
+ }
69
+ ```
70
+
71
+ </section>
72
+
73
+ <!-- /.usage -->
74
+
75
+ <section class="notes">
76
+
77
+ ## Notes
78
+
79
+ - The difference between this API and [`fs.closeSync()`][node-fs] is that [`fs.closeSync()`][node-fs] will throw if an `error` is encountered (e.g., if given an invalid file descriptor) and this API will return an `error`.
80
+
81
+ </section>
82
+
83
+ <!-- /.notes -->
84
+
85
+ <section class="examples">
86
+
87
+ ## Examples
88
+
89
+ <!-- eslint-disable stdlib/no-redeclare -->
90
+
91
+ <!-- eslint no-undef: "error" -->
92
+
93
+ ```javascript
94
+ var join = require( 'path' ).join;
95
+ var openSync = require( '@stdlib/fs/open' ).sync;
96
+ var close = require( '@stdlib/fs/close' );
97
+
98
+ var err;
99
+
100
+ /* Sync */
101
+
102
+ var fd = openSync( join( __dirname, 'package.json' ), 'r+' );
103
+ if ( fd instanceof Error ) {
104
+ console.error( fd.message );
105
+ } else {
106
+ err = close.sync( fd );
107
+ // returns undefined
108
+
109
+ if ( err instanceof Error ) {
110
+ console.error( err.message );
111
+ } else {
112
+ console.log( 'Synchronously closed file descriptor.' );
113
+ }
114
+ }
115
+
116
+ /* Async */
117
+
118
+ fd = openSync( join( __dirname, 'package.json' ), 'r+' );
119
+ if ( fd instanceof Error ) {
120
+ console.error( fd.message );
121
+ } else {
122
+ close( fd, done );
123
+ }
124
+
125
+ function done( error ) {
126
+ if ( error ) {
127
+ console.error( error.message );
128
+ } else {
129
+ console.log( 'Asynchronously closed file descriptor.' );
130
+ }
131
+ }
132
+ ```
133
+
134
+ </section>
135
+
136
+ <!-- /.examples -->
137
+
138
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139
+
140
+ <section class="related">
141
+
142
+ * * *
143
+
144
+ ## See Also
145
+
146
+ - <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>
147
+ - <span class="package-name">[`@stdlib/fs/open`][@stdlib/fs/open]</span><span class="delimiter">: </span><span class="description">open a file.</span>
148
+ - <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>
149
+
150
+ </section>
151
+
152
+ <!-- /.related -->
153
+
154
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155
+
156
+ <section class="links">
157
+
158
+ [node-fs]: https://nodejs.org/api/fs.html
159
+
160
+ <!-- <related-links> -->
161
+
162
+ [@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
163
+
164
+ [@stdlib/fs/open]: https://github.com/stdlib-js/fs/tree/main/open
165
+
166
+ [@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
167
+
168
+ <!-- </related-links> -->
169
+
170
+ </section>
171
+
172
+ <!-- /.links -->
@@ -0,0 +1,45 @@
1
+
2
+ {{alias}}( fd, clbk )
3
+ Asynchronously closes a file descriptor, so that the file descriptor no
4
+ longer refers to any file and may be reused.
5
+
6
+ Parameters
7
+ ----------
8
+ fd: integer
9
+ File descriptor.
10
+
11
+ clbk: Function
12
+ Callback to invoke upon closing a file descriptor.
13
+
14
+ Examples
15
+ --------
16
+ > function done( error ) {
17
+ ... if ( error ) {
18
+ ... console.error( error.message );
19
+ ... }
20
+ ... };
21
+ > var fd = {{alias:@stdlib/fs/open}}.sync( './beep/boop.js', 'r+' );
22
+ > if ( !{{alias:@stdlib/assert/is-error}}( fd ) ) { {{alias}}( fd, done ); };
23
+
24
+
25
+ {{alias}}.sync( fd )
26
+ Synchronously closes a file descriptor.
27
+
28
+ Parameters
29
+ ----------
30
+ fd: integer
31
+ File descriptor.
32
+
33
+ Returns
34
+ -------
35
+ out: Error|void
36
+ If an error occurs, an error object; otherwise, undefined.
37
+
38
+ Examples
39
+ --------
40
+ > var fd = {{alias:@stdlib/fs/open}}.sync( './beep/boop.js', 'r+' );
41
+ > if ( !{{alias:@stdlib/assert/is-error}}( fd ) ) { {{alias}}.sync( fd ); };
42
+
43
+ See Also
44
+ --------
45
+
@@ -0,0 +1,135 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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
+ /**
22
+ * Callback invoked after closing a file descriptor.
23
+ */
24
+ type Nullary = () => void;
25
+
26
+ /**
27
+ * Callback invoked after closing a file descriptor.
28
+ *
29
+ * @param err - error argument
30
+ */
31
+ type Unary = ( err: Error ) => void;
32
+
33
+ /**
34
+ * Callback invoked after closing a file descriptor.
35
+ *
36
+ * @param err - error argument
37
+ */
38
+ type Callback = Nullary | Unary;
39
+
40
+ /**
41
+ * Interface for closing a file descriptor.
42
+ */
43
+ interface Close {
44
+ /**
45
+ * Asynchronously closes a file descriptor.
46
+ *
47
+ * @param fd - file descriptor
48
+ * @param clbk - callback to invoke after closing a file descriptor
49
+ * @throws first argument must be a valid file descriptor (nonnegative integer)
50
+ *
51
+ * @example
52
+ * var openSync = require( './../../../open' ).sync;
53
+ *
54
+ * function done( error ) {
55
+ * if ( error ) {
56
+ * throw error;
57
+ * }
58
+ * }
59
+ *
60
+ * var fd = openSync( __filename, 'r+' );
61
+ * if ( fd instanceof Error ) {
62
+ * throw fd;
63
+ * }
64
+ *
65
+ * close( fd, done );
66
+ */
67
+ ( fd: number, clbk: Callback ): void;
68
+
69
+ /**
70
+ * Synchronously closes a file descriptor.
71
+ *
72
+ * @param fd - file descriptor
73
+ * @throws first argument must be a valid file descriptor (nonnegative integer)
74
+ * @returns an error object or `undefined`
75
+ *
76
+ * @example
77
+ * var openSync = require( './../../../open' ).sync;
78
+ *
79
+ * var fd = openSync( __filename, 'r+' );
80
+ *
81
+ * if ( fd instanceof Error ) {
82
+ * throw fd;
83
+ * }
84
+ *
85
+ * var err = close.sync( fd );
86
+ * if ( err instanceof Error ) {
87
+ * throw err;
88
+ * }
89
+ */
90
+ sync( fd: number ): Error | void;
91
+ }
92
+
93
+ /**
94
+ * Asynchronously closes a file descriptor.
95
+ *
96
+ * @param fd - file descriptor
97
+ * @param clbk - callback to invoke after closing a file descriptor
98
+ * @throws first argument must be a valid file descriptor (nonnegative integer)
99
+ *
100
+ * @example
101
+ * var openSync = require( '@stdlib/fs/open' ).sync;
102
+ *
103
+ * function done( error ) {
104
+ * if ( error ) {
105
+ * throw error;
106
+ * }
107
+ * }
108
+ *
109
+ * var fd = openSync( __filename, 'r+' );
110
+ * if ( fd instanceof Error ) {
111
+ * throw fd;
112
+ * }
113
+ *
114
+ * close( fd, done );
115
+ *
116
+ * @example
117
+ * var openSync = require( '@stdlib/fs/open' ).sync;
118
+ *
119
+ * var fd = openSync( __filename, 'r+' );
120
+ *
121
+ * if ( fd instanceof Error ) {
122
+ * throw fd;
123
+ * }
124
+ *
125
+ * var err = close.sync( fd );
126
+ * if ( err instanceof Error ) {
127
+ * throw err;
128
+ * }
129
+ */
130
+ declare var close: Close;
131
+
132
+
133
+ // EXPORTS //
134
+
135
+ export = close;
@@ -0,0 +1,89 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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
+ import close = require( './index' );
20
+
21
+ const done = ( error: Error ) => {
22
+ if ( error ) {
23
+ throw error;
24
+ }
25
+ };
26
+
27
+
28
+ // TESTS //
29
+
30
+ // The function does not have a return value...
31
+ {
32
+ const fd = 0;
33
+ close( fd, done ); // $ExpectType void
34
+ }
35
+
36
+ // The compiler throws an error if the function is provided a first argument which is not a number...
37
+ {
38
+ close( 'abc', done ); // $ExpectError
39
+ close( false, done ); // $ExpectError
40
+ close( true, done ); // $ExpectError
41
+ close( null, done ); // $ExpectError
42
+ close( undefined, done ); // $ExpectError
43
+ close( [], done ); // $ExpectError
44
+ close( {}, done ); // $ExpectError
45
+ close( ( x: number ): number => x, done ); // $ExpectError
46
+ }
47
+
48
+ // The compiler throws an error if the function is provided a second argument which is not a function with the expected signature...
49
+ {
50
+ const fd = 0;
51
+ close( fd, 1 ); // $ExpectError
52
+ close( fd, false ); // $ExpectError
53
+ close( fd, true ); // $ExpectError
54
+ close( fd, null ); // $ExpectError
55
+ close( fd, undefined ); // $ExpectError
56
+ close( fd, [] ); // $ExpectError
57
+ close( fd, {} ); // $ExpectError
58
+ close( fd, ( x: number ): number => x ); // $ExpectError
59
+ }
60
+
61
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
62
+ {
63
+ close(); // $ExpectError
64
+ close( 'C:\\foo\\bar\\baz' ); // $ExpectError
65
+ }
66
+
67
+ // Attached to main export is a `sync` method which returns an error or undefined...
68
+ {
69
+ const fd = 0;
70
+ close.sync( fd ); // $ExpectType void | Error
71
+ close.sync( 123 ); // $ExpectType void | Error
72
+ }
73
+
74
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a number...
75
+ {
76
+ close.sync( 'abc' ); // $ExpectError
77
+ close.sync( false ); // $ExpectError
78
+ close.sync( true ); // $ExpectError
79
+ close.sync( null ); // $ExpectError
80
+ close.sync( undefined ); // $ExpectError
81
+ close.sync( [] ); // $ExpectError
82
+ close.sync( {} ); // $ExpectError
83
+ close.sync( ( x: number ): number => x ); // $ExpectError
84
+ }
85
+
86
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
87
+ {
88
+ close.sync(); // $ExpectError
89
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 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
+ 'use strict';
20
+
21
+ /**
22
+ * Close a file descriptor.
23
+ *
24
+ * @module @stdlib/fs/close
25
+ *
26
+ * @example
27
+ * var openSync = require( '@stdlib/fs/open' ).sync;
28
+ * var close = require( '@stdlib/fs/close' );
29
+ *
30
+ * function done( error ) {
31
+ * if ( error ) {
32
+ * throw error;
33
+ * }
34
+ * }
35
+ *
36
+ * var fd = openSync( __filename, 'r+' );
37
+ * if ( fd instanceof Error ) {
38
+ * throw fd;
39
+ * }
40
+ *
41
+ * close( fd, done );
42
+ *
43
+ * @example
44
+ * var openSync = require( '@stdlib/fs/open' ).sync;
45
+ * var closeSync = require( '@stdlib/fs/close' ).sync;
46
+ *
47
+ * var fd = openSync( __filename, 'r+' );
48
+ *
49
+ * if ( fd instanceof Error ) {
50
+ * throw fd;
51
+ * }
52
+ *
53
+ * var err = closeSync( fd );
54
+ * if ( err instanceof Error ) {
55
+ * throw err;
56
+ * }
57
+ */
58
+
59
+ // MODULES //
60
+
61
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
62
+ var main = require( './main.js' );
63
+ var sync = require( './sync.js' );
64
+
65
+
66
+ // MAIN //
67
+
68
+ setReadOnly( main, 'sync', sync );
69
+
70
+
71
+ // EXPORTS //
72
+
73
+ module.exports = main;
@@ -0,0 +1,68 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2019 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
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var native = require( 'fs' ).close;
24
+ var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
25
+ var isFunction = require( '@stdlib/assert/is-function' );
26
+ var format = require( '@stdlib/string/format' );
27
+
28
+
29
+ // MAIN //
30
+
31
+ /**
32
+ * Asynchronously closes a file descriptor.
33
+ *
34
+ * @param {NonNegativeInteger} fd - file descriptor
35
+ * @param {Function} clbk - callback to invoke after closing a file descriptor
36
+ * @throws {TypeError} first argument must be a valid file descriptor (nonnegative integer)
37
+ * @throws {TypeError} last argument must be a function
38
+ *
39
+ * @example
40
+ * var openSync = require( '@stdlib/fs/open' ).sync;
41
+ *
42
+ * function done( error ) {
43
+ * if ( error ) {
44
+ * throw error;
45
+ * }
46
+ * }
47
+ *
48
+ * var fd = openSync( __filename, 'r+' );
49
+ * if ( fd instanceof Error ) {
50
+ * throw fd;
51
+ * }
52
+ *
53
+ * close( fd, done );
54
+ */
55
+ function close( fd, clbk ) { // eslint-disable-line stdlib/no-redeclare
56
+ if ( !isNonNegativeInteger( fd ) ) {
57
+ throw new TypeError( format( 'invalid argument. First argument must be a valid file descriptor (i.e., nonnegative integer). Value: `%s`.', fd ) );
58
+ }
59
+ if ( !isFunction( clbk ) ) {
60
+ throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', clbk ) );
61
+ }
62
+ native( fd, clbk );
63
+ }
64
+
65
+
66
+ // EXPORTS //
67
+
68
+ module.exports = close;