25
25
var resolve = require ( 'path' ) . resolve ;
26
26
var readFileSync = require ( '@stdlib/fs/read-file' ) . sync ;
27
27
var CLI = require ( '@stdlib/tools/cli' ) ;
28
+ var stdin = require ( '@stdlib/process/read-stdin' ) ;
28
29
var REPL = require ( '@stdlib/repl' ) ;
29
30
var lowercase = require ( '@stdlib/string/lowercase' ) ;
30
31
var Presentation = require ( './../lib' ) ; // eslint-disable-line stdlib/no-redeclare
@@ -36,6 +37,7 @@ var Presentation = require( './../lib' ); // eslint-disable-line stdlib/no-redec
36
37
* Main execution sequence.
37
38
*
38
39
* @private
40
+ * @returns {void }
39
41
*/
40
42
function main ( ) {
41
43
var flags ;
@@ -84,6 +86,8 @@ function main() {
84
86
if ( flags . timeout ) {
85
87
opts1 . timeout = parseInt ( flags . timeout , 10 ) ;
86
88
}
89
+ // Create a new REPL instance:
90
+ repl = new REPL ( opts1 ) ;
87
91
88
92
// Handle presentation options...
89
93
opts2 = { } ;
@@ -121,11 +125,12 @@ function main() {
121
125
if ( flags . autoclear === false ) {
122
126
opts2 . autoClear = flags . autoclear ;
123
127
}
128
+ // Check if we are receiving data from `stdin`...
129
+ if ( ! process . stdin . isTTY ) {
130
+ return stdin ( onRead ) ;
131
+ }
124
132
opts2 . load = args [ 0 ] ;
125
133
126
- // Create a new REPL instance:
127
- repl = new REPL ( opts1 ) ;
128
-
129
134
// Create a new REPL presentation:
130
135
pres = new Presentation ( repl , opts2 ) ;
131
136
@@ -146,6 +151,33 @@ function main() {
146
151
function onChange ( ) {
147
152
pres . show ( ) ;
148
153
}
154
+
155
+ /**
156
+ * Callback invoked upon reading from `stdin`.
157
+ *
158
+ * @private
159
+ * @param {(Error|null) } error - error object
160
+ * @param {Buffer } text - text
161
+ * @returns {void }
162
+ */
163
+ function onRead ( error , text ) {
164
+ var pres ;
165
+ if ( error ) {
166
+ process . exitCode = 1 ;
167
+ return console . error ( 'Error: %s' , error . message ) ; // eslint-disable-line no-console
168
+ }
169
+ // Create a new REPL presentation:
170
+ pres = new Presentation ( text . toString ( ) , repl , opts2 ) ;
171
+
172
+ // Render the first slide:
173
+ pres . show ( ) ;
174
+
175
+ // Check whether to begin watching the source presentation file...
176
+ if ( flags . watch ) {
177
+ pres . on ( 'change' , onChange ) ;
178
+ pres . watch ( ) ;
179
+ }
180
+ }
149
181
}
150
182
151
183
main ( ) ;
0 commit comments