Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update CLI

Steven Teycd51bf90216b
diff --git a/packages/cli/README.md b/packages/cli/README.md
index 5a404aa5cb..ad26b2bc56 100644
--- a/packages/cli/README.md
+++ b/packages/cli/README.md
@@ -2,9 +2,20 @@
A CLI for easily shortening URLs with the [Dub.co API](https://dub.co/api).
+## Available Commands
+
+| Command | Description |
+| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `dub login [key]` | Configure your workspace API key |
+| `dub config` | See your configured workspace credentials |
+| `dub domains` | Configure your workspace domain |
+| `dub shorten [url] [key]` | Create a short link. You can preemptively pass the URL and the generated short link key, or go through the CLI prompts. |
+| `dub links [options]` | Search for links in your Dub workspace. Available options include: `-s, --search <search>` to search for a link by name, or `-l, --limit <limit>` to limit the number of links returned (default is 10). |
+| `dub help [command]` | Display help for a specific command |
+
## Running Locally for Development
-1. Clone the repository, install dependencies and navigate to the cli folder:
+1. Clone the repository, install dependencies and navigate to the `cli` folder:
```bash
cd packages/cli
```
@@ -12,11 +23,11 @@ A CLI for easily shortening URLs with the [Dub.co API](https://dub.co/api).
```bash
pnpm dev
```
-3. In a separate terminal, run commands:
+3. In a separate terminal, navigate to the `cli` folder again and run an [available command](#available-commands):
```bash
pnpm start [command]
```
-4. See all available commands and options:
+4. See all [available commands](#available-commands) and options:
```bash
pnpm start help
```
@@ -26,19 +37,23 @@ A CLI for easily shortening URLs with the [Dub.co API](https://dub.co/api).
> **Warning**
> If you have previously installed `dub-cli` globally, uninstall it first to avoid conflicts
-1. Build the package:
+1. Clone the repository, install dependencies and navigate to the `cli` folder:
+ ```bash
+ cd packages/cli
+ ```
+2. Build the package:
```bash
pnpm build
```
-2. Link the package globally:
+3. Link the package globally:
```bash
npm link
```
-3. Verify the installation:
+4. Verify the installation:
```bash
dub -v
```
-4. Run commands:
+5. Run commands:
```bash
dub [command]
```
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 469d5fa235..29d9eabade 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -1,6 +1,6 @@
{
"name": "dub-cli",
- "version": "0.0.7",
+ "version": "0.0.10",
"description": "A CLI for easily shortening URLs with the Dub.co API.",
"publishConfig": {
"access": "public"
diff --git a/packages/cli/src/api/domains.ts b/packages/cli/src/api/domains.ts
index a72bb60e15..68a0656323 100644
--- a/packages/cli/src/api/domains.ts
+++ b/packages/cli/src/api/domains.ts
@@ -1,26 +1,29 @@
-import type { Domain } from "@/types";
import { getConfig } from "@/utils/config";
import { parseApiResponse } from "@/utils/parser";
+import { Dub } from "dub";
import fetch from "node-fetch";
export async function getDomains() {
const config = await getConfig();
- const options = {
- method: "GET",
- headers: {
- Authorization: `Bearer ${config.key}`,
- "Content-Type": "application/json",
- },
- };
+ const dub = new Dub({
+ token: config.key,
+ });
- const [domainsResponse, defaultDomainsResponse] = await Promise.all([
- fetch("https://api.dub.co/domains", options),
- fetch("https://api.dub.co/domains/default", options),
- ]);
+ const [{ result: domainsResponse }, defaultDomainsResponse] =
+ await Promise.all([
+ dub.domains.list(),
+ fetch("https://api.dub.co/domains/default", {
+ method: "GET",
+ headers: {
+ Authorization: `Bearer ${config.key}`,
+ "Content-Type": "application/json",
+ },
+ }),
+ ]);
const [domains, defaultDomains] = await Promise.all([
- parseApiResponse<Domain[]>(domainsResponse),
+ domainsResponse,
parseApiResponse<string[]>(defaultDomainsResponse),
]);
diff --git a/packages/cli/src/api/links.ts b/packages/cli/src/api/links.ts
index b7d8349603..3f60d9dc01 100644
--- a/packages/cli/src/api/links.ts
+++ b/packages/cli/src/api/links.ts
@@ -1,7 +1,7 @@
import { getConfig } from "@/utils/config";
import { Dub } from "dub";
-export async function createLink({ url, key }: { url: string; key?: string }) {
+export async function createLink({ url, key }: { url: string; key: string }) {
const config = await getConfig();
const dub = new Dub({
@@ -14,13 +14,3 @@ export async function createLink({ url, key }: { url: string; key?: string }) {
key: key,
});
}
-
-export async function getLinks() {
- const config = await getConfig();
-
- const dub = new Dub({
- token: config.key,
- });
-
- return await dub.links.list();
-}
diff --git a/packages/cli/src/commands/config.ts b/packages/cli/src/commands/config.ts
index 3d4fbda72a..e0b4b33946 100644
--- a/packages/cli/src/commands/config.ts
+++ b/packages/cli/src/commands/config.ts
@@ -7,7 +7,7 @@ import ora from "ora";
export const config = new Command()
.name("config")
- .description("see your configured workspace credentials")
+ .description("See your configured workspace credentials")
.action(async () => {
const spinner = ora("Getting config file").start();
diff --git a/packages/cli/src/commands/domains.ts b/packages/cli/src/commands/domains.ts
index 2477cf1a9e..da01531c13 100644
--- a/packages/cli/src/commands/domains.ts
+++ b/packages/cli/src/commands/domains.ts
@@ -56,7 +56,7 @@ export const domains = new Command()
},
);
- const getconfig = new Configstore("dubcli");
+ const getconfig = new Configstore("dub-cli");
getconfig.set("domain", options.domain);
spinner.succeed("Done");
diff --git a/packages/cli/src/commands/links.ts b/packages/cli/src/commands/links.ts
new file mode 100644
index 0000000000..c9bf90f64c
--- /dev/null
+++ b/packages/cli/src/commands/links.ts
@@ -0,0 +1,45 @@
+import { getConfig } from "@/utils/config";
+import { handleError } from "@/utils/handle-error";
+import { Command } from "commander";
+import { Dub } from "dub";
+import ora from "ora";
+
+export const links = new Command()
+ .command("links")
+ .description("Search for links in your Dub workspace.")
+ .option("-s, --search [search]", "Search term to filter links by")
+ .option("-l, --limit [limit]", "Number of links to fetch")
+ .action(async ({ search, limit }) => {
+ try {
+ const config = await getConfig();
+
+ const spinner = ora("Fetching links").start();
+
+ const dub = new Dub({
+ token: config.key,
+ });
+
+ const links = await dub.links.list({
+ search,
+ pageSize: limit ? parseInt(limit) : 10,
+ });
+ spinner.stop();
+
+ const formattedLinks = links.result.map((link) => ({
+ "Short Link": link.shortLink,
+ "Destination URL": link.url,
+ Clicks: link.clicks,
+ "Created At": new Date(link.createdAt).toLocaleDateString("en-US", {
+ month: "short",
+ day: "numeric",
+ hour: "numeric",
+ minute: "numeric",
+ hour12: true,
+ }),
+ }));
+
+ console.table(formattedLinks);
+ } catch (error) {
+ handleError(error);
+ }
+ });
diff --git a/packages/cli/src/commands/login.ts b/packages/cli/src/commands/login.ts
index 4555980992..09dbfc6891 100644
--- a/packages/cli/src/commands/login.ts
+++ b/packages/cli/src/commands/login.ts
@@ -53,7 +53,7 @@ export const login = new Command()
domain: "dub.sh",
};
- const config = new Configstore("dubcli");
+ const config = new Configstore("dub-cli");
config.set(configInfo);
if (!config.path) {
diff --git a/packages/cli/src/commands/shorten.ts b/packages/cli/src/commands/shorten.ts
index 3d72fefd82..dc7f8f6b48 100644
--- a/packages/cli/src/commands/shorten.ts
+++ b/packages/cli/src/commands/shorten.ts
@@ -1,4 +1,4 @@
-import { createLink, getLinks } from "@/api/links";
+import { createLink } from "@/api/links";
import { getConfig } from "@/utils/config";
import { getNanoid } from "@/utils/get-nanoid";
import { handleError } from "@/utils/handle-error";
@@ -65,20 +65,3 @@ export const shorten = new Command()
handleError(error);
}
});
-
-shorten
- .command("list")
- .description("View the list of the last 100 links")
- .action(async () => {
- try {
- await getConfig();
-
- const spinner = ora("Fetching links").start();
- const links = await getLinks();
- spinner.stop();
-
- console.table(links, ["shortLink", "clicks"]);
- } catch (error) {
- handleError(error);
- }
- });
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index 6bb97d5682..abec459ee6 100644
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -6,6 +6,7 @@ import { login } from "@/commands/login";
import { shorten } from "@/commands/shorten";
import { getPackageInfo } from "@/utils/get-package-info";
import { Command } from "commander";
+import { links } from "./commands/links";
process.on("SIGINT", () => process.exit(0));
process.on("SIGTERM", () => process.exit(0));
@@ -15,7 +16,7 @@ async function main() {
const program = new Command()
.name("dub")
- .description("a cli for shortening url")
+ .description("A CLI for shortening links with the Dub.co API.")
.version(
packageInfo.version || "1.0.0",
"-v, --version",
@@ -26,7 +27,8 @@ async function main() {
.addCommand(login)
.addCommand(config)
.addCommand(domains)
- .addCommand(shorten);
+ .addCommand(shorten)
+ .addCommand(links);
program.parse();
}
diff --git a/packages/cli/src/utils/config.ts b/packages/cli/src/utils/config.ts
index 7b405dab0a..502108d4d6 100644
--- a/packages/cli/src/utils/config.ts
+++ b/packages/cli/src/utils/config.ts
@@ -2,7 +2,7 @@ import type { DubConfig } from "@/types";
import Configstore from "configstore";
export async function getConfig(): Promise<DubConfig> {
- const getConfig = new Configstore("dubcli");
+ const getConfig = new Configstore("dub-cli");
if (!getConfig.size) {
throw new Error(