designsnitch 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.ko.md +471 -0
- package/README.md +335 -244
- package/bin/designsnitch.js +73 -13
- package/package.json +4 -4
- package/skills/designsnitch-check/SKILL.md +107 -0
- package/skills/designsnitch-eslint/SKILL.md +128 -0
- package/skills/designsnitch-hooks/SKILL.md +165 -0
- package/skills/designsnitch-palette/SKILL.md +140 -0
- package/skills/designsnitch-setup/SKILL.md +115 -0
- package/src/check.js +128 -42
- package/src/cli.js +66 -25
- package/src/colors.js +166 -0
- package/src/config.js +39 -22
- package/src/core/classes.js +14 -10
- package/src/core/css.js +54 -0
- package/src/core/tokens.js +27 -51
- package/src/engine.js +37 -0
- package/src/files.js +46 -0
- package/src/format.js +44 -19
- package/src/hook.js +163 -0
- package/src/index.js +4 -7
- package/src/install.js +153 -0
- package/src/messages.js +275 -0
- package/src/rules/color-unknown.js +17 -6
- package/src/rules/color-use-token.js +23 -12
- package/src/rules/load-tokens.js +25 -19
- package/src/rules/suggest.js +12 -4
- package/src/rules/use-ds-component.js +34 -10
- package/skills/designsnitch/SKILL.md +0 -107
- package/src/core/scale.js +0 -71
- package/src/rules/scale-labels.js +0 -6
- package/src/rules/scale-unknown.js +0 -43
- package/src/rules/scale-use-token.js +0 -38
package/bin/designsnitch.js
CHANGED
|
@@ -1,14 +1,74 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
process.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
|
|
3
|
+
const MINIMUM_NODE_MAJOR = 22;
|
|
4
|
+
|
|
5
|
+
/** @param {string} version process.versions.node 와 같은 형식의 버전 */
|
|
6
|
+
export function isSupportedNodeVersion(version) {
|
|
7
|
+
return Number(version.split(".", 1)[0]) >= MINIMUM_NODE_MAJOR;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const version = process.versions.node;
|
|
11
|
+
const supported = isSupportedNodeVersion(version);
|
|
12
|
+
if (await isEntrypoint()) await main();
|
|
13
|
+
|
|
14
|
+
async function isEntrypoint() {
|
|
15
|
+
const entry = process.argv[1];
|
|
16
|
+
if (!entry) return false;
|
|
17
|
+
// 설치된 실행 파일은 심볼릭 링크일 수 있다.
|
|
18
|
+
const { existsSync, realpathSync } = await import("node:fs");
|
|
19
|
+
return existsSync(entry) && realpathSync(entry) === realpathSync(new URL(import.meta.url));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function main() {
|
|
23
|
+
const argv = process.argv.slice(2);
|
|
24
|
+
if (!supported) {
|
|
25
|
+
let flagLang;
|
|
26
|
+
for (const [index, arg] of argv.entries()) {
|
|
27
|
+
if (arg === "--") break;
|
|
28
|
+
if (arg === "--lang") flagLang = argv[index + 1];
|
|
29
|
+
else if (arg.startsWith("--lang=")) flagLang = arg.slice("--lang=".length);
|
|
30
|
+
}
|
|
31
|
+
const { messages, resolveLang } = await import("../src/messages.js");
|
|
32
|
+
const message = messages(resolveLang(flagLang)).nodeVersionUnsupported;
|
|
33
|
+
process.stderr.write(`${message({ minimum: MINIMUM_NODE_MAJOR, version })}\n`);
|
|
34
|
+
process.exitCode = 2;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 출력을 받는 쪽이 먼저 닫으면(예: `designsnitch check | head`) 조용히 끝낸다.
|
|
39
|
+
process.stdout.on("error", (error) => {
|
|
40
|
+
if (error.code === "EPIPE") process.exit();
|
|
41
|
+
throw error;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const { run } = await import("../src/cli.js");
|
|
45
|
+
process.exitCode = await run(argv, {
|
|
46
|
+
cwd: process.cwd(),
|
|
47
|
+
// hook 명령은 에이전트가 표준 입력으로 주는 JSON 을 읽는다.
|
|
48
|
+
stdin: argv[0] === "hook" && !process.stdin.isTTY ? await readStdin() : undefined,
|
|
49
|
+
stdout: process.stdout,
|
|
50
|
+
stderr: process.stderr,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 입력이 끝나지 않는 환경에서도 멈추지 않도록 잠깐만 기다린다.
|
|
55
|
+
function readStdin(timeoutMs = 2000) {
|
|
56
|
+
return new Promise((resolve) => {
|
|
57
|
+
let data = "";
|
|
58
|
+
const done = () => {
|
|
59
|
+
process.stdin.pause();
|
|
60
|
+
resolve(data);
|
|
61
|
+
};
|
|
62
|
+
const timer = setTimeout(done, timeoutMs);
|
|
63
|
+
process.stdin.setEncoding("utf8");
|
|
64
|
+
process.stdin.on("data", (chunk) => (data += chunk));
|
|
65
|
+
process.stdin.on("end", () => {
|
|
66
|
+
clearTimeout(timer);
|
|
67
|
+
done();
|
|
68
|
+
});
|
|
69
|
+
process.stdin.on("error", () => {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
done();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "designsnitch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "AI
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Keep AI-written UI code manageable and consistent, even without a design system. CLI, agent skills, ESLint plugin and agent hook for React/Next.js with Tailwind CSS v4.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"exports": "./src/index.js",
|
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@typescript-eslint/parser": "^8.73.0",
|
|
19
|
-
"eslint": "
|
|
19
|
+
"eslint": "^9 || ^10",
|
|
20
20
|
"postcss": "^8.4.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"vitest": "^3.0.0"
|
|
24
24
|
},
|
|
25
25
|
"bin": {
|
|
26
|
-
"designsnitch": "
|
|
26
|
+
"designsnitch": "bin/designsnitch.js"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=22"
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-check
|
|
3
|
+
description: After creating or editing UI code in a React/Next.js + Tailwind project, check it against the project's design system (color tokens and shared components) and fix what is off. Use when you have changed components, pages, className values or styles, or when the user asks for a design system check or cleanup.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# designsnitch-check
|
|
7
|
+
|
|
8
|
+
After you change UI code, run the design system check before you finish. The CLI makes the verdicts and tells you what to do with each one. This document covers how to act on that, and how to ask the user when a decision is theirs.
|
|
9
|
+
|
|
10
|
+
## 1. Run the check
|
|
11
|
+
|
|
12
|
+
Pass the files you changed. Without paths, the whole project is checked.
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx designsnitch check src/features/checkout/PayButton.tsx --format json
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The exit code is 0 (no errors), 1 (errors found), or 2 (could not run). On 2, read the message on stderr.
|
|
19
|
+
|
|
20
|
+
- The config file `designsnitch.config.json` is missing: follow the `designsnitch-setup` skill first, then come back.
|
|
21
|
+
- The token CSS could not be read: the message names the file, line and column. Fix that syntax error first. Nothing can be judged while the standard itself is broken.
|
|
22
|
+
|
|
23
|
+
## 2. Read the result
|
|
24
|
+
|
|
25
|
+
```json
|
|
26
|
+
{
|
|
27
|
+
"designSystem": { "colorTokens": 175, "components": 4, "replacedTags": 2 },
|
|
28
|
+
"problems": [],
|
|
29
|
+
"next": [
|
|
30
|
+
"action \"replace\" (1): replace found with the value in candidates.",
|
|
31
|
+
"action \"ask\" (1): do not change these. Ask the user, and include how often and for what each color is used, which `designsnitch colors` shows.",
|
|
32
|
+
"After fixing, run the same command again."
|
|
33
|
+
],
|
|
34
|
+
"summary": { "files": 1, "errors": 2, "warnings": 0 },
|
|
35
|
+
"violations": [
|
|
36
|
+
{
|
|
37
|
+
"file": "src/features/checkout/PayButton.tsx",
|
|
38
|
+
"line": 7,
|
|
39
|
+
"column": 28,
|
|
40
|
+
"endLine": 7,
|
|
41
|
+
"endColumn": 40,
|
|
42
|
+
"rule": "color-use-token",
|
|
43
|
+
"severity": "error",
|
|
44
|
+
"message": "bg-[#3B82F6] has the same color as --color-primary-500. Use instead: bg-primary-500",
|
|
45
|
+
"found": "bg-[#3B82F6]",
|
|
46
|
+
"candidates": ["bg-primary-500"],
|
|
47
|
+
"action": "replace"
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
- `next` lists what to do, in order. `next` and `message` are sentences, in English or in Korean depending on the project's `lang` setting. The fields below carry the same information, so you never need to parse them.
|
|
54
|
+
- `action` says how to handle each violation. Section 3 covers each value.
|
|
55
|
+
- `found` is the offending piece of code. `candidates` lists what to use instead: classes or components in JS and TS files, and `var(--token)` in `.css` files.
|
|
56
|
+
- `designSystem` counts the tokens and components read from the config. `colorTokens` 0 means the project has no color tokens that can be read: follow the `designsnitch-palette` skill to establish them before touching any color.
|
|
57
|
+
- A non-empty `problems` means the config is broken and the results cannot be trusted. Tell the user before fixing anything.
|
|
58
|
+
|
|
59
|
+
| `rule` | Severity | Meaning |
|
|
60
|
+
| --- | --- | --- |
|
|
61
|
+
| `color-use-token` | error | A hex color that has a token was written as an arbitrary value, or directly in a CSS declaration. |
|
|
62
|
+
| `color-unknown` | warning | A hex color with no matching token. |
|
|
63
|
+
| `use-ds-component` | error | A raw tag was used outside the design system directory although a shared component replaces it. |
|
|
64
|
+
| `parse-error` | error | The file could not be parsed. |
|
|
65
|
+
|
|
66
|
+
## 3. Act on each violation
|
|
67
|
+
|
|
68
|
+
| `action` | What it means | What to do |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| `replace` | An error whose only candidate keeps the screen and the behavior the same. | Replace `found` with the candidate. |
|
|
71
|
+
| `ask` | A person has to decide: several candidates, a color that has no token, a token whose value differs between themes, or a component whose props are dynamic or spread. `message` says which. | Do not change it. Ask the user as described in section 4. |
|
|
72
|
+
| `repair` | The file could not be parsed. | Fix the syntax error, then check again. |
|
|
73
|
+
|
|
74
|
+
For `use-ds-component`, replacing means more than renaming the tag. Read the design system component file to learn its props, add the import, and remove className values that duplicate styles the component already has.
|
|
75
|
+
|
|
76
|
+
## 4. Ask so the user can decide
|
|
77
|
+
|
|
78
|
+
Deciding is the user's job. Your job is to make the decision easy. Do not hand over a bare list of violations. For each decision, give:
|
|
79
|
+
|
|
80
|
+
1. **What has to be decided**, in one sentence.
|
|
81
|
+
2. **The facts.** Run `npx designsnitch colors --format json` and report how often the color is used, in how many files, and for what (text, background, border). Open one or two of the locations if the use is unclear.
|
|
82
|
+
3. **What each choice changes.** Say whether the screen will look different, and how many places would be edited.
|
|
83
|
+
4. **Your recommendation and why.**
|
|
84
|
+
|
|
85
|
+
Group decisions that are the same question. Thirty uses of one unknown color are one decision, not thirty.
|
|
86
|
+
|
|
87
|
+
An example for an error with several candidates:
|
|
88
|
+
|
|
89
|
+
> `text-[#484646]` matches two tokens: `--color-text-primary` and `--color-neutral-30`. It appears 137 times, almost always as body text (`text-`). Either token renders the same color today, so nothing changes on screen. They will diverge if the neutral scale is ever retuned. I recommend `text-text-primary`, because the use is text and that token is named for it. Shall I replace all 137?
|
|
90
|
+
|
|
91
|
+
An example for a color with no token:
|
|
92
|
+
|
|
93
|
+
> `#3B82F5` is used 3 times, all as a button background. It is one step away from `--color-primary-500` (`#3B82F6`), which is used 88 times, so it is probably a typo. Replacing it changes the color by an amount the eye cannot see. I recommend replacing the 3 with `bg-primary-500`. If it is a separate color on purpose, it needs its own token.
|
|
94
|
+
|
|
95
|
+
## 5. Check again
|
|
96
|
+
|
|
97
|
+
After fixing, run the same command again. Stop when the only items left are waiting on the user's decision.
|
|
98
|
+
|
|
99
|
+
## 6. Report
|
|
100
|
+
|
|
101
|
+
Report what you fixed and what you left, separately. For what you left, use the form from section 4.
|
|
102
|
+
|
|
103
|
+
## What not to do
|
|
104
|
+
|
|
105
|
+
- Do not edit `designsnitch.config.json` or the token CSS to make the check pass. They are the standard the code is judged against. If one of them seems wrong, explain why and ask the user.
|
|
106
|
+
- Do not add `eslint-disable` or `designsnitch-disable-next-line` comments, lower a rule's severity, or turn a rule off to make the check pass. If an exception seems necessary, ask the user.
|
|
107
|
+
- Do not pick one of several candidates on your own.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-eslint
|
|
3
|
+
description: Put the designsnitch rules into a project's ESLint so design system violations are reported as errors and warnings in the editor and in the lint step. Installs ESLint when the project has none. Use as the last step of setting up designsnitch, or when the user asks to enforce the design system through ESLint or lint.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# designsnitch-eslint
|
|
7
|
+
|
|
8
|
+
The ESLint plugin is how designsnitch blocks and warns. `designsnitch check` tells an AI agent what to fix. The plugin is what makes a violation show up in the editor and fail the lint step. Setting up designsnitch is not finished until the rules run in ESLint.
|
|
9
|
+
|
|
10
|
+
`designsnitch.config.json` must exist first. If it does not, follow the `designsnitch-setup` skill.
|
|
11
|
+
|
|
12
|
+
## 1. Find out what the project has
|
|
13
|
+
|
|
14
|
+
| The project has | Do |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| A flat config (`eslint.config.js`, `.mjs` or `.ts`) that already has `designsnitch/` rules | Nothing. Go to step 5 and verify. |
|
|
17
|
+
| A flat config without `designsnitch/` rules | Step 3A |
|
|
18
|
+
| No ESLint at all | Step 3B |
|
|
19
|
+
| A legacy config (`.eslintrc*`) | Stop. Tell the user the rules need a flat config and that converting the project's config is their decision. |
|
|
20
|
+
|
|
21
|
+
## 2. Count the existing violations
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npx designsnitch check --format json
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Look at `summary.errors`. Error-level rules make the lint step fail on every existing violation.
|
|
28
|
+
|
|
29
|
+
If there are existing errors, ask the user before adding the rules. Give them what they need to decide:
|
|
30
|
+
|
|
31
|
+
- How many errors there are, by rule, and in how many files.
|
|
32
|
+
- What happens if the rules are added now: the lint step fails until those are fixed.
|
|
33
|
+
- How many of the errors have exactly one candidate, since those can be fixed mechanically with the `designsnitch-check` skill.
|
|
34
|
+
- Your recommendation. Usually: fix the mechanical ones first, then add the rules.
|
|
35
|
+
|
|
36
|
+
## 3A. Add the rules to an existing config
|
|
37
|
+
|
|
38
|
+
Install the package if it is not a dependency yet.
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npm install --save-dev designsnitch
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Add one entry to the end of the exported array. Keep everything that is already there.
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
// eslint.config.js
|
|
48
|
+
import designsnitch from "designsnitch";
|
|
49
|
+
import designSystem from "./designsnitch.config.json" with { type: "json" };
|
|
50
|
+
|
|
51
|
+
export default [
|
|
52
|
+
// ...the project's existing config stays as it is
|
|
53
|
+
{
|
|
54
|
+
files: ["**/*.{js,jsx,ts,tsx}"],
|
|
55
|
+
plugins: { designsnitch },
|
|
56
|
+
// Reuse the CLI's config so the standard is written in one place.
|
|
57
|
+
settings: { designsnitch: designSystem },
|
|
58
|
+
rules: {
|
|
59
|
+
// error: the design system has a token or component for this, and it was not used
|
|
60
|
+
"designsnitch/color-use-token": "error",
|
|
61
|
+
"designsnitch/use-ds-component": "error",
|
|
62
|
+
// warn: the color is not in the design system, so a person has to decide
|
|
63
|
+
"designsnitch/color-unknown": "warn",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## 3B. Install ESLint when the project has none
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
npm install --save-dev eslint typescript-eslint designsnitch
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Create `eslint.config.js` with only what designsnitch needs. Do not add other rule sets: which other lint rules the project wants is not yours to decide.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
// eslint.config.js
|
|
79
|
+
import designsnitch from "designsnitch";
|
|
80
|
+
import tseslint from "typescript-eslint";
|
|
81
|
+
import designSystem from "./designsnitch.config.json" with { type: "json" };
|
|
82
|
+
|
|
83
|
+
export default [
|
|
84
|
+
{ ignores: ["node_modules/**", ".next/**", "dist/**", "build/**", "out/**", "coverage/**"] },
|
|
85
|
+
{
|
|
86
|
+
files: ["**/*.{js,jsx,ts,tsx}"],
|
|
87
|
+
languageOptions: {
|
|
88
|
+
parser: tseslint.parser,
|
|
89
|
+
parserOptions: { ecmaFeatures: { jsx: true } },
|
|
90
|
+
},
|
|
91
|
+
plugins: { designsnitch },
|
|
92
|
+
settings: { designsnitch: designSystem },
|
|
93
|
+
rules: {
|
|
94
|
+
"designsnitch/color-use-token": "error",
|
|
95
|
+
"designsnitch/use-ds-component": "error",
|
|
96
|
+
"designsnitch/color-unknown": "warn",
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
If `package.json` has `"type": "module"`, name the file `eslint.config.js`. Otherwise name it `eslint.config.mjs`.
|
|
103
|
+
|
|
104
|
+
Add a lint script if the project has none.
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
{ "scripts": { "lint": "eslint ." } }
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 4. Keep the severities
|
|
111
|
+
|
|
112
|
+
They follow one rule: it is an error when the design system already has the answer, and a warning when it does not. Errors block. Warnings ask a person to decide. If the user wants a softer start, that is their decision to make.
|
|
113
|
+
|
|
114
|
+
## 5. Verify
|
|
115
|
+
|
|
116
|
+
Run the project's lint command.
|
|
117
|
+
|
|
118
|
+
- The `designsnitch/` rules should report the same violations in JS and TS files as `npx designsnitch check`.
|
|
119
|
+
- CSS files are checked by the CLI only. Violations in `.css` files will not appear in the lint output.
|
|
120
|
+
|
|
121
|
+
Tell the user what was installed and changed, and how many errors and warnings the lint step reports now.
|
|
122
|
+
|
|
123
|
+
## What not to do
|
|
124
|
+
|
|
125
|
+
- Do not lower an error to a warning, or turn a rule off, to make lint pass.
|
|
126
|
+
- Do not remove or rewrite the project's existing ESLint config.
|
|
127
|
+
- Do not convert a legacy `.eslintrc` to a flat config as part of this.
|
|
128
|
+
- Do not add lint rules other than the `designsnitch/` ones.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-hooks
|
|
3
|
+
description: Register designsnitch as a hook of the coding agent (Claude Code, Codex, Gemini CLI or Grok Build), so the agent is told about design system errors right after it edits a file and cannot finish while errors it can fix remain. Use as part of setting up designsnitch, or when the user asks to make AI-written code follow the design system automatically.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# designsnitch-hooks
|
|
7
|
+
|
|
8
|
+
A skill is advice: an agent may or may not follow it. A hook is run by the agent's own runtime, so it happens every time. This skill registers `designsnitch hook` as a hook, which makes the design system check part of how the agent works in this project.
|
|
9
|
+
|
|
10
|
+
`designsnitch.config.json` must exist first. If it does not, follow the `designsnitch-setup` skill. Without a config file the hook does nothing.
|
|
11
|
+
|
|
12
|
+
## What the hook does
|
|
13
|
+
|
|
14
|
+
`npx designsnitch hook` checks only the files the agent just changed, and blocks only errors the change added. It compares each file with its version in git `HEAD`: errors that were already there do not block.
|
|
15
|
+
|
|
16
|
+
| When it runs | What happens |
|
|
17
|
+
| --- | --- |
|
|
18
|
+
| Right after the agent edits a file | If that file has errors, the agent receives them with what to do next. |
|
|
19
|
+
| When the agent is about to finish | If errors the agent can fix itself remain in the changed files, the agent is not allowed to finish. |
|
|
20
|
+
|
|
21
|
+
- Warnings never block.
|
|
22
|
+
- Errors that need a person's decision (`ask`) are reported after an edit, but do not stop the agent from finishing. The agent has to stop in order to ask.
|
|
23
|
+
- Files the agent did not touch are left alone. Errors that were already in the files it touched are reported only if the change added more of them.
|
|
24
|
+
|
|
25
|
+
## 1. Register the hook in Claude Code
|
|
26
|
+
|
|
27
|
+
Run the installer. It adds the hook to `.claude/settings.json`, keeps any hooks and settings that are already there, and can be run again safely.
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npx designsnitch install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
It also copies the designsnitch skills into `.claude/skills`, replacing older copies of them.
|
|
34
|
+
|
|
35
|
+
If you need to do it by hand, this is what it adds to `.claude/settings.json`.
|
|
36
|
+
|
|
37
|
+
```json
|
|
38
|
+
{
|
|
39
|
+
"hooks": {
|
|
40
|
+
"PostToolUse": [
|
|
41
|
+
{
|
|
42
|
+
"matcher": "Edit|Write|MultiEdit",
|
|
43
|
+
"hooks": [{ "type": "command", "command": "npx designsnitch hook" }]
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"Stop": [
|
|
47
|
+
{
|
|
48
|
+
"hooks": [{ "type": "command", "command": "npx designsnitch hook" }]
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`.claude/settings.json` is shared with the team through git, so the hook applies to everyone who uses Claude Code in this project. If the user wants the hook only for themselves, do not run the installer: add the entries above to `.claude/settings.local.json` by hand.
|
|
56
|
+
|
|
57
|
+
## 2. Register the hook in Codex
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
npx designsnitch install --agent codex
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
It adds the hook to `.codex/hooks.json` and copies the skills into `.agents/skills`. Use `--agent all` to set up every supported agent together.
|
|
64
|
+
|
|
65
|
+
```json
|
|
66
|
+
{
|
|
67
|
+
"hooks": {
|
|
68
|
+
"PostToolUse": [
|
|
69
|
+
{
|
|
70
|
+
"matcher": "apply_patch",
|
|
71
|
+
"hooks": [{ "type": "command", "command": "npx designsnitch hook", "timeout": 60 }]
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
"Stop": [
|
|
75
|
+
{
|
|
76
|
+
"hooks": [{ "type": "command", "command": "npx designsnitch hook", "timeout": 60 }]
|
|
77
|
+
}
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Two things are different in Codex. Tell the user about both.
|
|
84
|
+
|
|
85
|
+
- **The hook does not run until the user approves it.** Codex runs project hooks only in a project the user has trusted, and only hooks the user has approved. You cannot do this for them. Ask the user to open Codex in the project and approve when it asks.
|
|
86
|
+
- **Codex often edits files with shell commands, which the `apply_patch` matcher does not see.** Those edits are still caught when Codex is about to finish: the `Stop` hook checks every file git reports as changed.
|
|
87
|
+
|
|
88
|
+
## 3. Register the hook in Gemini CLI
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
npx designsnitch install --agent gemini
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
It adds the hook to `.gemini/settings.json` and copies the skills into `.agents/skills`, which Gemini CLI reads as workspace skills. Gemini CLI uses different event names, and its `timeout` is in milliseconds.
|
|
95
|
+
|
|
96
|
+
```json
|
|
97
|
+
{
|
|
98
|
+
"hooks": {
|
|
99
|
+
"AfterTool": [
|
|
100
|
+
{
|
|
101
|
+
"matcher": "write_file|replace",
|
|
102
|
+
"hooks": [{ "name": "designsnitch", "type": "command", "command": "npx designsnitch hook", "timeout": 60000 }]
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
"AfterAgent": [
|
|
106
|
+
{
|
|
107
|
+
"hooks": [{ "name": "designsnitch", "type": "command", "command": "npx designsnitch hook", "timeout": 60000 }]
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
- `AfterTool` runs after `write_file` or `replace`. Exit code 2 replaces the tool's result with the errors, and the turn continues.
|
|
115
|
+
- `AfterAgent` runs when Gemini CLI is about to answer. Exit code 2 rejects the answer and makes it try again with the errors as feedback. `stop_hook_active` keeps it from looping.
|
|
116
|
+
|
|
117
|
+
Tell the user that Gemini CLI warns before it runs a new project hook, and that they need to allow it. If they have turned on folder trust, they also need to trust the folder. This setup follows the official Gemini CLI documentation and has not been tested with Gemini CLI. If the user reports that the hook does not fire, check that the installed Gemini CLI version supports hooks.
|
|
118
|
+
|
|
119
|
+
## 4. Register the hook in Grok Build
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
npx designsnitch install --agent grok
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
It writes `.grok/hooks/designsnitch.json` with the same `PostToolUse` and `Stop` hooks as Claude Code (`timeout` in seconds), and copies the skills into `.grok/skills`. Grok Build sends the hook its input in camelCase (`hookEventName`, `toolInput`), which `designsnitch hook` understands.
|
|
126
|
+
|
|
127
|
+
Tell the user two things.
|
|
128
|
+
|
|
129
|
+
- **The hook does not run until the project is trusted.** Ask the user to run `/hooks-trust` in Grok Build, or to start it with `--trust`.
|
|
130
|
+
- **Grok Build may report the errors without stopping.** Its documentation calls `PreToolUse` the only blocking event and does not say what exit code 2 does after a tool or at the end of a turn. This setup follows the official xAI documentation and has not been tested with Grok Build.
|
|
131
|
+
|
|
132
|
+
## 5. Other agents
|
|
133
|
+
|
|
134
|
+
`designsnitch hook` does not depend on a particular agent. It reads an optional JSON object from stdin:
|
|
135
|
+
|
|
136
|
+
- `tool_input.file_path`: the file that was just edited. Without it, the files that git reports as changed are checked.
|
|
137
|
+
- `hook_event_name` (or `hookEventName`): `Stop`, `SubagentStop` or `AfterAgent` means the agent is about to finish.
|
|
138
|
+
- `stop_hook_active`: when true, the hook does not block again.
|
|
139
|
+
|
|
140
|
+
It exits with 2 and writes what to fix to stderr when it blocks, and exits with 0 otherwise. If the agent in use supports command hooks with these conventions, register the same command there. Check that agent's hook documentation for the file and the event names, and tell the user if you could not confirm that it works.
|
|
141
|
+
|
|
142
|
+
## 6. Verify
|
|
143
|
+
|
|
144
|
+
Make the hook fire once.
|
|
145
|
+
|
|
146
|
+
1. Pick a source file and add a class that uses a tokenized color as a hex value, such as `bg-[#3B82F6]` when a token has that value.
|
|
147
|
+
2. Run the hook by hand with the same input the agent would send.
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
echo '{"hook_event_name":"PostToolUse","tool_input":{"file_path":"src/app/page.tsx"}}' | npx designsnitch hook; echo "exit: $?"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
It should print the error and exit with 2.
|
|
154
|
+
|
|
155
|
+
3. Undo the test change and run it again. It should print nothing and exit with 0.
|
|
156
|
+
|
|
157
|
+
## 7. Tell the user
|
|
158
|
+
|
|
159
|
+
Say which file you changed and what the hook will now do. Mention that it only looks at files the agent changes, so existing violations elsewhere stay until someone cleans them up with the `designsnitch-check` skill.
|
|
160
|
+
|
|
161
|
+
## What not to do
|
|
162
|
+
|
|
163
|
+
- Do not remove or rewrite hooks that are already registered.
|
|
164
|
+
- Do not register the hook when there is no `designsnitch.config.json`.
|
|
165
|
+
- Do not work around the hook when it blocks you. Fix what it reports, or ask the user about the items marked `ask`.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-palette
|
|
3
|
+
description: Create color tokens for a React/Next.js + Tailwind project that has none, based on the colors its code already uses. Use when a project has no color tokens or palette, when `designsnitch check` reports that no color token could be read, or when the user asks to derive a color palette or color tokens from existing code.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# designsnitch-palette
|
|
7
|
+
|
|
8
|
+
Some projects have no color tokens: colors are written straight into the code as `bg-[#3B82F6]`, `text-gray-700` or `style={{ color: "#3B82F6" }}`. designsnitch has nothing to check such code against. This skill turns the colors the code already uses into a set of tokens.
|
|
9
|
+
|
|
10
|
+
The CLI collects the colors. You analyze them and propose tokens. The user decides. Nothing is written before the user approves, because the tokens become the standard all UI code is judged against.
|
|
11
|
+
|
|
12
|
+
## 1. Collect the colors
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npx designsnitch colors --format json
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This works without a config file. Pass paths to limit it to part of the project.
|
|
19
|
+
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"summary": { "files": 420, "colors": 215, "paletteColors": 12, "uses": 1999 },
|
|
23
|
+
"colors": [
|
|
24
|
+
{
|
|
25
|
+
"color": "#484646",
|
|
26
|
+
"uses": 176,
|
|
27
|
+
"files": 53,
|
|
28
|
+
"utilities": { "text": 137, "border": 7 },
|
|
29
|
+
"literals": 32,
|
|
30
|
+
"properties": { "color": 4 },
|
|
31
|
+
"examples": [{ "file": "src/app/page.tsx", "line": 12 }]
|
|
32
|
+
}
|
|
33
|
+
],
|
|
34
|
+
"palette": [
|
|
35
|
+
{
|
|
36
|
+
"name": "gray-700",
|
|
37
|
+
"uses": 14,
|
|
38
|
+
"files": 6,
|
|
39
|
+
"utilities": { "text": 14 },
|
|
40
|
+
"examples": [{ "file": "src/app/page.tsx", "line": 30 }]
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
- `colors` are hex colors, most used first. `utilities` counts uses per Tailwind utility (`bg-[#484646]` counts under `bg`). `literals` counts strings that are a hex color on their own, such as inline styles and chart options. `properties` counts uses per CSS property in `.css` files.
|
|
47
|
+
- `palette` are colors from Tailwind's default palette used by name, such as `text-gray-700`.
|
|
48
|
+
- `examples` gives a few locations, each in a different file. Open them when you need to see how a color is used.
|
|
49
|
+
- If a config file exists, each color also has `tokens`: the tokens that already have this value. Colors with a non-empty `tokens` are already covered. Work on the rest.
|
|
50
|
+
|
|
51
|
+
## 2. Analyze
|
|
52
|
+
|
|
53
|
+
Go through `colors` from the top and sort each color into one of these.
|
|
54
|
+
|
|
55
|
+
**A color of its own.** It is used often and nothing close to it is used more. It gets a token.
|
|
56
|
+
|
|
57
|
+
**A near duplicate.** Its value is almost the same as a more used color, a few steps apart in one or two channels, such as `#3B82F5` next to `#3B82F6`. These are usually the same intended color written from memory. Group it under the more used color. Do not decide the merge yourself: merging changes what is rendered.
|
|
58
|
+
|
|
59
|
+
**The long tail.** It is used once or twice. Do not give it a token by default. List these for the user.
|
|
60
|
+
|
|
61
|
+
Then work out what each color of its own is for. `utilities` and `properties` show this.
|
|
62
|
+
|
|
63
|
+
| Mostly used with | Likely role |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `text`, or the `color` property | Text color |
|
|
66
|
+
| `border`, `divide`, `ring`, `outline`, or `border` properties | Border or outline |
|
|
67
|
+
| `bg`, or `background` properties | Background or surface |
|
|
68
|
+
| A mix, or mostly `literals` | A general color such as a brand or chart color. Open the examples to see. |
|
|
69
|
+
|
|
70
|
+
For `palette`, note which default palette colors the project relies on. They already work as classes and designsnitch does not flag them. Whether they belong to the design system is the user's call, so include them in the proposal as a question.
|
|
71
|
+
|
|
72
|
+
## 3. Propose
|
|
73
|
+
|
|
74
|
+
Show the user a proposal before writing anything.
|
|
75
|
+
|
|
76
|
+
| Token | Value | Uses | Role | Would absorb |
|
|
77
|
+
| --- | --- | --- | --- | --- |
|
|
78
|
+
| `--color-text-primary` | `#484646` | 176 | Body text | `#484645` (3 uses) |
|
|
79
|
+
| `--color-border-default` | `#e2e1e5` | 79 | Borders | |
|
|
80
|
+
|
|
81
|
+
Naming:
|
|
82
|
+
|
|
83
|
+
- If the project already has CSS variables or a naming habit, follow it.
|
|
84
|
+
- Otherwise name a color by its role when it has a clear one (`--color-text-primary`, `--color-border-default`, `--color-surface`), and by a scale when it is a general color (`--color-primary-500`, `--color-neutral-200`).
|
|
85
|
+
- Every token starts with `--color-`. Tailwind turns `--color-text-primary` into classes such as `text-text-primary` and `bg-text-primary`.
|
|
86
|
+
- Do not reuse a name from Tailwind's default palette, such as `--color-blue-500`, unless the user wants to override that color.
|
|
87
|
+
|
|
88
|
+
With the table, tell the user:
|
|
89
|
+
|
|
90
|
+
- How much the proposed tokens cover: the share of all uses, for example "12 tokens cover 81% of uses".
|
|
91
|
+
- The near duplicates you want to merge, each with both values and their use counts.
|
|
92
|
+
- The long tail you left out.
|
|
93
|
+
- The default palette colors in use, and the question of whether to keep them.
|
|
94
|
+
|
|
95
|
+
Ask the user to confirm or change the names, the merges and what to leave out. Keep the set small. A palette with a token for every color in the code is not a palette.
|
|
96
|
+
|
|
97
|
+
## 4. Write the tokens
|
|
98
|
+
|
|
99
|
+
After the user approves, add the tokens to the CSS file that imports Tailwind (`@import "tailwindcss"`), inside `@theme`. Add to the existing `@theme` block if there is one.
|
|
100
|
+
|
|
101
|
+
```css
|
|
102
|
+
@theme {
|
|
103
|
+
--color-text-primary: #484646;
|
|
104
|
+
--color-border-default: #e2e1e5;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Write the values as hex. designsnitch reads hex values and `var()` references to hex values, and nothing else.
|
|
109
|
+
|
|
110
|
+
If the project uses Tailwind v3 (a `tailwind.config.js` and no `@theme`), stop and tell the user: designsnitch does not read v3 config yet.
|
|
111
|
+
|
|
112
|
+
## 5. Point the config at the tokens
|
|
113
|
+
|
|
114
|
+
`designsnitch.config.json` must have `tokens` set to that CSS file. If the config file does not exist, follow the `designsnitch-setup` skill.
|
|
115
|
+
|
|
116
|
+
## 6. Verify
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
npx designsnitch check --format json
|
|
120
|
+
npx designsnitch colors
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
- `designSystem.colorTokens` in the check result should equal the number of tokens you wrote, and `problems` should be empty.
|
|
124
|
+
- In the `colors` output, each color you made a token for should now list that token.
|
|
125
|
+
|
|
126
|
+
## 7. Hand over
|
|
127
|
+
|
|
128
|
+
Tell the user what the first check found. Every place that writes a tokenized color as a hex class or in a CSS declaration is now a `color-use-token` error, with the token class or `var(--token)` as its candidate. Replacing them is the job of the `designsnitch-check` skill. On a whole project this can be a large change, so ask the user whether to do it now and for which part of the project.
|
|
129
|
+
|
|
130
|
+
Two kinds of use stay as they are:
|
|
131
|
+
|
|
132
|
+
- Near duplicates the user chose to merge still have their old value in the code. They show up as `color-unknown` warnings. Replacing them changes what is rendered, so do it only for the merges the user approved.
|
|
133
|
+
- Hex colors in string literals and default palette classes are not flagged by the check yet.
|
|
134
|
+
|
|
135
|
+
## What not to do
|
|
136
|
+
|
|
137
|
+
- Do not write tokens before the user approves the proposal.
|
|
138
|
+
- Do not invent colors that are not in the code, and do not adjust values to make a tidier scale.
|
|
139
|
+
- Do not merge near duplicates or drop colors on your own.
|
|
140
|
+
- Do not rename or remove tokens that already exist.
|