Skip to content

Instantly share code, notes, and snippets.

@thomasJang
Created November 1, 2018 12:41
Show Gist options
  • Select an option

  • Save thomasJang/92a738a2a3b9bbeb9d4bcff8066abfc8 to your computer and use it in GitHub Desktop.

Select an option

Save thomasJang/92a738a2a3b9bbeb9d4bcff8066abfc8 to your computer and use it in GitHub Desktop.
generate-less-var.ts
const path = require('path');
const fs = require('fs-extra');
const chalk = require('chalk');
const srcPath = '../src/styles/theme.ts';
const srcResoloved = path.resolve(__dirname, srcPath);
const target = path.resolve(__dirname, '../src/styles/less/theme-vars.less');
const buildLessVar = () => {
delete require.cache[srcResoloved];
const theme = require(srcPath);
fs.writeFileSync(
target,
Object.keys(theme.default)
.map(k => `@${k.replace('_', '-')}: ${theme.default[k]};`)
.join('\r\n'),
);
};
// watch file theme.js to less-var
fs.watchFile(srcResoloved, { interval: 1000 }, (event: any, filename: any) => {
try {
buildLessVar();
console.log(chalk.red('update LESS file : ') + target);
} catch (e) {
console.log(chalk.bgRed(e));
}
});
console.log(chalk.yellow.bold(`Watching theme files : `) + srcResoloved);
// build less vars
buildLessVar();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment