Skip to content

Commit 97bc3f4

Browse files
committed
feat(cli): add auth command
1 parent dcfd92f commit 97bc3f4

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

demo/next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev": "next dev",
77
"build": "next build",
8+
"i18n": "replexica i18n",
89
"start": "next start",
910
"lint": "next lint"
1011
},

packages/cli/src/auth.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Command } from "commander";
2+
3+
export default new Command()
4+
.command("auth")
5+
.description("Authenticate with Replexica")
6+
.helpOption("-h, --help", "Show help")
7+
.action(async () => {
8+
const message = `
9+
To obtain Replexica API key, please follow the following link:
10+
11+
https://replexica.com/app/settings
12+
13+
Once you have your API key, please save it in the .env file in the root of your project.
14+
`.trim();
15+
16+
console.log(message);
17+
});

packages/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dotenv from 'dotenv';
22
import { Command } from 'commander';
33

44
import i18nCmd from './i18n.js';
5+
import authCmd from './auth.js';
56

67
dotenv.config();
78

@@ -10,4 +11,5 @@ export default new Command()
1011
.description('Replexica CLI')
1112
.helpOption('-h, --help', 'Show help')
1213
.addCommand(i18nCmd)
14+
.addCommand(authCmd)
1315
.parse(process.argv);

packages/cli/src/services/env.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,22 @@ const EnvSchema = Z.object({
66
});
77

88
export function getEnv() {
9-
return EnvSchema.parse(process.env);
10-
}
9+
try {
10+
return EnvSchema.parse(process.env);
11+
} catch (error) {
12+
if (error instanceof Z.ZodError) {
13+
// handle missing api key
14+
if (
15+
error.errors.some((err) => err.path.find((p) => p === 'REPLEXICA_API_KEY') &&
16+
err.message.toLowerCase().includes('required'))
17+
) {
18+
console.log(`REPLEXICA_API_KEY is missing in env variables. Did you forget to run 'replexica auth'?`);
19+
return process.exit(1);
20+
} else {
21+
throw error;
22+
}
23+
} else {
24+
throw error;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)