Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 1x 8x 1x 7x 7x 8x 4x 4x 4x 2x 2x 180x 1x 2x 1x 1x 1x 1x 3x 9x 10x 9x 9x 1x 1x 8x 8x 4x 4x 4x 9x 2x 2x 4x 14x 5x 5x 5x 5x 1x 1x 4x 5x 4x 1x 1x 3x 270x 3x 3x 3x 3x 3x 16x 16x 1x 1x 15x 9x 5x 1x 1x 1x | #!/usr/bin/env node
// SPDX-License-Identifier: MIT
/**
* turbo-themes CLI — site-native theme tooling (#944).
*
* turbo-themes validate-native <file.css|file.json> [--json]
* Validate a native theme file against the token contract.
* Exit 0 when valid, 1 otherwise.
*
* turbo-themes init-native-theme <theme-id> [--out <path>]
* Emit a scaffold CSS file declaring every required token.
*/
import { readFileSync, writeFileSync, existsSync } from 'node:fs';
import {
formatNativeThemeValidation,
REQUIRED_NATIVE_TOKENS,
validateNativeThemeCss,
validateNativeThemeTokens,
} from './validator.js';
const USAGE = `turbo-themes <command>
Commands:
validate-native <file.css|file.json> [--json]
Validate a site-native theme file against the token contract.
init-native-theme <theme-id> [--out <path>] [--force]
Write a scaffold declaring every required token.
Options:
--json Machine-readable report (validate-native)
--out Output path (init-native-theme; default: <theme-id>.css)
--force Overwrite the output file if it exists (init-native-theme)
-h, --help
`;
function readThemeFile(path: string): {
css?: string;
tokens?: Record<string, string>;
error?: string;
} {
if (!existsSync(path)) {
return { error: `file not found: ${path}` };
}
const content = readFileSync(path, 'utf8');
const looksLikeJson = path.endsWith('.json') || content.trim().startsWith('{');
if (looksLikeJson) {
try {
const parsed = JSON.parse(content);
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
const tokens = parsed as Record<string, unknown>;
const nonString = Object.entries(tokens)
.filter(([, v]) => typeof v !== 'string')
.map(([k]) => k);
if (nonString.length > 0) {
return { error: `JSON values must be strings for: ${nonString.join(', ')}` };
}
return { tokens: tokens as Record<string, string> };
}
return { error: 'JSON input must be an object of token name → value' };
} catch (e) {
return { error: `invalid JSON in ${path}: ${(e as Error).message}` };
}
}
return { css: content };
}
function runValidateNative(args: string[]): number {
const asJson = args.includes('--json');
const positional = args.filter((a) => !a.startsWith('--'));
const file = positional[0];
if (!file || positional.length !== 1) {
console.error('usage: turbo-themes validate-native <file.css|file.json> [--json]');
return 2;
}
const { css, tokens, error } = readThemeFile(file);
if (error) {
console.error(`error: ${error}`);
return 2;
}
const result = css !== undefined ? validateNativeThemeCss(css) : validateNativeThemeTokens(tokens!);
if (asJson) {
console.log(JSON.stringify(result, null, 2));
} else {
console.log(formatNativeThemeValidation(result, file));
}
return result.valid ? 0 : 1;
}
function runInitNativeTheme(args: string[]): number {
const positional = args.filter((a) => !a.startsWith('--'));
const outIndex = args.indexOf('--out');
const force = args.includes('--force');
const themeId = positional[0];
if (!themeId || !/^[a-z0-9-]+$/i.test(themeId)) {
console.error('usage: turbo-themes init-native-theme <theme-id> [--out <path>]');
return 2;
}
const out = outIndex >= 0 ? args[outIndex + 1] : `${themeId}.css`;
Iif (!out) {
console.error('usage: turbo-themes init-native-theme <theme-id> [--out <path>]');
return 2;
}
if (!force && existsSync(out)) {
console.error(`error: ${out} already exists - pass --force to overwrite it`);
return 2;
}
const scaffold = `/* ${themeId} — site-native theme scaffold (turbo-themes init-native-theme)
*
* Replace each placeholder with your palette, then validate:
* turbo-themes validate-native ${out}
* Required tokens: ${REQUIRED_NATIVE_TOKENS.length}. Optional tokens
* (spacing/elevation/animation/opacity) may be added later.
*/
[data-theme='${themeId}'] {
${REQUIRED_NATIVE_TOKENS.map((name) => ` ${name}: initial;`).join('\n')}
}
`;
writeFileSync(out, scaffold);
console.log(`✅ scaffold written to ${out} (${REQUIRED_NATIVE_TOKENS.length} tokens)`);
console.log(' every value is `initial` — replace with real colors, then run:');
console.log(` turbo-themes validate-native ${out}`);
return 0;
}
export function runCli(argv: string[] = process.argv.slice(2)): number {
const [command, ...rest] = argv;
if (!command || command === '-h' || command === '--help' || command === 'help') {
console.log(USAGE);
return 0;
}
switch (command) {
case 'validate-native':
return runValidateNative(rest);
case 'init-native-theme':
return runInitNativeTheme(rest);
default:
console.error(`unknown command: ${command}\n`);
console.log(USAGE);
return 2;
}
}
|