From dd2ffec934e95cd1b768dfb33e2432140c7d6703 Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Mon, 28 Aug 2017 12:55:53 -0500 Subject: [PATCH] Allow using a custom config with option to replace instead of default merging --- src/index.js | 88 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 30 deletions(-) diff --git a/src/index.js b/src/index.js index d6c6c4d08..858b8ca48 100755 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ #!/usr/bin/env node import fs from 'fs' +import _ from 'lodash' import path from 'path' import postcss from 'postcss' import defaultConfig from './defaultConfig' @@ -11,38 +12,11 @@ let splitFileName = filename => { return filename.split('.') } -program - .version('0.1.0') - .usage('[options] ') - .option('-c, --config [value]', 'Pass custom configuration') - .parse(process.argv) - -let inputFile = program.args[0] -let outputFile = - program.args[1] || `${splitFileName(program.args[0])[0]}-output.css` - -if (!inputFile) { - console.error('No input file given!') - process.exit(1) -} - -fs.readFile(path.join(program.config), (err, config) => { - if (err) { - console.error(`config file ${program.config} does not exist`) - process.exit(1) - } - - const customConfig = JSON.parse(config.toString()) - - let finalConfig = { - ...defaultConfig, - ...customConfig, - } - +let buildTailwind = config => { console.log('Building Tailwind!') fs.readFile(inputFile, (err, css) => { - postcss([tailwind(finalConfig)]) + postcss([tailwind(config)]) .process(css) .then(result => { fs.writeFileSync(outputFile, result.css) @@ -51,4 +25,58 @@ fs.readFile(path.join(program.config), (err, config) => { }) console.log('Finished building Tailwind!') -}) +} + +let getConfig = (customConfig, merge) => { + if (merge) { + return customConfig + } else { + return _.merge(defaultConfig, customConfig) + } +} + +program + .version('0.1.0') + .usage('[options] ') + .option('-c, --config [path]', 'set config path') + .option( + '-r, --replace', + 'replace the built-in configuration with the provided config file' + ) + .parse(process.argv) + +let inputFile = program.args[0] + +if (!inputFile) { + console.error('No input file given!') + process.exit(1) +} + +let outputFile = + program.args[1] || `${splitFileName(program.args[0])[0]}-output.css` + +if (program.config !== undefined) { + fs.exists(program.config, exists => { + if (!exists) { + console.error(`Config file [${program.config}] does not exist.`) + process.exit(1) + } + + fs.readFile(program.config, (err, config) => { + if (err) { + console.error( + `There was a problem reading config file [${program.config}].` + ) + process.exit(1) + } + + const customConfig = JSON.parse(config.toString()) + + let finalConfig = getConfig(customConfig, program.replace) + + buildTailwind(finalConfig) + }) + }) +} else { + buildTailwind(defaultConfig) +}