Skip to content

Commit

Permalink
Add support for --use option
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Jan 14, 2017
1 parent 207fae1 commit 108c937
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
49 changes: 26 additions & 23 deletions index.js
Expand Up @@ -8,16 +8,7 @@ const globber = require('globby')
const watcher = require('chokidar')

const postcss = require('postcss')
const postcssLoadConfig = require('postcss-load-config')

const postcssrc = () => {
return postcssLoadConfig()
.catch(e => {
// Ignore PostCSS config not found error:
if (e.message.indexOf('No PostCSS Config found') === -1) throw e
else return {plugins: [], options: {}}
})
}
const postcssrc = require('postcss-load-config')

const logo = `
/|\\
Expand Down Expand Up @@ -46,6 +37,7 @@ const argv = require('yargs')
.alias('d', 'dir').describe('d', 'Output Directory')
.alias('r', 'replace').describe('r', 'Replace the input file')
.alias('m', 'map').describe('m', 'Sourcemaps')
.alias('u', 'use').describe('u', 'List of plugins to apply').array('u')
.alias('p', 'parser').describe('p', 'Parser')
.alias('s', 'syntax').describe('s', 'Syntax')
.alias('t', 'stringifier').describe('t', 'Stringifier')
Expand All @@ -59,13 +51,6 @@ let dir = argv.dir
let input = argv._
let output = argv.output

const defaultOptions = {
parser: argv.parser ? require(argv.parser) : undefined,
syntax: argv.syntax ? require(argv.syntax) : undefined,
stringifier: argv.stringifier ? require(argv.stringifier) : undefined,
map: argv.map
}

if (argv.env) process.env.NODE_ENV = argv.env

if (argv.replace) output = input
Expand All @@ -76,7 +61,7 @@ console.warn(chalk.bold.red(logo)) // Use warn to avoid writing to stdout

spinner.text = `Loading Config`
spinner.start()
Promise.all([globber(input), postcssrc()]).then((arr) => {
Promise.all([globber(input), config()]).then((arr) => {
// Until parameter destructuring is supported:
let files = arr[0]
let config = arr[1]
Expand All @@ -97,7 +82,7 @@ Promise.all([globber(input), postcssrc()]).then((arr) => {
.on('change', (file) => {
spinner.text = `Processing ${chalk.green(`${file}`)}`

postcssrc().then((config) => {
config().then((config) => {
return processFile(file, config, watcher)
})
.then(() => {
Expand All @@ -115,13 +100,11 @@ function processFile (file, config, watcher) {
spinner.start()

let options = Object.assign(
{},
defaultOptions,
config.options,
{
from: file,
to: output || path.join(dir, path.basename(file))
}
},
config.options
)

options.to = path.resolve(options.to)
Expand All @@ -145,6 +128,26 @@ function processFile (file, config, watcher) {
})
}

function config () {
if (argv.use || argv.parser || argv.stringifier || argv.syntax) {
return {
plugins: argv.use ? argv.use.map(plugin => require(plugin)) : [],
options: {
parser: argv.parser ? require(argv.parser) : undefined,
syntax: argv.syntax ? require(argv.syntax) : undefined,
stringifier: argv.stringifier ? require(argv.stringifier) : undefined,
map: argv.map
}
}
} else {
return postcssrc()
.catch(err => {
if (err.message.indexOf('No PostCSS Config found') === -1) throw err
else return {plugins: [], options: {}}
})
}
}

function errorHandler (err) {
try {
spinner.fail()
Expand Down
7 changes: 7 additions & 0 deletions test/postcss-cli.js
Expand Up @@ -19,6 +19,13 @@ test('--dir works', async function (t) {
t.is(await read(path.join(outDir, 'a-blue.css')), await read('test/fixtures/a-blue.css'))
})

test('--use works', async function (t) {
var out = tmp('.css')
var { error } = await run(['test/fixtures/imports-a-red.css', '--use', 'postcss-import', '-o', out])
t.ifError(error)
t.is(await read(out), await read('test/fixtures/a-red.css'))
})

test('--parser works', async function (t) {
var out = tmp('.css')
var { error } = await run(['test/fixtures/sugar-white.sss', '--parser', 'sugarss', '-o', out])
Expand Down

0 comments on commit 108c937

Please sign in to comment.