|
1 |
| -import { existsSync, readFileSync } from "fs"; |
2 |
| -import { join } from "path"; |
3 |
| -import { configurationOptions } from "./lib/options"; |
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { configurationOptions } from "./lib/types"; |
4 | 4 |
|
5 |
| -const CONFIG_FILE_NAME = ".richiejs"; |
| 5 | +export default function loadConfig(): configurationOptions { |
| 6 | + const CONFIG_FILE_NAME = "richie.config.js"; |
6 | 7 |
|
7 |
| -const projectConfigFile = join(process.cwd(), CONFIG_FILE_NAME); |
8 |
| -const projectHasConfig = existsSync(projectConfigFile); |
| 8 | + const projectConfigFile = join(process.cwd(), CONFIG_FILE_NAME); |
| 9 | + const projectHasConfig = existsSync(projectConfigFile); |
9 | 10 |
|
10 |
| -let projectConfig: configurationOptions = {} as configurationOptions; |
11 |
| -let defaultConfig: configurationOptions = {} as configurationOptions; |
| 11 | + let projectConfig: configurationOptions = {} as configurationOptions; |
| 12 | + let defaultConfig: configurationOptions = {} as configurationOptions; |
12 | 13 |
|
13 |
| -if (projectHasConfig) { |
14 |
| - //load project config |
15 |
| - try { |
16 |
| - projectConfig = JSON.parse( |
17 |
| - readFileSync(projectConfigFile, { encoding: "utf8" }), |
18 |
| - ); |
19 |
| - } catch (err) { |
20 |
| - if (err instanceof SyntaxError) { |
21 |
| - console.log( |
22 |
| - "Error: Check configuration file if there any syntax mistake", |
23 |
| - ); |
24 |
| - } else { |
25 |
| - console.log("Unexpected Error while loading settings"); |
| 14 | + if (projectHasConfig) { |
| 15 | + //load project config |
| 16 | + try { |
| 17 | + projectConfig = require(projectConfigFile).default; |
| 18 | + } catch (err) { |
| 19 | + console.log("Error while loading settings\n", err); |
| 20 | + process.exit(1); |
26 | 21 | }
|
27 |
| - process.exit(1); |
28 | 22 | }
|
29 |
| -} |
30 |
| -//load default configuration |
31 |
| -defaultConfig = JSON.parse( |
32 |
| - readFileSync(join(__dirname, CONFIG_FILE_NAME), { encoding: "utf8" }), |
33 |
| -); |
34 | 23 |
|
35 |
| -const configurations: configurationOptions = { |
36 |
| - ...defaultConfig, |
37 |
| - ...projectConfig, |
38 |
| -}; |
| 24 | + //load default configuration |
| 25 | + defaultConfig = require(join(__dirname, CONFIG_FILE_NAME)).default; |
| 26 | + |
| 27 | + const configurations: configurationOptions = { |
| 28 | + ...defaultConfig, |
| 29 | + ...projectConfig, |
| 30 | + }; |
39 | 31 |
|
40 |
| -export default configurations; |
| 32 | + return configurations; |
| 33 | +} |
0 commit comments