Skip to content

Commit

Permalink
replaced shelljs with built in node functions
Browse files Browse the repository at this point in the history
  • Loading branch information
taeguscromis committed Feb 28, 2024
1 parent 490dbd6 commit bb502ca
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 37 deletions.
1 change: 0 additions & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ try {
alias: "h",
type: Boolean
}]);

} catch (err) {
console.error("\nUknown command line parameter. Use --help for instructions.");
process.exit();
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 19 additions & 20 deletions units/download.js
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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";
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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 });
});
}

Expand All @@ -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");
Expand Down
22 changes: 11 additions & 11 deletions units/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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") {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand Down
3 changes: 1 addition & 2 deletions units/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
Expand Down

0 comments on commit bb502ca

Please sign in to comment.