@zeroheight/adoption-cli 0.4.1 → 0.4.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Release notes
2
2
 
3
+ ## [0.4.3](https://www.npmjs.com/package/@zeroheight/adoption-cli/v/0.4.3) - 17th September 2024
4
+
5
+ - Update Ink, ink-link, ink-text-input, ink-select-input dependencies
6
+
7
+ ## [0.4.2](https://www.npmjs.com/package/@zeroheight/adoption-cli/v/0.4.2) - 10th September 2024
8
+
9
+ - Update README
10
+
3
11
  ## [0.4.1](https://www.npmjs.com/package/@zeroheight/adoption-cli/v/0.4.1) - 9th August 2024
4
12
 
5
13
  - Update README
package/README.md CHANGED
@@ -12,13 +12,15 @@ npm i @zeroheight/adoption-cli
12
12
 
13
13
  ## Usage
14
14
 
15
+ ### Component usage analysis
16
+
15
17
  In the repository in which you wish to analyze the component usage, run the following command:
16
18
 
17
19
  ```bash
18
20
  zh-adoption analyze
19
21
  ```
20
22
 
21
- ### Options
23
+ #### Options
22
24
 
23
25
  `-e` / `--extensions`
24
26
 
@@ -52,11 +54,12 @@ Pass in `false` to disable the interactive mode e.g. when running in a CI enviro
52
54
  zh-adoption analyze --interactive false -r "My Repo"
53
55
  ```
54
56
 
55
- ---
57
+
56
58
 
57
59
  To send adoption data to your [zeroheight](https://zeroheight.com/) account you will need to authenticate using a Client ID and Access Token.
58
60
 
59
- ### Interactive mode
61
+ ### Authentication
62
+ #### Interactive mode
60
63
 
61
64
  When running the `analyze` command, you will be prompted to authenticate. This will save the Client ID and Access Token to your local machine.
62
65
 
@@ -66,7 +69,7 @@ Alternatively, you can authenticate by running the following command:
66
69
  zh-adoption auth
67
70
  ```
68
71
 
69
- ### Non-interactive mode
72
+ #### Non-interactive mode
70
73
 
71
74
  When running the `analyze` command with the `--interactive false` flag, you will need to provide the Client ID and Access Token as environment variables.
72
75
 
package/dist/cli.js CHANGED
@@ -2,18 +2,20 @@
2
2
  import * as React from "react";
3
3
  import { Command } from "commander";
4
4
  import { render } from "ink-render-string";
5
+ import HelpInfo from "./components/help-info.js";
5
6
  import { analyzeCommand } from "./commands/analyze.js";
6
7
  import { authCommand } from "./commands/auth.js";
7
- import HelpInfo from "./components/help-info.js";
8
+ import { trackPackageCommand } from "./commands/track-package.js";
8
9
  const program = new Command();
9
10
  const { output, cleanup } = render(React.createElement(HelpInfo, null));
10
11
  program
11
12
  .name("zh-adoption")
12
13
  .description("CLI for measuring design system usage usage in your products")
13
- .version("0.4.0")
14
+ .version("0.4.3")
14
15
  .addHelpText("before", output)
15
16
  .addCommand(analyzeCommand())
16
- .addCommand(authCommand());
17
+ .addCommand(authCommand())
18
+ .addCommand(trackPackageCommand());
17
19
  cleanup();
18
20
  // Only start parsing if run as CLI, don't start parsing during testing
19
21
  if (process.env["NODE_ENV"] !== "test") {
@@ -0,0 +1,4 @@
1
+ import { Command } from "commander";
2
+ import { RenderOptions } from "ink";
3
+ export declare function trackPackageAction(renderOptions?: RenderOptions): Promise<void>;
4
+ export declare function trackPackageCommand(): Command;
@@ -0,0 +1,22 @@
1
+ import * as React from "react";
2
+ import { Command } from "commander";
3
+ import { render } from "ink";
4
+ import TrackPackage from "../components/track-package/track-package.js";
5
+ export async function trackPackageAction(renderOptions) {
6
+ render(React.createElement(TrackPackage, null), renderOptions);
7
+ }
8
+ export function trackPackageCommand() {
9
+ const command = new Command();
10
+ return command
11
+ .command("track-package")
12
+ .description("Track the latest version of your package in zeroheight")
13
+ .action(async () => {
14
+ try {
15
+ await trackPackageAction();
16
+ }
17
+ catch (e) {
18
+ console.error(e);
19
+ process.exitCode = 1;
20
+ }
21
+ });
22
+ }
@@ -0,0 +1,16 @@
1
+ export declare function getPackageInfo(): Promise<{
2
+ name: null;
3
+ path: null;
4
+ version: null;
5
+ error: string;
6
+ } | {
7
+ name: any;
8
+ path: string;
9
+ version: any;
10
+ error: null;
11
+ } | {
12
+ name: null;
13
+ path: string;
14
+ version: null;
15
+ error: string;
16
+ }>;
@@ -0,0 +1,32 @@
1
+ import { readFile } from "fs/promises";
2
+ import { findFiles } from "./analyze.utils.js";
3
+ export async function getPackageInfo() {
4
+ const base = process.cwd();
5
+ const files = await findFiles(base, "**/**/package.json", "");
6
+ if (files.length === 0) {
7
+ return {
8
+ name: null,
9
+ path: null,
10
+ version: null,
11
+ error: "Can't find any package files"
12
+ };
13
+ }
14
+ const relativeBase = `.${files[0]?.split(base).pop()}`;
15
+ try {
16
+ const fileContents = await readFile(files[0], "utf-8");
17
+ return {
18
+ name: JSON.parse(fileContents).name,
19
+ path: relativeBase,
20
+ version: JSON.parse(fileContents).version,
21
+ error: null
22
+ };
23
+ }
24
+ catch {
25
+ return {
26
+ name: null,
27
+ path: relativeBase,
28
+ version: null,
29
+ error: `Can't parse file ${files[0]}`
30
+ };
31
+ }
32
+ }
@@ -1,5 +1,6 @@
1
1
  import { RawUsageMap } from "../commands/analyze.js";
2
2
  import { Credentials } from "./config.js";
3
3
  export declare function getZeroheightURL(): URL;
4
+ export declare function submitPackageDetails(name: string, path: string, version: string, credentials: Credentials): Promise<any>;
4
5
  export declare function submitUsageData(usage: RawUsageMap, repoName: string, credentials: Credentials): Promise<any>;
5
6
  export declare function getExistingRepoNames(credentials: Credentials): Promise<any>;
@@ -6,6 +6,13 @@ export function getZeroheightURL() {
6
6
  return new URL("https://zeroheight.com");
7
7
  }
8
8
  }
9
+ export async function submitPackageDetails(name, path, version, credentials) {
10
+ return post("/open_api/v2/design_system_packages", {
11
+ name,
12
+ path,
13
+ latest_version: version,
14
+ }, credentials);
15
+ }
9
16
  export async function submitUsageData(usage, repoName, credentials) {
10
17
  return post("/open_api/v1/component_usages", {
11
18
  repo_name: repoName,
@@ -35,6 +42,7 @@ async function request(path, credentials, init) {
35
42
  const response = await fetch(url, {
36
43
  ...init,
37
44
  headers: {
45
+ "X-API-CLIENT-NAME": 'cli',
38
46
  "X-API-CLIENT": credentials.client,
39
47
  "X-API-KEY": credentials.token,
40
48
  "Content-Type": "application/json",
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export default function TrackPackage(): React.JSX.Element;
@@ -0,0 +1,100 @@
1
+ import React from "react";
2
+ import { Box, Newline, Text, useApp } from "ink";
3
+ import Spinner from "ink-spinner";
4
+ import ConfirmInput from "../ui/confirm-input.js";
5
+ import { readConfig } from "../../common/config.js";
6
+ import { getPackageInfo } from "../../commands/track-package.utils.js";
7
+ import { submitPackageDetails } from "../../common/api.js";
8
+ var Step;
9
+ (function (Step) {
10
+ Step[Step["COMPLETE"] = 0] = "COMPLETE";
11
+ Step[Step["DETAILS_FOUND"] = 1] = "DETAILS_FOUND";
12
+ Step[Step["ERRORED"] = 2] = "ERRORED";
13
+ Step[Step["FINDING_DETAILS"] = 3] = "FINDING_DETAILS";
14
+ Step[Step["SENDING_DETAILS"] = 4] = "SENDING_DETAILS";
15
+ })(Step || (Step = {}));
16
+ export default function TrackPackage() {
17
+ const { exit } = useApp();
18
+ const [currentStep, setCurrentStep] = React.useState(Step.FINDING_DETAILS);
19
+ const [credentials, setCredentials] = React.useState(null);
20
+ const [errorMessage, setErrorMessage] = React.useState(null);
21
+ const [packageName, setPackageName] = React.useState(null);
22
+ const [packagePath, setPackagePath] = React.useState(null);
23
+ const [packageVersion, setPackageVersion] = React.useState(null);
24
+ const [shouldSend, setShouldSend] = React.useState("");
25
+ async function sendData() {
26
+ setCurrentStep(Step.SENDING_DETAILS);
27
+ try {
28
+ await submitPackageDetails(packageName, packagePath, packageVersion, credentials);
29
+ setCurrentStep(Step.COMPLETE);
30
+ }
31
+ catch (e) {
32
+ const errorMessage = e.message === "Unauthorized"
33
+ ? "Unauthorized. Please reset your authentication by running: zh-adoption auth"
34
+ : "Failed to send data to zeroheight";
35
+ setErrorMessage(errorMessage);
36
+ setCurrentStep(Step.ERRORED);
37
+ }
38
+ finally {
39
+ exit();
40
+ }
41
+ }
42
+ const handleSendToZh = async (shouldSendData) => {
43
+ if (shouldSendData)
44
+ return sendData();
45
+ exit();
46
+ };
47
+ React.useEffect(() => {
48
+ (async () => {
49
+ const config = await readConfig();
50
+ if (config) {
51
+ setCredentials({ token: config.token, client: config.client });
52
+ }
53
+ const { name, path, version, error } = await getPackageInfo();
54
+ if (name && path && version) {
55
+ setPackageName(name);
56
+ setPackagePath(path);
57
+ setPackageVersion(version);
58
+ setCurrentStep(Step.DETAILS_FOUND);
59
+ }
60
+ else {
61
+ setErrorMessage(error);
62
+ setCurrentStep(Step.ERRORED);
63
+ }
64
+ })();
65
+ }, []);
66
+ switch (currentStep) {
67
+ case Step.FINDING_DETAILS:
68
+ return (React.createElement(Text, null,
69
+ React.createElement(Text, { color: "green" },
70
+ React.createElement(Spinner, { type: "dots" })),
71
+ " Searching for package file..."));
72
+ case Step.DETAILS_FOUND:
73
+ return (React.createElement(Box, { flexDirection: "column" },
74
+ React.createElement(Text, null,
75
+ React.createElement(Text, { color: "green" }, "Details founds:"),
76
+ " ",
77
+ packageName,
78
+ "@",
79
+ packageVersion),
80
+ React.createElement(Newline, null),
81
+ React.createElement(Text, null,
82
+ "Send to zeroheight? ",
83
+ React.createElement(Text, { dimColor: true }, "(Y/n):")),
84
+ React.createElement(ConfirmInput, { isChecked: true, onChange: setShouldSend, onSubmit: handleSendToZh, value: shouldSend })));
85
+ case Step.SENDING_DETAILS:
86
+ return (React.createElement(Text, null,
87
+ React.createElement(Text, { color: "green" },
88
+ React.createElement(Spinner, { type: "dots" })),
89
+ " Sending details to zeroheight..."));
90
+ case Step.COMPLETE:
91
+ return (React.createElement(Text, null, "Complete - details sent to your zeroheight account"));
92
+ case Step.ERRORED:
93
+ return (React.createElement(Text, null,
94
+ React.createElement(Text, { color: "red" }, "Error:"),
95
+ " ",
96
+ errorMessage));
97
+ default:
98
+ return React.createElement(Text, null, "Done");
99
+ }
100
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeroheight/adoption-cli",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "license": "ISC",
5
5
  "main": "dist/cli.js",
6
6
  "bin": {
@@ -17,7 +17,7 @@
17
17
  "start-dev": "export NODE_ENV=dev && zh-adoption",
18
18
  "build": "tsc",
19
19
  "dev": "tsc --watch",
20
- "lint": "prettier --check .",
20
+ "lint": "prettier .",
21
21
  "test": "vitest"
22
22
  },
23
23
  "files": [
@@ -31,12 +31,12 @@
31
31
  "commander": "^12.0.0",
32
32
  "glob": "^10.3.15",
33
33
  "ignore": "^5.3.1",
34
- "ink": "^4.1.0",
35
- "ink-link": "^3.0.0",
34
+ "ink": "^5.0.0",
35
+ "ink-link": "^4.1.0",
36
36
  "ink-render-string": "^1.0.0",
37
- "ink-select-input": "^5.0.0",
37
+ "ink-select-input": "^6.0.0",
38
38
  "ink-spinner": "^5.0.0",
39
- "ink-text-input": "^5.0.1",
39
+ "ink-text-input": "^6.0.0",
40
40
  "oxc-parser": "^0.22.0",
41
41
  "react": "^18.2.0",
42
42
  "yn": "^5.0.0"