Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/rstack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Comment thread
coderabbitai[bot] marked this conversation as resolved.
configFilePath?: string;
};

type ConfigState = {
configs: Configs;
configPath?: string;
Expand Down Expand Up @@ -108,17 +117,20 @@ export const define: Define = {
staged: (config) => setConfig('staged', config),
};

export const loadRstackConfig = async (): Promise<LoadedRstackConfig> => {
export const loadRstackConfig = async ({
configFilePath,
}: LoadRstackConfigOptions = {}): Promise<LoadedRstackConfig> => {
Comment thread
chenjiahan marked this conversation as resolved.
const state = getConfigState();
const configPath = configFilePath ?? state.configPath;
state.configs = {};

try {
const { filePath, dependencies } = await loadConfig({
loader: 'native',
exportName: false,
fresh: true,
...(state.configPath !== undefined
? { path: state.configPath }
...(configPath !== undefined
? { path: configPath }
: {
configFileNames: [
'rstack.config.ts',
Expand Down
3 changes: 3 additions & 0 deletions packages/rstack/tests/config/load-config/explicit.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { define } from 'rstack';

define.app({});
28 changes: 28 additions & 0 deletions packages/rstack/tests/config/load-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
17 changes: 0 additions & 17 deletions packages/rstack/tests/config/load-state/index.test.ts

This file was deleted.