-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
269 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
|
Oops, something went wrong.