Skip to content

Commit 3b2c9cf

Browse files
committed
allow to pipe in data to the CLI
1 parent 0fba679 commit 3b2c9cf

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/cli.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ async function outputFile(file, contents) {
5050
await fs.promises.writeFile(file, contents, 'utf8')
5151
}
5252

53+
function drainStdin() {
54+
return new Promise((resolve, reject) => {
55+
let result = ''
56+
process.stdin.on('data', (chunk) => {
57+
result += chunk
58+
})
59+
process.stdin.on('end', () => resolve(result))
60+
process.stdin.on('error', (err) => reject(err))
61+
})
62+
}
63+
5364
function help({ message, usage, commands, options }) {
5465
let indent = 2
5566

@@ -364,7 +375,7 @@ async function build() {
364375
input = args['--input'] = args['_'][1]
365376
}
366377

367-
if (input && !fs.existsSync((input = path.resolve(input)))) {
378+
if (input && input !== '-' && !fs.existsSync((input = path.resolve(input)))) {
368379
console.error(`Specified input file ${args['--input']} does not exist.`)
369380
process.exit(9)
370381
}
@@ -558,9 +569,21 @@ async function build() {
558569
})
559570
}
560571

561-
let css = input
562-
? fs.readFileSync(path.resolve(input), 'utf8')
563-
: '@tailwind base; @tailwind components; @tailwind utilities'
572+
let css = await (() => {
573+
// Piping in data, let's drain the stdin
574+
if (input === '-') {
575+
return drainStdin()
576+
}
577+
578+
// Input file has been provided
579+
if (input) {
580+
return fs.readFileSync(path.resolve(input), 'utf8')
581+
}
582+
583+
// No input file provided, fallback to default atrules
584+
return '@tailwind base; @tailwind components; @tailwind utilities'
585+
})()
586+
564587
return processCSS(css)
565588
}
566589

@@ -694,9 +717,21 @@ async function build() {
694717
})
695718
}
696719

697-
let css = input
698-
? fs.readFileSync(path.resolve(input), 'utf8')
699-
: '@tailwind base; @tailwind components; @tailwind utilities'
720+
let css = await (() => {
721+
// Piping in data, let's drain the stdin
722+
if (input === '-') {
723+
return drainStdin()
724+
}
725+
726+
// Input file has been provided
727+
if (input) {
728+
return fs.readFileSync(path.resolve(input), 'utf8')
729+
}
730+
731+
// No input file provided, fallback to default atrules
732+
return '@tailwind base; @tailwind components; @tailwind utilities'
733+
})()
734+
700735
let result = await processCSS(css)
701736
env.DEBUG && console.timeEnd('Finished in')
702737
return result

0 commit comments

Comments
 (0)