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
5 changes: 3 additions & 2 deletions src/helpers/config-file-loader.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export function configFilePaths(name: string): string[] {
export async function configLoaderFile<
S extends ServiceMap = ServiceMap,
C extends ModuleConfiguration = ModuleConfiguration,
>({ application, logger }: ConfigLoaderParams<S, C>): ConfigLoaderReturn {
>({ application, logger, internal }: ConfigLoaderParams<S, C>): ConfigLoaderReturn {
const CLI_SWITCHES = minimist(process.argv);
const configFile = CLI_SWITCHES.config;
const configFile =
CLI_SWITCHES.config ?? internal?.boot?.options?.configuration?.boilerplate?.CONFIG;
let files: string[];
if (is.boolean(configFile)) {
logger.fatal({ argv: process.argv }, "system failed to parse argv");
Expand Down
2 changes: 1 addition & 1 deletion src/services/wiring.service.mts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createBoilerplate() {
return CreateLibrary({
configuration: {
/**
* Only usable by **cli switch**.
* Only usable by **cli switch** / application bootstrap.
* Pass path to a config file for loader
*
* ```bash
Expand Down
81 changes: 81 additions & 0 deletions testing/configuration.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,87 @@ describe("Configuration", () => {

expect(readSpy).toHaveBeenCalledWith("./config_file", "utf8");
});

it("uses bootstrap-provided CONFIG when no CLI switch", async () => {
vi.spyOn(fs, "existsSync").mockImplementation(() => true);

const readSpy = vi.spyOn(fs, "readFileSync").mockImplementation(() => ``);
const logger = createMockLogger();
process.argv = [""];
await configLoaderFile({
// @ts-expect-error not needed for test
application: {},
internal: {
boot: {
options: {
configuration: {
boilerplate: {
CONFIG: "./bootstrap_config.yaml",
},
},
},
},
} as InternalDefinition,
logger,
});

expect(readSpy).toHaveBeenCalledWith("./bootstrap_config.yaml", "utf8");
});

it("CLI switch takes precedence over bootstrap-provided CONFIG", async () => {
vi.spyOn(fs, "existsSync").mockImplementation(() => true);

const readSpy = vi.spyOn(fs, "readFileSync").mockImplementation(() => ``);
const logger = createMockLogger();
process.argv = ["", "--config=./cli_config.yaml"];
await configLoaderFile({
// @ts-expect-error not needed for test
application: {},
internal: {
boot: {
options: {
configuration: {
boilerplate: {
CONFIG: "./bootstrap_config.yaml",
},
},
},
},
} as InternalDefinition,
logger,
});

expect(readSpy).toHaveBeenCalledWith("./cli_config.yaml", "utf8");
});

it("bootstrap CONFIG overrides auto-discovery", async () => {
vi.spyOn(fs, "existsSync").mockImplementation(() => true);
// @ts-expect-error rest isn't needed
vi.spyOn(fs, "statSync").mockImplementation(() => ({ isFile: () => true }));

const readSpy = vi.spyOn(fs, "readFileSync").mockImplementation(() => ``);
const logger = createMockLogger();
process.argv = [""];
await configLoaderFile({
// @ts-expect-error not needed for test
application: { name: "test-app" },
internal: {
boot: {
options: {
configuration: {
boilerplate: {
CONFIG: "./explicit_config.yaml",
},
},
},
},
} as InternalDefinition,
logger,
});

expect(readSpy).toHaveBeenCalledWith("./explicit_config.yaml", "utf8");
expect(readSpy).not.toHaveBeenCalledWith(expect.stringContaining(".test-app"), "utf8");
});
});

// #MARK: loadConfigFromFile
Expand Down