@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,231 @@
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 resolve = require( 'path' ).resolve;
24
+ var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25
+ var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' );
26
+ var cwd = require( '@stdlib/process/cwd' );
27
+ var exists = require( './../../exists' ).sync;
28
+ var nulls = require( '@stdlib/array/base/nulls' );
29
+ var format = require( '@stdlib/string/format' );
30
+ var validate = require( './validate.js' );
31
+
32
+
33
+ // VARIABLES //
34
+
35
+ var MODES = {
36
+ 'first': first,
37
+ 'some': some,
38
+ 'all': all,
39
+ 'each': each
40
+ };
41
+
42
+
43
+ // FUNCTIONS //
44
+
45
+ /**
46
+ * Synchronously resolves the first path match from a set of paths by walking parent directories.
47
+ *
48
+ * @private
49
+ * @param {Array<string>} paths - paths to resolve
50
+ * @param {string} dir - base directory
51
+ * @returns {Array<string>} resolved paths
52
+ */
53
+ function first( paths, dir ) {
54
+ var child;
55
+ var spath;
56
+ var out;
57
+ var i;
58
+
59
+ // Start at a base directory and continue moving up through each parent directory...
60
+ out = [];
61
+ while ( child !== dir ) {
62
+ for ( i = 0; i < paths.length; i++ ) {
63
+ spath = resolve( dir, paths[ i ] );
64
+ if ( exists( spath ) ) {
65
+ out.push( spath );
66
+ return out;
67
+ }
68
+ }
69
+ child = dir;
70
+ dir = resolve( dir, '..' );
71
+ }
72
+ return out;
73
+ }
74
+
75
+ /**
76
+ * Synchronously resolves one or more paths from a set of paths at a directory level by walking parent directories.
77
+ *
78
+ * @private
79
+ * @param {Array<string>} paths - paths to resolve
80
+ * @param {string} dir - base directory
81
+ * @returns {Array<string>} resolved paths
82
+ */
83
+ function some( paths, dir ) {
84
+ var child;
85
+ var spath;
86
+ var out;
87
+ var i;
88
+
89
+ // Start at a base directory and continue moving up through each parent directory...
90
+ out = [];
91
+ while ( child !== dir ) {
92
+ for ( i = 0; i < paths.length; i++ ) {
93
+ spath = resolve( dir, paths[ i ] );
94
+ if ( exists( spath ) ) {
95
+ out.push( spath );
96
+ }
97
+ }
98
+ if ( out.length > 0 ) {
99
+ return out;
100
+ }
101
+ child = dir;
102
+ dir = resolve( dir, '..' );
103
+ }
104
+ return out;
105
+ }
106
+
107
+ /**
108
+ * Synchronously resolves all paths from a set of paths at a directory level by walking parent directories.
109
+ *
110
+ * @private
111
+ * @param {Array<string>} paths - paths to resolve
112
+ * @param {string} dir - base directory
113
+ * @returns {Array<string>} resolved paths
114
+ */
115
+ function all( paths, dir ) {
116
+ var child;
117
+ var spath;
118
+ var out;
119
+ var i;
120
+
121
+ // Start at a base directory and continue moving up through each parent directory...
122
+ out = [];
123
+ while ( child !== dir ) {
124
+ for ( i = 0; i < paths.length; i++ ) {
125
+ spath = resolve( dir, paths[ i ] );
126
+ if ( exists( spath ) ) {
127
+ out.push( spath );
128
+ }
129
+ }
130
+ if ( out.length === paths.length ) {
131
+ return out;
132
+ }
133
+ out = [];
134
+ child = dir;
135
+ dir = resolve( dir, '..' );
136
+ }
137
+ return out;
138
+ }
139
+
140
+ /**
141
+ * Synchronously resolves each path from a set of paths by walking parent directories.
142
+ *
143
+ * @private
144
+ * @param {Array<string>} paths - paths to resolve
145
+ * @param {string} dir - base directory
146
+ * @returns {Array<(string|null)>} resolved paths
147
+ */
148
+ function each( paths, dir ) {
149
+ var count;
150
+ var child;
151
+ var spath;
152
+ var out;
153
+ var i;
154
+
155
+ count = 0;
156
+ out = nulls( paths.length );
157
+
158
+ // Start at a base directory and continue moving up through each parent directory...
159
+ while ( child !== dir ) {
160
+ for ( i = 0; i < paths.length; i++ ) {
161
+ if ( out[ i ] !== null ) {
162
+ continue;
163
+ }
164
+ spath = resolve( dir, paths[ i ] );
165
+ if ( exists( spath ) ) {
166
+ out[ i ] = spath;
167
+ count += 1;
168
+ }
169
+ }
170
+ if ( count === paths.length ) {
171
+ break;
172
+ }
173
+ child = dir;
174
+ dir = resolve( dir, '..' );
175
+ }
176
+ return out;
177
+ }
178
+
179
+
180
+ // MAIN //
181
+
182
+ /**
183
+ * Synchronously resolves paths from a set of paths by walking parent directories.
184
+ *
185
+ * @param {Array<string>} paths - paths to resolve
186
+ * @param {Options} [options] - function options
187
+ * @param {string} [options.dir] - base directory
188
+ * @param {string} [options.mode='all'] - mode of operation
189
+ * @throws {TypeError} first argument must be an array of strings
190
+ * @throws {TypeError} options argument must be an object
191
+ * @throws {TypeError} must provide valid options
192
+ * @returns {Array<(string|null)>} resolved paths
193
+ *
194
+ * @example
195
+ * var paths = resolveParentPaths( [ 'package.json', 'package-lock.json' ] );
196
+ */
197
+ function resolveParentPaths( paths, options ) {
198
+ var opts;
199
+ var mode;
200
+ var dir;
201
+ var fcn;
202
+ var err;
203
+
204
+ if ( !isStringArray( paths ) ) {
205
+ if ( isArrayLikeObject( paths ) && paths.length === 0 ) {
206
+ return [];
207
+ }
208
+ throw new TypeError( format( 'invalid argument. First argument must be an array of strings. Value: `%s`.', paths ) );
209
+ }
210
+ opts = {};
211
+ if ( arguments.length > 1 ) {
212
+ err = validate( opts, options );
213
+ if ( err ) {
214
+ throw err;
215
+ }
216
+ }
217
+ if ( opts.dir ) {
218
+ dir = resolve( cwd(), opts.dir );
219
+ } else {
220
+ dir = cwd();
221
+ }
222
+ mode = opts.mode || 'all';
223
+
224
+ fcn = MODES[ mode ];
225
+ return fcn( paths, dir );
226
+ }
227
+
228
+
229
+ // EXPORTS //
230
+
231
+ module.exports = resolveParentPaths;
@@ -0,0 +1,81 @@
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 isObject = require( '@stdlib/assert/is-plain-object' );
24
+ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25
+ var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26
+ var contains = require( '@stdlib/array/base/assert/contains' ).factory;
27
+ var format = require( '@stdlib/string/format' );
28
+
29
+
30
+ // VARIABLES //
31
+
32
+ var isMode = contains( [ 'first', 'some', 'all', 'each' ] );
33
+
34
+
35
+ // MAIN //
36
+
37
+ /**
38
+ * Validates function options.
39
+ *
40
+ * @private
41
+ * @param {Object} opts - destination object
42
+ * @param {Options} options - function options
43
+ * @param {string} [options.dir] - base directory
44
+ * @param {string} [options.mode] - operation mode
45
+ * @returns {(Error|null)} error object or null
46
+ *
47
+ * @example
48
+ * var opts = {};
49
+ * var options = {
50
+ * 'dir': '/foo/bar/baz',
51
+ * 'mode': 'some'
52
+ * };
53
+ *
54
+ * var err = validate( opts, options );
55
+ * if ( err ) {
56
+ * throw err;
57
+ * }
58
+ */
59
+ function validate( opts, options ) {
60
+ if ( !isObject( options ) ) {
61
+ return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
62
+ }
63
+ if ( hasOwnProp( options, 'dir' ) ) {
64
+ opts.dir = options.dir;
65
+ if ( !isString( opts.dir ) ) {
66
+ return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'dir', opts.dir ) );
67
+ }
68
+ }
69
+ if ( hasOwnProp( options, 'mode' ) ) {
70
+ opts.mode = options.mode;
71
+ if ( !isMode( opts.mode ) ) {
72
+ return new TypeError( format( 'invalid option. `%s` option must be a valid mode. Option: `%s`.', 'mode', opts.mode ) );
73
+ }
74
+ }
75
+ return null;
76
+ }
77
+
78
+
79
+ // EXPORTS //
80
+
81
+ module.exports = validate;
@@ -0,0 +1,78 @@
1
+ {
2
+ "name": "@stdlib/fs/resolve-parent-paths",
3
+ "version": "0.0.0",
4
+ "description": "Resolve paths from a set of paths by walking parent directories.",
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
+ "resolve-parent-paths": "./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
+ "resolve",
59
+ "parent",
60
+ "path",
61
+ "async",
62
+ "sync",
63
+ "file",
64
+ "directory",
65
+ "dir",
66
+ "find",
67
+ "up",
68
+ "findup",
69
+ "find-up",
70
+ "upsearch",
71
+ "search",
72
+ "lookup",
73
+ "look-up",
74
+ "locate",
75
+ "walk",
76
+ "filesystem"
77
+ ]
78
+ }
@@ -0,0 +1,233 @@
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
+ # Unlink
22
+
23
+ > Remove a directory entry.
24
+
25
+ <section class="usage">
26
+
27
+ ## Usage
28
+
29
+ ```javascript
30
+ var unlink = require( '@stdlib/fs/unlink' );
31
+ ```
32
+
33
+ #### unlink( path, clbk )
34
+
35
+ Asynchronously removes a directory entry specified by `path`.
36
+
37
+ <!-- run-disable -->
38
+
39
+ ```javascript
40
+ var join = require( 'path' ).join;
41
+ var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
42
+
43
+ unlink( fpath, done );
44
+
45
+ function done( error ) {
46
+ if ( error ) {
47
+ throw error;
48
+ }
49
+ }
50
+ ```
51
+
52
+ #### unlink.sync( path )
53
+
54
+ Synchronously removes a directory entry specified by `path`.
55
+
56
+ <!-- run-disable -->
57
+
58
+ ```javascript
59
+ var join = require( 'path' ).join;
60
+ var fpath = join( __dirname, 'examples', 'fixtures', 'file.txt' );
61
+
62
+ var err = unlink.sync( fpath );
63
+ if ( err instanceof Error ) {
64
+ throw err;
65
+ }
66
+ ```
67
+
68
+ </section>
69
+
70
+ <!-- /.usage -->
71
+
72
+ <section class="notes">
73
+
74
+ ## Notes
75
+
76
+ - If a provided `path` is a symbolic link, the function removes the symbolic link named by the `path` and does not affect any file or directory named by the contents of the symbolic link. Otherwise, the function removes the link named by the provided `path` and decrements the link count of the file referenced by the link.
77
+
78
+ When a file's link count becomes `0` 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; however, the removal of file contents is postponed until all references to the file are closed.
79
+
80
+ If the `path` refers to a socket, FIFO, or device, processes which have the object open may continue to use it.
81
+
82
+ - The path argument should **not** be a directory. To remove a directory, use [`rmdir()`][@stdlib/fs/rmdir].
83
+
84
+ - The difference between `unlink.sync` and [`fs.unlinkSync()`][node-fs] is that [`fs.unlinkSync()`][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
85
+
86
+ <!-- run-disable -->
87
+
88
+ ```javascript
89
+ var fs = require( 'fs' );
90
+
91
+ // Check for path existence to prevent an error being thrown...
92
+ if ( fs.existsSync( '/path/to' ) ) {
93
+ fs.unlinkSync( '/path/to/file.txt' );
94
+ }
95
+ ```
96
+
97
+ can be replaced by an approach which addresses existence via `error` handling.
98
+
99
+ <!-- run-disable -->
100
+
101
+ ```javascript
102
+ var unlink = require( '@stdlib/fs/unlink' );
103
+
104
+ // Explicitly handle the error...
105
+ var err = unlink.sync( '/path/to/file.txt' );
106
+ if ( err instanceof Error ) {
107
+ // You choose what to do...
108
+ throw err;
109
+ }
110
+ ```
111
+
112
+ </section>
113
+
114
+ <!-- /.notes -->
115
+
116
+ <section class="examples">
117
+
118
+ ## Examples
119
+
120
+ <!-- eslint no-undef: "error" -->
121
+
122
+ ```javascript
123
+ var join = require( 'path' ).join;
124
+ var readFile = require( '@stdlib/fs/read-file' ).sync;
125
+ var writeFile = require( '@stdlib/fs/write-file' ).sync;
126
+ var exists = require( '@stdlib/fs/exists' ).sync;
127
+ var unlink = require( '@stdlib/fs/unlink' ).sync;
128
+
129
+ var src = join( __dirname, 'examples', 'fixtures', 'file.txt' );
130
+ var dest = join( __dirname, 'examples', 'tmp.txt' );
131
+
132
+ // Create a temporary file:
133
+ writeFile( dest, readFile( src ) );
134
+
135
+ // Confirm that the temporary file exists:
136
+ console.log( exists( dest ) );
137
+ // => true
138
+
139
+ // Delete the temporary file:
140
+ unlink( dest );
141
+
142
+ // Confirm that the temporary file no longer exists:
143
+ console.log( exists( dest ) );
144
+ // => false
145
+ ```
146
+
147
+ </section>
148
+
149
+ <!-- /.examples -->
150
+
151
+ * * *
152
+
153
+ <section class="cli">
154
+
155
+ ## CLI
156
+
157
+ <section class="usage">
158
+
159
+ ### Usage
160
+
161
+ ```text
162
+ Usage: unlink [options] <path>
163
+
164
+ Options:
165
+
166
+ -h, --help Print this message.
167
+ -V, --version Print the package version.
168
+ ```
169
+
170
+ </section>
171
+
172
+ <!-- /.usage -->
173
+
174
+ <section class="notes">
175
+
176
+ ### Notes
177
+
178
+ - Relative paths are resolved relative to the current working directory.
179
+ - Errors are written to `stderr`.
180
+
181
+ </section>
182
+
183
+ <!-- /.notes -->
184
+
185
+ <section class="examples">
186
+
187
+ ### Examples
188
+
189
+ <!-- run-disable -->
190
+
191
+ ```bash
192
+ $ unlink ./examples/fixtures/file.txt
193
+ ```
194
+
195
+ </section>
196
+
197
+ <!-- /.examples -->
198
+
199
+ </section>
200
+
201
+ <!-- /.cli -->
202
+
203
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
204
+
205
+ <section class="related">
206
+
207
+ * * *
208
+
209
+ ## See Also
210
+
211
+ - <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>
212
+
213
+ </section>
214
+
215
+ <!-- /.related -->
216
+
217
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
218
+
219
+ <section class="links">
220
+
221
+ [node-fs]: https://nodejs.org/api/fs.html
222
+
223
+ [@stdlib/fs/rmdir]: https://github.com/stdlib-js/fs/tree/main/rmdir
224
+
225
+ <!-- <related-links> -->
226
+
227
+ [@stdlib/fs/exists]: https://github.com/stdlib-js/fs/tree/main/exists
228
+
229
+ <!-- </related-links> -->
230
+
231
+ </section>
232
+
233
+ <!-- /.links -->
package/unlink/bin/cli ADDED
@@ -0,0 +1,81 @@
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 unlink = require( './../lib' );
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Main execution sequence.
36
+ *
37
+ * @private
38
+ * @returns {void}
39
+ */
40
+ function main() {
41
+ var flags;
42
+ var fpath;
43
+ var args;
44
+ var cli;
45
+
46
+ // Create a command-line interface:
47
+ cli = new CLI({
48
+ 'pkg': require( './../package.json' ),
49
+ 'options': require( './../etc/cli_opts.json' ),
50
+ 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
51
+ 'encoding': 'utf8'
52
+ })
53
+ });
54
+
55
+ // Get any provided command-line options:
56
+ flags = cli.flags();
57
+ if ( flags.help || flags.version ) {
58
+ return;
59
+ }
60
+
61
+ // Get any provided command-line arguments:
62
+ args = cli.args();
63
+
64
+ fpath = resolve( cwd(), args[ 0 ] );
65
+ unlink( fpath, done );
66
+
67
+ /**
68
+ * Callback invoked upon removing directory entry.
69
+ *
70
+ * @private
71
+ * @param {Error} [error] - error object
72
+ * @returns {void}
73
+ */
74
+ function done( error ) {
75
+ if ( error ) {
76
+ return cli.error( error );
77
+ }
78
+ }
79
+ }
80
+
81
+ main();