Skip to content

Commit 214fc4d

Browse files
committed
chore: wip
1 parent b3c47f2 commit 214fc4d

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

bin/cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ cli
8484
if (options?.verbose) {
8585
log.debug(`Running staged lint for hook: ${hook}`)
8686
log.debug(`Working directory: ${process.cwd()}`)
87+
if (options?.autoRestage !== undefined) {
88+
log.debug(`Auto-restage: ${options.autoRestage}`)
89+
}
8790
}
8891

89-
const success = await runStagedLint(hook, config, process.cwd(), options?.verbose)
92+
const success = await runStagedLint(hook, config, process.cwd(), options?.verbose, options?.autoRestage)
9093

9194
if (success) {
9295
log.success('Staged lint completed successfully')

src/staged-lint.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ export class StagedLintProcessor {
3939
// Process each pattern in the config
4040
for (const [pattern, commands] of Object.entries(config)) {
4141
const matchingFiles = this.getMatchingFiles(stagedFiles, pattern)
42-
if (matchingFiles.length === 0)
42+
if (matchingFiles.length === 0) {
43+
this.log(`No files match pattern "${pattern}" - skipping`)
4344
continue
45+
}
4446

45-
this.log(`Processing pattern "${pattern}" for ${matchingFiles.length} files`)
47+
this.log(`Pattern "${pattern}" matched ${matchingFiles.length} file(s)`)
48+
if (this.verbose) {
49+
this.log(`Matched files: ${matchingFiles.join(', ')}`)
50+
}
4651

4752
const commandArray = Array.isArray(commands) ? commands : [commands]
4853

@@ -207,7 +212,11 @@ export class StagedLintProcessor {
207212
? command.replace('{files}', files.join(' '))
208213
: `${command} ${files.join(' ')}`
209214

210-
this.log(`Running: ${finalCommand}`)
215+
this.log(`Running command on ${files.length} file(s): ${command}`)
216+
if (this.verbose) {
217+
this.log(`Files: ${files.join(', ')}`)
218+
this.log(`Full command: ${finalCommand}`)
219+
}
211220

212221
const result = execSync(finalCommand, {
213222
cwd: this.projectRoot,
@@ -219,6 +228,7 @@ export class StagedLintProcessor {
219228
console.warn(result)
220229
}
221230

231+
this.log(`Command completed successfully for ${files.length} file(s)`)
222232
return true
223233
}
224234
catch (error: any) {
@@ -228,6 +238,7 @@ export class StagedLintProcessor {
228238
if (error.stderr)
229239
console.error('[ERROR] Command stderr:', error.stderr)
230240
console.error(`[ERROR] Command failed: ${command}`)
241+
console.error(`[ERROR] Failed on files: ${files.join(', ')}`)
231242
return false
232243
}
233244
}
@@ -361,12 +372,16 @@ export async function runStagedLint(
361372
config: GitHooksConfig,
362373
projectRoot: string,
363374
verbose: boolean = false,
375+
autoRestage?: boolean,
364376
): Promise<boolean> {
365377
if (!config) {
366378
console.error(`[ERROR] No configuration found`)
367379
return false
368380
}
369381

382+
// Determine autoRestage setting: CLI option > hook config > global config > default true
383+
let shouldAutoRestage = autoRestage !== undefined ? autoRestage : true
384+
370385
// Try both the original hook name and its mapped version
371386
const hookVariants = [hook, HOOK_NAME_MAP[hook]].filter(Boolean)
372387

@@ -377,8 +392,15 @@ export async function runStagedLint(
377392
if (typeof hookConfig === 'object' && !Array.isArray(hookConfig)) {
378393
const stagedLintConfig = (hookConfig as { 'stagedLint'?: StagedLintConfig, 'staged-lint'?: StagedLintConfig }).stagedLint
379394
|| (hookConfig as { 'stagedLint'?: StagedLintConfig, 'staged-lint'?: StagedLintConfig })['staged-lint']
395+
396+
// Check for hook-specific autoRestage setting
397+
const hookAutoRestage = (hookConfig as { autoRestage?: boolean }).autoRestage
398+
if (autoRestage === undefined && hookAutoRestage !== undefined) {
399+
shouldAutoRestage = hookAutoRestage
400+
}
401+
380402
if (stagedLintConfig) {
381-
const processor = new StagedLintProcessor(projectRoot, verbose, true)
403+
const processor = new StagedLintProcessor(projectRoot, verbose, shouldAutoRestage)
382404
return processor.process(stagedLintConfig)
383405
}
384406
}
@@ -387,7 +409,12 @@ export async function runStagedLint(
387409

388410
// If no hook-specific configuration, check for global stagedLint
389411
if (config.stagedLint || config['staged-lint']) {
390-
const processor = new StagedLintProcessor(projectRoot, verbose, true)
412+
// Use global autoRestage if no CLI override
413+
if (autoRestage === undefined && config.autoRestage !== undefined) {
414+
shouldAutoRestage = config.autoRestage
415+
}
416+
417+
const processor = new StagedLintProcessor(projectRoot, verbose, shouldAutoRestage)
391418
return processor.process(config.stagedLint || config['staged-lint']!)
392419
}
393420

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ export type GitHooksConfig = {
2323
[K in typeof VALID_GIT_HOOKS[number] | CamelCaseHooks]?: string | {
2424
'stagedLint'?: StagedLintConfig
2525
'staged-lint'?: StagedLintConfig // Legacy support
26+
'autoRestage'?: boolean // Allow per-hook autoRestage setting
2627
}
2728
} & {
2829
'preserveUnused'?: boolean | (typeof VALID_GIT_HOOKS[number] | CamelCaseHooks)[]
2930
'verbose'?: boolean
3031
'stagedLint'?: StagedLintConfig
3132
'staged-lint'?: StagedLintConfig // Legacy support
32-
'autoRestage'?: boolean
33+
'autoRestage'?: boolean // Global autoRestage setting
3334
}
3435

3536
export interface SetHooksFromConfigOptions {

0 commit comments

Comments
 (0)