Skip to content

Commit

Permalink
fix(workspace-tools): Added code to properly parse out the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
sullivanpj committed Jan 17, 2024
1 parent a2a86ef commit c345fef
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 457 deletions.
39 changes: 25 additions & 14 deletions packages/config-tools/src/create-storm-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getConfigEnv, getExtensionEnv } from "./env/get-env";
import { setConfigEnv } from "./env/set-env";
import { StormConfigSchema } from "./schema";
import type { StormConfig } from "./types";
import { findWorkspaceRoot, writeWarning } from "./utilities";
import { getDefaultConfig } from "./utilities/get-default-config";

const _extension_cache = new WeakMap<{ extensionName: string }, any>();
Expand Down Expand Up @@ -97,21 +98,31 @@ export const createConfigExtension = <
/**
* Load the config file values for the current Storm workspace into environment variables
*/
export const loadStormConfig = async (workspaceRoot?: string) => {
try {
let config = {} as StormConfig;
export const loadStormConfig = async (workspaceRoot?: string): Promise<StormConfig> => {
let config = {} as StormConfig;

const configFile = await getConfigFile(workspaceRoot);
if (configFile) {
config = StormConfigSchema.parse(configFile);
} else {
console.warn(
"No Storm config file found in the current workspace. Please ensure this is the expected behavior - you can add a `storm.config.js` file to the root of your workspace if it is not."
);
}
let _workspaceRoot = workspaceRoot;
if (!_workspaceRoot) {
_workspaceRoot = findWorkspaceRoot();
}

setConfigEnv(getDefaultConfig(config, workspaceRoot));
} catch (e) {
console.error(e);
const configFile = await getDefaultConfig(
{
...(await getConfigFile(_workspaceRoot)),
...getConfigEnv()
},
_workspaceRoot
);
if (configFile) {
config = StormConfigSchema.parse(configFile);
} else {
writeWarning(
config,
"No Storm config file found in the current workspace. Please ensure this is the expected behavior - you can add a `storm.config.js` file to the root of your workspace if it is not."
);
}

setConfigEnv(config);

return config;
};
4 changes: 2 additions & 2 deletions packages/config-tools/src/utilities/find-up.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from "fs";
import { join } from "path";
import { existsSync } from "node:fs";
import { join } from "node:path";

const MAX_PATH_SEARCH_DEPTH = 30;
let depth = 0;
Expand Down
7 changes: 0 additions & 7 deletions packages/config-tools/src/utilities/find-workspace-root.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/**
* Find Workspace Root
*
* @remarks
* Find the monorepo root directory
*/

import { findFolderUp } from "./find-up";

const rootFiles = [
Expand Down
4 changes: 2 additions & 2 deletions packages/config-tools/src/utilities/get-default-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, readFileSync } from "fs";
import { join } from "path";
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { StormConfigSchema } from "../schema";
import type { ColorConfig, StormConfig } from "../types";
import { findWorkspaceRoot } from "./find-workspace-root";
Expand Down
4 changes: 2 additions & 2 deletions packages/workspace-tools/src/base/base-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ExecutorContext } from "@nx/devkit";
import {
type StormConfig,
getStopwatch,
prepareWorkspace,
loadStormConfig,
writeDebug,
writeError,
writeFatal,
Expand Down Expand Up @@ -75,7 +75,7 @@ export const withRunExecutor =
- projectName: ${projectName}\n`
);

config = await prepareWorkspace();
config = await loadStormConfig(workspaceRoot);
writeTrace(
config,
`Loaded Storm config into env: \n${Object.keys(process.env)
Expand Down
4 changes: 2 additions & 2 deletions packages/workspace-tools/src/base/base-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Tree } from "@nx/devkit";
import {
type StormConfig,
getStopwatch,
prepareWorkspace,
loadStormConfig,
writeDebug,
writeError,
writeFatal,
Expand Down Expand Up @@ -60,7 +60,7 @@ export const withRunGenerator =
- workspaceRoot: ${workspaceRoot}`
);

config = await prepareWorkspace();
config = await loadStormConfig(workspaceRoot);
writeTrace(
config,
`Loaded Storm config into env: \n${Object.keys(process.env)
Expand Down
Loading

0 comments on commit c345fef

Please sign in to comment.