Skip to content

Commit

Permalink
allow to pipe in data to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Jan 4, 2022
1 parent 7fae1ad commit b042956
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/cli.js
Expand Up @@ -50,6 +50,21 @@ async function outputFile(file, contents) {
await fs.promises.writeFile(file, contents, 'utf8')
}

function drainStdin() {
return new Promise((resolve, reject) => {
let result = ''
process.stdin.on('readable', () => {
let chunk
while ((chunk = process.stdin.read()) !== null) {
result += chunk
}
})

process.stdin.on('end', () => resolve(result))
process.stdin.on('error', (err) => reject(err))
})
}

function help({ message, usage, commands, options }) {
let indent = 2

Expand Down Expand Up @@ -558,9 +573,21 @@ async function build() {
})
}

let css = input
? fs.readFileSync(path.resolve(input), 'utf8')
: '@tailwind base; @tailwind components; @tailwind utilities'
let css = await (async () => {
// Input file has been provided
if (input) {
fs.readFileSync(path.resolve(input), 'utf8')
}

// Piping in data, let's drain the stdin
if (!process.stdin.isTTY) {
return await drainStdin()
}

// No input file provided, fallback to default atrules
return '@tailwind base; @tailwind components; @tailwind utilities'
})()

return processCSS(css)
}

Expand Down Expand Up @@ -694,9 +721,21 @@ async function build() {
})
}

let css = input
? fs.readFileSync(path.resolve(input), 'utf8')
: '@tailwind base; @tailwind components; @tailwind utilities'
let css = await (async () => {
// Input file has been provided
if (input) {
fs.readFileSync(path.resolve(input), 'utf8')
}

// Piping in data, let's drain the stdin
if (!process.stdin.isTTY) {
return await drainStdin()
}

// No input file provided, fallback to default atrules
return '@tailwind base; @tailwind components; @tailwind utilities'
})()

let result = await processCSS(css)
env.DEBUG && console.timeEnd('Finished in')
return result
Expand Down

0 comments on commit b042956

Please sign in to comment.