README.md (4862B)
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 remove 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 class="links"> 204 205 [node-fs]: https://nodejs.org/api/fs.html 206 207 [@stdlib/fs/rmdir]: https://www.npmjs.com/package/@stdlib/fs/tree/main/rmdir 208 209 </section> 210 211 <!-- /.links -->