Skip to content

Commit

Permalink
Internal refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Apr 23, 2024
1 parent 4b4dbd5 commit bbe521e
Show file tree
Hide file tree
Showing 30 changed files with 277 additions and 1,322 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _For detailed documentation, visit [pup.56k.guru](https://pup.56k.guru)._
To install Pup, open your terminal and execute the following command:

```bash
deno run -Ar jsr:@pup/[email protected].31 setup --channel prerelease
deno run -Ar jsr:@pup/[email protected].32 setup --channel prerelease
```

This command downloads the latest version of Pup and installs it on your system. The `--channel prerelease` option is included as there is no stable version of Pup yet. Read more abour release
Expand Down
2 changes: 1 addition & 1 deletion application.meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

const Application = {
name: "pup",
version: "1.0.0-rc.31",
version: "1.0.0-rc.32",
url: "jsr:@pup/pup@$VERSION",
canary_url: "https://raw.githubusercontent.com/Hexagon/pup/main/pup.ts",
deno: null, /* Minimum stable version of Deno required to run Pup (without --unstable-* flags) */
Expand Down
11 changes: 5 additions & 6 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"name": "@pup/pup",
"version": "1.0.0-rc.31",
"version": "1.0.0-rc.32",

"exports": {
".": "./pup.ts",
"./mod.ts": "./mod.ts",
"./telemetry": "./telemetry.ts",
"./telemetry.ts": "./telemetry.ts"
"./lib": "./mod.ts"
},

"unstable": [
Expand Down Expand Up @@ -49,16 +47,17 @@
"@cross/service": "jsr:@cross/service@^1.0.3",
"@cross/test": "jsr:@cross/test@^0.0.9",
"@cross/utils": "jsr:@cross/utils@^0.11.0",
"@hexagon/bundlee": "jsr:@hexagon/bundlee@^0.9.6/mod.ts",
"@hexagon/croner": "jsr:@hexagon/croner@^8.0.1",
"@oak/oak": "jsr:@oak/oak@^15.0.0",
"@pup/api-client": "jsr:@pup/api-client@^1.0.0",
"@pup/api-definitions": "jsr:@pup/api-definitions@^1.0.0",
"@pup/common": "jsr:@pup/common@^1.0.0",
"@std/assert": "jsr:@std/assert@^0.223.0",
"@std/async": "jsr:@std/async@^0.223.0",
"@std/encoding": "jsr:@std/encoding@^0.223.0",
"@std/io": "jsr:@std/io@^0.223.0",
"@std/path": "jsr:@std/path@^0.223.0",
"@std/semver": "jsr:@std/semver@^0.223.0",
"@std/testing": "jsr:@std/testing@^0.223.0",
"@std/uuid": "jsr:@std/uuid@^0.223.0",
"dax-sh": "npm:dax-sh@^0.40.0",
"filesize": "npm:filesize@^10.1.1",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Universal Process Manager"
},
"substitute": {
"$PUP_VERSION": "1.0.0-rc.31"
"$PUP_VERSION": "1.0.0-rc.32"
},
"top_links": [
{
Expand Down
12 changes: 12 additions & 0 deletions docs/src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ nav_order: 13

All notable changes to this project will be documented in this section.

## [1.0.0-rc.32] - 2024-04-23

**Breaking:** Any plugins or packages importing telemetry from `@pup/pup/telemetry.ts` needs to be updated to import from `@pup/telemetry` instead.

## Changes

- chore(core): Internal refactor, splitting several parts of pup into separate reusable libraries:
- `@pup/common` - <https://github.com/hexagon/pup-common>: Common utilities across Pup, Telemetry and Plug-ins.
- `@pup/api-definitions` - <https://github.com/hexagon/pup-api-definitions>: API definitions used by Pup and it's Rest API.
- `@pup/api-client` - <https://github.com/hexagon/pup-api-client>: The Pup Rest API, used by the cli interface, telemetry and plugins.
- `@pup/telemetry` - <https://github.com/hexagon/pup-api-definitions>: Runtime agnostic library for enabling Node, Deno and Bun process with Pup Telemetry and IPC capabilities.

## [1.0.0-rc.31] - 2024-04-23

- fix(telementry): Tear down in correct order
Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This example demonstrates the telemetry feature of pup, which ...
The simplest use case, where you only want to monitor your client metrics is used like this:

```ts
import { PupTelemetry } from "jsr:@pup/pup@$PUP_VERSION/telemetry"
import { PupTelemetry } from "jsr:@pup/telemetry"

new PupTelemetry()

Expand All @@ -32,7 +32,7 @@ telemetry (including main process) using `status` on the cli.

```ts
// PupTelemetry is a singleton, so it can be imported one or many times in your application
import { PupTelemetry } from "jsr:@pup/pup@$PUP_VERSION/telemetry" // Pin this to a specific version of pup
import { PupTelemetry } from "jsr:@pup/telemetry"
const telemetry = new PupTelemetry()

// One part of your application ...
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/telemetry/task-with-telemetry-1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// See docs/examples/telemetry/README.md for full documentation on telemetry, including using the IPC
// - Pin this to the latest version of pup, or include in import map
import { PupTelemetry } from "jsr:@pup/pup@1.0.0-rc.31/telemetry"
import { PupTelemetry } from "jsr:@pup/pup-telemetry"
const telemetry = new PupTelemetry(1)

// The task
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/telemetry/task-with-telemetry-2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// See docs/examples/telemetry/README.md for full documentation on telemetry, including using the IPC
// - Pin this to the latest version of pup, or include in import map
import { PupTelemetry } from "jsr:@pup/[email protected]/telemetry"
import { PupTelemetry } from "jsr:@pup/telemetry"
const telemetry = new PupTelemetry(1)

// The task
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import JSON5 from "json5"
import { join } from "@std/path"
import { exists, readFile, writeFile } from "@cross/fs"
import type { ArgsParser } from "@cross/utils"
import { toResolvedAbsolutePath } from "../common/utils.ts"
import { toResolvedAbsolutePath } from "@pup/common/path"
import { exit } from "node:process"

/**
Expand Down
85 changes: 57 additions & 28 deletions lib/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { printStatus } from "./status.ts"
import { upgrade } from "./upgrade.ts"

// Import common utilities
import { toPersistentPath, toResolvedAbsolutePath, toTempPath } from "../common/utils.ts"
import { toPersistentPath, toResolvedAbsolutePath, toTempPath } from "@pup/common/path"
import { exists, readFile } from "@cross/fs"

// Import external dependencies
Expand All @@ -31,8 +31,7 @@ import { installService, uninstallService } from "@cross/service"
import { Colors, exit } from "@cross/utils"
import { chdir, cwd } from "@cross/fs"
import { GenerateToken } from "../common/token.ts"
import { RestClient } from "../common/restclient.ts"
import { ApiApplicationState } from "../core/api.ts"
import { PupRestClient } from "@pup/api-client"
import { CurrentRuntime, Runtime } from "@cross/runtime"
import { Prop } from "../common/prop.ts"
import { encodeBase64 } from "@std/encoding/base64"
Expand Down Expand Up @@ -223,7 +222,7 @@ async function main() {

// Send api request
const apiBaseUrl = `http://${configuration.api?.hostname || DEFAULT_REST_API_HOSTNAME}:${port}`
client = new RestClient(apiBaseUrl, token!)
client = new PupRestClient(apiBaseUrl, token!)
} catch (_e) {
/* Ignore */
}
Expand Down Expand Up @@ -475,17 +474,19 @@ async function main() {
console.error("Can not print status, could not create api client.")
return exit(1)
}
const responseState = await client.get("/state")
if (responseState.ok) {
const dataState: ApiApplicationState = await responseState.json()
console.log("")
printHeader()
await printStatus(configFile!, configuration!, cwd(), dataState)
exit(0)
console.log("Action completed successfully")
exit(0)
} else {
console.error("Action failed: Invalid response received.")
try {
const responseState = await client.getState()
if (responseState.data) {
console.log("")
printHeader()
printStatus(configFile!, configuration!, cwd(), responseState.data)
exit(0)
} else {
console.error("Action failed: Invalid response received.")
exit(1)
}
} catch (_e) {
console.error("Action failed: Could not contact the Pup instance.")
exit(1)
}
}
Expand All @@ -499,17 +500,45 @@ async function main() {
exit(1)
}
if (baseArgument === op) {
let url = `/${op.toLowerCase().trim()}`
if (secondaryBaseArgument) {
url = `/processes/${secondaryBaseArgument.toLocaleLowerCase().trim()}${url}`
}
const result = await client!.post(url, undefined)
if (result.ok) {
console.log("Action completed successfully")
exit(0)
} else {
console.error("Action failed: Invalid response received.")
exit(1)
try {
let responseState // Declare responseState in the outer try/catch scope
switch (op) { // Use 'switch' instead of 'switch case'
case "restart":
if (secondaryBaseArgument) responseState = await client?.restartProcess(secondaryBaseArgument.toLocaleLowerCase().trim())
break
case "start":
// Implement the call to client?.startProcess(...)
if (secondaryBaseArgument) responseState = await client?.startProcess(secondaryBaseArgument.toLocaleLowerCase().trim())
break
case "stop":
// Implement the call to client?.stopProcess(...)
if (secondaryBaseArgument) responseState = await client?.stopProcess(secondaryBaseArgument.toLocaleLowerCase().trim())
break
case "block":
// Implement the call to client?.blockProcess(...)
if (secondaryBaseArgument) responseState = await client?.blockProcess(secondaryBaseArgument.toLocaleLowerCase().trim())
break
case "unblock":
// Implement the call to client?.unblockProcess(...)
if (secondaryBaseArgument) responseState = await client?.unblockProcess(secondaryBaseArgument.toLocaleLowerCase().trim())
break
case "terminate":
responseState = await client?.terminate()
break
default:
console.error(`Invalid operation: ${op}`)
return exit(1)
}
if (!responseState?.error) {
console.error("Success")
return exit(0)
} else {
console.error(`Error: ${responseState.error}`)
return exit(1)
}
} catch (e) {
console.error("Action failed:", e)
return exit(1)
}
}
}
Expand All @@ -522,8 +551,8 @@ async function main() {
* Error handling: Pup already running
*/
try {
const response = await client?.get("/state")
if (response?.ok) {
const response = await client?.getState()
if (response) {
console.warn(`Pup already running. Exiting.`)
exit(1)
}
Expand Down
7 changes: 4 additions & 3 deletions lib/cli/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
* @license MIT
*/

import { type ProcessInformation, ProcessState } from "../core/process.ts"
import { type ProcessInformation } from "../core/process.ts"
import { ApiProcessState } from "@pup/api-definitions"
import { type Column, Columns, type Row } from "./columns.ts"
import { Colors } from "@cross/utils"
import { filesize } from "filesize"
import { blockedFormatter, codeFormatter, naFormatter, statusFormatter } from "./formatters/strings.ts"
import { timeagoFormatter } from "./formatters/times.ts"
import { Configuration, DEFAULT_REST_API_HOSTNAME } from "../core/configuration.ts"
import { resolve } from "@std/path"
import { ApiApplicationState } from "../core/api.ts"
import { ApiApplicationState } from "@pup/api-definitions"

/**
* Helper which print the status of all running processes,
Expand Down Expand Up @@ -53,7 +54,7 @@ export function printStatus(configFile: string, configuration: Configuration, cw
taskTable.push({
Id: " " + currentTask.id,
Type: currentTask.type.slice(0, 4) || "N/A",
Status: ProcessState[currentTask.status] || "N/A",
Status: ApiProcessState[currentTask.status] || "N/A",
Blocked: currentTask.blocked ? "Yes" : "No",
Started: timeagoFormatter(currentTask.started ? currentTask.started : "N/A"),
Exited: timeagoFormatter(currentTask.exited ? currentTask.exited : "N/A"),
Expand Down
61 changes: 0 additions & 61 deletions lib/common/eventemitter.ts

This file was deleted.

Loading

0 comments on commit bbe521e

Please sign in to comment.