file-js
Abstract representation of a pathname
Installation
npm install --save file-js
Common examples
The example below lists all files in myDir
const File = require('file-js');
const files = await new File('myDir').getList();
files.forEach(console.log);
Check file types
const pathname = new File('myFile');
if (await pathname.isFile()) {
console.log(`process ${pathname}`)
}
List files for a directory
Synchronously list files:
const dir = new File('myDirectory');
const files = dir.getListSync()
console.log(files.forEach(console.log));
Asynchronously list files:
const dir = new File('myDirectory');
dir.getList().forEach(console.log);
Check permissions
Check that a pathname has write permission:
const file = new File('myFile');
if (await file.isWritable()) {
console.log(`Able to write to ${file.getName()}`);
}
Check that a pathname is executable:
const file = new File('myFile');
if (await file.isExecutable()) {
console.log(`Able to execute ${file.getName()}`);
}
Pathname changes and access
Get the last time a pathname was modified:
const file = new File('myFile');
const lastModified = file.lastModifiedSync();
console.log(`${file.getName()} was last modified on ${lastModified}`);
Get the last time a pathname was accessed:
const file = new File('myFile');
const lastAccessed = file.lastAccessedSync();
console.log(`${file.getName()} was last accessed on ${lastAccessed}`);
File size
Check a file is less than 1k:
const file = new File('myFile');
if (file.sizeSync() < 1024) {
console.log(`${file.getName()} < 1k`);
}
Recursive delete
Deletes a folder and all of its contents:
const file = new File('myDir/');
file.deleteRecursively()
.then(() => console.log('myDir/ deleted'))
.catch(console.error);
Recursive copy
Copies a folder and all of its contents. Optionally overwriting an existing destination:
If destinationDir/
already exists, you will not be able to copy:
const file = new File('sourceDir/');
file.copyRecursively('destinationDir/')
.then(() => console.log('sourceDir/ copied to destinationDir/'))
.catch(console.error); // message: 'Directory: "destinationDir/" already exists.'
If destinationDir/
already exists and you want to overwrite it:
const file = new File('sourceDir/');
file.copyRecursively('destinationDir/', { overwrite: true })
.then(() => console.log('sourceDir/ copied to destinationDir/'))
.catch(console.error);