33 */
44
55import { existsSync } from "node:fs" ;
6- import { dirname , resolve } from "node:path" ;
6+ import { dirname , join , resolve } from "node:path" ;
77import { fileURLToPath } from "node:url" ;
88import { DEFAULT_HTTP_PORT , SERVER_NAME , SERVER_VERSION } from "./constants.js" ;
99
@@ -16,24 +16,47 @@ export interface ServerConfig {
1616}
1717
1818/**
19- * Find project root by looking for pnpm-workspace.yaml
19+ * Find specs directory by checking multiple locations:
20+ * 1. Environment variable CITYJSON_SPECS_PATH
21+ * 2. Relative to package (for npm install): <package>/specs
22+ * 3. Relative to workspace root (for development): <workspace>/specs
2023 */
21- function findProjectRoot ( startDir : string ) : string {
22- let current = startDir ;
24+ function findSpecsPath ( packageDir : string ) : string {
25+ // Check environment variable first
26+ if ( process . env . CITYJSON_SPECS_PATH ) {
27+ return process . env . CITYJSON_SPECS_PATH ;
28+ }
29+
30+ // Check relative to package (npm install case)
31+ // dist/index.js -> package root -> specs
32+ const packageRoot = dirname ( packageDir ) ;
33+ const packageSpecsPath = join ( packageRoot , "specs" ) ;
34+ if ( existsSync ( packageSpecsPath ) ) {
35+ return packageSpecsPath ;
36+ }
37+
38+ // Check relative to workspace root (development case)
39+ let current = packageDir ;
2340 while ( current !== "/" && ! existsSync ( resolve ( current , "pnpm-workspace.yaml" ) ) ) {
2441 current = dirname ( current ) ;
2542 }
26- return current ;
43+ if ( current !== "/" ) {
44+ const workspaceSpecsPath = join ( current , "specs" ) ;
45+ if ( existsSync ( workspaceSpecsPath ) ) {
46+ return workspaceSpecsPath ;
47+ }
48+ }
49+
50+ // Fallback to package relative path
51+ return packageSpecsPath ;
2752}
2853
2954/**
3055 * Load configuration from environment variables
3156 */
3257export function loadConfig ( ) : ServerConfig {
3358 const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
34- const projectRoot = findProjectRoot ( __dirname ) ;
35-
36- const specsPath = process . env . CITYJSON_SPECS_PATH || resolve ( projectRoot , "specs" ) ;
59+ const specsPath = findSpecsPath ( __dirname ) ;
3760 const transport = ( process . env . TRANSPORT as "stdio" | "http" ) || "stdio" ;
3861 const httpPort = process . env . PORT ? Number . parseInt ( process . env . PORT , 10 ) : DEFAULT_HTTP_PORT ;
3962
0 commit comments