diff --git a/packages/rstack/src/config.ts b/packages/rstack/src/config.ts index 81fa02f..ce73030 100644 --- a/packages/rstack/src/config.ts +++ b/packages/rstack/src/config.ts @@ -24,6 +24,15 @@ type LoadedRstackConfig = { dependencies: string[]; }; +type LoadRstackConfigOptions = { + /** + * The path to the Rstack config file, can be a relative or absolute path. + * If `configFilePath` is not provided, the config path set by the CLI is used. + * If neither path is provided, the function will search for the config file in the current working directory. + */ + configFilePath?: string; +}; + type ConfigState = { configs: Configs; configPath?: string; @@ -108,8 +117,11 @@ export const define: Define = { staged: (config) => setConfig('staged', config), }; -export const loadRstackConfig = async (): Promise => { +export const loadRstackConfig = async ({ + configFilePath, +}: LoadRstackConfigOptions = {}): Promise => { const state = getConfigState(); + const configPath = configFilePath ?? state.configPath; state.configs = {}; try { @@ -117,8 +129,8 @@ export const loadRstackConfig = async (): Promise => { loader: 'native', exportName: false, fresh: true, - ...(state.configPath !== undefined - ? { path: state.configPath } + ...(configPath !== undefined + ? { path: configPath } : { configFileNames: [ 'rstack.config.ts', diff --git a/packages/rstack/tests/config/load-config/explicit.config.ts b/packages/rstack/tests/config/load-config/explicit.config.ts new file mode 100644 index 0000000..89e13f6 --- /dev/null +++ b/packages/rstack/tests/config/load-config/explicit.config.ts @@ -0,0 +1,3 @@ +import { define } from 'rstack'; + +define.app({}); diff --git a/packages/rstack/tests/config/load-config/index.test.ts b/packages/rstack/tests/config/load-config/index.test.ts new file mode 100644 index 0000000..aa2d834 --- /dev/null +++ b/packages/rstack/tests/config/load-config/index.test.ts @@ -0,0 +1,28 @@ +import path from 'node:path'; +import { afterEach, expect, test } from 'rstack/test'; +import { getConfigState, loadRstackConfig } from '../../../src/config.ts'; + +const state = getConfigState(); + +afterEach(() => { + state.configs = {}; + delete state.configPath; +}); + +test('should reset config state before and after loading', async () => { + state.configs = { app: {} }; + state.configPath = path.join(import.meta.dirname, 'rstack.config.ts'); + + await expect(loadRstackConfig()).rejects.toThrow('test config error'); + expect(state.configs).toEqual({}); +}); + +test('should prefer an explicit config path over the state config path', async () => { + const configFilePath = path.join(import.meta.dirname, 'explicit.config.ts'); + state.configPath = path.join(import.meta.dirname, 'rstack.config.ts'); + + const { configs, filePath } = await loadRstackConfig({ configFilePath }); + + expect(configs.app).toEqual({}); + expect(filePath).toBe(configFilePath); +}); diff --git a/packages/rstack/tests/config/load-state/rstack.config.ts b/packages/rstack/tests/config/load-config/rstack.config.ts similarity index 100% rename from packages/rstack/tests/config/load-state/rstack.config.ts rename to packages/rstack/tests/config/load-config/rstack.config.ts diff --git a/packages/rstack/tests/config/load-state/index.test.ts b/packages/rstack/tests/config/load-state/index.test.ts deleted file mode 100644 index 1f4e34f..0000000 --- a/packages/rstack/tests/config/load-state/index.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -import path from 'node:path'; -import { expect, test } from 'rstack/test'; -import { getConfigState, loadRstackConfig } from '../../../src/config.ts'; - -test('should reset config state before and after loading', async () => { - const state = getConfigState(); - state.configs = { app: {} }; - state.configPath = path.join(import.meta.dirname, 'rstack.config.ts'); - - try { - await expect(loadRstackConfig()).rejects.toThrow('test config error'); - expect(state.configs).toEqual({}); - } finally { - state.configs = {}; - delete state.configPath; - } -});