Skip to content

Commit

Permalink
fix: allow to store API key in environment variable
Browse files Browse the repository at this point in the history
`cwd/.env` and `cwd/.env.storypointer` are supported.
  • Loading branch information
jamacku committed Aug 12, 2024
1 parent f10554e commit 8bb68d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ web_modules/
.env.test.local
.env.production.local
.env.local
.env.storypointer

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
14 changes: 11 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import chalk from 'chalk';
import { Command } from 'commander';
import select, { Separator } from '@inquirer/select';
import path from 'path';

Check warning on line 5 in src/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main.ts#L5

Added line #L5 was not covered by tests
import { z } from 'zod';

import 'dotenv/config';
import dotenv from 'dotenv';

Check warning on line 8 in src/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main.ts#L8

Added line #L8 was not covered by tests
import '@total-typescript/ts-reset';

import { Jira } from './jira';
import { raise } from './util';
import { raise, tokenUnavailable } from './util';

Check warning on line 12 in src/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main.ts#L12

Added line #L12 was not covered by tests
import {
colorPrioritySchema,
colorSizeSchema,
Expand All @@ -21,6 +22,13 @@ import {
SizeWithExit,
} from './schema/jira';

dotenv.config({
path: [
`${path.resolve(process.cwd(), '.env.storypointer')}`,
`${path.resolve(process.cwd(), '.env')}`,
],
});

Check warning on line 30 in src/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main.ts#L25-L30

Added lines #L25 - L30 were not covered by tests

const cli = async () => {
const program = new Command();

Expand All @@ -37,7 +45,7 @@ const cli = async () => {

program.parse();

const token = process.env.JIRA_API_TOKEN ?? raise('JIRA_API_TOKEN not set.');
const token = process.env.JIRA_API_TOKEN ?? tokenUnavailable();

Check warning on line 48 in src/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main.ts#L48

Added line #L48 was not covered by tests
const jira = new Jira('https://issues.redhat.com', token);

const version = await jira.getVersion();
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function raise(error: string): never {
throw new Error(error);
}

export function tokenUnavailable(): never {
return raise(
`JIRA_API_TOKEN not set.\nPlease set the JIRA_API_TOKEN environment variable in ~/.env.storypointer or ~/.env.`
);
}

Check warning on line 9 in src/util.ts

View check run for this annotation

Codecov / codecov/patch

src/util.ts#L6-L9

Added lines #L6 - L9 were not covered by tests

0 comments on commit 8bb68d3

Please sign in to comment.