From bb502ca4b73402e5ca9c59609fe0cf808ea2145b Mon Sep 17 00:00:00 2001 From: Taegus Date: Wed, 28 Feb 2024 11:56:37 +0100 Subject: [PATCH] replaced shelljs with built in node functions --- build.bat | 1 - build.sh | 1 - index.js | 1 - package.json | 1 - units/download.js | 39 +++++++++++++++++++-------------------- units/service.js | 22 +++++++++++----------- units/utils.js | 3 +-- 7 files changed, 31 insertions(+), 37 deletions(-) diff --git a/build.bat b/build.bat index f3ffb87..7bb6d18 100644 --- a/build.bat +++ b/build.bat @@ -8,7 +8,6 @@ call nexe index.js --build -o .\bin\win\guardian-win64.exe copy .\tools\cgservice.exe .\bin\win xcopy /s /q /i html bin\win\html xcopy /s /q /i dist\index.js bin\win\index.js* -xcopy /s /q /i dist\exec-child.js bin\win\exec-child.js* copy .\commands\win\*.* .\bin\win copy exclude.txt .\bin\win\exclude.txt copy package.json .\bin\win\package.json diff --git a/build.sh b/build.sh index f1f39b0..c602dd9 100644 --- a/build.sh +++ b/build.sh @@ -10,5 +10,4 @@ cp ./package.json ./bin/linux/package.json cp ./config.json.sample ./bin/linux/config.json cp ./ccx-guardian.service.template ./bin/linux cp ./dist/index.js ./bin/linux/index.js -cp ./dist/exec-child.js ./bin/linux/exec-child.js tar -czf ./bin/linux/guardian-linux64.tar.gz --exclude guardian-linux64.tar.gz -C ./bin/linux . diff --git a/index.js b/index.js index e467cf7..7f4a6e7 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,6 @@ try { alias: "h", type: Boolean }]); - } catch (err) { console.error("\nUknown command line parameter. Use --help for instructions."); process.exit(); diff --git a/package.json b/package.json index bb16555..7bb0caf 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "pure-uuid": "^1.6.2", "read-last-lines": "^1.8.0", "request": "^2.88.2", - "shelljs": "^0.8.5", "string-template": "^1.0.0", "tar": "^6.1.11", "username": "^7.0.0", diff --git a/units/download.js b/units/download.js index f814884..b123074 100644 --- a/units/download.js +++ b/units/download.js @@ -1,4 +1,4 @@ -// Copyright (c) 2019-2022, Taegus Cromis, The Conceal Developers +// Copyright (c) 2019-2024, Taegus Cromis, The Conceal Developers // // Please see the included LICENSE file for more information. @@ -7,7 +7,6 @@ import downloadRelease from "download-github-release"; import extractZIP from "extract-zip"; import extractTAR from "tar"; import osInfo from "linux-os-info"; -import shell from "shelljs"; import path from "path"; import fs from "fs"; import os from "os"; @@ -53,11 +52,11 @@ export function downloadLatestDaemon(nodePath, callback) { var linuxOSInfo = null; if (fs.existsSync(finalTempDir)) { - shell.rm('-rf', finalTempDir); + fs.rmSync(finalTempDir, { recursive: true, force: true }); } // create the temp dir again - shell.mkdir('-p', finalTempDir); + fs.mkdirSync(finalTempDir, { recursive: true }); // only for linux try to get it if (process.platform === "linux") { @@ -103,17 +102,17 @@ export function downloadLatestDaemon(nodePath, callback) { if (items.length > 0) { extractArchive(path.join(finalTempDir, items[0]), finalTempDir, function (success) { if (success) { - shell.rm('-rf', path.join(finalTempDir, items[0])); + fs.rmSync(path.join(finalTempDir, items[0]), { recursive: true, force: true }); fs.readdir(finalTempDir, function (err, items) { if (items.length > 0) { if (process.platform === "win32") { - shell.cp(path.join(finalTempDir, getNodeExecutableName()), path.dirname(nodePath)); + fs.cp(path.join(finalTempDir, getNodeExecutableName()), path.join(path.dirname(nodePath), getNodeExecutableName())); } else { - shell.cp(path.join(finalTempDir, items[0], getNodeExecutableName()), path.dirname(nodePath)); + fs.cp(path.join(finalTempDir, items[0], getNodeExecutableName()), path.join(path.dirname(nodePath), getNodeExecutableName())); } - shell.rm('-rf', finalTempDir); - shell.chmod('+x', nodePath); + fs.rmSync(finalTempDir, { recursive: true, force: true }); + fs.chmod(nodePath, fs.constants.S_IRWXU); callback(null); } else { callback("No downloaded archives found"); @@ -137,12 +136,12 @@ export function downloadLatestGuardian(callback) { var finalTempDir = path.join(os.tmpdir(), ensureNodeUniqueId()); if (!fs.existsSync(tempDir)) { - shell.mkdir('-p', tempDir); + fs.mkdirSync(tempDir, { recursive: true }); } // remove and remake the dir - shell.rm('-rf', finalTempDir); - shell.mkdir('-p', finalTempDir); + fs.rmSync(finalTempDir, { recursive: true, force: true }); + fs.mkdirSync(finalTempDir, { recursive: true }); // Define a function to filter assets. var filterAssetGuardian = function (asset) { @@ -163,14 +162,14 @@ export function downloadLatestGuardian(callback) { if (items.length > 0) { extractArchive(path.join(finalTempDir, items[0]), finalTempDir, function (success) { if (success) { - shell.rm(path.join(finalTempDir, '*.zip')); - shell.rm(path.join(finalTempDir, '*.tar')); - shell.rm(path.join(finalTempDir, '*.gz')); + fs.rmSync(path.join(finalTempDir, '*.zip'), { force: true }); + fs.rmSync(path.join(finalTempDir, '*.tar'), { force: true }); + fs.rmSync(path.join(finalTempDir, '*.gz'), { force: true }); // check for files we need to exclude if (fs.existsSync(path.join(finalTempDir, 'exclude.txt'))) { fs.readFileSync(path.join(finalTempDir, 'exclude.txt'), 'utf-8').split(/\r?\n/).forEach(function (line) { - shell.rm(path.join(finalTempDir, line)); + fs.rmSync(finalTempDir, { force: true }); }); } @@ -180,10 +179,10 @@ export function downloadLatestGuardian(callback) { // get the backup name for the old file and rename it to that name var backupName = executableName.substr(0, extensionPos < 0 ? executableName.length : extensionPos) + ".old"; - shell.mv(path.join(process.cwd(), getGuardianExecutableName()), path.join(process.cwd(), backupName)); - shell.cp('-rf', path.join(finalTempDir, '*'), process.cwd()); - shell.chmod('+x', process.cwd()); - shell.rm('-rf', finalTempDir); + fs.renameSync(path.join(process.cwd(), getGuardianExecutableName()), path.join(process.cwd(), backupName)); + fs.cp(path.join(finalTempDir, '*'), process.cwd(), { recursive: true, force: true }); + fs.chmod(process.cwd(), fs.constants.S_IRWXU); + fs.rmSync(finalTempDir, { recursive: true, force: true }); callback(null); } else { callback("Failed to extract the archive"); diff --git a/units/service.js b/units/service.js index ce61806..79cd210 100644 --- a/units/service.js +++ b/units/service.js @@ -2,10 +2,10 @@ // // Please see the included LICENSE file for more information. +import { exec } from "child_process"; import xmlbuilder from "xmlbuilder"; import { username } from "username"; import format from "string-template"; -import shell from "shelljs"; import path from "path"; import fs from "fs"; import os from "os"; @@ -24,7 +24,7 @@ export function install(configOpts, configFileName) { if (err) { console.log('\nError trying to save the XML: ' + err); } else { - shell.exec('cgservice.exe install'); + exec('cgservice.exe install'); } }); } else if (process.platform == "linux") { @@ -41,7 +41,7 @@ export function install(configOpts, configFileName) { console.log('\nError trying to save the service file: ' + err); } else { console.log('\nService is succesfully installed.\n'); - shell.exec('systemctl daemon-reload'); + exec('systemctl daemon-reload'); } }); } else { @@ -55,7 +55,7 @@ export function install(configOpts, configFileName) { export function remove(configOpts, configFileName) { try { if (process.platform == "win32") { - shell.exec('cgservice.exe uninstall'); + exec('cgservice.exe uninstall'); } else if (process.platform == "linux") { fs.unlink("/etc/systemd/system/ccx-guardian.service", function (err) { if (err) { @@ -75,10 +75,10 @@ export function remove(configOpts, configFileName) { export function start(configOpts, configFileName) { try { if (process.platform == "win32") { - shell.exec('cgservice.exe start'); + exec('cgservice.exe start'); } else if (process.platform == "linux") { - shell.exec('systemctl start ccx-guardian'); - shell.exec('systemctl status ccx-guardian'); + exec('systemctl start ccx-guardian'); + exec('systemctl status ccx-guardian'); } else { console.log("\nPlatform is not supported!\n"); } @@ -90,9 +90,9 @@ export function start(configOpts, configFileName) { export function stop(configOpts, configFileName) { try { if (process.platform == "win32") { - shell.exec('cgservice.exe stop'); + exec('cgservice.exe stop'); } else if (process.platform == "linux") { - shell.exec('systemctl stop ccx-guardian'); + exec('systemctl stop ccx-guardian'); } else { console.log("\nPlatform is not supported!\n"); } @@ -104,9 +104,9 @@ export function stop(configOpts, configFileName) { export function status(configOpts, configFileName) { try { if (process.platform == "win32") { - shell.exec('cgservice.exe status'); + exec('cgservice.exe status'); } else if (process.platform == "linux") { - shell.exec('systemctl status ccx-guardian'); + exec('systemctl status ccx-guardian'); } else { console.log("\nPlatform is not supported!\n"); } diff --git a/units/utils.js b/units/utils.js index 9472c9a..0dbc0b0 100644 --- a/units/utils.js +++ b/units/utils.js @@ -3,7 +3,6 @@ // Please see the included LICENSE file for more information. import UUID from "pure-uuid"; -import shell from "shelljs"; import path from "path"; import fs from "fs"; @@ -12,7 +11,7 @@ export function ensureUserDataDir() { userDataDir = path.join(userDataDir, "ccxNodeGuard"); if (!fs.existsSync(userDataDir)) { - shell.mkdir('-p', userDataDir); + fs.mkdirSync(userDataDir, { recursive: true }); } return userDataDir;