Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING(upgrader): remove version file and use deno.json instead. #19

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
182 changes: 94 additions & 88 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
10 changes: 4 additions & 6 deletions deno.jsonc → deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
6 changes: 6 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
38 changes: 15 additions & 23 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -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`,
Expand Down
14 changes: 14 additions & 0 deletions tools/.release_up.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"changelog": {},
"github": {
"release": true
},
"regex": [
{
"file": "deno.json",
"patterns": [
"(?<=\"version\": \")(.*)(?=\",)"
]
}
]
}
3 changes: 0 additions & 3 deletions version.json

This file was deleted.

7 changes: 0 additions & 7 deletions version.ts

This file was deleted.

Loading