-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
51 lines (40 loc) · 1.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const sassVarsToJSON = require('sass-vars-to-json');
const camelCase = require('camelcase');
const argv = process.argv.slice(2);
const fs = require('fs');
async function generateJS() {
const vars = await sassVarsToJSON('./src/assets/scss/_color-variables.scss');
if (!vars) {
console.log('SCSS variables not generated');
return;
}
const colors = convertKeysToCamelCase(vars);
const warningMessage = '/* eslint-disable */\n\r/** File generated using gulp task: Please do not edit manually */\n\n';
const variableName = 'const themeColors = ';
const json = JSON.stringify(colors, null, 4) + ';';
const destinationPath = (argv[0]) ? argv[0] : './src/assets/js/app/theme-colors.js';
fs.writeFile(destinationPath, `${warningMessage} ${variableName} ${json}`, function (err) {
if (err) throw err;
console.log('Generated new colors file !');
});
}
function convertKeysToCamelCase(vars) {
const newVar = {};
for (let [key, value] of Object.entries(vars)) {
if (typeof value === 'object') {
value = convertKeysToCamelCase(value);
}
if (key[0] == '$')
key = key.substring(1);
newVar[camelCase(key)] = value;
}
return newVar;
}
async function init () {
try {
return await generateJS();
} catch (err) {
console.err(err);
}
}
return init();