Files
webstudio/packages/css-data/bin/css-to-ws.ts
T
Bogdan Chadkin 78a0fa98c1 refactor: output hyphenated properties from css parser (#4900)
We are going to switch to hyphenated properties in styles. Here
refactored css parser to output hyphenated property instead of camel
case and added camelCaseProperty utility which does the opposite of
hyphenateProperty.
2025-02-22 18:11:57 +07:00

54 lines
1.7 KiB
TypeScript
Executable File

#!/usr/bin/env tsx
import { parseArgs, type ParseArgsConfig } from "node:util";
import * as path from "node:path";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { camelCaseProperty, parseCss } from "../src/parse-css";
const cliOptions = {
allowPositionals: true,
strict: true,
} as const satisfies ParseArgsConfig;
const cliArgs = parseArgs({ args: process.argv.slice(2), ...cliOptions });
if (cliArgs.positionals.length < 2) {
console.error("Please provide the source and destination paths");
process.exit(1);
}
const sourcePath = path.resolve(process.cwd(), cliArgs.positionals[0]);
const destinationPath = path.resolve(process.cwd(), cliArgs.positionals[1]);
const objectGroupBy = <Item>(list: Item[], by: (item: Item) => string) => {
const records: Record<string, Item[]> = {};
for (const item of list) {
const key = by(item);
let list = records[key];
if (list === undefined) {
list = [];
records[key] = list;
}
list.push(item);
}
return records;
};
const css = readFileSync(sourcePath, "utf8");
const parsed = parseCss(css).map(({ property, ...styleDecl }) => ({
selector: styleDecl.selector,
breakpoint: styleDecl.breakpoint,
state: styleDecl.state,
property: camelCaseProperty(property),
value: styleDecl.value,
}));
const records = objectGroupBy(parsed, (item) => item.selector);
mkdirSync(path.dirname(destinationPath), { recursive: true });
const code = `/* eslint-disable */
/* This file was generated by css-to-ws.ts */
export const styles = ${JSON.stringify(records, null, 2)};
`;
writeFileSync(destinationPath, code, "utf8");
console.info("✅ CSS parsed and Webstudio Data generated");