Skip to content

Commit

Permalink
Improve tz normalize error message
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Dec 15, 2024
1 parent d54632a commit bcfc934
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-planets-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@terrazzo/cli": patch
"@terrazzo/parser": patch
"@terrazzo/token-tools": patch
---

Improve error message for tz normalize
3 changes: 2 additions & 1 deletion packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const { values: flags, positionals } = parseArgs({
options: {
config: { type: 'string', short: 'c' },
out: { type: 'string', short: 'o' },
output: { type: 'string' },
help: { type: 'boolean' },
silent: { type: 'boolean' },
quiet: { type: 'boolean' }, // secret alias for --silent because I can’t remember it
Expand Down Expand Up @@ -92,7 +93,7 @@ export default async function main() {

// normalize
if (cmd === 'normalize') {
await normalizeCmd(positionals[1], { logger, output: flags.out });
await normalizeCmd(positionals[1], { logger, output: flags.out ?? flags.output });
process.exit(0);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function findMember(name: string) {
export async function normalizeCmd(filename: string, { logger, output }: NormalizeOptions) {
try {
if (!filename) {
logger.error({ message: 'Expected input: `tz normalize input.json -o output.json`' });
logger.error({ message: 'Expected input: `tz normalize <tokens.json> -o output.json`' });
return;
}
const sourceLoc = new URL(filename, cwd);
Expand Down
17 changes: 17 additions & 0 deletions packages/cli/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@ const cmd = '../../../bin/cli.js';

describe('tz normalize', () => {
test('basic', async () => {
const cwd = new URL('./fixtures/normalize/', import.meta.url);
await execa(cmd, ['normalize', 'input.json', '--output', 'actual.json'], { cwd });
await expect(fs.readFileSync(new URL('./actual.json', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./want.json', cwd)),
);
});

test('basic (shortcut)', async () => {
const cwd = new URL('./fixtures/normalize/', import.meta.url);
await execa(cmd, ['normalize', 'input.json', '-o', 'actual.json'], { cwd });
await expect(fs.readFileSync(new URL('./actual.json', cwd), 'utf8')).toMatchFileSnapshot(
fileURLToPath(new URL('./want.json', cwd)),
);
});

test('missing input', async () => {
const cwd = new URL('./fixtures/normalize/', import.meta.url);
await expect(execa(cmd, ['normalize', '--output', 'actual.json'], { cwd })).rejects.toThrow(
`Command failed with exit code 1: ../../../bin/cli.js normalize --output actual.json
✗ Expected input: \`tz normalize input.json -o output.json\``,
);
});
});
2 changes: 1 addition & 1 deletion www/src/pages/docs/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ npx tz check [filename]
The DTCG format has gone through many iterations over the years. Terrazzo lenient in the inputs it allows, such as allowing legacy sRGB hex colors without throwing a parsing error. To quickly update your tokens to the latest version of the format, run the following command:

```sh
tz normalize tokens.json -o normalized.json
tz normalize <input.json> -o normalized.json
```

This will keep your tokens 100% as-authored, but will upgrade older DTCG files to the updated format and will safely fix minor issues. But just as a safety precaution, it requires saving to a new location just so you can review the changes before committing them.
Expand Down

0 comments on commit bcfc934

Please sign in to comment.