Skip to content

Commit

Permalink
build: add collect vars script
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Feb 3, 2021
1 parent d659a42 commit da0c8f7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
15 changes: 15 additions & 0 deletions build/gen-css-vars-dts.js
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)
}
}
})()
35 changes: 35 additions & 0 deletions build/utils/collect-vars.js
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
2 changes: 1 addition & 1 deletion scripts/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require('fs').promises
const path = require('path')

exports.walk = async function * walk (dir) {
Expand Down

0 comments on commit da0c8f7

Please sign in to comment.