File

Module: File

Perform common file system tasks like write, read and download.
Author:
  • FeniXEngine Contributors
License:

Methods

(static) cleanPath(path) → {string}

Cleans the path by replacing two forward slashes with one.
Example
import { cleanPath } from 'fenix-tools'

const badUrl = 'C://Path//to/something//'

console.log(cleanPath(badUrl)) // => 'C:/Path/to/something/'
Since:
  • 1.0.0
Parameters
Name Type Description
path string The path you want to clean.
Returns
The cleaned string.
Type
string

(async, static) downloadFile(config) → {Promise}

Async
An async function which downloads a file via node's http or https.
Example
import {downloadFile, tryWriteFile} from 'fenix-tools'

downloadFile({
 url: 'http://fenixenginemv.gitlab.io/img/fenix-logo-signature.png'
port: 80,
})
.then(data => {
  console.log(data) // => The downloaded file's data
  tryWriteFile('/', data) // => writes file to machine
})
Since:
  • 1.0.0
See:
Parameters
Name Type Description
config object A configuration object.
Properties
Name Type Attributes Default Description
url string The url to the file you want to download
port number <optional>
443 The port number to use
agent object <optional>
http.globalAgent How much gold the party starts with.
Returns (Async)
- Returns a promise that resolves the file's data from the url
Type
Promise

(static) fileExists(path, extensions) → {Boolean}

Check if a file exists.
Example
import { fileExists } from 'fenix-tools'

console.log(fileExists('./path/to/file')) // => returns true or false
Since:
  • 1.0.0
See:
Parameters
Name Type Description
path string The path to the file
extensions array the extension(s) of file you want to check
Returns
true if the file exists.
Type
Boolean

(async, static) hasWebAccess(urlopt) → {promise}

Async
An async function which determines if the current computer has internet access by pinging to a server. If no url is provided it checks navigator.online
Example
import { hasWebAccess } from 'fenix-tools'

const canDownload = await hasWebAccess('http://google.com')
console.log(canDownload) // => returns true

hasWebAccess('http://google.com')
.then('Web connection is live!)
Since:
  • 1.0.0
Parameters
Name Type Attributes Description
url string <optional>
A url to a server
Returns (Async)
A promise that resolves to true or false
Type
promise

(async, static) loadJSON(path) → {promise}

Async
Loads a JSON file. and parses it Decides to use node or fetch based on platform. If using nwjs it will use node else it falls back to fetch()
Example
import {loadJSON} from 'fenix-tools'

loadJSON('./data/highscores.json')
.then(data => {
  // success for parsing and loading JSON file
  console.log(data)  // => A parsed JSON object.
})
Since:
  • 1.0.0
See:
Parameters
Name Type Description
path any The path to the JSON file.
Returns (Async)
promise - A promise that resolves the parsed JSON
Type
promise

(static) loadLocalFile(path, encodingopt) → {promise}

Load a file on the local machine.
Example
import {loadLocalFile} from 'fenix-tools'

loadLocalFile('./img/pictures/character1.png)
.then(data => {
 // Local file loaded success
 console.log(data)  // => A parsed JSON object.
})
Since:
  • 1.0.0
Parameters
Name Type Attributes Default Description
path string Path to the file.
encoding string <optional>
'utf8' The type of file encoding
Returns
A promise that resolves the data
Type
promise

(static) localFileExists(paths) → {boolean}

Checks if file(s) exist on a local machine using node fs.
Example
import {localFileExists} from 'fenix-tools'

localFileExists('./img/pictures/myPicture.png') // => Returns true or false
Since:
  • 1.0.0
Parameters
Name Type Description
paths string | array A path or array of paths to check.
Returns
True if the file path or paths exist
Type
boolean

(static) projectPath() → {string}

Gets the location of the current game.exe, a full root project path.
Example
import {projectPath} from 'fenix-tools'

projectPath() // => 'C:/fullpath/to/project'
Since:
  • 1.0.0
Returns
The current project root path
Type
string

(static) tryWriteFile(filepath, data, optionsopt) → {promise}

Attempts to write given data to a file.
Example
import { tryWriteFile } from 'fenix-tools'

tryWriteFile('path/to/file', 'Data to write to file', 'utf8')
.then(() => {
 // Success writing file to machine
})
Since:
  • 1.0.0
Parameters
Name Type Attributes Default Description
filepath string Path to an existing or new file you want to write to
data string The data you would like to write to the file
options string | object <optional>
'utf8' The options object or a string with the encoding
Returns
A promise that resolves if successfully written to file
Type
promise

(async, static) webFileExists(url) → {promise}

Async
An async function for checking if a file exist on a server.
Example
import { webFileExists } from 'fenix-tools'

const file = 'http://example.com/file.png'
 const fileExists = await webFileExists(file)
console.log(fileExists)  // => returns true if file exists
// or
webFileExists(file)
.then(console.log('File exists!'))
.catch(console.log('Unable to get file))
Since:
  • 1.0.0
Parameters
Name Type Description
url string The url to the file
Returns (Async)
Promise that resolves to true if the file exists.
Type
promise