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
5 changes: 5 additions & 0 deletions .changeset/init-blank-husky-choice-1758.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"secureflow-cli": minor
---

feat(cli): [NSoC_2026] allow choosing between Blank and Husky in secureflow --init (#1758)
37 changes: 37 additions & 0 deletions cli/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ vi.mock("./lib/output-args.js", () => ({
vi.mock("./lib/audio-player.js", () => ({ playVoiceDebrief: m.playVoiceDebrief }));
vi.mock("./mcp/index.js", () => ({ startMcpServer: m.startMcpServer }));

vi.mock("./lib/tui.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./lib/tui.js")>();
return {
...actual,
initSecureFlow: vi.fn(actual.initSecureFlow),
};
});

const violation = { line: 3, text: "console.log(secret)", reason: "secret" };
const scanResult = (path: string, violations: unknown[] = []) => ({
path,
Expand Down Expand Up @@ -384,3 +392,32 @@ describe("voice debrief", () => {
expect(m.playVoiceDebrief).not.toHaveBeenCalled();
});
});

describe("--init flag & initialization modes (#1758)", () => {
it("initializes secureflow and prints greeting with exit code 0 on success", async () => {
const { initSecureFlow } = await import("./lib/tui.js");
vi.mocked(initSecureFlow).mockResolvedValueOnce({
success: true,
message: "Successfully added npx secureflow to package.json prepare script",
hookPath: "package.json",
});

const code = await runCli("--init", "blank");
expect(code).toBe(0);
expect(initSecureFlow).toHaveBeenCalled();
expect(output(logSpy)).toContain("[SecureFlow Init]");
});

it("exits with code 1 when initialization fails", async () => {
const { initSecureFlow } = await import("./lib/tui.js");
vi.mocked(initSecureFlow).mockResolvedValueOnce({
success: false,
message: "Failed to write package.json",
hookPath: "",
});

const code = await runCli("--init", "--blank");
expect(code).toBe(1);
expect(output(errorSpy)).toContain("Failed to write package.json");
});
});
14 changes: 11 additions & 3 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
import {
TerminalSpinner,
formatTuiScanSummary,
initHuskyPreCommitHook,

Check warning on line 40 in cli/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint, Typecheck, & Build (22)

'initHuskyPreCommitHook' is defined but never used
initBlankPrepareScript,

Check warning on line 41 in cli/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint, Typecheck, & Build (22)

'initBlankPrepareScript' is defined but never used
initSecureFlow,
formatInitGreeting,
colors,

Check warning on line 44 in cli/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint, Typecheck, & Build (22)

'colors' is defined but never used
} from "./lib/tui.js";

const VERBOSE = process.argv.includes("--verbose");
Expand Down Expand Up @@ -89,7 +91,9 @@
explain --web3 Generate AI security analysis and remediation for Web3 / Smart Contract findings

Options:
--init Initialize local .husky/pre-commit hook configured with SecureFlow
--init Initialize SecureFlow (choose between Blank and Husky)
--husky Initialize using Husky pre-commit hook (.husky/pre-commit)
--blank Initialize using Blank mode (package.json prepare script)
--verbose Enable verbose logging
--format <format> Output format: text, json, sarif, csv, html, markdown (default: text)
-o, --output <path> Write output to specified file path
Expand Down Expand Up @@ -195,8 +199,12 @@
}

async function main(): Promise<number> {
if (process.argv.includes("--init")) {
const result = initHuskyPreCommitHook();
if (
process.argv.some(
(arg) => arg === "--init" || arg.startsWith("--init=") || arg === "--husky" || arg === "--blank",
)
) {
const result = await initSecureFlow();
const greeting = formatInitGreeting(result);
if (result.success) {
console.log(greeting);
Expand Down
204 changes: 204 additions & 0 deletions cli/src/lib/tui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
renderWarningTable,
formatTuiScanSummary,
initHuskyPreCommitHook,
initBlankPrepareScript,
detectInitMode,
promptInitChoice,
initSecureFlow,
formatInitGreeting,
type TableRow,
} from "./tui.js";
Expand Down Expand Up @@ -344,4 +348,204 @@ describe("cli/src/lib/tui (#1712)", () => {
expect(greeting).toContain("https://custom-repo.example.com/docs");
});
});
describe("initBlankPrepareScript (#1758)", () => {
let tempDir: string;

beforeEach(() => {
execSyncMock.mockReset();
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "secureflow-blank-test-"));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it("runs npm init --y when package.json does not exist and adds prepare script", () => {
const commandsExecuted: string[] = [];
const mockExec = vi.fn((cmd: string) => {
commandsExecuted.push(cmd);
if (cmd === "npm init --y") {
fs.writeFileSync(
path.join(tempDir, "package.json"),
JSON.stringify({ name: "new-pkg", version: "1.0.0" }, null, 2),
);
}
});

const res = initBlankPrepareScript(tempDir, mockExec);

expect(res.success).toBe(true);
expect(mockExec).toHaveBeenCalledWith(
"npm init --y",
expect.objectContaining({ cwd: tempDir }),
);
expect(commandsExecuted).toEqual(["npm init --y"]);

const pkg = JSON.parse(fs.readFileSync(path.join(tempDir, "package.json"), "utf-8"));
expect(pkg.scripts.prepare).toBe("npx secureflow");
expect(res.message).toContain("Successfully added npx secureflow");
});

it("skips npm init --y when package.json already exists and adds prepare script", () => {
const pkgPath = path.join(tempDir, "package.json");
fs.writeFileSync(
pkgPath,
JSON.stringify({ name: "existing-pkg", version: "1.0.0", scripts: { test: "vitest" } }, null, 2),
);

const mockExec = vi.fn();
const res = initBlankPrepareScript(tempDir, mockExec);

expect(res.success).toBe(true);
expect(mockExec).not.toHaveBeenCalled();

const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
expect(pkg.scripts.test).toBe("vitest");
expect(pkg.scripts.prepare).toBe("npx secureflow");
});

it("appends npx secureflow with && when a prepare script already exists (e.g. xyz && npx secureflow)", () => {
const pkgPath = path.join(tempDir, "package.json");
fs.writeFileSync(
pkgPath,
JSON.stringify({ name: "existing-pkg", scripts: { prepare: "xyz" } }, null, 2),
);

const res = initBlankPrepareScript(tempDir);

expect(res.success).toBe(true);
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
expect(pkg.scripts.prepare).toBe("xyz && npx secureflow");
expect(res.message).toContain("Appended npx secureflow to existing prepare script");
});

it("recognizes when secureflow is already present in prepare script and avoids duplication", () => {
const pkgPath = path.join(tempDir, "package.json");
fs.writeFileSync(
pkgPath,
JSON.stringify({ name: "existing-pkg", scripts: { prepare: "npm run build && npx secureflow" } }, null, 2),
);

const res = initBlankPrepareScript(tempDir);

expect(res.success).toBe(true);
expect(res.message).toContain("already configured");
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
expect(pkg.scripts.prepare).toBe("npm run build && npx secureflow");
});

it("handles invalid package.json JSON syntax gracefully", () => {
const pkgPath = path.join(tempDir, "package.json");
fs.writeFileSync(pkgPath, "{ invalid json ... ");

const res = initBlankPrepareScript(tempDir);

expect(res.success).toBe(false);
expect(res.message).toContain("Failed to parse package.json");
});
});

describe("detectInitMode & promptInitChoice (#1758)", () => {
it("detects blank mode from command line arguments", () => {
expect(detectInitMode(["node", "cli", "--init", "blank"])).toBe("blank");
expect(detectInitMode(["node", "cli", "--init=blank"])).toBe("blank");
expect(detectInitMode(["node", "cli", "--blank"])).toBe("blank");
expect(detectInitMode(["node", "cli", "-b"])).toBe("blank");
expect(detectInitMode(["node", "cli", "--init", "2"])).toBe("blank");
});

it("detects husky mode from command line arguments", () => {
expect(detectInitMode(["node", "cli", "--init", "husky"])).toBe("husky");
expect(detectInitMode(["node", "cli", "--init=husky"])).toBe("husky");
expect(detectInitMode(["node", "cli", "--husky"])).toBe("husky");
expect(detectInitMode(["node", "cli", "--init", "1"])).toBe("husky");
});

it("returns null when no mode argument is provided with --init", () => {
expect(detectInitMode(["node", "cli", "--init"])).toBeNull();
expect(detectInitMode(["node", "cli"])).toBeNull();
});

it("promptInitChoice resolves to blank when user selects 2 or blank", async () => {
const mockRl = {
question: vi.fn((_prompt: string, cb: (ans: string) => void) => cb("2")),
close: vi.fn(),
on: vi.fn(),
} as any;

const choice = await promptInitChoice(mockRl, process.stdin, { write: vi.fn() } as any);
expect(choice).toBe("blank");
});

it("promptInitChoice resolves to husky when user selects 1, husky, or presses Enter", async () => {
const mockRl1 = {
question: vi.fn((_prompt: string, cb: (ans: string) => void) => cb("husky")),
close: vi.fn(),
on: vi.fn(),
} as any;

const choice1 = await promptInitChoice(mockRl1, process.stdin, { write: vi.fn() } as any);
expect(choice1).toBe("husky");

const mockRl2 = {
question: vi.fn((_prompt: string, cb: (ans: string) => void) => cb("")),
close: vi.fn(),
on: vi.fn(),
} as any;

const choice2 = await promptInitChoice(mockRl2, process.stdin, { write: vi.fn() } as any);
expect(choice2).toBe("husky");
});
});

describe("initSecureFlow orchestrator (#1758)", () => {
let tempDir: string;

beforeEach(() => {
execSyncMock.mockReset();
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "secureflow-init-orch-"));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it("routes to Blank mode when mode is specified as blank", async () => {
const pkgPath = path.join(tempDir, "package.json");
fs.writeFileSync(pkgPath, JSON.stringify({ name: "orch-test", scripts: {} }));

const res = await initSecureFlow({
mode: "blank",
targetDir: tempDir,
});

expect(res.success).toBe(true);
expect(res.hookPath).toBe(pkgPath);
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
expect(pkg.scripts.prepare).toBe("npx secureflow");
});

it("routes to Husky mode when mode is specified as husky", async () => {
const res = await initSecureFlow({
mode: "husky",
targetDir: tempDir,
});

expect(res.success).toBe(true);
expect(res.hookPath).toBe(path.join(tempDir, ".husky", "pre-commit"));
expect(fs.existsSync(res.hookPath)).toBe(true);
});

it("formats greeting with package.json prepare script details in blank mode", () => {
const greeting = formatInitGreeting({
success: true,
message: "Successfully added npx secureflow to package.json prepare script",
hookPath: path.join(tempDir, "package.json"),
});

expect(greeting).toContain("Hook Location:");
expect(greeting).toContain("package.json");
expect(greeting).toContain("npm run prepare");
});
});
});
Loading
Loading