Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/cli-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export async function suggestPrepareHook(cwd: string = process.cwd()): Promise<b
if (existing?.includes('skilld'))
return true

const prepareCmd = buildPrepareScript(existing)
const prepareCmd = buildPrepareScript(existing, cwd)

if (!isInteractive()) {
p.log.info(
Expand Down Expand Up @@ -505,8 +505,9 @@ export async function suggestPrepareHook(cwd: string = process.cwd()): Promise<b
/**
* Build the full prepare script value, safely appending to any existing command.
*/
export function buildPrepareScript(existing: string | undefined): string {
const cmd = 'skilld prepare || true'
export function buildPrepareScript(existing: string | undefined, cwd: string = process.cwd()): string {
const bin = isNpxExecution() && !isSkilldDep(cwd) ? 'npx skilld' : 'skilld'
const cmd = `${bin} prepare || true`
if (!existing || !existing.trim())
return cmd

Expand All @@ -520,6 +521,29 @@ export function buildPrepareScript(existing: string | undefined): string {
return `${cleaned} && (${cmd})`
}

/**
* Detect if the current process was launched via npx, pnpm dlx, or similar one-shot runners.
*/
function isNpxExecution(): boolean {
// npm/pnpm set npm_command=exec when running via npx/dlx
if (process.env.npm_command === 'exec')
return true
// Fallback: check if the resolved binary path contains npx or dlx cache dirs
const execPath = process.env._ || ''
return /npx|\.store|dlx/.test(execPath)
}

/**
* Check if skilld is listed as a dependency (dev or regular) in the project's package.json.
*/
function isSkilldDep(cwd: string): boolean {
const pkg = readPackageJsonSafe(join(cwd, 'package.json'))
if (!pkg)
return false
const deps = pkg.parsed as Record<string, any>
return !!(deps.dependencies?.skilld || deps.devDependencies?.skilld)
}

export function getRepoHint(name: string, cwd: string): string | undefined {
const result = readPackageJsonSafe(join(cwd, 'node_modules', name, 'package.json'))
if (!result)
Expand Down
68 changes: 54 additions & 14 deletions test/unit/prepare-hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,84 @@
import { describe, expect, it } from 'vitest'
import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { buildPrepareScript } from '../../src/cli-helpers.ts'
import { editJsonProperty } from '../../src/core/package-json.ts'

function makeTempCwd(hasSkilld: boolean): string {
const dir = mkdtempSync(join(tmpdir(), 'prepare-hook-'))
const pkg: Record<string, any> = { name: 'test-pkg' }
if (hasSkilld)
pkg.devDependencies = { skilld: '^1.0.0' }
writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg))
return dir
}
Comment on lines +1 to +15

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify fs mocking requirement vs direct fs usage in this unit test
rg -n "vi\\.mock\\('node:fs'\\)|mkdtempSync|writeFileSync" test/unit/prepare-hook.test.ts

Repository: harlan-zw/skilld

Length of output: 242


🏁 Script executed:

cat -n test/unit/prepare-hook.test.ts | head -50

Repository: harlan-zw/skilld

Length of output: 2511


🏁 Script executed:

fd --glob "test/unit/*.test.ts" --type f | head -5

Repository: harlan-zw/skilld

Length of output: 42


🏁 Script executed:

rg -l "vi\.mock\('node:fs'\)" test/unit/ --type ts

Repository: harlan-zw/skilld

Length of output: 42


🏁 Script executed:

fd --type f test/unit/

Repository: harlan-zw/skilld

Length of output: 405


🏁 Script executed:

fd --type f test/e2e/

Repository: harlan-zw/skilld

Length of output: 401


🏁 Script executed:

rg "vi\.mock" test/ --type ts -l

Repository: harlan-zw/skilld

Length of output: 536


🏁 Script executed:

find . -name "prepare-hook.test.ts" -type f

Repository: harlan-zw/skilld

Length of output: 92


🏁 Script executed:

ls -la test/unit/ | head -20

Repository: harlan-zw/skilld

Length of output: 1382


🏁 Script executed:

head -20 test/unit/cache.test.ts

Repository: harlan-zw/skilld

Length of output: 604


Mock the node:fs module in this test file.

This unit test imports and directly uses mkdtempSync and writeFileSync without mocking them. Other unit tests in the same directory (e.g., test/unit/cache.test.ts) properly mock node:fs using vi.mock('node:fs') at the file top. Apply the same pattern here to isolate the test from the filesystem.

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/unit/prepare-hook.test.ts` around lines 1 - 15, Add a module mock for
node:fs at the top of this test file so filesystem calls are isolated: call
vi.mock('node:fs') before any imports that call mkdtempSync or writeFileSync;
update or provide mock implementations for mkdtempSync and writeFileSync used by
makeTempCwd (or rely on Vitest auto-mocking and set mocked return values where
needed) so tests no longer touch the real filesystem and makeTempCwd uses the
mocked functions.


function simulateNpx() {
process.env.npm_command = 'exec'
}

function clearNpxEnv() {
delete process.env.npm_command
}

describe('prepare hook script building', () => {
const buildPrepare = buildPrepareScript
const cwdWithSkilld = makeTempCwd(true)
const cwdWithout = makeTempCwd(false)
const standalone = 'skilld prepare || true'
const npxStandalone = 'npx skilld prepare || true'

afterEach(() => clearNpxEnv())

it('returns standalone when no existing script', () => {
expect(buildPrepare(undefined)).toBe(standalone)
it('uses skilld when installed as dependency (even via npx)', () => {
simulateNpx()
expect(buildPrepareScript(undefined, cwdWithSkilld)).toBe(standalone)
})

it('uses npx skilld when run via npx and not a dependency', () => {
simulateNpx()
expect(buildPrepareScript(undefined, cwdWithout)).toBe(npxStandalone)
})

it('uses skilld when installed globally (not npx, not a dep)', () => {
clearNpxEnv()
expect(buildPrepareScript(undefined, cwdWithout)).toBe(standalone)
})

it('returns standalone when existing script is empty', () => {
expect(buildPrepare('')).toBe(standalone)
expect(buildPrepare(' ')).toBe(standalone)
expect(buildPrepareScript('', cwdWithSkilld)).toBe(standalone)
expect(buildPrepareScript(' ', cwdWithSkilld)).toBe(standalone)
})

it('appends with && and parens to existing script', () => {
expect(buildPrepare('husky')).toBe('husky && (skilld prepare || true)')
expect(buildPrepareScript('husky', cwdWithSkilld)).toBe('husky && (skilld prepare || true)')
})

it('handles existing script with multiple commands', () => {
expect(buildPrepare('husky && lint-staged')).toBe('husky && lint-staged && (skilld prepare || true)')
expect(buildPrepareScript('husky && lint-staged', cwdWithSkilld)).toBe('husky && lint-staged && (skilld prepare || true)')
})

it('strips trailing && from existing script', () => {
expect(buildPrepare('husky &&')).toBe('husky && (skilld prepare || true)')
expect(buildPrepare('husky && ')).toBe('husky && (skilld prepare || true)')
expect(buildPrepareScript('husky &&', cwdWithSkilld)).toBe('husky && (skilld prepare || true)')
expect(buildPrepareScript('husky && ', cwdWithSkilld)).toBe('husky && (skilld prepare || true)')
})

it('strips trailing ; from existing script', () => {
expect(buildPrepare('husky;')).toBe('husky && (skilld prepare || true)')
expect(buildPrepareScript('husky;', cwdWithSkilld)).toBe('husky && (skilld prepare || true)')
})

it('strips trailing || from existing script', () => {
expect(buildPrepare('husky ||')).toBe('husky && (skilld prepare || true)')
expect(buildPrepareScript('husky ||', cwdWithSkilld)).toBe('husky && (skilld prepare || true)')
})

it('handles only operators as existing script', () => {
expect(buildPrepare('&&')).toBe(standalone)
expect(buildPrepare(';')).toBe(standalone)
expect(buildPrepareScript('&&', cwdWithSkilld)).toBe(standalone)
expect(buildPrepareScript(';', cwdWithSkilld)).toBe(standalone)
})

it('appends npx variant to existing script when npx + not a dep', () => {
simulateNpx()
expect(buildPrepareScript('husky', cwdWithout)).toBe('husky && (npx skilld prepare || true)')
})

describe('surgical package.json editing', () => {
Expand Down
Loading