fs
access
accessfunction(path: PathLike, mode?: number): Observable<void>import { access } from '@rxnode/fs';
const file = 'package.json';
// Check if the file exists in the current directory.
access(file, fs.constants.F_OK).subscribe({
next() {
console.log(`${file} exists`);
},
error(err) {
console.log(`${file} does not exist`);
},
});
// Check if the file is readable.
access(file, fs.constants.R_OK).subscribe({
next() {
console.log(`${file} is readable`);
},
error(err) {
console.log(`${file} is not readable`);
},
});
// Check if the file is writable.
access(file, fs.constants.W_OK).subscribe({
next() {
console.log(`${file} is writable`);
},
error(err) {
console.log(`${file} is not writable`);
},
});
// Check if the file exists in the current directory, and if it is writable.
access(file, fs.constants.F_OK | fs.constants.W_OK).subscribe({
next() {
console.log(`${file} exists, and it is writable`);
},
error(err) {
console.error(
`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`
);
},
});Last updated
Was this helpful?