Skip to content

Commit a6975e3

Browse files
Extract readConfig logic to a dedicated config.ts module for improved separation of concerns.
1 parent 5240dd1 commit a6975e3

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cwd, readConfig, readFile, writeFile } from "./utils/filesystem";
1+
import { cwd, readFile, writeFile } from "./utils/filesystem";
22
import { context, getOctokit } from "@actions/github";
33
import { parse } from "./utils/inputs";
44
import { info } from "@actions/core";
@@ -8,6 +8,7 @@ import { setPreview } from "./utils/preview";
88
import { setOutputs } from "./utils/outputs";
99
import { getPackageManager } from "./utils/packageManagers";
1010
import { titleCase } from "./utils/strings";
11+
import { readConfig } from './utils/config'
1112

1213
const previewUpdater = async () => {
1314
// Inputs

src/utils/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Config, defaultConfig } from '../types/config'
2+
import { deepmerge } from 'deepmerge-ts'
3+
import * as yaml from 'js-yaml'
4+
import { readFile } from './filesystem'
5+
6+
export const readConfig = (config: Config, userConfigPath: string): Config => {
7+
const content: string = readFile(config, userConfigPath);
8+
9+
if (content === "") {
10+
return <Config>deepmerge(defaultConfig, config);
11+
}
12+
13+
const userConfig = <Config>yaml.load(content);
14+
15+
return <Config>deepmerge(defaultConfig, userConfig, config);
16+
};

src/utils/filesystem.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ export const writeFile = (
3737
fs.writeFileSync(filePath(config, filename), content);
3838
};
3939

40-
export const readConfig = (config: Config, userConfigPath: string): Config => {
41-
const content: string = readFile(config, userConfigPath);
42-
43-
if (content === "") {
44-
return <Config>deepmerge(defaultConfig, config);
45-
}
46-
47-
const userConfig = <Config>yaml.load(content);
48-
49-
return <Config>deepmerge(defaultConfig, userConfig, config);
50-
};
51-
5240
export const exec = async (command: string): Promise<string> => {
5341
const execAsync = promisify(nodeExec);
5442

tests/unit/filesystem.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { rawTestConfig } from "../helpers/config";
22
import { type Config, defaultConfig } from "../../src/types/config";
3-
import { readConfig } from "../../src/utils/filesystem";
43
import { CONFIG_PATH } from "../../src/utils/inputs";
4+
import { readConfig } from '../../src/utils/config'
55

66
test("read config", () => {
77
const data: Config = readConfig(rawTestConfig, CONFIG_PATH.defaultValue);

0 commit comments

Comments
 (0)