-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract config loader into module (#335)
* Extract config loader into module * Added additional dependsOn * Set global config * Don't swallow stack traces in errors, just let Node handle it * Use correct config path * Use current config path while `resolveId` is called. Path might change when renaming config file --------- Co-authored-by: Daniel Lehr <[email protected]>
- Loading branch information
Showing
18 changed files
with
217 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/dist | ||
/lib | ||
/standalone | ||
|
||
stats.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@zudoku/config", | ||
"type": "module", | ||
"version": "0.14.1", | ||
"keywords": [ | ||
"zudoku" | ||
], | ||
"description": "Zudoku configuration loader", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/zuplo/zudoku", | ||
"directory": "packages/config" | ||
}, | ||
"author": "Zuplo <[email protected]>", | ||
"license": "MIT", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"engines": { | ||
"node": ">=20" | ||
}, | ||
"dependencies": { | ||
"tsx": "4.19.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.16.11", | ||
"typescript": "5.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "config", | ||
"$schema": "../../node_modules/nx/schemas/nx-schema.json", | ||
"targets": { | ||
"build": { | ||
"inputs": [ | ||
"{projectRoot}/**/*.ts", | ||
"{projectRoot}/tsconfig.json", | ||
"{projectRoot}/package.json" | ||
], | ||
"outputs": ["{projectRoot}/dist"], | ||
"cache": true | ||
}, | ||
"build:ci": { | ||
"dependsOn": ["build"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { stat } from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { fileURLToPath, pathToFileURL } from "node:url"; | ||
import { tsImport } from "tsx/esm/api"; | ||
|
||
export const zudokuConfigFiles = [ | ||
"zudoku.config.js", | ||
"zudoku.config.jsx", | ||
"zudoku.config.ts", | ||
"zudoku.config.tsx", | ||
"zudoku.config.mjs", | ||
]; | ||
|
||
const fileExists = (path: string) => | ||
stat(path) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
let configPath: string | undefined; | ||
|
||
async function getConfigFilePath(rootDir: string): Promise<string> { | ||
// Also check if file exists, so renaming the file will trigger a restart as well | ||
if (configPath && (await fileExists(configPath))) { | ||
return configPath; | ||
} | ||
|
||
for (const fileName of zudokuConfigFiles) { | ||
const filepath = path.join(rootDir, fileName); | ||
|
||
if (await fileExists(filepath)) { | ||
configPath = filepath; | ||
return filepath; | ||
} | ||
} | ||
configPath = undefined; | ||
throw new Error(`No zudoku config file found in project root.`); | ||
} | ||
|
||
export type ConfigWithMeta<TConfig> = TConfig & { | ||
__meta: { dependencies: string[]; path: string }; | ||
}; | ||
|
||
export async function loadZudokuConfig<TConfig>( | ||
rootDir: string, | ||
): Promise<ConfigWithMeta<TConfig>> { | ||
const filepath = await getConfigFilePath(rootDir); | ||
|
||
const configFilePath = pathToFileURL(filepath).href; | ||
|
||
const dependencies: string[] = []; | ||
const loadedConfig = await tsImport(configFilePath, { | ||
parentURL: import.meta.url, | ||
onImport: (file: string) => { | ||
const path = fileURLToPath( | ||
file.startsWith("file://") ? file : pathToFileURL(file).href, | ||
); | ||
|
||
if (path.startsWith(rootDir)) { | ||
dependencies.push(path); | ||
} | ||
}, | ||
}).then((m) => m.default as TConfig); | ||
|
||
if (!loadedConfig) { | ||
throw new Error(`Failed to load config file: ${filepath}`); | ||
} | ||
|
||
const config: ConfigWithMeta<TConfig> = { | ||
...loadedConfig, | ||
__meta: { dependencies, path: filepath }, | ||
}; | ||
|
||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { | ||
ConfigWithMeta, | ||
loadZudokuConfig, | ||
zudokuConfigFiles, | ||
} from "./config.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"lib": ["ESNext"], | ||
"module": "NodeNext", | ||
"moduleResolution": "nodenext", | ||
"outDir": "dist", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"declaration": true | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["node_modules", "dist"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.