@zurb/cli 0.1.0 → 0.2.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/README.md CHANGED
@@ -1,17 +1,17 @@
1
1
  # @zurb/cli
2
2
 
3
- One command for every ZURB tool. It starts with Studios and Clue (ZURB-4692).
3
+ One command for every ZURB tool. It starts with Studios and Clue.
4
4
 
5
5
  ```bash
6
- npx @zurb/cli login # approve in the browser, signed in to Studios
6
+ npm install -g @zurb/cli
7
+ zurb login # approve in the browser, signed in to Studios
7
8
  zurb --help # what you can run
8
- zurb clue find "why do trial users stall?" --customer informatica
9
+ zurb clue find "why do trial users stall?"
9
10
  zurb studios customers list --output json
10
11
  ```
11
12
 
12
13
  Commands are shaped `zurb <tool> <thing> <action>`. The CLI only holds `login` and `logout`. Every
13
- other command, and the help that lists it, comes from Studios (`GET /api/v1/commands`, defined in
14
- `lib/commands.ts`). That has two effects:
14
+ other command, and the help that lists it, comes from Studios. That has two effects:
15
15
 
16
16
  - A new command needs a Studios deploy, not a CLI release.
17
17
  - People only see what they can run. `admin …` commands exist only for platform admins.
@@ -27,8 +27,10 @@ other command, and the help that lists it, comes from Studios (`GET /api/v1/comm
27
27
  The token lasts 30 days, acts as you, and is stored at `~/.zurb/config.json` (mode 600). Studios
28
28
  keeps only its hash. `zurb logout` revokes it.
29
29
 
30
- For scripts and CI, set `ZURB_TOKEN`. `ZURB_URL` points the CLI at another Studios (for example
31
- `http://localhost:3540`).
30
+ That token only reads. Commands that change data need `zurb login --write`. Only platform admins
31
+ can approve that sign-in, and it lasts a day.
32
+
33
+ For scripts and CI, set `ZURB_TOKEN`.
32
34
 
33
35
  ## Input
34
36
 
@@ -36,11 +38,4 @@ For scripts and CI, set `ZURB_TOKEN`. `ZURB_URL` points the CLI at another Studi
36
38
  - `--key value` fills the rest, using the command's schema.
37
39
  - `--json '{…}'` or `--json @file.json` gives all the input at once.
38
40
  - `--output json` prints the data for scripts and agents.
39
-
40
- ## Developing
41
-
42
- ```bash
43
- cd cli && npm install
44
- npm test && npm run typecheck
45
- ZURB_URL=http://localhost:3540 npm run dev -- --help
46
- ```
41
+ - `--customer` can be left off when you have one customer.
package/dist/bin.js CHANGED
@@ -2,11 +2,11 @@
2
2
  import { ApiError, call } from "./api.js";
3
3
  import { matchCommand, parseInput } from "./args.js";
4
4
  import { clearConfig, readConfig, writeConfig } from "./config.js";
5
- import { commandHelp, mainHelp } from "./help.js";
5
+ import { commandHelp, loginMessage, mainHelp } from "./help.js";
6
6
  import { login } from "./login.js";
7
7
  // zurb (ZURB-4692). login and logout live here; every other command, and the help listing them,
8
8
  // comes from Studios (GET /api/v1/commands), so the CLI never has to change for a new command.
9
- const VERSION = "0.1.0";
9
+ const VERSION = "0.2.0";
10
10
  const say = (s) => process.stdout.write(s + "\n");
11
11
  const warn = (s) => process.stderr.write(s + "\n");
12
12
  async function main(argv) {
@@ -16,9 +16,10 @@ async function main(argv) {
16
16
  if (words[0] === "--version" || words[0] === "-v")
17
17
  return say(VERSION), 0;
18
18
  if (words[0] === "login") {
19
- const got = await login(config.url, warn);
20
- await writeConfig({ url: config.url, token: got.token, email: got.email, expiresAt: got.expiresAt });
21
- say(`Signed in as ${got.email} until ${got.expiresAt.slice(0, 10)}.`);
19
+ const write = words.slice(1).includes("--write");
20
+ const got = await login(config.url, warn, write);
21
+ await writeConfig({ url: config.url, token: got.token, email: got.email, expiresAt: got.expiresAt, write: Boolean(got.write) });
22
+ say(loginMessage(got));
22
23
  return 0;
23
24
  }
24
25
  if (words[0] === "logout") {
package/dist/help.js CHANGED
@@ -4,7 +4,7 @@ const pad = (s, n) => (s.length >= n ? s + " " : s.padEnd(n));
4
4
  const usage = (c) => [c.name, ...c.args.map((a) => `<${a}>`)].join(" ");
5
5
  export function mainHelp(manifest, signedIn) {
6
6
  const lines = ["Usage: zurb <tool> <thing> <action> [flags]", ""];
7
- lines.push("ACCOUNT", ` ${pad("login | logout", 40)}sign in with your Studios account`);
7
+ lines.push("ACCOUNT", ` ${pad("login | logout", 40)}sign in with your Studios account`, ` ${pad("login --write", 40)}sign in to change data, for a day (platform admins)`);
8
8
  if (!manifest) {
9
9
  lines.push("", "Run zurb login to see the commands you can use.");
10
10
  }
@@ -22,6 +22,8 @@ export function mainHelp(manifest, signedIn) {
22
22
  }
23
23
  }
24
24
  lines.push("", "FLAGS", " --json '{…}' | --json @file.json input all at once", " --output json | text json for scripts and agents", " <command> --help that command's flags");
25
+ if (manifest?.some((c) => c.writes))
26
+ lines.push("", "Commands that change data need zurb login --write.");
25
27
  if (signedIn)
26
28
  lines.push("", `Signed in as ${signedIn}.`);
27
29
  return lines.join("\n");
@@ -34,6 +36,8 @@ export function commandHelp(c) {
34
36
  const props = c.input.properties ?? {};
35
37
  const required = new Set(c.input.required ?? []);
36
38
  const lines = [`Usage: zurb ${usage(c)} [flags]`, "", c.summary];
39
+ if (c.writes)
40
+ lines.push("It changes data, so it needs a sign-in from zurb login --write.");
37
41
  const flags = Object.entries(props).filter(([k]) => !c.args.includes(k));
38
42
  if (flags.length) {
39
43
  lines.push("", "FLAGS");
@@ -44,3 +48,10 @@ export function commandHelp(c) {
44
48
  }
45
49
  return lines.join("\n");
46
50
  }
51
+ /** What `zurb login` says once it's done: a sign-in that can change data says so, and when it ends. */
52
+ export function loginMessage(got) {
53
+ if (!got.write)
54
+ return `Signed in as ${got.email} until ${got.expiresAt.slice(0, 10)}.`;
55
+ const until = new Date(got.expiresAt).toLocaleString([], { dateStyle: "medium", timeStyle: "short" });
56
+ return `Signed in as ${got.email}, able to change data, until ${until}. Run zurb login for an everyday sign-in after.`;
57
+ }
package/dist/login.js CHANGED
@@ -5,14 +5,18 @@ import { hostname } from "node:os";
5
5
  import { call } from "./api.js";
6
6
  // `zurb login` (ZURB-4692): a server on 127.0.0.1 waits for the browser, Studios' approval page
7
7
  // sends back a one-time code, and the CLI trades it, with a PKCE verifier only it knows, for a token.
8
+ // `zurb login --write` asks for a token that can change data, which only platform admins can
9
+ // approve, for a day (ZURB-4696).
8
10
  const b64url = (b) => b.toString("base64url");
9
11
  export const challengeFor = (verifier) => b64url(createHash("sha256").update(verifier).digest());
10
12
  /** A token name Studios accepts: letters, digits and a little punctuation, up to 60 characters. */
11
13
  export const tokenName = (host = hostname()) => `zurb CLI on ${host.replace(/\.local$/, "").replace(/[^\w .@'()-]/g, "-")}`.slice(0, 60);
12
- export function approvalUrl(base, port, state, challenge, name) {
14
+ export function approvalUrl(base, port, state, challenge, name, write = false) {
13
15
  const url = new URL("/cli/login", base);
14
16
  for (const [k, v] of Object.entries({ port: String(port), state, challenge, name }))
15
17
  url.searchParams.set(k, v);
18
+ if (write)
19
+ url.searchParams.set("write", "1");
16
20
  return url.toString();
17
21
  }
18
22
  function openBrowser(url) {
@@ -25,7 +29,7 @@ function openBrowser(url) {
25
29
  }
26
30
  }
27
31
  const PAGE = (msg) => `<!doctype html><meta charset="utf-8"><title>zurb</title><body style="font:16px system-ui;display:grid;place-items:center;height:90vh"><p>${msg}</p>`;
28
- export async function login(base, log) {
32
+ export async function login(base, log, write = false) {
29
33
  const verifier = b64url(randomBytes(32));
30
34
  const state = b64url(randomBytes(16));
31
35
  const code = await new Promise((resolve, reject) => {
@@ -58,7 +62,7 @@ export async function login(base, log) {
58
62
  server.listen(0, "127.0.0.1", () => {
59
63
  const address = server.address();
60
64
  const port = typeof address === "object" && address ? address.port : 0;
61
- const url = approvalUrl(base, port, state, challengeFor(verifier), tokenName());
65
+ const url = approvalUrl(base, port, state, challengeFor(verifier), tokenName(), write);
62
66
  log(`Opening your browser to approve this sign-in. If it doesn't open, go to:\n ${url}`);
63
67
  openBrowser(url);
64
68
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zurb/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "One command for every ZURB tool, starting with Studios and Clue.",
5
5
  "type": "module",
6
6
  "bin": { "zurb": "dist/bin.js" },