Methods
(static) create()
Static factory method to create an instance of File
Returns:
File instance
Example
import File from 'file-js';
const file = File.create();
getAbsolutePath()
Returns the absolutePath
Returns:
String
Example
import File from 'file-js';
const file = File.create('myFile');
console.log(file.getAbsolutePath());
getCanonicalPath()
Returns the canonical path
Returns:
String
Example
import File from 'file-js';
const file = File.create('myFile');
console.log(file.getCanonicalPath());
getDepthSync()
Synchronously caculate the depth of a directory
Returns:
boolean
Example
import File from 'file-js';
const file = File.create('myDirectory');
console.log(file.getDepthSync());
getFiles(globopt)
Get list of file objects, if pathname is a directory
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
glob |
string |
<optional> |
file glob |
Returns:
a promise. If the Promise fulfils, the fulfilment value is a list of File objects
Example
import File from 'file-js';
// get last modified time of all json files
const file = File.create('./myDirectory');
file.getFiles('*.json')
.then((jsonFiles) => {
console.log(jsonFiles.map(file => file.lastModifiedSync()));
});
getFileSync()
Synchronously get list of file objects, if pathname is a directory
Returns:
array of files
Example
import File from 'file-js';
const file = File.create('./myHiddenFile');
const files = file.getFileSync();
console.log(files);
getList(globopt)
Get list of file objects, if pathname is a directory
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
glob |
string |
<optional> |
file glob |
Returns:
a promise. If the Promise fulfils, the fulfilment value is a list of pathnames
Example
import File from 'file-js';
// get all json files
const file = File.create('./myDirectory');
file.getFiles('*.json')
.then((jsonFiles) => {
console.log(jsonFiles);
});
getListSync()
Synchronously get list of files, if pathname is a directory
Returns:
array of files
Example
import File from 'file-js';
const file = File.create('./myHiddenFile');
const files = file.getListSync();
console.log(files);
getName()
Returns the pathname as a string
Returns:
String
Example
import File from 'file-js';
const file = File.create('myDirectory');
console.log(file.getName());
getPathExtension()
Returns the file extension.
Returns:
String
Example
import File from 'file-js';
const file = File.create('./tmp.sh');
console.log(file.getPathExtension()); // sh
isDirectory()
Determine if pathname is a file
Returns:
If the Promise fulfils, the fulfilment value is a boolean indicating if the pathname is a file
Example
import File from 'file-js';
const file = File.create('myDirectory');
file.isFile((isFile) => {
console.log(isFile);
});
isDirectory()
Determine if pathname is a directory
Returns:
If the Promise fulfils, the fulfilment value is a boolean indicating if the pathname is a directory
Example
import File from 'file-js';
const file = File.create('myDirectory');
file.isDirectory((isDirectory) => {
console.log(isDirectory);
});
isDirectory()
Determine if pathname is a file
Returns:
If the Promise fulfils, the fulfilment value is a boolean indicating if the pathname is a file
Example
import File from 'file-js';
const file = File.create('myDirectory');
file.isFile((isFile) => {
console.log(isFile);
});
isDirectorySync()
Synchronously determine if pathname is a directory
Returns:
boolean
Example
import File from 'file-js';
const file = File.create('myDirectory');
if (file.isDirectorySync()) {
console.log('processing directory');
}
isFileSync()
Synchronously determine if pathname is a file
Returns:
boolean
Example
import File from 'file-js';
const file = File.create('myDirectory');
if (file.isFileSync()) {
console.log('processing file');
}
isHiddenSync()
Synchronously determine if pathname is a hidden file
Returns:
boolean
Example
import File from 'file-js';
const file = File.create('./myHiddenFile');
if (file.isHiddenSync()) {
console.log('processing hidden file');
}
isSocket()
Determine if pathname is a Socket
Returns:
If the Promise fulfils, the fulfilment value is a boolean indicating if the pathname is a Socket
Example
import File from 'file-js';
const file = File.create('mySocket');
file.isSocket((isSocket) => {
console.log(isSocket);
});
isSocketSync()
Synchronously determine if pathname is a socket
Returns:
boolean
Example
import File from 'file-js';
const file = File.create('mysocket');
if (file.isSocketSync()) {
console.log('processing socket');
}
rename(pathname)
Renames the abstract pathname
Parameters:
Name | Type | Description |
---|---|---|
pathname |
string | File | pathname either as a string or File instance |
Returns:
If the Promise fulfils, the fulfilment value is undefined
Example
import File from 'file-js';
const original = File.create('fileA');
const renameTo = File.create('fileB');
original
.rename(renameTo)
.then(() => {
console.log(original.getName()) // prints fileA
});
withLock()
Locks the pathname
Returns:
returning value of function
Example
import File from 'file-js';
const file = File.create('myFile');
file.with(() => {
if (file.isFileSync()) {
file.delete();
}
});