-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Releases the initial (`0.1.0`) version. Provides a basic implementation that emits `.d.css.ts` files with named exports for the class names in the files matching the given glob. Updates README with guidance. Adds `glob` and `css-tree`.
- Loading branch information
Showing
4 changed files
with
279 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env node | ||
|
||
import { existsSync } from "node:fs"; | ||
import { readFile, writeFile } from "node:fs/promises"; | ||
import { join, parse as parsePath } from "node:path"; | ||
|
||
import { parse as parseCss, walk } from "css-tree"; | ||
import { glob } from "glob"; | ||
|
||
await main(process.argv[2]); | ||
|
||
async function main(pattern) { | ||
if (!pattern) { | ||
console.error(`Expected glob pattern`); | ||
process.exit(2); | ||
} | ||
|
||
const files = await glob(pattern); | ||
|
||
const time = new Date().toISOString(); | ||
const results = await Promise.all( | ||
files.map((file) => generateDeclaration(file, time)), | ||
); | ||
|
||
const errors = results.filter(Boolean); | ||
if (errors.length > 0) { | ||
console.error(`Errors encountered`, errors); | ||
process.exit(3); | ||
} | ||
|
||
process.exit(0); | ||
} | ||
|
||
async function generateDeclaration(path, time) { | ||
// Handle case where the file got deleted by the time we got here | ||
if (!existsSync(path)) return; | ||
|
||
const css = await readFile(path, `utf8`); | ||
|
||
let ts = `// Generated from ${path} by css-typed at ${time}\n\n`; | ||
|
||
const ast = parseCss(css, { filename: path }); | ||
walk(ast, (node) => { | ||
if (node.type === `ClassSelector`) { | ||
ts += `export const ${node.name}: string;\n`; | ||
} | ||
}); | ||
|
||
await writeFile(dtsPath(path), ts, `utf8`); | ||
return undefined; | ||
} | ||
|
||
function dtsPath(path) { | ||
const { dir, name, ext } = parsePath(path); | ||
return join(dir, `${name}.d${ext}.ts`); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters