From ace4c58fc7294d94749872ed962fb3b84be8e38c Mon Sep 17 00:00:00 2001 From: Jeremy Thomas Date: Sat, 16 Jun 2018 19:58:58 +0100 Subject: [PATCH] Split plugins --- docs/scripts/.gitignore | 1 + docs/scripts/01-initial.js | 15 +++++ docs/scripts/02-derived.js | 13 +++++ docs/scripts/03-other.js | 13 +++++ .../01-read-initial-variables.js | 7 +-- .../02-read-derived-variables.js | 16 +++--- .../plugins/03-read-other-variables.js | 37 ++++++++++++ docs/scripts/{ => plugins}/utils.js | 57 ++++++++++++++++--- docs/scripts/variables.js | 37 ++++++------ 9 files changed, 156 insertions(+), 40 deletions(-) create mode 100644 docs/scripts/01-initial.js create mode 100644 docs/scripts/02-derived.js create mode 100644 docs/scripts/03-other.js rename docs/scripts/{ => plugins}/01-read-initial-variables.js (85%) rename docs/scripts/{ => plugins}/02-read-derived-variables.js (71%) create mode 100644 docs/scripts/plugins/03-read-other-variables.js rename docs/scripts/{ => plugins}/utils.js (57%) diff --git a/docs/scripts/.gitignore b/docs/scripts/.gitignore index dd0cabdb..2e49dc00 100644 --- a/docs/scripts/.gitignore +++ b/docs/scripts/.gitignore @@ -2,3 +2,4 @@ /build /output +/variables diff --git a/docs/scripts/01-initial.js b/docs/scripts/01-initial.js new file mode 100644 index 00000000..ee513b0f --- /dev/null +++ b/docs/scripts/01-initial.js @@ -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; + }); diff --git a/docs/scripts/02-derived.js b/docs/scripts/02-derived.js new file mode 100644 index 00000000..aa01eafc --- /dev/null +++ b/docs/scripts/02-derived.js @@ -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; + }); diff --git a/docs/scripts/03-other.js b/docs/scripts/03-other.js new file mode 100644 index 00000000..8f3e0d61 --- /dev/null +++ b/docs/scripts/03-other.js @@ -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; + }); diff --git a/docs/scripts/01-read-initial-variables.js b/docs/scripts/plugins/01-read-initial-variables.js similarity index 85% rename from docs/scripts/01-read-initial-variables.js rename to docs/scripts/plugins/01-read-initial-variables.js index bd7c6a48..bb60f15f 100644 --- a/docs/scripts/01-read-initial-variables.js +++ b/docs/scripts/plugins/01-read-initial-variables.js @@ -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); + }); }; } diff --git a/docs/scripts/02-read-derived-variables.js b/docs/scripts/plugins/02-read-derived-variables.js similarity index 71% rename from docs/scripts/02-read-derived-variables.js rename to docs/scripts/plugins/02-read-derived-variables.js index b8f6278d..03b335e9 100644 --- a/docs/scripts/02-read-derived-variables.js +++ b/docs/scripts/plugins/02-read-derived-variables.js @@ -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); + }); }; } diff --git a/docs/scripts/plugins/03-read-other-variables.js b/docs/scripts/plugins/03-read-other-variables.js new file mode 100644 index 00000000..fe83838a --- /dev/null +++ b/docs/scripts/plugins/03-read-other-variables.js @@ -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); + }); + }; +} diff --git a/docs/scripts/utils.js b/docs/scripts/plugins/utils.js similarity index 57% rename from docs/scripts/utils.js rename to docs/scripts/plugins/utils.js index 654eae91..d3c2416f 100644 --- a/docs/scripts/utils.js +++ b/docs/scripts/plugins/utils.js @@ -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; + } + + 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); } } -const output_base = './output/'; - 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; diff --git a/docs/scripts/variables.js b/docs/scripts/variables.js index f378e210..f5def8eb 100644 --- a/docs/scripts/variables.js +++ b/docs/scripts/variables.js @@ -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;