Skip to content

Commit

Permalink
feat(config-tools): Significant improvements to logic to get config f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
sullivanpj committed Apr 9, 2024
1 parent c0bc133 commit 0a0ac89
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 96 deletions.
2 changes: 1 addition & 1 deletion packages/build-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This package is part of the <b>⚡Storm-Ops</b> monorepo. The Storm-Ops packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-0.4.29-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;
[![Version](https://img.shields.io/badge/version-0.5.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;
[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with docusaurus](https://img.shields.io/badge/documented_with-docusaurus-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://docusaurus.io/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-ops/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)

> [!IMPORTANT]
Expand Down
2 changes: 2 additions & 0 deletions packages/config-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"main": "./index.js",
"types": "declarations.d.ts",
"dependencies": {
"c12": "^1.10.0",
"chalk": "5.3.0",
"cosmiconfig": "9.0.0",
"deepmerge": "4.3.1",
"zod": "3.22.4"
},
"publishConfig": {
Expand Down
120 changes: 30 additions & 90 deletions packages/config-tools/src/config-file/get-config-file.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
import type { CosmiconfigResult, PublicExplorer, Config } from "cosmiconfig";
import type { StormConfigInput } from "@storm-software/config";
import { join } from "node:path";
import { findWorkspaceRoot } from "../utilities/find-workspace-root";
import { readFile, stat } from "node:fs/promises";
import { loadConfig } from "c12";
import merge from "deepmerge";

let _cosmiconfig: any = undefined;
let defaultExplorer: PublicExplorer | undefined;

/**
* Get the config file for the current Storm workspace
*
* @param fileName - The name of the config file to search for
* @param filePath - The path to search for the config file in
* @returns The config file for the current Storm workspace
*/
export const getConfigFileExplorer = async (
fileName: string
): Promise<PublicExplorer | undefined> => {
if (!_cosmiconfig) {
const mod = await import("cosmiconfig");
if (mod?.cosmiconfig) {
_cosmiconfig = mod.cosmiconfig;
}

if (!_cosmiconfig) {
return undefined;
}
}

return _cosmiconfig(fileName, { cache: true });
};
// let _cosmiconfig: any = undefined;
// let defaultExplorer: PublicExplorer | undefined;

/**
* Get the config file for the current Storm workspace
Expand All @@ -41,36 +16,13 @@ export const getConfigFileExplorer = async (
export const getConfigFileByName = async (
fileName: string,
filePath?: string
): Promise<CosmiconfigResult | undefined> => {
return (await getConfigFileExplorer(fileName))?.search(filePath);
};

/**
* Get the config file for the current Storm workspace
*
* @param fileName - The name of the config file to search for
* @param filePath - The path to search for the config file in
* @returns The config file for the current Storm workspace
*/
export const getJsonConfigFile = async (
fileName: string,
filePath?: string
): Promise<CosmiconfigResult | undefined> => {
// const fse = await import("fs-extra/esm");

const jsonPath = join(
filePath ?? process.cwd(),
fileName.endsWith(".json") ? fileName : `${fileName}.json`
);
const isEmpty = !!(await stat(jsonPath).catch((_) => false));
): Promise<Partial<StormConfigInput> | undefined> => {
const workspacePath = filePath ? filePath : findWorkspaceRoot(filePath);

return isEmpty
? {
config: JSON.parse(await readFile(jsonPath, "utf-8")),
filepath: jsonPath,
isEmpty
}
: { config: {} as Config, filepath: jsonPath, isEmpty };
return loadConfig<Partial<StormConfigInput>>({
cwd: workspacePath,
name: fileName
});
};

/**
Expand All @@ -84,45 +36,33 @@ export const getConfigFile = async (
): Promise<Partial<StormConfigInput> | undefined> => {
const workspacePath = filePath ? filePath : findWorkspaceRoot(filePath);

// let cosmiconfigResult = await getJsonConfigFile("storm", workspacePath);
// if (!cosmiconfigResult || cosmiconfigResult.isEmpty) {
if (!defaultExplorer) {
defaultExplorer = await getConfigFileExplorer("storm");
}

let cosmiconfigResult: any = null;
if (defaultExplorer) {
cosmiconfigResult = await defaultExplorer.search(workspacePath);
}

if ((!cosmiconfigResult || cosmiconfigResult.isEmpty) && additionalFileNames.length > 0) {
for (const additionalFileName of additionalFileNames) {
cosmiconfigResult = await getJsonConfigFile(additionalFileName, workspacePath);
if (cosmiconfigResult && !cosmiconfigResult.isEmpty) {
break;
}

cosmiconfigResult = await getConfigFileByName(additionalFileName, workspacePath);
if (cosmiconfigResult && !cosmiconfigResult.isEmpty) {
break;
let { config, configFile } = await loadConfig<Partial<StormConfigInput>>({
cwd: workspacePath,
name: "storm"
});

if (additionalFileNames) {
const results = await Promise.all(
additionalFileNames.map(fileName =>
loadConfig({
cwd: workspacePath,
name: fileName
})
)
);

for (const result of results) {
if (result) {
config = merge(config ?? {}, result.config ?? {});
}
}
}
// }

if (
!cosmiconfigResult ||
Object.keys(cosmiconfigResult).length === 0 ||
cosmiconfigResult.isEmpty ||
!cosmiconfigResult.filepath
) {
if (!config) {
return undefined;
}

const config: Partial<StormConfigInput> = cosmiconfigResult.config ?? {};
if (cosmiconfigResult.filepath) {
config.configFile = cosmiconfigResult.filepath;
}
config.configFile = configFile;
config.runtimeVersion = "0.0.1";

return config;
Expand Down
3 changes: 1 addition & 2 deletions packages/workspace-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This package is part of the <b>⚡Storm-Ops</b> monorepo. The Storm-Ops packages

<h3 align="center">💻 Visit <a href="https://stormsoftware.com" target="_blank">stormsoftware.com</a> to stay up to date with this developer</h3><br />

[![Version](https://img.shields.io/badge/version-1.69.0-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;
[![Version](https://img.shields.io/badge/version-1.70.1-1fb2a6.svg?style=for-the-badge&color=1fb2a6)](https://prettier.io/)&nbsp;
[![Nx](https://img.shields.io/badge/Nx-17.0.2-lightgrey?style=for-the-badge&logo=nx&logoWidth=20&&color=1fb2a6)](http://nx.dev/)&nbsp;[![NextJs](https://img.shields.io/badge/Next.js-14.0.2-lightgrey?style=for-the-badge&logo=nextdotjs&logoWidth=20&color=1fb2a6)](https://nextjs.org/)&nbsp;[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=for-the-badge&logo=commitlint&color=1fb2a6)](http://commitizen.github.io/cz-cli/)&nbsp;![Semantic-Release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=for-the-badge&color=1fb2a6)&nbsp;[![documented with docusaurus](https://img.shields.io/badge/documented_with-docusaurus-success.svg?style=for-the-badge&logo=readthedocs&color=1fb2a6)](https://docusaurus.io/)&nbsp;![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/storm-software/storm-ops/cr.yml?style=for-the-badge&logo=github-actions&color=1fb2a6)

> [!IMPORTANT]
Expand Down Expand Up @@ -346,7 +346,6 @@ The following executor options are available:
| clean | `boolean` | Remove previous output before build. | `true` |
| includeSrc | `boolean` | Should the source files be added to the distribution folder in an \`src\` directory. | |
| debug | `boolean` | Should output be unminified with source mappings. | |
| **banner \*** | `string` | A short heading added to the top of each typescript file added in the output folder's \`src\` directory. | "This code was developed by Storm Software (<https://stormsoftware.com>) and is licensed under the Apache License 2.0." |
| minify | `boolean` | Should the build process minify the output files? | |
| verbose | `boolean` | Should write extra log outputs with details from the executor. | |
| plugins | `object[]` | List of Rollup plugins to use during processing | `[]` |
Expand Down
Loading

0 comments on commit 0a0ac89

Please sign in to comment.