Skip to content

Commit

Permalink
env variables opt
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Jan 4, 2025
1 parent 300f1d0 commit 3e4c82f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-carpets-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mdcast": patch
---

env vars opt
File renamed without changes.
35 changes: 32 additions & 3 deletions src/commands/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ type PostOptions = {
dryRun: boolean;
};

function validateEnvVars(platform: Platforms) {
switch (platform) {
case Platforms.DEVTO:
if (!env.DEVTO_API_KEY) {
handleError(
"DEVTO_API_KEY env variable is required for Dev.to platform"
);
}
break;
case Platforms.HASHNODE:
if (!env.HASHNODE_TOKEN) {
handleError(
"HASHNODE_TOKEN env variable is required for Hashnode platform"
);
}
break;
case Platforms.MEDIUM:
if (!env.MEDIUM_TOKEN) {
handleError(
"MEDIUM_TOKEN env variable is required for Medium platform"
);
}
break;
}
}

export default async function post(
path: string,
{ platforms, dryRun }: PostOptions
Expand All @@ -34,9 +60,12 @@ export default async function post(
const markdownClient = new MarkdownClient(path, config.markdown);
const postData: Post = await markdownToPost(markdownClient);

// Validate env vars for each requested platform
platforms.forEach(validateEnvVars);

if (platforms.includes(Platforms.DEVTO)) {
const connection_settings = {
api_key: env.DEVTO_API_KEY,
api_key: env.DEVTO_API_KEY!,
organization_id: env.DEVTO_ORG_ID,
};
const devto = new DevToClient(
Expand All @@ -51,7 +80,7 @@ export default async function post(

if (platforms.includes(Platforms.HASHNODE)) {
const connection_settings = {
token: env.HASHNODE_TOKEN,
token: env.HASHNODE_TOKEN!,
publication_id: env.HASHNODE_PUBLICATION_ID,
};
const hashnode = new HashnodeClient(
Expand All @@ -66,7 +95,7 @@ export default async function post(

if (platforms.includes(Platforms.MEDIUM)) {
const connection_settings = {
token: env.MEDIUM_TOKEN,
token: env.MEDIUM_TOKEN!,
publication_name: env.MEDIUM_PUBLICATION_NAME,
};
const medium = new MediumClient(
Expand Down
6 changes: 3 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const env = createEnv({
*/
clientPrefix: "PUBLIC_",
server: {
DEVTO_API_KEY: z.string(),
DEVTO_API_KEY: z.string().optional(),
DEVTO_ORG_ID: z.string().optional(),
HASHNODE_TOKEN: z.string(),
HASHNODE_TOKEN: z.string().optional(),
HASHNODE_PUBLICATION_ID: z.string().optional(),
MEDIUM_TOKEN: z.string(),
MEDIUM_TOKEN: z.string().optional(),
MEDIUM_PUBLICATION_NAME: z.string().optional(),
},
client: {},
Expand Down

0 comments on commit 3e4c82f

Please sign in to comment.