add test harness for cli.js and stdout bugfix

- when used on the CLI, lib/cli.js was writing the output of log
statements to STDOUT; this caused consumers of the CLI who wanted to
pipe the processed output (css) to other unix utilities to end up with
invalid CSS due to the log messages appearing at the start and end of
the files

- this commit fixes this by replacing `console.log` with `console.warn`
and `console.error`, which both write output to STDERR
This commit is contained in:
David Mosher 2018-05-03 13:42:45 -04:00
parent 14f1ba6cb9
commit 862bce837e
4 changed files with 32 additions and 5 deletions

21
__tests__/cli.test.js Normal file
View File

@ -0,0 +1,21 @@
import { spawnSync } from 'child_process'
import fs from 'fs'
import path from 'path'
function runCli(task, options) {
return spawnSync('node', [`${path.join(process.cwd(), 'lib/cli.js')}`, `${task}`, ...options])
}
function pathToFixture(fixture) {
return path.resolve(`${__dirname}/fixtures/${fixture}`)
}
function readFixture(fixture) {
return fs.readFileSync(pathToFixture(fixture), 'utf8')
}
test('stdout only contains processed output', () => {
const expected = readFixture('tailwind-cli-output.css')
const result = runCli('build', [pathToFixture('tailwind-cli-input.css')])
expect(result.stdout.toString()).toEqual(expected)
})

View File

@ -0,0 +1,3 @@
body {
color: green;
}

View File

@ -0,0 +1,3 @@
body {
color: green;
}

View File

@ -20,7 +20,7 @@ function writeStrategy(options) {
}
function buildTailwind(inputFile, config, write) {
console.log('Building Tailwind!')
console.warn('Building Tailwind!')
const input = fs.readFileSync(inputFile, 'utf8')
@ -28,9 +28,9 @@ function buildTailwind(inputFile, config, write) {
.process(input, { from: inputFile })
.then(result => {
write(result.css)
console.log('Finished building Tailwind!')
console.warn('Finished building Tailwind!')
})
.catch(error => console.log(error))
.catch(error => console.error(error))
}
const packageJson = require(path.resolve(__dirname, '../package.json'))
@ -48,7 +48,7 @@ program
}
if (fs.existsSync(destination)) {
console.log(`Destination ${destination} already exists, aborting.`)
console.error(`Destination ${destination} already exists, aborting.`)
process.exit(1)
}
@ -58,7 +58,7 @@ program
destination,
output.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
)
console.log(`Generated Tailwind config: ${destination}`)
console.warn(`Generated Tailwind config: ${destination}`)
process.exit()
})