Skip to content

Commit

Permalink
fix: no multiple warning for loading config file (#74)
Browse files Browse the repository at this point in the history
closes #68
  • Loading branch information
agoldis authored Mar 10, 2023
1 parent ba5f47a commit a87ffc7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
9 changes: 7 additions & 2 deletions packages/cypress-cloud/lib/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TestingType,
} from "../../types";
import { getCurrentsConfig } from "../config";
import { withError } from "../log";
import { sanitizeAndConvertNestedArgs } from "./parser";
import { program } from "./program";

Expand Down Expand Up @@ -115,15 +116,19 @@ export async function getRunParameters(

if (!key) {
return program.error(
"Missing 'key'. Please either pass it as a cli flag '-k, --key <record-key>', set it in currents.config.js, or set CURRENTS_RECORD_KEY environment variable."
withError(
"Missing 'key'. Please either pass it as a cli flag '-k, --key <record-key>', set it in currents.config.js, or set CURRENTS_RECORD_KEY environment variable."
)
);
}

const _projectId = process.env.CURRENTS_PROJECT_ID ?? projectId;

if (!_projectId) {
return program.error(
"Missing 'projectId'. Please either set it in currents.config.js, or as CURRENTS_PROJECT_ID environment variable."
withError(
"Missing 'projectId'. Please either set it in currents.config.js, or as CURRENTS_PROJECT_ID environment variable."
)
);
}

Expand Down
13 changes: 10 additions & 3 deletions packages/cypress-cloud/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export type CurrentsConfig = {
component: ComponentConfig;
};

let _config: CurrentsConfig | null = null;
export function getCurrentsConfig(): CurrentsConfig {
if (_config) {
return _config;
}

const configFilePath = getConfigFilePath();
debug("loading currents config file from '%s'", configFilePath);

Expand All @@ -36,13 +41,15 @@ export function getCurrentsConfig(): CurrentsConfig {

try {
const fsConfig = require(configFilePath);
return {
_config = {
...defaultConfig,
...fsConfig,
};
} as CurrentsConfig;
return _config;
} catch (e) {
warn("failed to load currents config file: %s", configFilePath);
return defaultConfig;
_config = defaultConfig;
return _config;
}
}

Expand Down
13 changes: 9 additions & 4 deletions packages/cypress-cloud/lib/log.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import chalk from "chalk";
import util from "util";

const log = (...args: unknown[]) => console.log(" ", util.format(...args));
const log = (...args: unknown[]) => console.log(util.format(...args));

export const info = log;

export const withError = (msg: string) =>
chalk.bgRed.white(" ERROR ") + " " + msg;
export const withWarning = (msg: string) =>
chalk.bgYellow.black(" WARNING ") + " " + msg;

export const warn = (...args: unknown[]) =>
log(chalk.bgYellow.black(" WARNING "), util.format(...args));
log(withWarning(util.format(...args)));

export const success = (...args: unknown[]) =>
log(chalk.green(util.format(...args)));

export const error = (...args: unknown[]) =>
log(chalk.bgRed.white(" ERROR "), util.format(...args));
log(withError(util.format(...args)));

type Color = "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white";
export const title = (color: Color, ...args: unknown[]) =>
Expand All @@ -21,7 +26,7 @@ export const title = (color: Color, ...args: unknown[]) =>
export const divider = () =>
console.log("\n" + chalk.gray(Array(100).fill("=").join("")) + "\n");

export const spacer = (n: number = 2) =>
export const spacer = (n: number = 0) =>
console.log(Array(n).fill("").join("\n"));

export const cyan = chalk.cyan;
Expand Down

0 comments on commit a87ffc7

Please sign in to comment.