diff --git a/README.md b/README.md index 531fbdc..081c3b5 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ### Install ``` -deno install -f --allow-run --allow-net https://deno.land/x/dkill@0.10.0/cli.ts +deno install -f jsr:@sylc/dkill ``` You can then use it using command `dkill` diff --git a/cli.ts b/cli.ts index eddf566..1accc1a 100644 --- a/cli.ts +++ b/cli.ts @@ -4,7 +4,7 @@ import { procList } from "./src/procList.ts"; import { upgrader } from "./src/upgrader.ts"; import { assertMinVersion } from "./src/utils/versions.ts"; -import vJson from "./version.json" with { type: "json" }; +import vJson from "./deno.json" with { type: "json" }; // check minimum version of deno const minVRequired = "1.31.1"; // uses deno.Command @@ -15,100 +15,106 @@ if (!assertMinVersion(Deno.version.deno, minVRequired)) { Deno.exit(1); } -await new Command() - .name("dkill") - .version(vJson.version) - .description( - `Kill any processes by +export const run = async () => { + await new Command() + .name("dkill") + .version(vJson.version) + .description( + `Kill any processes by - port: Prefix port number by a colon. ex: 'dkill :3000' - pid: A valid integer. ex: 'dkill 12654' - process name: A string ex: 'dkill Code.exe' You can specify multiple targets at once: 'dkill node.exe :5000 :3000 164'`, - ) - .arguments("<...targets>") - .option("-i, --interactive", "Interactive mode (Not available on MacOS)", { - standalone: true, - }) - .option("-v, --verbose", "Increase verbosity") - .option( - "-d, --dryrun", - "Dry run, List the pids that would have been killed. Does not kill anything", - ) - .option( - "-u, --upgrade", - "Print out the command to upgrade if a new version is found. This will not process any other command", - { + ) + .arguments("<...targets>") + .option("-i, --interactive", "Interactive mode (Not available on MacOS)", { standalone: true, - }, - ) - .action( - async (opts, ...targets) => { - if (opts.upgrade) { - // upgrading version. - await upgrader({ - packageName: "dkill", - currentVersion: vJson.version, - }); - return; - } + }) + .option("-v, --verbose", "Increase verbosity") + .option( + "-d, --dryrun", + "Dry run, List the pids that would have been killed. Does not kill anything", + ) + .option( + "-u, --upgrade", + "Print out the command to upgrade if a new version is found. This will not process any other command", + { + standalone: true, + }, + ) + .action( + async (opts, ...targets) => { + if (opts.upgrade) { + // upgrading version. + await upgrader({ + packageName: "dkill", + currentVersion: vJson.version, + }); + return; + } - const ports: number[] = []; - const pids: number[] = []; - const procs: string[] = []; + const ports: number[] = []; + const pids: number[] = []; + const procs: string[] = []; - if (opts.interactive) { - if (Deno.build.os === "darwin") { - console.error("Not implemented on macos"); - Deno.exit(1); - } - // list processes - const pList = await procList(); - const pickedProcesses: string[] = await Checkbox.prompt({ - message: "Pick processes to kill", - options: pList.map((item) => ({ - name: `${item.pid} | ${item.proc} | ${item.cmd}`, - value: `${item.pid}`, - })), - search: true, - }); - pickedProcesses.forEach((p) => pids.push(+p)); - } else { - targets.forEach((target) => { - // Check if port - if (target.startsWith(":")) { - const port = +target.slice(1); - if (!Number.isInteger(port)) { - console.log(`Invalid port number "port"`); - return; - } - ports.push(port); - } else if (Number.isInteger(+target)) { - // check if pid - pids.push(+target); - } else { - // must be a string - procs.push(target); + if (opts.interactive) { + if (Deno.build.os === "darwin") { + console.error("Not implemented on macos"); + Deno.exit(1); } - }); - } - const killed = await dkill( - { ports, pids, procs }, - { - verbose: opts.verbose, - dryrun: opts.dryrun, - includeCmds: true, - }, - ); + // list processes + const pList = await procList(); + const pickedProcesses: string[] = await Checkbox.prompt({ + message: "Pick processes to kill", + options: pList.map((item) => ({ + name: `${item.pid} | ${item.proc} | ${item.cmd}`, + value: `${item.pid}`, + })), + search: true, + }); + pickedProcesses.forEach((p) => pids.push(+p)); + } else { + targets.forEach((target) => { + // Check if port + if (target.startsWith(":")) { + const port = +target.slice(1); + if (!Number.isInteger(port)) { + console.log(`Invalid port number "port"`); + return; + } + ports.push(port); + } else if (Number.isInteger(+target)) { + // check if pid + pids.push(+target); + } else { + // must be a string + procs.push(target); + } + }); + } + const killed = await dkill( + { ports, pids, procs }, + { + verbose: opts.verbose, + dryrun: opts.dryrun, + includeCmds: true, + }, + ); + + if (killed && killed.length) { + // TODO improve table output + // console.table(killed.map(pidItem => ({ ...pidItem, port: `:${pidItem.port}`, killed: pidItem.killed ? 'yes' : 'x'}))); + console.table(killed); + opts?.dryrun && console.log("Nothing has been killed"); + } else { + console.log("No process found"); + } + }, + ) + .parse(Deno.args); +}; - if (killed && killed.length) { - // TODO improve table output - // console.table(killed.map(pidItem => ({ ...pidItem, port: `:${pidItem.port}`, killed: pidItem.killed ? 'yes' : 'x'}))); - console.table(killed); - opts?.dryrun && console.log("Nothing has been killed"); - } else { - console.log("No process found"); - } - }, - ) - .parse(Deno.args); +if (import.meta.main) { + await run(); +} diff --git a/deno.jsonc b/deno.json similarity index 58% rename from deno.jsonc rename to deno.json index 2dd9f6b..7718adf 100644 --- a/deno.jsonc +++ b/deno.json @@ -6,13 +6,11 @@ "test": "deno test --allow-run --allow-net --allow-read", "testm": "deno run --allow-run --allow-net ./src/tests/utils.ts", "dev": "deno run --allow-run --allow-net ./cli.ts", - "release": "deno run -A https://deno.land/x/release_up@0.7.0/cli.ts --regex \"(?<=@)(.*)(?=\/cli)\" --github --versionFile --changelog" + "release": "deno run -A https://deno.land/x/release_up@0.8.0/cli.ts --config ./tools/.release_up.json" }, "fmt": { - "files": { - "exclude": [ - "CHANGELOG.md" - ] - } + "exclude": [ + "CHANGELOG.md" + ] } } diff --git a/mod.ts b/mod.ts index 981a617..ec5c5ae 100644 --- a/mod.ts +++ b/mod.ts @@ -4,3 +4,9 @@ export * from "./src/pidToCmd.ts"; export * from "./src/portToPid.ts"; export * from "./src/procList.ts"; export * from "./src/procToPid.ts"; + +import * as cli from "./cli.ts"; + +if (import.meta.main) { + await cli.run(); +} diff --git a/src/upgrader.ts b/src/upgrader.ts index f7fa2a3..55642f3 100644 --- a/src/upgrader.ts +++ b/src/upgrader.ts @@ -1,13 +1,12 @@ -async function fetchNewFlags(url: string) { - const versionRes = await fetch(url); - const versionText = await versionRes.text(); - - const parsed = versionText.match(/denoFlags =[\s\S]*];/); - if (!parsed) throw Error("Cannot parse flags"); - const flags = JSON.parse( - parsed[0].slice("denoFlags =".length, parsed[0].length - 1), +async function fetchNewCommand(version: string) { + const readMeRes = await fetch( + `https://jsr.io/@sylc/dkill/${version}/README.md`, ); - return flags; + const readmeText = await readMeRes.text(); + + const parsed = readmeText.match(/deno install .*/); + if (!parsed || parsed.length === 0) throw Error("Cannot parse new command"); + return parsed[0]; } export async function upgrader(config: { @@ -22,22 +21,15 @@ export async function upgrader(config: { ) ).json(); // We do not consider the < comparison because - // it is unlikely to eb > (not doing canary release) + // it is unlikely to be > (not doing canary release) // so if not equal, it must be lower version + // TODO: use @std/semver if (config.currentVersion !== versions.latest) { - const newFlags = await fetchNewFlags( - `https://deno.land/x/${config.packageName}@${versions.latest}/version.ts`, - ); - - console.log( - `Current version: ${config.currentVersion}; latest Version: ${versions.latest}`, - ); - console.log("Run the below command to update:"); - console.log( - `deno install -f ${ - newFlags.join(" ") - } https://deno.land/x/${config.packageName}@${versions.latest}/cli.ts`, - ); + // retrieve command from new version + // const command = await fetchNewCommand(versions.latest); + const command = await fetchNewCommand("0.10.1"); + console.log("A new version is available. Run the below command to update:"); + console.log(command); } else { console.log( `Local version ${config.currentVersion} is the most recent release`, diff --git a/tools/.release_up.json b/tools/.release_up.json new file mode 100644 index 0000000..344e2ef --- /dev/null +++ b/tools/.release_up.json @@ -0,0 +1,14 @@ +{ + "changelog": {}, + "github": { + "release": true + }, + "regex": [ + { + "file": "deno.json", + "patterns": [ + "(?<=\"version\": \")(.*)(?=\",)" + ] + } + ] +} diff --git a/version.json b/version.json deleted file mode 100644 index 3b8a492..0000000 --- a/version.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "0.10.1" -} diff --git a/version.ts b/version.ts deleted file mode 100644 index c19120e..0000000 --- a/version.ts +++ /dev/null @@ -1,7 +0,0 @@ -// List of flags required to run the cli -// experimental used by upgrader -// deno-fmt-ignore -export const denoFlags = [ - "--allow-run", - "--allow-net" -];