Read derived variables

This commit is contained in:
Jeremy Thomas 2018-06-16 19:01:35 +01:00
parent 87bf26ae2f
commit 7442c980e5
6 changed files with 133 additions and 98 deletions

View File

@ -1,3 +1,4 @@
# Folders # Folders
/build
/output /output

View File

@ -20,13 +20,11 @@ function plugin() {
if (variable != false) { if (variable != false) {
variables.by_name[variable.name] = variable; variables.by_name[variable.name] = variable;
variables[file_name].push(variable.name); variables.list.push(variable.name);
} }
}); });
metalsmith.variables = variables;
utils.writeFile('./output/initial-variables.json', variables);
}); });
utils.writeFile(utils.files.initial_variables, variables);
}; };
} }

View File

@ -0,0 +1,34 @@
module.exports = plugin;
const utils = require('./utils');
const fs = require('fs');
let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
function plugin() {
let variables = {
by_name: {},
};
return (files, metalsmith, done) => {
setImmediate(done);
Object.keys(files).forEach(file_path => {
const {file_name, lines} = utils.getLines(files, file_path);
variables[file_name] = [];
lines.forEach(line => {
const variable = utils.parseLine(line);
if (variable != false) {
variable.computed_value = utils.getComputedValue(variable.value, variable.type, initial_variables);
variables.by_name[variable.name] = variable;
variables.list.push(variable.name);
}
});
});
utils.writeFile(utils.files.derived_variables, variables);
};
}

View File

@ -1,23 +0,0 @@
module.exports = plugin;
var utils = require('./utils');
function plugin() {
return (files, metalsmith, done) => {
setImmediate(done);
Object.keys(files).forEach(file_path => {
const {file_name, lines} = utils.getLines(files, file_path);
lines.forEach(line => {
const variable = utils.parseLine(line);
if (variable != false) {
console.log('variable', variable);
}
});
metalsmith.variables = variables;
});
};
}

View File

@ -1,69 +1,91 @@
const fs = require('fs'); const fs = require('fs');
function getVariableType(value) { let utils = {
if (value.startsWith('hsl')) { getVariableType: (value) => {
return 'color'; if (value.startsWith('hsl')) {
} else if (value.startsWith('$')) { return 'color';
return 'variable'; } else if (value.startsWith('$')) {
} else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') { return 'variable';
return 'font-family'; } else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
} else if (value == 'true' || value == 'false') { return 'font-family';
return 'boolean'; } else if (value == 'true' || value == 'false') {
} else if (value.includes('+')) { return 'boolean';
return 'computed'; } else if (value.includes('+')) {
} else if (value.endsWith('px') || value.endsWith('rem')) { return 'computed';
return 'size'; } else if (value.endsWith('00')) {
} return 'font-weight';
} else if (value.endsWith('px') || value.endsWith('rem')) {
return 'string'; return 'size';
}
function parseLine(line) {
if (line.startsWith('$') && line.endsWith('!default')) {
const colon_index = line.indexOf(':');
const variable_name = line.substring(0, colon_index).trim();
const default_index = line.indexOf('!default');
const variable_value = line.substring(colon_index + 1, default_index).trim();
return variable = {
name: variable_name,
value: variable_value,
type: getVariableType(variable_value),
};
}
return false;
}
function getLines(files, file_path) {
const file = files[file_path];
const slash_index = file_path.lastIndexOf('/');
const dot_index = file_path.lastIndexOf('.');
const file_name = file_path.substring(slash_index + 1, dot_index);
return {
file_name,
lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
}
}
function writeFile(file_path, data) {
const json_data = JSON.stringify(data, null, ' ');
fs.writeFile(file_path, json_data, err => {
if (err) {
return console.log(err);
} }
console.log('The file was saved!'); return 'string';
}); },
parseLine: (line) => {
if (line.startsWith('$') && line.endsWith('!default')) {
const colon_index = line.indexOf(':');
const variable_name = line.substring(0, colon_index).trim();
const default_index = line.indexOf('!default');
const variable_value = line.substring(colon_index + 1, default_index).trim();
return variable = {
name: variable_name,
value: variable_value,
type: utils.getVariableType(variable_value),
};
}
return false;
},
getLines: (files, file_path) => {
const file = files[file_path];
const slash_index = file_path.lastIndexOf('/');
const dot_index = file_path.lastIndexOf('.');
const file_name = file_path.substring(slash_index + 1, dot_index);
return {
file_name,
lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
}
},
writeFile: (file_path, data) => {
const json_data = JSON.stringify(data, null, ' ');
fs.writeFile(file_path, json_data, err => {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
});
},
getComputedValue: (value, type, initial_variables) => {
if (value.startsWith('$') && value in initial_variables.by_name) {
const second_value = initial_variables.by_name[value].value;
console.log('second_value', second_value);
if (second_value.startsWith('$') && second_value in initial_variables) {
const third_value = initial_variables.by_name[second_value].value;
console.log('third_value', third_value);
return third_value;
}
return second_value;
}
return value;
}
} }
let plugin = {}; const output_base = './output/';
plugin.getVariableType = getVariableType;
plugin.getLines = getLines;
plugin.parseLine = parseLine;
plugin.writeFile = writeFile;
module.exports = plugin; utils.files = {};
utils.files.initial_variables = `${output_base}initial-variables.json`;
utils.files.derived_variables = `${output_base}derived-variables.json`;
module.exports = utils;

View File

@ -6,18 +6,21 @@ var writemetadata = require('metalsmith-writemetadata');
// var regex = '**/*.sass'; // var regex = '**/*.sass';
var regex_initial = '**/initial-variables.sass'; var regex_initial = '**/initial-variables.sass';
var regex_derived = '**/derived-variables.sass'; var regex_derived = '**/derived-variables.sass';
var initial_plugin = require('./read-initial-variables'); var initial_plugin = require('./01-read-initial-variables');
var derived_plugin = require('./read-derived-variables'); var derived_plugin = require('./02-read-derived-variables');
Metalsmith(__dirname) Metalsmith(__dirname)
.source('../../sass/utilities') .source('../../sass')
// .destination('./output')
// .clean(true)
.use(filter(regex_initial)) .use(filter(regex_initial))
.use(initial_plugin()) .use(initial_plugin())
// .use(filter(regex_derived)) .build(function(err) {
// .use(derived_plugin()) if (err) throw err;
// .use(writemetadata()) });
Metalsmith(__dirname)
.source('../../sass')
.use(filter(regex_derived))
.use(derived_plugin())
.build(function(err) { .build(function(err) {
if (err) throw err; if (err) throw err;
}); });