@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,259 @@
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
+ # Rename
22
+
23
+ > Rename a file.
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var rename = require( '@stdlib/fs/rename' );
31
+ ```
32
+
33
+ #### rename( oldPath, newPath, clbk )
34
+
35
+ Asynchronously renames a file specified by `oldPath` to `newPath`.
36
+
37
+ <!-- run-disable -->
38
+
39
+ ```javascript
40
+ var join = require( 'path' ).join;
41
+ var oldPath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
42
+ var newPath = join( __dirname, 'examples', 'fixtures', 'tmp.txt' );
43
+
44
+ rename( oldPath, newPath, done );
45
+
46
+ function done( error ) {
47
+ if ( error ) {
48
+ throw error;
49
+ }
50
+ }
51
+ ```
52
+
53
+ #### rename.sync( oldPath, newPath )
54
+
55
+ Synchronously renames a file specified by `oldPath` to `newPath`.
56
+
57
+ <!-- run-disable -->
58
+
59
+ ```javascript
60
+ var join = require( 'path' ).join;
61
+ var oldPath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
62
+ var newPath = join( __dirname, 'examples', 'fixtures', 'tmp.txt' );
63
+
64
+ var err = rename.sync( oldPath, newPath );
65
+ if ( err instanceof Error ) {
66
+ throw err;
67
+ }
68
+ ```
69
+
70
+ </section>
71
+
72
+ <!-- /.usage -->
73
+
74
+ <section class="notes">
75
+
76
+ ## Notes
77
+
78
+ - `oldPath` can specify a directory. In this case, `newPath` must either **not** exist, or it must specify an **empty** directory.
79
+
80
+ - `oldPath` should **not** name an ancestor directory of `newPath`.
81
+
82
+ - If `oldPath` points to the pathname of a file that is **not** a directory, `newPath` should **not** point to the pathname of a directory.
83
+
84
+ - Write access permission is **required** for both the directory containing `oldPath` and the directory containing `newPath`.
85
+
86
+ - If the link named by `newPath` exists, `newPath` is removed and `oldPath` is renamed to `newPath`. The link named by `newPath` will remain visible to other threads throughout the renaming operation and refer to either the file referred to by `newPath` or to the file referred to by `oldPath` before the operation began.
87
+
88
+ - If `oldPath` and `newPath` resolve to either the same existing directory entry or to different directory entries for the same existing file, no action is taken, and no error is returned.
89
+
90
+ - If `oldPath` points to a pathname of a symbolic link, the symbolic link is renamed. If the `newPath` points to a pathname of a symbolic link, the symbolic link is removed.
91
+
92
+ - If a link named by `newPath` exists and the file's link count becomes `0` when it is removed and no process has the file open, the space occupied by the file is freed and the file is no longer accessible. If one or more processes have the file open when the last link is removed, the link is removed before the function returns, but the removal of file contents is postponed until all references to the file are closed.
93
+
94
+ - The difference between `rename.sync` and [`fs.rename()`][node-fs] is that [`fs.renameSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent path) and this API will return an `error`. Hence, the following anti-pattern
95
+
96
+ <!-- run-disable -->
97
+
98
+ ```javascript
99
+ var fs = require( 'fs' );
100
+
101
+ // Check for path existence to prevent an error being thrown...
102
+ if ( fs.existsSync( '/path/to/file.txt' ) ) {
103
+ fs.renameSync( '/path/to/file.txt', '/path/to/tmp.txt' );
104
+ }
105
+ ```
106
+
107
+ can be replaced by an approach which addresses existence via `error` handling.
108
+
109
+ <!-- run-disable -->
110
+
111
+ ```javascript
112
+ var rename = require( '@stdlib/fs/rename' );
113
+
114
+ // Explicitly handle the error...
115
+ var err = rename.sync( '/path/to/file.txt', '/path/to/tmp.txt' );
116
+ if ( err instanceof Error ) {
117
+ // You choose what to do...
118
+ throw err;
119
+ }
120
+ ```
121
+
122
+ </section>
123
+
124
+ <!-- /.notes -->
125
+
126
+ <section class="examples">
127
+
128
+ ## Examples
129
+
130
+ <!-- eslint no-undef: "error" -->
131
+
132
+ ```javascript
133
+ var join = require( 'path' ).join;
134
+ var readFile = require( '@stdlib/fs/read-file' ).sync;
135
+ var writeFile = require( '@stdlib/fs/write-file' ).sync;
136
+ var exists = require( '@stdlib/fs/exists' ).sync;
137
+ var unlink = require( '@stdlib/fs/unlink' ).sync;
138
+ var rename = require( '@stdlib/fs/rename' ).sync;
139
+
140
+ var src = join( __dirname, 'examples', 'fixtures', 'file.txt' );
141
+ var tmp = join( __dirname, 'examples', 'tmp.txt' );
142
+ var dest = join( __dirname, 'examples', 'foo.txt' );
143
+
144
+ // Create a temporary file:
145
+ writeFile( tmp, readFile( src ) );
146
+
147
+ // Confirm that the temporary file exists:
148
+ console.log( exists( tmp ) );
149
+ // => true
150
+
151
+ // Rename the temporary file:
152
+ rename( tmp, dest );
153
+
154
+ // Confirm that the renamed temporary file exists:
155
+ console.log( exists( dest ) );
156
+ // => true
157
+
158
+ // Remove the temporary file:
159
+ unlink( dest );
160
+
161
+ // Confirm that the temporary file no longer exists:
162
+ console.log( exists( dest ) );
163
+ // => false
164
+ ```
165
+
166
+ </section>
167
+
168
+ <!-- /.examples -->
169
+
170
+ * * *
171
+
172
+ <section class="cli">
173
+
174
+ ## CLI
175
+
176
+ <section class="usage">
177
+
178
+ ### Usage
179
+
180
+ ```text
181
+ Usage: rename [options] <old_path> <new_path>
182
+
183
+ Options:
184
+
185
+ -h, --help Print this message.
186
+ -V, --version Print the package version.
187
+ ```
188
+
189
+ </section>
190
+
191
+ <!-- /.usage -->
192
+
193
+ <section class="notes">
194
+
195
+ ### Notes
196
+
197
+ - Relative paths are resolved relative to the current working directory.
198
+ - Errors are written to `stderr`.
199
+
200
+ </section>
201
+
202
+ <!-- /.notes -->
203
+
204
+ <section class="examples">
205
+
206
+ ### Examples
207
+
208
+ <!-- run-disable -->
209
+
210
+ ```bash
211
+ $ rename ./examples/fixtures/file.txt ./examples/fixtures/tmp.txt
212
+ ```
213
+
214
+ </section>
215
+
216
+ <!-- /.examples -->
217
+
218
+ </section>
219
+
220
+ <!-- /.cli -->
221
+
222
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
223
+
224
+ <section class="related">
225
+
226
+ * * *
227
+
228
+ ## See Also
229
+
230
+ - <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>
231
+ - <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>
232
+ - <span class="package-name">[`@stdlib/fs/write-file`][@stdlib/fs/write-file]</span><span class="delimiter">: </span><span class="description">write data to a file.</span>
233
+ - <span class="package-name">[`@stdlib/fs/unlink`][@stdlib/fs/unlink]</span><span class="delimiter">: </span><span class="description">remove a directory entry.</span>
234
+
235
+ </section>
236
+
237
+ <!-- /.related -->
238
+
239
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
240
+
241
+ <section class="links">
242
+
243
+ [node-fs]: https://nodejs.org/api/fs.html
244
+
245
+ <!-- <related-links> -->
246
+
247
+ [@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
248
+
249
+ [@stdlib/fs/read-file]: https://github.com/stdlib-js/fs/tree/main/read-file
250
+
251
+ [@stdlib/fs/write-file]: https://github.com/stdlib-js/fs/tree/main/write-file
252
+
253
+ [@stdlib/fs/unlink]: https://github.com/stdlib-js/fs/tree/main/unlink
254
+
255
+ <!-- </related-links> -->
256
+
257
+ </section>
258
+
259
+ <!-- /.links -->
package/rename/bin/cli ADDED
@@ -0,0 +1,89 @@
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 cwd = require( '@stdlib/process/cwd' );
29
+ var rename = require( './../lib' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Main execution sequence.
36
+ *
37
+ * @private
38
+ * @returns {void}
39
+ */
40
+ function main() {
41
+ var oldPath;
42
+ var newPath;
43
+ var flags;
44
+ var args;
45
+ var cli;
46
+ var dir;
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
+ if ( args.length !== 2 ) {
67
+ return cli.error( new Error( 'invalid invocation. Must provide two arguments: an old path and a new path.' ) );
68
+ }
69
+ dir = cwd();
70
+ oldPath = resolve( dir, args[ 0 ] );
71
+ newPath = resolve( dir, args[ 1 ] );
72
+
73
+ rename( oldPath, newPath, done );
74
+
75
+ /**
76
+ * Callback invoked upon renaming a file.
77
+ *
78
+ * @private
79
+ * @param {Error} [error] - error object
80
+ * @returns {void}
81
+ */
82
+ function done( error ) {
83
+ if ( error ) {
84
+ return cli.error( error );
85
+ }
86
+ }
87
+ }
88
+
89
+ main();
@@ -0,0 +1,80 @@
1
+
2
+ {{alias}}( oldPath, newPath, clbk )
3
+ Asynchronously renames a file.
4
+
5
+ The old path can specify a directory. In this case, the new path must either
6
+ not exist, or it must specify an empty directory.
7
+
8
+ The old pathname should not name an ancestor directory of the new pathname.
9
+
10
+ If the old path points to the pathname of a file that is not a directory,
11
+ the new path should not point to the pathname of a directory.
12
+
13
+ Write access permission is required for both the directory containing the
14
+ old path and the directory containing the new path.
15
+
16
+ If the link named by the new path exists, the new path is removed and the
17
+ old path is renamed to the new path. The link named by the new path will
18
+ remain visible to other threads throughout the renaming operation and refer
19
+ to either the file referred to by the new path or to the file referred to by
20
+ the old path before the operation began.
21
+
22
+ If the old path and the new path resolve to either the same existing
23
+ directory entry or to different directory entries for the same existing
24
+ file, no action is taken, and no error is returned.
25
+
26
+ If the old path points to a pathname of a symbolic link, the symbolic link
27
+ is renamed. If the new path points to a pathname of a symbolic link, the
28
+ symbolic link is removed.
29
+
30
+ If a link named by the new path exists and the file's link count becomes 0
31
+ when it is removed and no process has the file open, the space occupied by
32
+ the file is freed and the file is no longer accessible. If one or more
33
+ processes have the file open when the last link is removed, the link is
34
+ removed before the function returns, but the removal of file contents is
35
+ postponed until all references to the file are closed.
36
+
37
+ Parameters
38
+ ----------
39
+ oldPath: string|Buffer
40
+ Old path.
41
+
42
+ newPath: string|Buffer
43
+ New path.
44
+
45
+ clbk: Function
46
+ Callback to invoke upon renaming a file.
47
+
48
+ Examples
49
+ --------
50
+ > function done( error ) {
51
+ ... if ( error ) {
52
+ ... console.error( error.message );
53
+ ... }
54
+ ... };
55
+ > {{alias}}( './beep/boop.txt', './beep/foo.txt', done );
56
+
57
+
58
+ {{alias}}.sync( oldPath, newPath )
59
+ Synchronously renames a file.
60
+
61
+ Parameters
62
+ ----------
63
+ oldPath: string|Buffer
64
+ Old path.
65
+
66
+ newPath: string|Buffer
67
+ New path.
68
+
69
+ Returns
70
+ -------
71
+ err: Error|null
72
+ Error object or null.
73
+
74
+ Examples
75
+ --------
76
+ > var err = {{alias}}.sync( './beep/boop.txt', './beep/foo.txt' );
77
+
78
+ See Also
79
+ --------
80
+
@@ -0,0 +1,109 @@
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
+ /// <reference types="node"/>
22
+
23
+ import { Buffer } from 'buffer';
24
+
25
+ /**
26
+ * Callback invoked after renaming a file.
27
+ */
28
+ type Nullary = () => void;
29
+
30
+ /**
31
+ * Callback invoked after renaming a file.
32
+ *
33
+ * @param err - error argument
34
+ */
35
+ type Unary = ( err: Error | null ) => void;
36
+
37
+ /**
38
+ * Callback invoked after renaming a file.
39
+ *
40
+ * @param err - error argument
41
+ */
42
+ type Callback = Nullary | Unary;
43
+
44
+ /**
45
+ * Interface for renaming a file.
46
+ */
47
+ interface Rename {
48
+ /**
49
+ * Renames a file.
50
+ *
51
+ * @param oldPath - old path
52
+ * @param newPath - new path
53
+ * @param clbk - callback to invoke after renaming a path
54
+ *
55
+ * @example
56
+ * function done( error ) {
57
+ * if ( error ) {
58
+ * throw error;
59
+ * }
60
+ * }
61
+ *
62
+ * rename( './beep/boop.txt', './beep/foo.txt', done );
63
+ */
64
+ ( oldPath: string | Buffer, newPath: string | Buffer, clbk: Callback ): void;
65
+
66
+ /**
67
+ * Synchronously renames a file.
68
+ *
69
+ * @param oldPath - old path
70
+ * @param newPath - new path
71
+ * @returns error object or null
72
+ *
73
+ * @example
74
+ * var err = rename.sync( './beep/boop.txt', './beep/foo.txt' );
75
+ * if ( err instanceof Error ) {
76
+ * throw err;
77
+ * }
78
+ */
79
+ sync( oldPath: string | Buffer, newPath: string | Buffer ): Error | null;
80
+ }
81
+
82
+ /**
83
+ * Asynchronously renames a file.
84
+ *
85
+ * @param oldPath - old path
86
+ * @param newPath - new path
87
+ * @param clbk - callback to invoke after renaming a path
88
+ *
89
+ * @example
90
+ * function done( error ) {
91
+ * if ( error ) {
92
+ * throw error;
93
+ * }
94
+ * }
95
+ *
96
+ * rename( './beep/boop.txt', './beep/foo.txt', done );
97
+ *
98
+ * @example
99
+ * var err = rename.sync( './beep/boop.txt', './beep/foo.txt' );
100
+ * if ( err instanceof Error ) {
101
+ * throw err;
102
+ * }
103
+ */
104
+ declare var rename: Rename;
105
+
106
+
107
+ // EXPORTS //
108
+
109
+ export = rename;
@@ -0,0 +1,111 @@
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 rename = require( './index' );
20
+
21
+ const done = ( error: Error | null ): void => {
22
+ if ( error ) {
23
+ throw error;
24
+ }
25
+ };
26
+
27
+
28
+ // TESTS //
29
+
30
+ // The function does not have a return value...
31
+ {
32
+ rename( './beep/boop.txt', './beep/foo.txt', done ); // $ExpectType void
33
+ }
34
+
35
+ // The compiler throws an error if the function is provided a first argument which is not a string...
36
+ {
37
+ rename( 123, './beep/foo.txt', done ); // $ExpectError
38
+ rename( false, './beep/foo.txt', done ); // $ExpectError
39
+ rename( true, './beep/foo.txt', done ); // $ExpectError
40
+ rename( null, './beep/foo.txt', done ); // $ExpectError
41
+ rename( undefined, './beep/foo.txt', done ); // $ExpectError
42
+ rename( [], './beep/foo.txt', done ); // $ExpectError
43
+ rename( {}, './beep/foo.txt', done ); // $ExpectError
44
+ rename( ( x: number ): number => x, './beep/foo.txt', done ); // $ExpectError
45
+ }
46
+
47
+ // The compiler throws an error if the function is provided a second argument which is not a string...
48
+ {
49
+ rename( './beep/boop.txt', 123, done ); // $ExpectError
50
+ rename( './beep/boop.txt', false, done ); // $ExpectError
51
+ rename( './beep/boop.txt', true, done ); // $ExpectError
52
+ rename( './beep/boop.txt', null, done ); // $ExpectError
53
+ rename( './beep/boop.txt', undefined, done ); // $ExpectError
54
+ rename( './beep/boop.txt', [], done ); // $ExpectError
55
+ rename( './beep/boop.txt', {}, done ); // $ExpectError
56
+ rename( './beep/boop.txt', ( x: number ): number => x, done ); // $ExpectError
57
+ }
58
+
59
+ // The compiler throws an error if the function is provided a third argument which is not a function with the expected signature...
60
+ {
61
+ rename( './beep/boop.txt', './beep/foo.txt', 1 ); // $ExpectError
62
+ rename( './beep/boop.txt', './beep/foo.txt', false ); // $ExpectError
63
+ rename( './beep/boop.txt', './beep/foo.txt', true ); // $ExpectError
64
+ rename( './beep/boop.txt', './beep/foo.txt', null ); // $ExpectError
65
+ rename( './beep/boop.txt', './beep/foo.txt', undefined ); // $ExpectError
66
+ rename( './beep/boop.txt', './beep/foo.txt', [] ); // $ExpectError
67
+ rename( './beep/boop.txt', './beep/foo.txt', {} ); // $ExpectError
68
+ rename( './beep/boop.txt', './beep/foo.txt', ( x: number ): number => x ); // $ExpectError
69
+ }
70
+
71
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
72
+ {
73
+ rename(); // $ExpectError
74
+ rename( 'C:\\foo\\bar\\baz' ); // $ExpectError
75
+ rename( './beep/boop.txt', './beep/foo.txt' ); // $ExpectError
76
+ }
77
+
78
+ // Attached to main export is a `sync` method which returns an error or null...
79
+ {
80
+ rename.sync( './beep/boop.txt', './beep/foo.txt' ); // $ExpectType Error | null
81
+ }
82
+
83
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a string...
84
+ {
85
+ rename.sync( 1, './beep/foo.txt' ); // $ExpectError
86
+ rename.sync( false, './beep/foo.txt' ); // $ExpectError
87
+ rename.sync( true, './beep/foo.txt' ); // $ExpectError
88
+ rename.sync( null, './beep/foo.txt' ); // $ExpectError
89
+ rename.sync( undefined, './beep/foo.txt' ); // $ExpectError
90
+ rename.sync( [], './beep/foo.txt' ); // $ExpectError
91
+ rename.sync( {}, './beep/foo.txt' ); // $ExpectError
92
+ rename.sync( ( x: number ): number => x, './beep/foo.txt' ); // $ExpectError
93
+ }
94
+
95
+ // The compiler throws an error if the `sync` method is provided a second argument which is not a string...
96
+ {
97
+ rename.sync( './beep/boop.txt', 1 ); // $ExpectError
98
+ rename.sync( './beep/boop.txt', false ); // $ExpectError
99
+ rename.sync( './beep/boop.txt', true ); // $ExpectError
100
+ rename.sync( './beep/boop.txt', null ); // $ExpectError
101
+ rename.sync( './beep/boop.txt', undefined ); // $ExpectError
102
+ rename.sync( './beep/boop.txt', [] ); // $ExpectError
103
+ rename.sync( './beep/boop.txt', {} ); // $ExpectError
104
+ rename.sync( './beep/boop.txt', ( x: number ): number => x ); // $ExpectError
105
+ }
106
+
107
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
108
+ {
109
+ rename.sync(); // $ExpectError
110
+ rename.sync( './beep/boop.txt' ); // $ExpectError
111
+ }
@@ -0,0 +1,7 @@
1
+
2
+ Usage: rename [options] <old_path> <new_path>
3
+
4
+ Options:
5
+
6
+ -h, --help Print this message.
7
+ -V, --version Print the package version.
@@ -0,0 +1,15 @@
1
+ {
2
+ "string": [],
3
+ "boolean": [
4
+ "help",
5
+ "version"
6
+ ],
7
+ "alias": {
8
+ "help": [
9
+ "h"
10
+ ],
11
+ "version": [
12
+ "V"
13
+ ]
14
+ }
15
+ }