nemu-cli 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/output.js ADDED
@@ -0,0 +1,64 @@
1
+ import { bold, dim, out } from "./ui.js";
2
+ let json_mode = false;
3
+ export function set_json_mode(enabled) {
4
+ json_mode = enabled;
5
+ }
6
+ export function is_json_mode() {
7
+ return json_mode;
8
+ }
9
+ export function emit(value) {
10
+ out(JSON.stringify(value, null, 2));
11
+ }
12
+ function cell(value) {
13
+ if (value === null || value === undefined)
14
+ return "";
15
+ if (typeof value === "boolean")
16
+ return value ? "yes" : "no";
17
+ if (typeof value === "object")
18
+ return JSON.stringify(value);
19
+ return String(value);
20
+ }
21
+ export function table(rows, columns, empty = "Nothing to show.") {
22
+ if (json_mode) {
23
+ emit(rows);
24
+ return;
25
+ }
26
+ if (rows.length === 0) {
27
+ out(dim(` ${empty}`));
28
+ return;
29
+ }
30
+ const header = columns.map((column) => column.toUpperCase());
31
+ const body = rows.map((row) => columns.map((column) => cell(row[column])));
32
+ const widths = header.map((label, index) => Math.max(label.length, ...body.map((cells) => (cells[index] ?? "").length), 0));
33
+ const render = (cells) => cells
34
+ .map((value, index) => value.padEnd(widths[index] ?? 0))
35
+ .join(" ")
36
+ .trimEnd();
37
+ out(` ${dim(render(header))}`);
38
+ for (const cells of body)
39
+ out(` ${render(cells)}`);
40
+ out(dim(` ${rows.length} row${rows.length === 1 ? "" : "s"}`));
41
+ }
42
+ export function detail(record, title) {
43
+ if (json_mode) {
44
+ emit(record);
45
+ return;
46
+ }
47
+ if (title !== undefined) {
48
+ out();
49
+ out(` ${bold(title)}`);
50
+ }
51
+ const keys = Object.keys(record);
52
+ const width = Math.max(...keys.map((key) => key.length), 0);
53
+ for (const key of keys) {
54
+ out(` ${dim(key.padEnd(width))} ${cell(record[key])}`);
55
+ }
56
+ out();
57
+ }
58
+ export function done(message, payload) {
59
+ if (json_mode) {
60
+ emit({ ok: true, message, ...(payload ?? {}) });
61
+ return;
62
+ }
63
+ out(` ${message}`);
64
+ }
@@ -0,0 +1,52 @@
1
+ import { NemuDevice } from "@nemu-ai/sdk";
2
+ import { api_url, client_id, is_expired, read_credentials, write_credentials, } from "./config.js";
3
+ export class NotSignedIn extends Error {
4
+ constructor() {
5
+ super("Not signed in. Run nemu login.");
6
+ this.name = "NotSignedIn";
7
+ }
8
+ }
9
+ export function device_client(url = api_url()) {
10
+ return new NemuDevice({
11
+ client_id: client_id(),
12
+ base_url: `${url.replace(/\/+$/, "")}/api/v1`,
13
+ });
14
+ }
15
+ export async function client() {
16
+ const credentials = await read_credentials();
17
+ if (credentials === null)
18
+ throw new NotSignedIn();
19
+ if (is_expired(credentials) && credentials.refresh_token === undefined) {
20
+ throw new NotSignedIn();
21
+ }
22
+ const device = device_client(credentials.api_url);
23
+ return device.from_tokens({
24
+ access_token: credentials.access_token,
25
+ refresh_token: credentials.refresh_token,
26
+ expires_at: credentials.expires_at,
27
+ scope: credentials.scope,
28
+ }, (rotated) => {
29
+ void write_credentials({
30
+ ...credentials,
31
+ access_token: rotated.access_token,
32
+ refresh_token: rotated.refresh_token ?? credentials.refresh_token,
33
+ expires_at: rotated.expires_at ?? Date.now() + 3_600_000,
34
+ });
35
+ });
36
+ }
37
+ export async function resolve_workspace(nemu, requested) {
38
+ const workspaces = await nemu.workspaces.list();
39
+ if (workspaces.length === 0) {
40
+ throw new Error("This account has no workspaces.");
41
+ }
42
+ if (requested === undefined) {
43
+ return workspaces[0];
44
+ }
45
+ const match = workspaces.find((workspace) => workspace.id === requested ||
46
+ workspace.name.toLowerCase() === requested.toLowerCase());
47
+ if (!match) {
48
+ const names = workspaces.map((workspace) => workspace.name).join(", ");
49
+ throw new Error(`No workspace matched "${requested}". Have: ${names}`);
50
+ }
51
+ return match;
52
+ }
package/dist/ui.js ADDED
@@ -0,0 +1,55 @@
1
+ import { spawn } from "node:child_process";
2
+ const SUPPORTS_COLOR = process.env.NO_COLOR === undefined &&
3
+ process.env.TERM !== "dumb" &&
4
+ process.stdout.isTTY === true;
5
+ function paint(code, value) {
6
+ return SUPPORTS_COLOR ? `[${code}m${value}` : value;
7
+ }
8
+ export const dim = (value) => paint("2", value);
9
+ export const bold = (value) => paint("1", value);
10
+ export const green = (value) => paint("32", value);
11
+ export const red = (value) => paint("31", value);
12
+ export const cyan = (value) => paint("36", value);
13
+ export function out(line = "") {
14
+ process.stdout.write(`${line}\n`);
15
+ }
16
+ export function fail(message) {
17
+ process.stderr.write(`${red("Error")} ${message}\n`);
18
+ }
19
+ export function success(message) {
20
+ out(`${green("✓")} ${message}`);
21
+ }
22
+ const FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
23
+ export function spinner(label) {
24
+ if (process.stdout.isTTY !== true) {
25
+ out(` ${label}`);
26
+ return { stop: () => { } };
27
+ }
28
+ let index = 0;
29
+ const timer = setInterval(() => {
30
+ process.stdout.write(`\r ${dim(FRAMES[index % FRAMES.length])} ${label}`);
31
+ index += 1;
32
+ }, 90);
33
+ return {
34
+ stop: () => {
35
+ clearInterval(timer);
36
+ process.stdout.write(`\r${" ".repeat(label.length + 6)}\r`);
37
+ },
38
+ };
39
+ }
40
+ export function open_browser(url) {
41
+ const command = process.platform === "darwin"
42
+ ? "open"
43
+ : process.platform === "win32"
44
+ ? "cmd"
45
+ : "xdg-open";
46
+ const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
47
+ try {
48
+ const child = spawn(command, args, { stdio: "ignore", detached: true });
49
+ child.on("error", () => { });
50
+ child.unref();
51
+ }
52
+ catch {
53
+ return;
54
+ }
55
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "nemu-cli",
3
+ "version": "1.0.0",
4
+ "description": "Command line access to Nemu. Signs in with the OAuth device grant, no secret required.",
5
+ "bin": {
6
+ "nemu": "./dist/index.js"
7
+ },
8
+ "type": "module",
9
+ "files": [
10
+ "dist",
11
+ "CHANGELOG.md"
12
+ ],
13
+ "scripts": {
14
+ "build": "npm run clean && tsc -p tsconfig.json",
15
+ "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
16
+ "prepublishOnly": "npm run clean && npm run build",
17
+ "test": "bun test",
18
+ "lint": "tsc --noEmit"
19
+ },
20
+ "keywords": [
21
+ "nemu",
22
+ "ai",
23
+ "cli",
24
+ "oauth",
25
+ "device-flow"
26
+ ],
27
+ "author": "hiraeeth",
28
+ "license": "MIT",
29
+ "devDependencies": {
30
+ "@types/node": "^20.0.0",
31
+ "typescript": "^5"
32
+ },
33
+ "engines": {
34
+ "node": ">=18.0.0"
35
+ },
36
+ "homepage": "https://docs.nemu.cc",
37
+ "dependencies": {
38
+ "@nemu-ai/sdk": "^1.1.0"
39
+ }
40
+ }