|
6 | 6 |
|
7 | 7 | import fs from 'fs-extra'; |
8 | 8 | import type { IParserConfig } from 'langium'; |
| 9 | +import { EOL } from 'os'; |
9 | 10 | import * as path from 'path'; |
10 | 11 | import type { GenerateOptions } from './generate.js'; |
11 | 12 | import { log } from './generator/util.js'; |
@@ -92,15 +93,24 @@ export async function loadConfig(options: GenerateOptions): Promise<LangiumConfi |
92 | 93 | filePath = path.normalize(defaultFile); |
93 | 94 | } |
94 | 95 | const relativePath = path.dirname(filePath); |
| 96 | + if (!path.isAbsolute(filePath)) { |
| 97 | + filePath = path.resolve(process.cwd(), filePath); |
| 98 | + } |
95 | 99 | log('log', options, `Reading config from ${chalk.white.bold(filePath)}`); |
96 | 100 | try { |
97 | 101 | const obj = await fs.readJson(filePath, { encoding: 'utf-8' }); |
98 | | - const config: LangiumConfig = path.basename(filePath) === 'package.json' ? obj.langium : obj; |
| 102 | + const config: LangiumConfig | undefined = path.basename(filePath) === 'package.json' ? obj.langium : obj; |
| 103 | + if (!config) { |
| 104 | + throw new Error('Langium config is missing.'); |
| 105 | + } |
99 | 106 | config[RelativePath] = relativePath; |
100 | 107 | config.importExtension ??= '.js'; |
101 | 108 | return config; |
102 | 109 | } catch (err) { |
103 | | - log('error', options, chalk.red('Failed to read config file.'), err); |
| 110 | + const suffix = options.file |
| 111 | + ? path.basename(filePath) === 'package.json' ? `an object named 'langium' in ${filePath}` : filePath |
| 112 | + : `${path.resolve(path.dirname(filePath), 'langium-config.json')} or an object named 'langium' in ${path.resolve(path.dirname(filePath), 'package.json')}`; |
| 113 | + log('error', options, chalk.red(`Failed to read Langium config: Could not find ${suffix}.${EOL}`), err); |
104 | 114 | process.exit(1); |
105 | 115 | } |
106 | 116 | } |
0 commit comments