Skip to content

Commit

Permalink
Extract config loader into module (#335)
Browse files Browse the repository at this point in the history
* 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
ntotten and dan-lee authored Nov 17, 2024
1 parent e3fe94f commit c44f4e9
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 92 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: nx release version --specifier 0.0.0-${{ steps.vars.outputs.sha_short }} --git-tag=false

- name: Publish Modules
run: pnpm publish --provenance --filter zudoku --filter create-zudoku-app --tag canary --no-git-checks
run: pnpm publish --provenance --filter zudoku --filter create-zudoku-app --filter config --tag canary --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

Expand All @@ -63,5 +63,3 @@ jobs:
- name: Has Stats
run: echo "exists=${{ hashFiles('./packages/zudoku/stats.html') != '' }}" >> $GITHUB_OUTPUT
id: stats-file


2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
run: nx release version --specifier 0.0.0-${{ steps.vars.outputs.sha_short }} --git-tag=false

- name: Publish Modules
run: pnpm publish --provenance --filter zudoku --filter create-zudoku-app --no-git-checks --tag ${{ github.event_name == 'push' && 'dev' || 'latest' }}
run: pnpm publish --provenance --filter zudoku --filter create-zudoku-app --filter config --no-git-checks --tag ${{ github.event_name == 'push' && 'dev' || 'latest' }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ nx login

## Build

To build all projects run:
To build the project run:

```
nx run-many -t build
nx run zudoku:build
```

## Dev
Expand Down
6 changes: 3 additions & 3 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"parallel": 5,
"targetDefaults": {
"dev": {
"dependsOn": ["zudoku:build"]
"dependsOn": ["^build"]
},
"build": {
"dependsOn": ["zudoku:build"],
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/dist"],
"cache": false
},
Expand All @@ -28,7 +28,7 @@
}
],
"release": {
"projects": ["zudoku", "create-zudoku-app"],
"projects": ["zudoku", "create-zudoku-app", "config"],
"releaseTagPattern": "v{version}",
"versionPlans": true,
"version": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "nx run-many -t=build -p zudoku config",
"mismatches": "syncpack list-mismatches",
"lint": "eslint --cache --fix .",
"lint:ci": "eslint .",
Expand Down
5 changes: 5 additions & 0 deletions packages/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/dist
/lib
/standalone

stats.html
35 changes: 35 additions & 0 deletions packages/config/package.json
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"
}
}
18 changes: 18 additions & 0 deletions packages/config/project.json
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"]
}
}
}
74 changes: 74 additions & 0 deletions packages/config/src/config.ts
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;
}
5 changes: 5 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
ConfigWithMeta,
loadZudokuConfig,
zudokuConfigFiles,
} from "./config.js";
16 changes: 16 additions & 0 deletions packages/config/tsconfig.json
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"]
}
2 changes: 1 addition & 1 deletion packages/zudoku/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
"@types/react": "18.3.11",
"@types/react-dom": "18.3.1",
"@vitejs/plugin-react": "4.3.1",
"@zudoku/config": "workspace:*",
"@zudoku/httpsnippet": "10.0.9",
"@zudoku/react-helmet-async": "2.0.4",
"autoprefixer": "10.4.20",
Expand Down Expand Up @@ -219,7 +220,6 @@
"strip-ansi": "7.1.0",
"tailwind-merge": "2.5.4",
"tailwindcss": "3.4.13",
"tsx": "4.19.1",
"ulidx": "2.4.1",
"unist-util-visit": "5.0.0",
"urql": "4.1.0",
Expand Down
16 changes: 13 additions & 3 deletions packages/zudoku/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,45 @@
}
},
"build": {
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/dist"],
"inputs": [
"{projectRoot}/**/*.ts",
"{projectRoot}/**/*.tsx",
"!{projectRoot}/**/*.css"
"!{projectRoot}/**/*.css",
"{projectRoot}/tsconfig.json",
"{projectRoot}/package.json"
],
"cache": true
},
"build:vite": {
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/lib", "{projectRoot}/stats.html"],
"inputs": [
"{projectRoot}/vite.config.ts",
"{projectRoot}/**/*.ts",
"{projectRoot}/**/*.tsx",
"!{projectRoot}/**/*.css"
"!{projectRoot}/**/*.css",
"{projectRoot}/tsconfig.json",
"{projectRoot}/package.json"
],
"cache": true
},
"build:standalone:vite": {
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/standalone"],
"inputs": [
"{projectRoot}/vite.standalone.config.ts",
"{projectRoot}/**/*.ts",
"{projectRoot}/**/*.tsx",
"!{projectRoot}/**/*.css"
"!{projectRoot}/**/*.css",
"{projectRoot}/tsconfig.json",
"{projectRoot}/package.json"
],
"cache": true
},
"build:standalone:html": {
"dependsOn": ["^build"],
"dependsOn": ["build:standalone:vite"]
},
"codegen": {}
Expand Down
3 changes: 1 addition & 2 deletions packages/zudoku/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ if (gte(process.versions.node, MIN_NODE_VERSION)) {
if (err instanceof Error) {
Sentry.captureException(err);
}
await printCriticalFailureToConsoleAndExit(err.message ?? err);
cli.showHelp();
throw err;
} finally {
await shutdownAnalytics();
}
Expand Down
Loading

0 comments on commit c44f4e9

Please sign in to comment.