Hey guys, I put together a little .uno file to allow the use of some System.IO functions in Javascript. It’s kind of like the FuseJS/Storage API but with a few extra functions. You can download it here, hopefully some people will find it useful.
Documentation
How to use
- 1: Download the FileOps.uno file and place it in the root directory of your Fuse project (the one that contains your
.unoproj
file). - 2: Declare it somewhere in your Main
.ux
file like so:
<FileOps ux:Global='FileOps' />
- 3: Reference it in your JS:
var FileOps = require('FileOps');
Available Functions
FileOps.FileWrite("path/to/file.txt", "text")
- Filepath is a string which references the file to be made/written to. Can just be a filename if the file is in the current working directory.
- Text is a string which will be written to the file.
FileOps.FileRead("file.txt")
- Returns a string containing the contents of the specified file.
FileOps.GetUserDataDirectory()
- Returns a string containing the path of the User Data Directory.
- On Android this is the app’s data folder:
/data/data/com.AppName/files
- On Windows this is the Local App Data folder:
C:\Users\Username\AppData\Local
- I don’t know what it is on Mac/iOS, I don’t have a device to test it on.
FileOps.GetWorkingDirectory()
- Returns a string containing the path of the current working directory.
- By default on Android, this is the root of your phone so just:
/
- By default on Windows, this is the folder containing the SublimeText.exe file, in my case:
D:\Program Files\Sublime Text 3
FileOps.ChangeDirectory("path")
- Takes one argument; the path to which you want to change the current working directory (as a string)
- Works like the commonly used
cd
command
FileOps.MakeDirectory("foldername")
- Makes a new folder in the current working directory with foldername as its name
- Takes one argument; foldername, which should be a string
FileOps.FileExists("path/to/file.txt")
- Checks if the specified file exists
- Returns the result as a boolean; either
true
orfalse
- Takes one argument as a string; the path to a file. Can just be a filename if the file is in the current working directory.
FileOps.DirectoryExists("dirname")
- Works the same as
FileOps.FileExists
, but for Directories instead
FileOps.DeleteFile("path/to/file.txt")
- Deletes the specified file
- Takes one argument as a string; the path to a file. Can just be a filename if the file is in the current working directory.
- Returns
true
on successful deletion. - Returns
false
and logs to the console if the specified file does not exist.
FileOps.DeleteDirectory("dirname", recursive)
- Deletes the specified directory
- Takes two arguments; the directory name and recursive
-
recursive
should be a string containing the values either"true"
or"false"
. Take care not to specify it as a boolean. When true it deletes all directories, subdirectories and files within the specified directory. - Returns
true
on successful deletion. - Returns
false
and logs to the console if the specified directory does not exist.
FileOps.RenameFile("oldName", "newName", overwrite)
- Renames a file
oldName
tonewName
-
overwrite
should be a string containing the values either"true"
or"false"
. Take care not to specify it as a boolean. Defaults to false, so you don’t have to specify it unless you want it to be true. - When true, if
newName
already exists, it overwrites it when renaming and returns a booleantrue
on successful rename - When false, if
newName
already exists, it returns a booleanfalse
and logs to the console. IfnewName
doesn’t exist, it just returns a booleantrue
- If
oldName
doesn’t exist, returns booleanfalse
and logs to the console