| 129 | |
| 130 | // initialize config file |
| 131 | function initConfig(): boolean { |
| 132 | const configPath = '.htmlhintrc' |
| 133 | const defaultConfig = JSON.stringify(HTMLHint.defaultRuleset, null, 2) |
| 134 | |
| 135 | try { |
| 136 | // Use 'wx' flag to create file only if it doesn't exist (atomic operation) |
| 137 | writeFileSync(configPath, defaultConfig, { encoding: 'utf-8', flag: 'wx' }) |
| 138 | console.log(chalk.green('Created configuration file: %s'), configPath) |
| 139 | console.log('') |
| 140 | console.log('Configuration file contents:') |
| 141 | console.log(chalk.gray(defaultConfig)) |
| 142 | return true |
| 143 | } catch (error) { |
| 144 | // Check if the error is because the file already exists |
| 145 | if (error instanceof Error && 'code' in error && error.code === 'EEXIST') { |
| 146 | console.log( |
| 147 | chalk.yellow('Configuration file already exists: %s'), |
| 148 | configPath |
| 149 | ) |
| 150 | return true // File exists is a successful state - no error |
| 151 | } |
| 152 | |
| 153 | console.log( |
| 154 | chalk.red('Failed to create configuration file: %s'), |
| 155 | error instanceof Error ? error.message : String(error) |
| 156 | ) |
| 157 | return false |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | function hintTargets( |
| 162 | arrTargets: string[], |