forked from tusen-ai/naive-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const fs = require('fs').promises | ||
const path = require('path') | ||
const { genDts, collectVars } = require('./utils/collect-vars.js') | ||
const { walk } = require('../scripts/utils') | ||
|
||
const srcPath = path.resolve(__dirname, '..', 'src') | ||
|
||
;(async () => { | ||
for await (const p of walk(srcPath)) { | ||
if (p.endsWith('.cssr.ts')) { | ||
const dts = genDts(collectVars(await fs.readFile(p, 'utf-8'))) | ||
console.log(p, dts) | ||
} | ||
} | ||
})() |
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,35 @@ | ||
const pattern = /var\(([^)]+)\)/g | ||
const patternDetail = /var\(([^)]+)\)/ | ||
const commentPattern = /^( *)(\*|(\/\/)|(\/\*))/g | ||
|
||
/** | ||
* Collect css vars | ||
* @param {string} code | ||
*/ | ||
function collectVars (code) { | ||
const vars = new Set() | ||
const lines = code.split('\n') | ||
lines.forEach((line) => { | ||
if (line.match(commentPattern)) return | ||
const result = line.match(pattern) | ||
if (result) { | ||
result.forEach((varExpr) => { | ||
vars.add(varExpr.match(patternDetail)[1]) | ||
}) | ||
} | ||
}) | ||
return Array.from(vars).sort() | ||
} | ||
|
||
/** | ||
* @param {string[]} vars | ||
*/ | ||
function genDts (vars) { | ||
console.log(vars) | ||
return `interface CssVars { | ||
${vars.map((v) => " '" + v + "': string").join('\n')} | ||
}\n` | ||
} | ||
|
||
exports.genDts = genDts | ||
exports.collectVars = collectVars |
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