import path from 'node:path'; import fs from 'fs-extra'; import { camelCase } from 'lodash-es'; export const variables = function (src: string, common = {}) { const map: any = Object.assign({}, common || {}); const normalize = path.posix.normalize(src); const dir = path.isAbsolute(src) ? normalize : path.posix.join(__dirname, '..', normalize); const text = fs.readFileSync(dir, 'utf-8'); text.split('\n').forEach(function (row: any) { if (row.includes('import')) { const value = row .replace('@import', '') .replace('(reference)', '') .replace(/["';]/g, '') .trim(); const parent = path.dirname(dir); const file = path.normalize(path.resolve(parent, value)); const data = variables(file, common); Object.assign(map, data); } else if (/@/.test(row) && /:/.test(row) && /;/.test(row)) { const [, _key] = row.match(/@([\w-]+)/); const key = camelCase(_key); if (key in map) { return; } const value = row.slice(row.lastIndexOf(':') + 1, row.lastIndexOf(';')).replace(/\s|@/g, ''); if (value in map) { map[key] = map[value]; } else { map[key] = value; } } }); return map; };