Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load/save functionality to the FileSystem extension #884

Merged
merged 12 commits into from
Jan 24, 2019
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 36 additions & 18 deletions Extensions/FileSystem/JsExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ module.exports = {
).setExtensionHelpPath("/all-features/filesystem");

extension
.addCondition(
"PathExists",
t("File or directory exists"),
t(
"Check if the file or directory exists."
),
t("The path _PARAM0_ exists"),
t("Filesystem/Windows, Linux, MacOS"),
"JsPlatform/Extensions/filesystem_path_exists24.png",
"JsPlatform/Extensions/filesystem_path_exists32.png"
)
.addParameter("string", t("Path to file or directory"), "", false)
.getCodeExtraInformation()
.setIncludeFile(
"Extensions/FileSystem/filesystemtools.js"
)
.setFunctionName("gdjs.fileSystem.pathExists");
.addCondition(
"PathExists",
t("File or directory exists"),
t(
"Check if the file or directory exists."
),
t("The path _PARAM0_ exists"),
t("Filesystem/Windows, Linux, MacOS"),
"JsPlatform/Extensions/filesystem_path_exists24.png",
"JsPlatform/Extensions/filesystem_path_exists32.png"
)
.addParameter("string", t("Path to file or directory"), "", false)
.getCodeExtraInformation()
.setIncludeFile(
"Extensions/FileSystem/filesystemtools.js"
)
.setFunctionName("gdjs.fileSystem.pathExists");

extension
.addAction(
Expand All @@ -59,6 +59,24 @@ module.exports = {
)
.setFunctionName("gdjs.fileSystem.makeDirectory");

extension
.addAction(
"SaveTextToFile",
Wend1go marked this conversation as resolved.
Show resolved Hide resolved
t("Save a text into a file"),
t("Save a text into a file."),
t("Save _PARAM0_ into file _PARAM1_"),
t("Filesystem/Windows, Linux, MacOS"),
"JsPlatform/Extensions/filesystem_save_file24.png",
"JsPlatform/Extensions/filesystem_save_file32.png"
)
.addParameter("string", t("Text"), "", false)
.addParameter("string", t("Save path"), "", false)
.getCodeExtraInformation()
.setIncludeFile(
"Extensions/FileSystem/filesystemtools.js"
)
.setFunctionName("gdjs.fileSystem.saveTextToFile");

extension
.addStrExpression(
"DesktopPath",
Expand Down Expand Up @@ -155,7 +173,7 @@ module.exports = {
)
.setFunctionName("gdjs.fileSystem.getTempPath");

extension
extension
.addStrExpression(
"PathDelimiter",
t("Path delimiter"),
Expand Down
17 changes: 17 additions & 0 deletions Extensions/FileSystem/filesystemtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ gdjs.fileSystem.makeDirectory = function (directory) {
}
}

/**
* Save a string into a file.
* @param {string} text The text string to be saved
* @param {string} savePath The absolute path on the filesystem
*/
gdjs.fileSystem.saveTextToFile = function (text, savePath) {
const fileSystem = typeof require !== 'undefined' ? require('fs') : null;

if (fileSystem) {
fileSystem.writeFile(savePath, text, (err) => {
if (err) {
console.error("Unable to save the text to path: '" + savePath + "' " + err);
Wend1go marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
}

/**
* Check if the file or directory exists.
* @param {string} path The path to the file or directory
Expand Down