Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement stdin input with optional "-" as the file name. (#3290)
* Implement stdin input with optional "-" as the file name. Set default output format to "es" to save CLI typing for the most common case. closes #1440 closes #3276 * * Test if we can delete process.stdin on CI * Only allow '-' to use stdin with the JS API * Use locally installed shx * Add test for stdin read error Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
- Loading branch information
1 parent
5e9a135
commit 61b3129
Showing
26 changed files
with
251 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export const stdinName = '-'; | ||
|
||
let stdinResult: string | Error | null = null; | ||
const pending: { reject: Function; resolve: Function }[] = []; | ||
|
||
export function readStdin() { | ||
return new Promise<string>((resolve, reject) => { | ||
if (typeof process == 'undefined' || typeof process.stdin == 'undefined') { | ||
reject(new Error('stdin input is invalid in browser')); | ||
return; | ||
} | ||
pending.push({ resolve, reject }); | ||
processPending(); | ||
if (pending.length === 1) { | ||
// stdin is read once - all callers will get the same result | ||
|
||
const chunks: Buffer[] = []; | ||
process.stdin.setEncoding('utf8'); | ||
process.stdin | ||
.on('data', chunk => { | ||
if (stdinResult === null) { | ||
chunks.push(chunk); | ||
} | ||
}) | ||
.on('end', () => { | ||
if (stdinResult === null) { | ||
stdinResult = chunks.join(''); | ||
chunks.length = 0; | ||
} | ||
processPending(); | ||
}) | ||
.on('error', err => { | ||
if (stdinResult === null) { | ||
stdinResult = err instanceof Error ? err : new Error(err); | ||
chunks.length = 0; | ||
} | ||
processPending(); | ||
}); | ||
process.stdin.resume(); | ||
} | ||
}); | ||
|
||
function processPending() { | ||
if (stdinResult !== null) { | ||
for (let it; (it = pending.shift()); ) { | ||
if (typeof stdinResult == 'string') { | ||
it.resolve(stdinResult); | ||
} else { | ||
it.reject(stdinResult); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'uses stdin in multiple targets', | ||
command: `shx echo "import {PRINT as p} from './a'; import C from './b'; 0 && fail() || p(C); export {C as value, p as print}" | rollup -c` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
const PRINT = x => console.log(x); | ||
|
||
var C = 123; | ||
|
||
PRINT(C); | ||
|
||
exports.print = PRINT; | ||
exports.value = C; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const PRINT = x => console.log(x); | ||
|
||
var C = 123; | ||
|
||
PRINT(C); | ||
|
||
export { PRINT as print, C as value }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const PRINT = x => console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 123; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default [ | ||
{ | ||
input: '-', | ||
output: { | ||
file: '_actual/cjs.js', | ||
format: 'cjs' | ||
} | ||
}, | ||
{ | ||
input: '-', | ||
output: { | ||
file: '_actual/es.js', | ||
format: 'es' | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'stdin input with no dash on CLI', | ||
command: `shx mkdir -p _actual && shx echo "0 && fail() || console.log('PASS');" | rollup > _actual/out.js` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('PASS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'stdin input of code that imports a copy of itself', | ||
command: `shx mkdir -p _actual && shx cat input.txt | rollup -f cjs --silent > _actual/out.js` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
let b = 2; | ||
var a = 4; | ||
|
||
console.log(a, b); | ||
|
||
exports.b = b; | ||
exports.default = a; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import a from "-"; | ||
import { b as c } from "-"; | ||
|
||
export let b = 2; | ||
export default 4; | ||
|
||
if (a > c) | ||
console.log(a, c); | ||
else | ||
console.log(c, a); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
description: 'stdin input with dash on CLI', | ||
command: `shx mkdir -p _actual && shx echo "0 && fail() || console.log('PASS');" | rollup - > _actual/out.js` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('PASS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const savedStdin = process.stdin; | ||
|
||
module.exports = { | ||
description: 'stdin reads should fail if process.stdin not present (as in a browser)', | ||
options: { | ||
input: '-' | ||
}, | ||
before() { | ||
delete process.stdin; | ||
}, | ||
after() { | ||
process.stdin = savedStdin; | ||
}, | ||
error: { | ||
// TODO we probably want a better error code here as this one is confusing | ||
code: 'PLUGIN_ERROR', | ||
hook: 'load', | ||
// TODO the error message does not need to refer to browsers as there are other possible scenarios | ||
message: 'Could not load -: stdin input is invalid in browser', | ||
plugin: 'Rollup Core', | ||
watchFiles: ['-'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
assert.equal( 1, 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { Readable } = require('stream'); | ||
const savedStdin = process.stdin; | ||
|
||
module.exports = { | ||
description: 'stdin reads should fail if process.stdin not present (as in a browser)', | ||
options: { | ||
input: '-' | ||
}, | ||
before() { | ||
delete process.stdin; | ||
process.stdin = new Readable({ | ||
encoding: 'utf8', | ||
read() { | ||
const error = new Error('Stream is broken.'); | ||
return this.destroy ? this.destroy(error) : this.emit('error', error); | ||
} | ||
}); | ||
}, | ||
after() { | ||
process.stdin = savedStdin; | ||
}, | ||
error: { | ||
code: 'PLUGIN_ERROR', | ||
hook: 'load', | ||
message: 'Could not load -: Stream is broken.', | ||
plugin: 'Rollup Core', | ||
watchFiles: ['-'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
assert.equal( 1, 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = { | ||
description: 'throws when using the "watch" option with stdin "-"', | ||
options: { | ||
input: '-', | ||
watch: true | ||
}, | ||
error: { | ||
// TODO add an error code | ||
message: 'watch mode is incompatible with stdin input' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
assert.equal( 1, 1 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters