Update variables include template

This commit is contained in:
Jeremy Thomas
2018-06-16 20:47:52 +01:00
parent ace4c58fc7
commit 1ff1edcf53
51 changed files with 2420 additions and 1498 deletions

View File

@@ -3,16 +3,17 @@ module.exports = plugin;
const utils = require('./utils');
function plugin() {
let variables = {
by_name: {},
list: [],
};
return (files, metalsmith, done) => {
setImmediate(done);
Object.keys(files).forEach(file_path => {
const {file_name, lines} = utils.getLines(files, file_path);
let variables = {
by_name: {},
list: [],
file_path,
};
lines.forEach(line => {
const variable = utils.parseLine(line);

View File

@@ -10,11 +10,12 @@ function plugin() {
setImmediate(done);
Object.keys(files).forEach(file_path => {
const {file_name, lines} = utils.getLines(files, file_path);
let variables = {
by_name: {},
list: [],
file_path,
};
const {file_name, lines} = utils.getLines(files, file_path);
lines.forEach(line => {
const variable = utils.parseLine(line);

View File

@@ -15,17 +15,24 @@ function plugin() {
return;
}
const {file_name, lines} = utils.getLines(files, file_path);
let variables = {
by_name: {},
list: [],
file_path,
};
const {file_name, lines} = utils.getLines(files, file_path);
lines.forEach(line => {
const variable = utils.parseLine(line);
if (variable != false) {
variable.computed_value = utils.getComputedValue(variable.value, variable.type, initial_variables, derived_variables);
const computed_data = utils.getComputedData(variable.value, variable.type, initial_variables, derived_variables);
if (Object.keys(computed_data).length > 0) {
variable.computed_type = computed_data.computed_type;
variable.computed_value = computed_data.computed_value;
}
variables.by_name[variable.name] = variable;
variables.list.push(variable.name);
}

View File

@@ -1,8 +1,14 @@
const fs = require('fs');
const path = require('path');
const base_path = '../_data/variables/';
let utils = {
getVariableType: (value) => {
if (!value) {
return 'string';
}
if (value.startsWith('hsl')) {
return 'color';
} else if (value.startsWith('$')) {
@@ -15,8 +21,12 @@ let utils = {
return 'computed';
} else if (value.endsWith('00')) {
return 'font-weight';
} else if (value.endsWith('px') || value.endsWith('rem')) {
} else if (value.endsWith('px') || value.endsWith('em')) {
return 'size';
} else if (value.includes('$')) {
return 'compound';
} else if (value.startsWith('findColorInvert')) {
return 'function';
}
return 'string';
@@ -54,7 +64,8 @@ let utils = {
writeFile: (file_path, data) => {
const json_data = JSON.stringify(data, null, ' ');
const json_file_path = 'variables/' + file_path.replace('.sass', '.json');
const json_file_path = base_path + file_path.replace('.sass', '.json');
utils.ensureDirectoryExistence(json_file_path);
fs.writeFile(json_file_path, json_data, err => {
@@ -83,7 +94,9 @@ let utils = {
return value;
},
getComputedValue: (value, type, initial_variables, derived_variables) => {
getComputedData: (value, type, initial_variables, derived_variables) => {
let computed_value = '';
if (value.startsWith('$')) {
let second_value;
@@ -102,13 +115,22 @@ let utils = {
third_value = derived_variables.by_name[second_value].value;
}
return third_value;
computed_value = third_value;
} else {
computed_value = second_value;
}
return second_value;
}
return value;
if (computed_value != '') {
const computed_type = utils.getVariableType(computed_value);
return {
computed_type,
computed_value,
}
}
return {};
},
ensureDirectoryExistence: (file_path) => {
@@ -124,7 +146,7 @@ let utils = {
}
utils.files = {};
utils.files.initial_variables = `./variables/utilities/initial-variables.json`;
utils.files.derived_variables = `./variables/utilities/derived-variables.json`;
utils.files.initial_variables = `${base_path}utilities/initial-variables.json`;
utils.files.derived_variables = `${base_path}utilities/derived-variables.json`;
module.exports = utils;