Skip to content

Commit

Permalink
Merge pull request #21 from saiichihashimoto/load-dot-env
Browse files Browse the repository at this point in the history
feat(env): load env vars using @next/env
  • Loading branch information
kodiakhq[bot] authored Jan 16, 2024
2 parents a13efb5 + c347d63 commit f2b2968
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test": "jest --color --cache"
},
"dependencies": {
"@next/env": "14.0.4",
"boxen": "5.1.2",
"chalk": "4.1.2",
"commander": "11.1.0",
Expand Down
36 changes: 36 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ describe("main", () => {

expect(winner).toBe("timeout");
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand Down Expand Up @@ -146,6 +152,12 @@ describe("main", () => {

expect(winner).not.toBe("timeout");
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand All @@ -172,6 +184,12 @@ describe("main", () => {

expect(fetchSpy).not.toHaveBeenCalled();
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand Down Expand Up @@ -257,6 +275,12 @@ describe("main", () => {

expect(fetchSpy).not.toHaveBeenCalled();
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand Down Expand Up @@ -365,6 +389,12 @@ describe("main", () => {

expect(fetchSpy).not.toHaveBeenCalled();
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand Down Expand Up @@ -411,6 +441,12 @@ describe("main", () => {

expect(fetchSpy).not.toHaveBeenCalled();
expect(destination.logs).toStrictEqual([
{
level: 20,
loadedEnvFiles: [],
msg: "Loaded Env Files",
time: 1696486441293,
},
{
config: "./vercel.json",
level: 20,
Expand Down
32 changes: 17 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fsNative from "node:fs";
import { promisify } from "node:util";

import { loadEnvConfig } from "@next/env";
import boxen from "boxen";
import chalk from "chalk";
import { Cron } from "croner";
Expand All @@ -19,7 +20,7 @@ export const zOpts = z
dry: z.boolean(),
ignoreTimestamp: z.boolean(),
pretty: z.boolean(),
secret: z.nullable(z.string()),
secret: z.optional(z.nullable(z.string())),
url: z.string(),
level: z.union([
z.literal("trace"),
Expand All @@ -35,7 +36,6 @@ export const zOpts = z

export const defaults = {
config: "./vercel.json",
secret: process.env.CRON_SECRET ?? null,
url: "http://localhost:3000",
} satisfies z.infer<typeof zOpts>;

Expand All @@ -49,19 +49,7 @@ export const main = async ({
fs?: typeof fsNative;
signal?: AbortSignal;
}) => {
const {
color,
config,
dry,
ignoreTimestamp,
pretty,
secret,
url,
level = "debug",
} = {
...defaults,
...opts,
};
const { color, dry, ignoreTimestamp, pretty, level = "debug" } = opts;

if (chalk.supportsColor && !color) {
chalk.level = 0;
Expand Down Expand Up @@ -95,6 +83,19 @@ export const main = async ({
? pino(loggerOptions)
: pino(loggerOptions, destination);

const { loadedEnvFiles } = loadEnvConfig(process.cwd(), false, logger);

const {
config,
url,
secret: secretNullable,
} = {
...defaults,
...opts,
};

const secret = secretNullable ?? process.env.CRON_SECRET;

if (logger.isLevelEnabled("info") && pretty) {
/* eslint-disable no-console -- boxen! */
console.log(
Expand All @@ -109,6 +110,7 @@ export const main = async ({
}

logger.trace({ opts }, "Parsed Options");
logger.debug({ loadedEnvFiles }, "Loaded Env Files");

const readFile = promisify(fs.readFile.bind(fs));

Expand Down
2 changes: 1 addition & 1 deletion src/vercel-cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import pkg from "../package.json";
.option("-p --config <config>", "Vercel Config", defaults.config)
.addOption(
new Option("-s --secret <secret>", "Cron Secret").default(
defaults.secret,
null,
"`process.env.CRON_SECRET`"
)
)
Expand Down

0 comments on commit f2b2968

Please sign in to comment.