Skip to content

Commit 3ae62f4

Browse files
Implemented renew (update) command, added list prompt select when no script given & minor patches
1 parent 62a47e3 commit 3ae62f4

File tree

13 files changed

+237
-189
lines changed

13 files changed

+237
-189
lines changed

app.meta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const getGHRawURL = (branch: string, resource: string) =>
22
`https://raw.githubusercontent.com/Techzy-Programmer/dxpm/${branch}${resource}`;
3-
const VERSION = "v0.2.3-us";
3+
const VERSION = "v0.2.4-us";
44

55
export {
66
getGHRawURL,

deps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ export { isPortAvailable } from "https://deno.land/x/[email protected]/mod.ts"
99
export { EventEmitter } from "https://deno.land/x/[email protected]/mod.ts";
1010
export { colors } from "https://deno.land/x/[email protected]/ansi/colors.ts";
1111
export { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
12+
export { Select } from "https://deno.land/x/[email protected]/prompt/select.ts";
13+
export { Confirm } from "https://deno.land/x/[email protected]/prompt/confirm.ts";
1214
export { isAbsolute, resolve, fromFileUrl, dirname } from "https://deno.land/[email protected]/path/mod.ts";
1315
export { Cell, Table, type TableType, type RowType, type CellType } from "https://deno.land/x/[email protected]/table/mod.ts";

installer.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
import { dirname } from "./deps.ts";
1+
import { installBinary } from "./lib/helper/utility.ts";
22
import { reloadAndCacheModule } from "./lib/helper/utility.ts";
33

4-
const dxpmUrl = "https://deno.land/x/dxpm/dxpm.ts";
5-
const configs = ["-fr", "-A", "--unstable-kv", "-n", "dxpm"];
64
const daemonUrl = "https://deno.land/x/dxpm/lib/bg/daemon.ts";
7-
const installAt = await Deno.realPath(dirname(Deno.execPath()) + "/..");
8-
95
console.log("Caching required modules...");
106
await reloadAndCacheModule(daemonUrl);
117

12-
const installer = new Deno.Command("deno", {
13-
args: ["install", ...configs, "--root", installAt, dxpmUrl],
14-
stderr: "null", stdout: "null", stdin: "null"
15-
}).spawn();
16-
17-
const { success } = await installer.status;
8+
const success = await installBinary();
189
console.log(success ? "\nDXPM 'Deno eXtensible Package Manager' installed successfully!\n"
1910
: "\nOops! DXPM installation failed, something's quite wrong! Please try again later.\n");

lib/cmd/eject.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/cmd/go.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { printProcessData } from "../helper/logs.ts";
22
import { parseConfig } from "../helper/parser.ts";
33
import { sendMSG } from "../helper/network.ts";
4-
import { wlog, elog } from "../helper/logs.ts";
4+
import { ilog, elog } from "../helper/logs.ts";
55
import { SpawnCmd } from "../helper/ipc.ts";
66
import { TaskDetail } from "../types.ts";
77
import { Command } from "../../deps.ts";
@@ -20,7 +20,7 @@ async function goHandler(config: string | undefined) {
2020
cwd: Deno.cwd()
2121
}
2222

23-
wlog("Script execution initiated");
23+
ilog("Script execution initiated");
2424
const response = await sendMSG<TaskDetail>(spawnCMD);
2525

2626
if (response === "Dead") {

lib/cmd/init.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
import { playCommand, pauseCommand, ejectCommand } from "./ppe.ts";
12
import { launchDaemonProc } from "../helper/proc-daemon.ts";
23
import { Command, isPortAvailable } from "../../deps.ts";
34
import { IPC_HOST, IPC_PORT } from "../helper/ipc.ts";
45
import { VERSION } from "../../app.meta.ts";
56
import { elog } from "../helper/logs.ts";
6-
import ejectCommand from "./eject.ts";
77
import renewCommand from "./renew.ts";
8-
import pauseCommand from "./pause.ts";
98
import autoCommand from "./auto.ts";
10-
import playCommand from "./play.ts";
119
import showCommand from "./show.ts";
1210
import spyCommand from "./spy.ts";
1311
import goCommand from "./go.ts";

lib/cmd/pause.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/cmd/play.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/cmd/ppe.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Play-Pause-Eject Command Handling
2+
import { Command } from "../../deps.ts";
3+
import { sendMSG } from "../helper/network.ts";
4+
import { askFromKV } from "../helper/utility.ts";
5+
import { elog, slog, wlog } from "../helper/logs.ts";
6+
import { PlayPauseEjectCmd } from "../helper/ipc.ts";
7+
8+
// #region Commands Initialization
9+
10+
const optTxt = "Id of script corresponding to one provided in the config file.";
11+
const optArg = "-s, --script <Id>";
12+
13+
const playCommand = new Command()
14+
.description("Re-starts the stopped scripts.").option(optArg, optTxt)
15+
.action(({ script }) => ppeHandler(script, "play"));
16+
17+
const pauseCommand = new Command()
18+
.description("Stops the active execution of configured scripts.").option(optArg, optTxt)
19+
.action(({ script }) => ppeHandler(script, "pause"));
20+
21+
const ejectCommand = new Command()
22+
.description("Stop and delete the configured script.").option(optArg, optTxt)
23+
.action(({ script }) => ppeHandler(script, "eject"));
24+
25+
// #endregion
26+
27+
const alt = {
28+
"play": "start",
29+
"pause": "stop",
30+
"eject": "delete",
31+
};
32+
33+
const altPast = {
34+
"play": "started",
35+
"pause": "stopped",
36+
"eject": "deleted",
37+
};
38+
39+
async function ppeHandler(script: string | undefined, type: "play" | "pause" | "eject") {
40+
if (!script) {
41+
script = await askFromKV(`Select a script to ${type} (${alt[type]}):`);
42+
if (!script) return wlog(`No scripts avaliable to ${type} (${alt[type]})!`);
43+
}
44+
45+
const ejectCmd: PlayPauseEjectCmd = {
46+
action: type,
47+
script
48+
}
49+
50+
const key = type === "pause" ? "paused" :
51+
type === "eject" ? "ejected" : "played";
52+
53+
const response = await sendMSG<{
54+
[key: string]: boolean,
55+
found: boolean
56+
}>(ejectCmd);
57+
58+
if (response === "Dead") {
59+
elog("SocketIO Error, Connection dead");
60+
return;
61+
}
62+
63+
if (!response || !response.found) {
64+
elog(`Script with id '${script}' not found!`);
65+
return;
66+
}
67+
68+
if (!response[key]) {
69+
elog(`Unable to ${alt[type]} script. Please try again later.`);
70+
return;
71+
}
72+
73+
slog(`${key}: Script ${altPast[type]} successfully.`);
74+
}
75+
76+
export {
77+
playCommand,
78+
pauseCommand,
79+
ejectCommand
80+
};

lib/cmd/renew.ts

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,80 @@
1+
import { Confirm } from "../../deps.ts";
12
import { Command } from "../../deps.ts";
2-
3-
/**
4-
* [Steps]
5-
* Cache dxpm.ts from deno.land/x/dxpm
6-
* Cache daemon.ts from deno.land/x/dxpm
7-
* Send kill cmd to running instance of daemon.ts
8-
* Run `deno install` with required flags and args
9-
*/
3+
import { VERSION } from "../../app.meta.ts";
4+
import { elog, ilog, slog, wlog } from "../helper/logs.ts";
5+
import { getLatestVersion, installBinary, reloadAndCacheModule, killDaemon, delay } from "../helper/utility.ts";
106

117
const renewCommand = new Command()
128
.description("Updates the DXPM executable to the latest version.")
139
.action(renewHandler);
1410

15-
function renewHandler() {
16-
console.log("Not implemented yet!");
11+
async function renewHandler() {
12+
ilog("Checking for updates...");
13+
const newVer = await getLatestVersion();
14+
const baseUrl = "https://deno.land/x/dxpm";
15+
16+
if (VERSION === newVer) {
17+
slog("You are on the latest version.");
18+
return;
19+
}
20+
21+
ilog(`Current version: ${VERSION}`);
22+
ilog(`New version found: ${newVer}`);
23+
24+
const update = await Confirm
25+
.prompt(`Would you like to update?`);
26+
27+
if (!update) {
28+
wlog("Update aborted.");
29+
return;
30+
}
31+
32+
ilog("Starting update operation...");
33+
ilog("Caching modules and it's dependencies...");
34+
await reloadAndCacheModule(`${baseUrl}/lib/bg/daemon.ts`);
35+
36+
ilog("Aborting active Daemon instance...");
37+
await killDaemon(); // Send kill cmd to running instance of Daemon
38+
39+
ilog("Installing binary...");
40+
41+
if (!await installBinary()) {
42+
return elog("(Update Error): Failed to install binary.");
43+
}
44+
45+
ilog("Restarting daemon process...");
46+
await runDXPM(); await delay(2_000);
47+
ilog("Verifying installation...");
48+
49+
if (await runDXPM("-V") === newVer) {
50+
slog("Update complete, You are now on the latest version.");
51+
return;
52+
}
53+
54+
elog("(Update Error): Version Mismatch! Check failed.");
55+
}
56+
57+
async function runDXPM(...progArgs: string[]) {
58+
const decoder = new TextDecoder();
59+
let dxpmCmd: Deno.Command;
60+
progArgs.unshift("dxpm");
61+
62+
if (Deno.build.os === "windows") {
63+
dxpmCmd = new Deno.Command("cmd", {
64+
args: ["/C", ...progArgs],
65+
stdout: "piped"
66+
});
67+
} else {
68+
dxpmCmd = new Deno.Command("bash", {
69+
stdout: "piped",
70+
args: progArgs
71+
});
72+
}
73+
74+
const stdOut = await dxpmCmd.spawn().stdout.getReader().read();
75+
const stdOutText = decoder.decode(stdOut.value).trim();
76+
77+
return stdOutText;
1778
}
1879

1980
export default renewCommand;

0 commit comments

Comments
 (0)