Skip to content

Commit 862bce8

Browse files
committed
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
1 parent 14f1ba6 commit 862bce8

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

__tests__/cli.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { spawnSync } from 'child_process'
2+
import fs from 'fs'
3+
import path from 'path'
4+
5+
function runCli(task, options) {
6+
return spawnSync('node', [`${path.join(process.cwd(), 'lib/cli.js')}`, `${task}`, ...options])
7+
}
8+
9+
function pathToFixture(fixture) {
10+
return path.resolve(`${__dirname}/fixtures/${fixture}`)
11+
}
12+
13+
function readFixture(fixture) {
14+
return fs.readFileSync(pathToFixture(fixture), 'utf8')
15+
}
16+
17+
test('stdout only contains processed output', () => {
18+
const expected = readFixture('tailwind-cli-output.css')
19+
const result = runCli('build', [pathToFixture('tailwind-cli-input.css')])
20+
expect(result.stdout.toString()).toEqual(expected)
21+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: green;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: green;
3+
}

src/cli.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ function writeStrategy(options) {
2020
}
2121

2222
function buildTailwind(inputFile, config, write) {
23-
console.log('Building Tailwind!')
23+
console.warn('Building Tailwind!')
2424

2525
const input = fs.readFileSync(inputFile, 'utf8')
2626

2727
return postcss([tailwind(config)])
2828
.process(input, { from: inputFile })
2929
.then(result => {
3030
write(result.css)
31-
console.log('Finished building Tailwind!')
31+
console.warn('Finished building Tailwind!')
3232
})
33-
.catch(error => console.log(error))
33+
.catch(error => console.error(error))
3434
}
3535

3636
const packageJson = require(path.resolve(__dirname, '../package.json'))
@@ -48,7 +48,7 @@ program
4848
}
4949

5050
if (fs.existsSync(destination)) {
51-
console.log(`Destination ${destination} already exists, aborting.`)
51+
console.error(`Destination ${destination} already exists, aborting.`)
5252
process.exit(1)
5353
}
5454

@@ -58,7 +58,7 @@ program
5858
destination,
5959
output.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
6060
)
61-
console.log(`Generated Tailwind config: ${destination}`)
61+
console.warn(`Generated Tailwind config: ${destination}`)
6262
process.exit()
6363
})
6464

0 commit comments

Comments
 (0)