Files
2024-01-25 04:05:58 +03:30

253 lines
6.9 KiB
JavaScript

/**
* Publish XEa ...
* a node js based application for publishing x-ea Expert ...
*
* Maintainer
*
* Hadi Khazaee Asl <https://saherelm.ir> (hadi_khazaee_asl@yahoo.com)
*/
//
//#region Imports ...
const XFileTools = require("./Node/x-file.tools");
const XArgsParser = require("./Node/x-arg.parser");
const XValueTools = require("./Node/x-value.tools");
const XLoggerTools = require("./Node/x-logger.tools");
//#endregion
//
//#region Constants / Variables ...
const MESSAGES = {
//
// Errors ...
InvalidArgs: "invalid arg ...",
ActionFailed: "action failed ...",
DuplicateName: "duplicate name ...",
//
// Messages ...
CheckNameValue: "checking pack name value ...",
CheckPackExists: "checking given pack name exists or not ...",
CreatePackFolder: "creating publish destination folder ...",
StartPacking: "start packing ...",
Succeed: "packing successfully finished ...",
};
//#endregion
//
//#region Actions ...
//#endregion
//
//#region Startup ...
const args = process.argv;
const packName = process.argv[2];
const packFolderPath = XFileTools.joinPath(__dirname, "Publishes");
//
// Startup function ...
async function startup() {
//
// Check pack name contains value ...
XLoggerTools.simpleInfoLog(MESSAGES.CheckNameValue);
if (!XValueTools.isValidArg(packName)) {
//
XLoggerTools.simpleErrorLog(MESSAGES.InvalidArgs);
exit();
}
//
// Check Pack Path Exists ...
if (!XFileTools.isDirectoryExists(packFolderPath)) {
//
// Create Pack Folder ...
const isCreated = XFileTools.createDirectory(packFolderPath);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
}
//
// Prepare Pack Dest Folder Path ...
const packPath = XFileTools.joinPath(packFolderPath, packName);
//
// Check Existance ...
XLoggerTools.simpleInfoLog(MESSAGES.CheckPackExists);
if (XFileTools.isDirectoryExists(packPath)) {
//
XLoggerTools.simpleErrorLog(MESSAGES.DuplicateName);
exit();
}
//
// Create Dest Pack Path Folder ...
XLoggerTools.simpleInfoLog(MESSAGES.CreatePackFolder);
let isCreated = XFileTools.createDirectory(packPath);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
//
// Generate a List of Files ...
//
// Ex4 Files ...
let ex4FilesList = await XFileTools.getRecursiveFilesList(
__dirname,
[".ex4"]
);
//
// MQ4 Files ...
let mq4FilesList = await XFileTools.getRecursiveFilesList(
__dirname,
[".mq4"]
);
//
// Filter BKP files ...
//
// From Ex4 ...
ex4FilesList = ex4FilesList.filter(f =>
!f.toLowerCase().includes("bkp")
&& f.toLowerCase().includes("x-saherelm.")
);
//
// From MQ4 ...
mq4FilesList = mq4FilesList.filter(f =>
!f.toLowerCase().includes("bkp")
&& f.toLowerCase().includes("x-saherelm.")
);
//
// Create required Dest Folders on Pack Dest ...
XLoggerTools.simpleInfoLog(MESSAGES.StartPacking);
//
const packEx4Folder = XFileTools.joinPath(packPath, "Ex4");
isCreated = XFileTools.createDirectory(packEx4Folder);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed());
exit();
}
//
const packMQ4Folder = XFileTools.joinPath(packPath, "MQ4");
isCreated = XFileTools.createDirectory(packMQ4Folder);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed());
exit();
}
//
const packReportsFolder = XFileTools.joinPath(packPath, "Reports");
isCreated = XFileTools.createDirectory(packReportsFolder);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed());
exit();
}
//
// Transfer Files ...
for(let idx = 0; idx < ex4FilesList.length; idx++) {
//
const ex4 = ex4FilesList[idx];
const mq4 = mq4FilesList[idx];
//
// Clear Current Dir and File Name to found Path ...
const clearEx4FilePath = ex4.replace(__dirname, "");
const clearMQ4FilePath = mq4.replace(__dirname, "");
//
const ex4FileName = XFileTools.basename(clearEx4FilePath);
const mq4FileName = XFileTools.basename(clearMQ4FilePath);
//
let ex4FilePath = clearEx4FilePath.replace(ex4FileName, "");
if (ex4FilePath.startsWith(XFileTools.PathSeparator)) {
ex4FilePath = ex4FilePath.substring(1);
}
if (ex4FilePath.endsWith(XFileTools.PathSeparator)) {
ex4FilePath = ex4FilePath.substring(0, ex4FilePath.length - 1);
}
ex4FilePath = XFileTools.joinPath(packEx4Folder, ex4FilePath);
if (!XFileTools.isDirectoryExists(ex4FilePath)) {
//
isCreated = XFileTools.createDirectory(ex4FilePath);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
}
let isTransfered = await XFileTools.copyFile(ex4, ex4FilePath);
if (!isTransfered) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
//
let mq4FilePath = clearMQ4FilePath.replace(mq4FileName, "");
if (mq4FilePath.startsWith(XFileTools.PathSeparator)) {
mq4FilePath = mq4FilePath.substring(1);
}
if (mq4FilePath.endsWith(XFileTools.PathSeparator)) {
mq4FilePath = mq4FilePath.substring(0, mq4FilePath.length - 1);
}
mq4FilePath = XFileTools.joinPath(packMQ4Folder, mq4FilePath);
if (!XFileTools.isDirectoryExists(mq4FilePath)) {
//
isCreated = XFileTools.createDirectory(mq4FilePath);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
}
isTransfered = await XFileTools.copyFile(mq4, mq4FilePath);
if (!isTransfered) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
}
//
// Adding Pack Time ...
const packVersionFile = XFileTools.joinPath(packPath, "Version.txt");
const packVersionContent = `packed at: ${new Date()}`;
isCreated = await XFileTools.writeFile(
packVersionFile,
packVersionContent
);
if (!isCreated) {
//
XLoggerTools.simpleErrorLog(MESSAGES.ActionFailed);
exit();
}
//
// Succeed the Action ...
XLoggerTools.simpleInfoLog(MESSAGES.Succeed);
exit();
}
/**
* exit from app ...
*/
function exit() {
process.exit();
}
//
// Startup Application ...
startup();
//#endregion