Skip to content

Commit

Permalink
add upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
sylc committed Feb 23, 2021
1 parent 5558e22 commit bc384d2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .release/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { version } from "../version.ts";

const config = {
name: "dkill",
description: "Deno cli to kill processes and ports",
description: "Deno cli to kill processes by pid and ports",
version,
entry: "./mod.ts",
homepage: "https://github.com/sylc/dkill",
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
[![nest badge](https://nest.land/badge.svg)](https://nest.land/package/your-module)
[![nest badge](https://nest.land/badge.svg)](https://nest.land/package/dkill)

<h1 align="center">
🎯 dkill
</h1>

<p align="center">
<b>kill processes and ports for Deno 🦕</b>
<b>kill processes by pid and ports for Deno 🦕</b>
</p>

## CLI Usage

### Run directly

```
deno run --unstable --allow-run --allow-net https://x.nest.land/dkill@0.5.0/cli.ts
deno run --unstable --allow-run --allow-net https://x.nest.land/dkill@v0.5.1/cli.ts
```

### Install

```
deno install --unstable --allow-run --allow-net https://x.nest.land/dkill@0.5.0/cli.ts
deno install --unstable --allow-run --allow-net https://x.nest.land/dkill@v0.5.1/cli.ts
```

or you can use deno.land
```
deno install --unstable --allow-run --allow-net https://deno.land/x/dkill@v0.5.1/cli.ts
```

You can then access use it using command `dkill`
Expand Down
18 changes: 16 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from "./deps.ts";
import { dkill } from "./mod.ts";
import { upgrader } from "./src/upgrader.ts";
import { version } from "./version.ts";

await new Command()
Expand All @@ -13,14 +14,27 @@ await new Command()
You can specify multiple targets at once: 'dkill :5000 :3000 164'`,
)
.arguments("<targets...:string>")
.arguments("<targets...>")
.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: { verbose: boolean; dryrun: boolean }, targets: string[]) => {
async (opts: { verbose: boolean; dryrun: boolean, upgrade: boolean }, targets: string[]) => {

if (opts.upgrade) {
// upgrading version.
await upgrader({packageName: 'dkill', currentVersion: version, denoLand: true, nestLand: true})
return;
}

const ports: number[] = [];
const pids: number[] = [];
const procs: string[] = [];
Expand Down
65 changes: 65 additions & 0 deletions src/upgrader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export async function upgrader(config: {
packageName: string;
currentVersion: string;
denoLand?: boolean;
nestLand?: boolean;
}) {
// try to find the install script
console.log('Run ONE of the below commands')
if (config.denoLand) {
const versions = await (
await fetch(
`https://cdn.deno.land/${config.packageName}/meta/versions.json`
)
).json();
if (config.currentVersion !== versions.latest) {
console.log(
`deno.land: deno install --unstable --allow-run --allow-net https://deno.land/x/${config.packageName}@${versions.latest}/cli.ts -f`
);
} else {
console.log("Already up to date from deno.land");
}
}
if (config.nestLand) {
const res = await fetch("https://nest.land/api/package-client", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ data: { name: config.packageName } }),
});
const info = await res.json();
// {
// "body": {
// "name": "dkill",
// "normalizedName": "dkill",
// "owner": "scau",
// "description": "Deno cli to kill processes and ports",
// "repository": "https://github.com/sylc/dkill",
// "latestVersion": "dkill@0.5.0",
// "latestStableVersion": "dkill@0.5.0",
// "packageUploadNames": [
// "dkill@0.2.0",
// "dkill@0.2.1",
// "dkill@0.2.2",
// "dkill@0.2.3",
// "dkill@0.3.0",
// "dkill@0.4.0",
// "dkill@0.5.0"
// ],
// "locked": null,
// "malicious": null,
// "unlisted": false,
// "updatedAt": "2021-02-21T06:06:18.931Z",
// "createdAt": "2021-02-06T12:40:59.473Z"
// }
// }
if (`${config.packageName}@${config.currentVersion}` !== info.body.latestVersion) {
console.log(
`nest.land: deno install --unstable --allow-run --allow-net https://x.nest.land/${info.body.latestVersion}/cli.ts -f`
);
} else {
console.log("Already up to date from nest.land");
}
}
}
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = "0.5.0";
export const version = "v0.5.1";

0 comments on commit bc384d2

Please sign in to comment.