Skip to content

Commit

Permalink
fix: Prevent accidental exit
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Apr 1, 2023
1 parent 981e7f2 commit 12dfd2c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {
cyan,
green,
yellow,
} from "https://deno.land/std@0.181.0/fmt/colors.ts";
} from "https://deno.land/std@0.182.0/fmt/colors.ts";

import { listLog } from "./src/log.ts";
import { isPackageManager, usePackageManager } from "./src/pm.ts";
import { exist, findUpNodeModules, findUpPackageJson } from "./src/fs.ts";
import { execa, normalFusing } from "./src/process.ts";
import { join } from "https://deno.land/std@0.181.0/path/mod.ts";
import { join } from "https://deno.land/std@0.182.0/path/mod.ts";
import { extractDeps, extractDepsFromPackageJson } from "./src/deps.ts";

const {
Expand Down
2 changes: 1 addition & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { exist } from "./fs.ts";
import { builtinModules as _builtinModules } from "node:module";
import { walk } from "https://deno.land/std@0.181.0/fs/walk.ts";
import { walk } from "https://deno.land/std@0.182.0/fs/walk.ts";

export const BUILTIN_MODULES = [
"module",
Expand Down
2 changes: 1 addition & 1 deletion src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { slash } from "./path.ts";
import { dirname, join } from "https://deno.land/std@0.181.0/path/mod.ts";
import { dirname, join } from "https://deno.land/std@0.182.0/path/mod.ts";

export async function exist(path: string) {
try {
Expand Down
7 changes: 4 additions & 3 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { green } from "https://deno.land/std@0.181.0/fmt/colors.ts";
import { green } from "https://deno.land/std@0.182.0/fmt/colors.ts";

export function listLog(list: string[], color = green) {
return list.reduce((s, v, i) => {
s += `${i === list.length - 1 ? " └─ " : " ├─ "}${color(v)}${i === list.length - 1 ? "" : "\n"
}`;
s += `${i === list.length - 1 ? " └─ " : " ├─ "}${color(v)}${
i === list.length - 1 ? "" : "\n"
}`;
return s;
}, "");
}
18 changes: 14 additions & 4 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { which } from "https://deno.land/x/[email protected]/mod.ts";
import { cyan, red, yellow } from "https://deno.land/std@0.181.0/fmt/colors.ts";
import { cyan, red, yellow } from "https://deno.land/std@0.182.0/fmt/colors.ts";

export async function execa(cmd: string[]) {
const command = await which(cmd.shift()!);
Expand All @@ -11,14 +11,24 @@ export async function execa(cmd: string[]) {
stdout: "inherit",
});

function childExit(signo?: Deno.Signal) {
Deno.close(process.rid);
Deno.kill(process.pid, signo);
}

// watch ctrl + c
Deno.addSignalListener("SIGINT", () => {
console.log(
`❎ The task was ${yellow("manually interrupted")}`,
);
Deno.kill(process.pid);
Deno.close(process.rid);
Deno.exit(128 + 2);

childExit("SIGINT");
Deno.exit(130);
});

// Prevent accidental exit
global.addEventListener("beforeunload", () => {
childExit();
});

const { success, code } = await process.status();
Expand Down

0 comments on commit 12dfd2c

Please sign in to comment.