Skip to content

Commit

Permalink
added init command
Browse files Browse the repository at this point in the history
  • Loading branch information
sujjeee committed Dec 16, 2023
1 parent 834793e commit 11e07a6
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 8 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "dubco",
"version": "0.0.",
"version": "0.0.1",
"description": "A CLI for shortening url.",
"publishConfig": {
"access": "public"
},
"type": "module",
"exports": "./dist/index.js",
"bin": "./dist/index.js",
"bin": {
"dubco": "./dist/index.js"
},
"license": "MIT",
"author": {
"name": "sujjeee",
Expand All @@ -18,11 +20,7 @@
"url": "https://github.com/suraj/dubco.git"
},
"files": ["dist"],
"keywords": [
"dub.co",
"url shortener",
"sujjeee"
],
"keywords": ["dub.co", "url shortener", "sujjeee"],
"scripts": {
"dev": "tsup --watch",
"build": "tsup",
Expand All @@ -41,6 +39,7 @@
"dependencies": {
"chalk": "^5.3.0",
"commander": "^11.1.0",
"configstore": "^6.0.0",
"fs-extra": "^11.2.0",
"node-fetch": "^3.3.2",
"ora": "^7.0.1",
Expand All @@ -50,6 +49,7 @@
},
"devDependencies": {
"@biomejs/biome": "1.4.1",
"@types/configstore": "^6.0.2",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.10.4",
"@types/prompts": "^2.4.9",
Expand Down
83 changes: 82 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node

import { handleError } from "@/src/utils/handle-error";
import { logger } from "@/src/utils/logger";
import chalk from "chalk";
import { Command } from "commander";
import Configstore from "configstore";
import ora from "ora";
import prompts from "prompts";

export const init = new Command()
.name("init")
.description("configure your dub.co authorization credentails")
.option(
"-c, --cwd <cwd>",
"the working directory. defaults to the current directory.",
process.cwd()
)
.action(async () => {
try {
await promptForConfig();
logger.info("");
logger.info(`${chalk.green("Success!")} Configuration completed.`);
logger.info("");
} catch (error) {
handleError(error);
}
});

export async function promptForConfig() {
const highlight = (text: string) => chalk.cyan(text);

const options = await prompts([
{
type: "text",
name: "authToken",
message: `Enter your Dub.co ${highlight("Authorization Token")} :`,
initial: "eyb4411s7saksiall1s2d2"
},
{
type: "text",
name: "domain",
message: `Enter your Dub.co ${highlight("Domain")} :`,
initial: "dub.co"
},
{
type: "text",
name: "projectSlug",
message: `Enter your Dub.co ${highlight("Project Slug")} :`,
initial: "example"
}
]);

const authorization = {
authToken: options.authToken,
domain: options.domain,
projectSlug: options.projectSlug
};

const spinner = ora("Configuring...").start();

const createConfig = new Configstore("dubco.config.json", authorization);

if (!createConfig.path) {
spinner.stop();
handleError(new Error("Failed to create config file"));
}

spinner.succeed("Configuration completed");
}
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

import { init } from "@/src/commands/init";
import { getPackageInfo } from "@/src/utils/get-package-info";
import { Command } from "commander";

process.on("SIGINT", () => process.exit(0));
process.on("SIGTERM", () => process.exit(0));

function main() {
const packageInfo = getPackageInfo();

const program = new Command()
.name("dubco")
.description("A CLI for shortening url.")
.version(
packageInfo.version || "1.0.0",
"-v, --version",
"display the version number"
);

program.addCommand(init);

program.parse();
}

main();
9 changes: 9 additions & 0 deletions src/utils/get-package-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";
import fs from "fs-extra";
import { type PackageJson } from "type-fest";

export function getPackageInfo() {
const packageJsonPath = path.join("package.json");

return fs.readJSONSync(packageJsonPath) as PackageJson;
}
16 changes: 16 additions & 0 deletions src/utils/handle-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { logger } from "@/src/utils/logger";

export function handleError(error: unknown) {
if (typeof error === "string") {
logger.error(error);
process.exit(1);
}

if (error instanceof Error) {
logger.error(error.message);
process.exit(1);
}

logger.error("Something went wrong. Please try again.");
process.exit(1);
}
19 changes: 19 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import chalk from "chalk";

export const logger = {
error(...args: unknown[]) {
console.log(chalk.red(...args));
},
warn(...args: unknown[]) {
console.log(chalk.yellow(...args));
},
info(...args: unknown[]) {
console.log(chalk.cyan(...args));
},
success(...args: unknown[]) {
console.log(chalk.green(...args));
},
break() {
console.log("");
}
};
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"composite": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": false,
"isolatedModules": true,
"moduleResolution": "node",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"@/*": ["./*"]
}
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}

Loading

0 comments on commit 11e07a6

Please sign in to comment.