Skip to content

Commit 91e2886

Browse files
committed
feat: implement init command for SKILL.md scaffolding
1 parent 049f4e4 commit 91e2886

3 files changed

Lines changed: 195 additions & 0 deletions

File tree

‎src/cli.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bun
22
import { cac } from "cac";
33
import { runFindCommand } from "./commands/find";
4+
import { runInitCommand } from "./commands/init";
45

56
const COMMANDS = ["add", "find", "check", "update", "init", "generate-lock"] as const;
67
type CommandName = (typeof COMMANDS)[number];
@@ -52,6 +53,10 @@ function runCommand(command: CommandName, args: string[], flags: GlobalFlags): n
5253
return runFindCommand(args, { verbose: flags.verbose });
5354
}
5455

56+
if (command === "init") {
57+
return runInitCommand(args, { yes: flags.yes });
58+
}
59+
5560
if (flags.verbose) {
5661
console.error(`Running ${command} with args: ${args.join(" ")}`);
5762
}

‎src/commands/init.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
type WriteLine = (line: string) => void;
5+
6+
export type RunInitCommandOptions = {
7+
cwd?: string;
8+
yes?: boolean;
9+
stdout?: WriteLine;
10+
stderr?: WriteLine;
11+
confirmOverwrite?: (skillFilePath: string) => boolean;
12+
};
13+
14+
const SKILL_NAME_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
15+
16+
export function runInitCommand(args: string[], options: RunInitCommandOptions = {}): number {
17+
const stdout = options.stdout ?? ((line: string) => console.log(line));
18+
const stderr = options.stderr ?? ((line: string) => console.error(line));
19+
const cwd = path.resolve(options.cwd ?? process.cwd());
20+
21+
const targetArg = args[0] ?? ".";
22+
const targetDir = path.resolve(cwd, targetArg);
23+
const skillName = path.basename(targetDir);
24+
25+
const validationError = validateSkillName(skillName);
26+
if (validationError) {
27+
stderr(validationError);
28+
return 1;
29+
}
30+
31+
fs.mkdirSync(targetDir, { recursive: true });
32+
const skillFilePath = path.join(targetDir, "SKILL.md");
33+
34+
if (fs.existsSync(skillFilePath) && options.yes !== true) {
35+
const canOverwrite = options.confirmOverwrite ? options.confirmOverwrite(skillFilePath) : false;
36+
if (!canOverwrite) {
37+
stderr(`SKILL.md already exists at ${skillFilePath}. Use --yes to overwrite.`);
38+
return 1;
39+
}
40+
}
41+
42+
const template = renderSkillTemplate(skillName);
43+
fs.writeFileSync(skillFilePath, template);
44+
45+
stdout(`Created ${skillFilePath}`);
46+
return 0;
47+
}
48+
49+
function validateSkillName(skillName: string): string | undefined {
50+
if (skillName.length < 1 || skillName.length > 64) {
51+
return "Skill name must be 1-64 characters.";
52+
}
53+
54+
if (!SKILL_NAME_PATTERN.test(skillName)) {
55+
return "Skill name must be lowercase alphanumeric with optional hyphens (e.g. my-skill).";
56+
}
57+
58+
return undefined;
59+
}
60+
61+
function renderSkillTemplate(skillName: string): string {
62+
const title = skillName
63+
.split("-")
64+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
65+
.join(" ");
66+
67+
return `---
68+
name: ${skillName}
69+
description: ${title} description.
70+
---
71+
72+
# ${title}
73+
74+
Describe what this skill does and when to use it.
75+
`;
76+
}

‎tests/commands/init.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, test } from "vitest";
5+
import { runInitCommand } from "../../src/commands/init";
6+
7+
const tempDirs: string[] = [];
8+
9+
function makeTempDir(prefix: string): string {
10+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
11+
tempDirs.push(dir);
12+
return dir;
13+
}
14+
15+
afterEach(() => {
16+
for (const dir of tempDirs.splice(0)) {
17+
fs.rmSync(dir, { recursive: true, force: true });
18+
}
19+
});
20+
21+
describe("runInitCommand", () => {
22+
test("creates SKILL.md in current directory", () => {
23+
const root = makeTempDir("skillet-init-");
24+
const cwd = path.join(root, "alpha-skill");
25+
fs.mkdirSync(cwd, { recursive: true });
26+
27+
const output: string[] = [];
28+
const exitCode = runInitCommand([], {
29+
cwd,
30+
yes: false,
31+
stdout: (line) => output.push(line),
32+
stderr: () => undefined,
33+
});
34+
35+
const skillFile = path.join(cwd, "SKILL.md");
36+
expect(exitCode).toBe(0);
37+
expect(fs.existsSync(skillFile)).toBe(true);
38+
expect(fs.readFileSync(skillFile, "utf8")).toContain("name: alpha-skill");
39+
expect(output[0]).toContain("Created");
40+
});
41+
42+
test("creates SKILL.md in specified directory", () => {
43+
const root = makeTempDir("skillet-init-");
44+
const cwd = root;
45+
46+
const output: string[] = [];
47+
const exitCode = runInitCommand(["beta-skill"], {
48+
cwd,
49+
yes: false,
50+
stdout: (line) => output.push(line),
51+
stderr: () => undefined,
52+
});
53+
54+
const skillFile = path.join(cwd, "beta-skill", "SKILL.md");
55+
expect(exitCode).toBe(0);
56+
expect(fs.readFileSync(skillFile, "utf8")).toContain("name: beta-skill");
57+
expect(fs.readFileSync(skillFile, "utf8")).toContain("description:");
58+
});
59+
60+
test("does not overwrite existing SKILL.md without confirmation", () => {
61+
const root = makeTempDir("skillet-init-");
62+
const cwd = path.join(root, "gamma-skill");
63+
fs.mkdirSync(cwd, { recursive: true });
64+
const skillFile = path.join(cwd, "SKILL.md");
65+
fs.writeFileSync(skillFile, "existing");
66+
67+
const errors: string[] = [];
68+
const exitCode = runInitCommand([], {
69+
cwd,
70+
yes: false,
71+
confirmOverwrite: () => false,
72+
stdout: () => undefined,
73+
stderr: (line) => errors.push(line),
74+
});
75+
76+
expect(exitCode).toBe(1);
77+
expect(fs.readFileSync(skillFile, "utf8")).toBe("existing");
78+
expect(errors.join("\n")).toContain("already exists");
79+
});
80+
81+
test("overwrites existing SKILL.md when --yes is set", () => {
82+
const root = makeTempDir("skillet-init-");
83+
const cwd = path.join(root, "delta-skill");
84+
fs.mkdirSync(cwd, { recursive: true });
85+
const skillFile = path.join(cwd, "SKILL.md");
86+
fs.writeFileSync(skillFile, "existing");
87+
88+
const exitCode = runInitCommand([], {
89+
cwd,
90+
yes: true,
91+
stdout: () => undefined,
92+
stderr: () => undefined,
93+
});
94+
95+
expect(exitCode).toBe(0);
96+
expect(fs.readFileSync(skillFile, "utf8")).toContain("name: delta-skill");
97+
});
98+
99+
test("rejects invalid skill names", () => {
100+
const root = makeTempDir("skillet-init-");
101+
const cwd = root;
102+
103+
const errors: string[] = [];
104+
const exitCode = runInitCommand(["InvalidName"], {
105+
cwd,
106+
yes: false,
107+
stdout: () => undefined,
108+
stderr: (line) => errors.push(line),
109+
});
110+
111+
expect(exitCode).toBe(1);
112+
expect(errors.join("\n")).toContain("must be lowercase");
113+
});
114+
});

0 commit comments

Comments
 (0)