Split plugins

This commit is contained in:
Jeremy Thomas 2018-06-16 19:58:58 +01:00
parent 7442c980e5
commit ace4c58fc7
9 changed files with 156 additions and 40 deletions

View File

@ -2,3 +2,4 @@
/build
/output
/variables

View File

@ -0,0 +1,15 @@
const Metalsmith = require('metalsmith');
const filter = require('metalsmith-filter');
const regex_initial = '**/initial-variables.sass';
const initial_plugin = require('./plugins/01-read-initial-variables');
Metalsmith(__dirname)
.source('../../sass')
.destination('./variables')
.clean(true)
.use(filter(regex_initial))
.use(initial_plugin())
.build(function(err) {
if (err) throw err;
});

View File

@ -0,0 +1,13 @@
const Metalsmith = require('metalsmith');
const filter = require('metalsmith-filter');
const regex_derived = '**/derived-variables.sass';
const derived_plugin = require('./plugins/02-read-derived-variables');
Metalsmith(__dirname)
.source('../../sass')
.use(filter(regex_derived))
.use(derived_plugin())
.build(function(err) {
if (err) throw err;
});

13
docs/scripts/03-other.js Normal file
View File

@ -0,0 +1,13 @@
const Metalsmith = require('metalsmith');
const filter = require('metalsmith-filter');
const regex_sass = '**/*.sass';
const other_plugin = require('./plugins/03-read-other-variables');
Metalsmith(__dirname)
.source('../../sass')
.use(filter(regex_sass))
.use(other_plugin())
.build(function(err) {
if (err) throw err;
});

View File

@ -5,6 +5,7 @@ const utils = require('./utils');
function plugin() {
let variables = {
by_name: {},
list: [],
};
return (files, metalsmith, done) => {
@ -13,8 +14,6 @@ function plugin() {
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);
@ -23,8 +22,8 @@ function plugin() {
variables.list.push(variable.name);
}
});
});
utils.writeFile(utils.files.initial_variables, variables);
utils.writeFile(file_path, variables);
});
};
}

View File

@ -6,29 +6,27 @@ 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 => {
let variables = {
by_name: {},
list: [],
};
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);
variable.computed_value = utils.getInitialValue(variable.value, variable.type, initial_variables);
variables.by_name[variable.name] = variable;
variables.list.push(variable.name);
}
});
});
utils.writeFile(utils.files.derived_variables, variables);
utils.writeFile(file_path, variables);
});
};
}

View File

@ -0,0 +1,37 @@
module.exports = plugin;
const utils = require('./utils');
const fs = require('fs');
let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
let derived_variables = JSON.parse(fs.readFileSync(utils.files.derived_variables));
function plugin() {
return (files, metalsmith, done) => {
setImmediate(done);
Object.keys(files).forEach(file_path => {
if (file_path.startsWith('utilities')) {
return;
}
let variables = {
by_name: {},
list: [],
};
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);
variables.by_name[variable.name] = variable;
variables.list.push(variable.name);
}
});
utils.writeFile(file_path, variables);
});
};
}

View File

@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
let utils = {
getVariableType: (value) => {
@ -53,22 +54,23 @@ let utils = {
writeFile: (file_path, data) => {
const json_data = JSON.stringify(data, null, ' ');
const json_file_path = 'variables/' + file_path.replace('.sass', '.json');
utils.ensureDirectoryExistence(json_file_path);
fs.writeFile(file_path, json_data, err => {
fs.writeFile(json_file_path, json_data, err => {
if (err) {
return console.log(err);
}
console.log('The file was saved!');
console.log(`The file ${json_file_path} was saved!`);
});
},
getComputedValue: (value, type, initial_variables) => {
getInitialValue: (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) {
if (second_value.startsWith('$') && second_value in initial_variables.by_name) {
const third_value = initial_variables.by_name[second_value].value;
console.log('third_value', third_value);
@ -79,13 +81,50 @@ let utils = {
}
return value;
}
},
getComputedValue: (value, type, initial_variables, derived_variables) => {
if (value.startsWith('$')) {
let second_value;
if (value in initial_variables.by_name) {
second_value = initial_variables.by_name[value].value;
} else if (value in derived_variables.by_name) {
second_value = derived_variables.by_name[value].value;
}
const output_base = './output/';
if (second_value && second_value.startsWith('$')) {
let third_value;
if (second_value in initial_variables.by_name) {
third_value = initial_variables.by_name[second_value].value;
} else if (second_value in derived_variables.by_name) {
third_value = derived_variables.by_name[second_value].value;
}
return third_value;
}
return second_value;
}
return value;
},
ensureDirectoryExistence: (file_path) => {
var dirname = path.dirname(file_path);
if (fs.existsSync(dirname)) {
return true;
}
utils.ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
}
utils.files = {};
utils.files.initial_variables = `${output_base}initial-variables.json`;
utils.files.derived_variables = `${output_base}derived-variables.json`;
utils.files.initial_variables = `./variables/utilities/initial-variables.json`;
utils.files.derived_variables = `./variables/utilities/derived-variables.json`;
module.exports = utils;

View File

@ -1,25 +1,26 @@
var Metalsmith = require('metalsmith');
var filter = require('metalsmith-filter');
var writemetadata = require('metalsmith-writemetadata');
const Metalsmith = require('metalsmith');
const filter = require('metalsmith-filter');
const writemetadata = require('metalsmith-writemetadata');
// var regex = '**/*-variables.sass';
// var regex = '**/*.sass';
var regex_initial = '**/initial-variables.sass';
var regex_derived = '**/derived-variables.sass';
var initial_plugin = require('./01-read-initial-variables');
var derived_plugin = require('./02-read-derived-variables');
// const regex = '**/*-variables.sass';
// const regex = '**/*.sass';
const regex_initial = '**/initial-variables.sass';
const regex_derived = '**/derived-variables.sass';
const regex_sass = '**/*.sass';
const initial_plugin = require('./01-read-initial-variables');
const derived_plugin = require('./02-read-derived-variables');
// Metalsmith(__dirname)
// .source('../../sass')
// .use(filter(regex_initial))
// .use(initial_plugin())
// .build(function(err) {
// if (err) throw err;
// });
Metalsmith(__dirname)
.source('../../sass')
.use(filter(regex_initial))
.use(initial_plugin())
.build(function(err) {
if (err) throw err;
});
Metalsmith(__dirname)
.source('../../sass')
.use(filter(regex_derived))
.use(filter(regex_sass))
.use(derived_plugin())
.build(function(err) {
if (err) throw err;