designsnitch 0.1.0 → 0.2.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 +426 -0
- package/README.md +289 -242
- package/bin/designsnitch.js +27 -1
- package/package.json +1 -1
- package/skills/designsnitch-check/SKILL.md +107 -0
- package/skills/designsnitch-eslint/SKILL.md +128 -0
- package/skills/designsnitch-hooks/SKILL.md +90 -0
- package/skills/designsnitch-palette/SKILL.md +140 -0
- package/skills/designsnitch-setup/SKILL.md +107 -0
- package/src/check.js +116 -32
- package/src/cli.js +64 -19
- package/src/colors.js +166 -0
- package/src/config.js +4 -1
- package/src/core/classes.js +14 -10
- package/src/core/css.js +54 -0
- package/src/core/tokens.js +0 -39
- package/src/engine.js +37 -0
- package/src/files.js +46 -0
- package/src/format.js +42 -3
- package/src/hook.js +100 -0
- package/src/index.js +4 -7
- package/src/install.js +95 -0
- package/src/rules/load-tokens.js +11 -4
- 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
|
@@ -7,8 +7,34 @@ process.stdout.on("error", (error) => {
|
|
|
7
7
|
throw error;
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const argv = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
process.exitCode = await run(argv, {
|
|
11
13
|
cwd: process.cwd(),
|
|
14
|
+
// hook 명령은 에이전트가 표준 입력으로 주는 JSON 을 읽는다.
|
|
15
|
+
stdin: argv[0] === "hook" && !process.stdin.isTTY ? await readStdin() : undefined,
|
|
12
16
|
stdout: process.stdout,
|
|
13
17
|
stderr: process.stderr,
|
|
14
18
|
});
|
|
19
|
+
|
|
20
|
+
// 입력이 끝나지 않는 환경에서도 멈추지 않도록 잠깐만 기다린다.
|
|
21
|
+
function readStdin(timeoutMs = 2000) {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
let data = "";
|
|
24
|
+
const done = () => {
|
|
25
|
+
process.stdin.pause();
|
|
26
|
+
resolve(data);
|
|
27
|
+
};
|
|
28
|
+
const timer = setTimeout(done, timeoutMs);
|
|
29
|
+
process.stdin.setEncoding("utf8");
|
|
30
|
+
process.stdin.on("data", (chunk) => (data += chunk));
|
|
31
|
+
process.stdin.on("end", () => {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
done();
|
|
34
|
+
});
|
|
35
|
+
process.stdin.on("error", () => {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
done();
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -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건은 found를 candidates의 값으로 바꾸세요.",
|
|
31
|
+
"action이 ask인 1건은 바꾸지 말고 사용자에게 물으세요. ...",
|
|
32
|
+
"고친 뒤 같은 명령을 다시 실행하세요."
|
|
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]의 색은 토큰 --color-primary-500의 값입니다. 대신 사용: 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. It is written in Korean, like `message`. The fields below carry the same information, so you do not need to parse either.
|
|
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 with exactly one candidate. | Replace `found` with the candidate. |
|
|
71
|
+
| `ask` | A person has to decide: an error with several candidates, or a color that has no token. | 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,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-hooks
|
|
3
|
+
description: Register designsnitch as a hook of the coding agent, 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.
|
|
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
|
+
- Violations that were already in files the agent did not touch are left alone.
|
|
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. Other agents
|
|
58
|
+
|
|
59
|
+
`designsnitch hook` does not depend on Claude Code. It reads an optional JSON object from stdin:
|
|
60
|
+
|
|
61
|
+
- `tool_input.file_path`: the file that was just edited. Without it, the files that git reports as changed are checked.
|
|
62
|
+
- `hook_event_name`: `Stop` or `SubagentStop` means the agent is about to finish.
|
|
63
|
+
- `stop_hook_active`: when true, the hook does not block again.
|
|
64
|
+
|
|
65
|
+
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.
|
|
66
|
+
|
|
67
|
+
## 3. Verify
|
|
68
|
+
|
|
69
|
+
Make the hook fire once.
|
|
70
|
+
|
|
71
|
+
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.
|
|
72
|
+
2. Run the hook by hand with the same input the agent would send.
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
echo '{"hook_event_name":"PostToolUse","tool_input":{"file_path":"src/app/page.tsx"}}' | npx designsnitch hook; echo "exit: $?"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
It should print the error and exit with 2.
|
|
79
|
+
|
|
80
|
+
3. Undo the test change and run it again. It should print nothing and exit with 0.
|
|
81
|
+
|
|
82
|
+
## 4. Tell the user
|
|
83
|
+
|
|
84
|
+
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.
|
|
85
|
+
|
|
86
|
+
## What not to do
|
|
87
|
+
|
|
88
|
+
- Do not remove or rewrite hooks that are already registered.
|
|
89
|
+
- Do not register the hook when there is no `designsnitch.config.json`.
|
|
90
|
+
- 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.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: designsnitch-setup
|
|
3
|
+
description: Set up designsnitch in a React/Next.js + Tailwind project. Finds the token CSS and the shared component directory in the codebase, writes designsnitch.config.json, and wires the rules into ESLint. Use when `designsnitch check` reports that the config file is missing, or when the user asks to set up designsnitch in a project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# designsnitch-setup
|
|
7
|
+
|
|
8
|
+
designsnitch needs to know where the project's design system lives. Read the codebase, write that down in `designsnitch.config.json`, and then make the ESLint plugin use it. The CLI and the plugin read the same file.
|
|
9
|
+
|
|
10
|
+
Only create the file when it is missing. If it already exists and seems wrong, explain why and ask the user before changing it: this file is the standard the code is judged against.
|
|
11
|
+
|
|
12
|
+
## 0. Check that the project is supported, and install the package
|
|
13
|
+
|
|
14
|
+
designsnitch supports React or Next.js projects that use Tailwind CSS v4. Look at `package.json` and the global CSS file.
|
|
15
|
+
|
|
16
|
+
- Tailwind v4 has `tailwindcss` version 4 in `package.json` and `@import "tailwindcss"` in a CSS file.
|
|
17
|
+
- If the project uses Tailwind v3 (a `tailwind.config.js` with a `theme` section and no `@theme` in CSS), or does not use Tailwind or React at all, stop and tell the user that designsnitch does not support it yet.
|
|
18
|
+
|
|
19
|
+
Then make sure the package is a dependency of the project. The commands below and the agent hook both run `npx designsnitch`, which should use the project's own copy.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install --save-dev designsnitch
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 1. Find the token CSS
|
|
26
|
+
|
|
27
|
+
It is the CSS file with an `@theme` block or `--color-*` variables, usually `app/globals.css` or `src/app/globals.css`. If you cannot tell which CSS file is the right one, ask the user.
|
|
28
|
+
|
|
29
|
+
designsnitch reads a variable as a color token only when its name starts with `--color-` and its value is a hex color or a `var()` that leads to one. Two cases need a decision before you go on.
|
|
30
|
+
|
|
31
|
+
**The project has no color variables at all.** There are no color tokens yet. Follow the `designsnitch-palette` skill to create them from the colors the code already uses, then continue here.
|
|
32
|
+
|
|
33
|
+
**The project has color variables under other names**, such as `--ink` or `--brand` in `:root`. These are the project's tokens, but designsnitch cannot see them. Do not create a second set of tokens next to them. Propose to the user that the existing variables be exposed as tokens by referencing them from `@theme`:
|
|
34
|
+
|
|
35
|
+
```css
|
|
36
|
+
@theme {
|
|
37
|
+
--color-ink: var(--ink);
|
|
38
|
+
--color-brand: var(--brand);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Tell the user what this changes: the values stay where they are, Tailwind gains classes such as `text-ink` and `bg-brand`, and designsnitch can then tell when code writes those colors by hand. This edits the token CSS, so make the change only after the user agrees.
|
|
43
|
+
|
|
44
|
+
## 2. Find the shared component directory
|
|
45
|
+
|
|
46
|
+
It is where basic UI components such as buttons and inputs live, usually `components/ui`.
|
|
47
|
+
|
|
48
|
+
If the project has no such directory, leave `components` out of the config. The component check is then skipped.
|
|
49
|
+
|
|
50
|
+
## 3. List the raw tags each component replaces
|
|
51
|
+
|
|
52
|
+
Read the files in that directory and find the components whose root element is a raw tag such as `button`, `input`, `select`, `textarea` or `dialog`. Use the exported component names. If several components replace the same tag, list them all.
|
|
53
|
+
|
|
54
|
+
Leave out components that only happen to contain a raw tag deep inside, such as a modal that has a close button. List the components a developer should reach for instead of writing the tag.
|
|
55
|
+
|
|
56
|
+
## 4. Write the file
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"tokens": "./src/app/globals.css",
|
|
61
|
+
"components": {
|
|
62
|
+
"path": "./src/components/ui",
|
|
63
|
+
"replaces": {
|
|
64
|
+
"button": ["SolidButton", "GlassButton", "IconButton"],
|
|
65
|
+
"input": "Input"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Paths are relative to the project root.
|
|
72
|
+
|
|
73
|
+
## 5. Verify
|
|
74
|
+
|
|
75
|
+
```sh
|
|
76
|
+
npx designsnitch check --format json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Look at `designSystem` and `problems` in the result.
|
|
80
|
+
|
|
81
|
+
- `designSystem.colorTokens` should match the number of `--color-*` tokens in the CSS. A 0 means the path is wrong or the tokens are in a format that cannot be read (only hex values and `var()` references to hex values are read).
|
|
82
|
+
- `designSystem.components` should match the number of components you listed.
|
|
83
|
+
- `problems` should be empty.
|
|
84
|
+
- Exit code 2 with a message about a missing path means a path in the config is wrong.
|
|
85
|
+
|
|
86
|
+
Fix the config until these hold. Ignore the violations for now: fixing them is the job of the `designsnitch-check` skill.
|
|
87
|
+
|
|
88
|
+
## 6. Wire the ESLint plugin
|
|
89
|
+
|
|
90
|
+
Follow the `designsnitch-eslint` skill now. It puts the rules into the project's ESLint, and installs ESLint if the project has none. Setup is not finished until this is done: the config file alone does not block anything.
|
|
91
|
+
|
|
92
|
+
## 7. Register the agent hook
|
|
93
|
+
|
|
94
|
+
Follow the `designsnitch-hooks` skill. The hook tells the coding agent about errors right after it edits a file, and keeps it from finishing while errors it can fix remain. The ESLint plugin blocks at lint time. The hook blocks while the code is being written.
|
|
95
|
+
|
|
96
|
+
## 8. Show the user
|
|
97
|
+
|
|
98
|
+
Show the config file and say which CSS file and directory you chose, and why. Give the counts from `designSystem`, the number of errors and warnings the first check found, and what was added to ESLint and to the agent's hooks.
|
|
99
|
+
|
|
100
|
+
If you were unsure about a choice, say so and give the alternative. For example, if two directories both look like the shared component directory, name both and say why you picked one.
|
|
101
|
+
|
|
102
|
+
## What not to do
|
|
103
|
+
|
|
104
|
+
- Do not guess paths or component names you could not find in the repository.
|
|
105
|
+
- Do not edit the token CSS without the user's agreement.
|
|
106
|
+
- Do not change an existing config file without asking.
|
|
107
|
+
- Do not start fixing violations as part of setup.
|