Skip to content

Commit 28d08d4

Browse files
committed
feat: improve error handling for missing configuration file
1 parent 5da7734 commit 28d08d4

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

cli.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
processUpgradeCommand,
1010
} from './src/commands.js'
1111
import { getDirectories } from './src/utilities.js'
12-
import { readFileSync } from 'fs'
12+
import { readFileSync, existsSync } from 'fs'
1313
import path from 'path'
1414
import { fileURLToPath } from 'url'
1515

@@ -34,13 +34,25 @@ const configDefaults = {
3434
*/
3535
function loadConfig() {
3636
const filePath = path.resolve(process.cwd(), '.create-frontend-component', 'config.json')
37-
const configFromFile = JSON.parse(
38-
readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
39-
)
4037

41-
return {
42-
...configDefaults,
43-
...configFromFile
38+
try {
39+
if (!existsSync(filePath)) {
40+
console.error(`Error: Configuration file not found at ${filePath}.`)
41+
console.error('Run "npx create-frontend-component init" to generate the configuration file.')
42+
process.exit(1)
43+
}
44+
45+
const fileContent = readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
46+
const configFromFile = JSON.parse(fileContent)
47+
48+
return {
49+
...configDefaults,
50+
...configFromFile
51+
}
52+
} catch (error) {
53+
console.error(`Error loading configuration file: ${error.message}`)
54+
console.error('Try running "npx create-frontend-component init" to reset the configuration.')
55+
process.exit(1)
4456
}
4557
}
4658

0 commit comments

Comments
 (0)