gulp,"终端模式"对于节点应用

时间:2015-09-14 08:19:32

标签: node.js gulp entry-point

我正在开发一个节点应用程序,其主要目的是解析和评估最终用户在浏览器中提供的输入字符串(解释器)。

但是,在开发解释器时必须经常在浏览器和终端之间来回切换,这有点胖。因此,我希望直接从终端获得解释器的入口点。

目前我找到了一种简单的方法(根据节点的文档https://nodejs.org/api/process.html#process_process_stdin):

gulp.task('term', function(){ 
    if(typeof process != 'undefined'){
        (function () {
            var util = require('util');
            var inputString = "";

            console.log("Instruction: provide a string followed by ctrl+d");

            process.stdin.setEncoding("utf8");
            process.stdin.on("readable", function(){
                var chunk = process.stdin.read();
                if (chunk) inputString += chunk;
            });

            process.stdin.on("end", function(){
                // executed on "end" signal (ctrl + d)
                console.log("here we send inputString to interpreter() -> ", inputString);
            });
        })();
    }
});

当运行gulp term gulp将完成然后进入"终端模式"在哪里等待userInput。完成会话(ctrl + d)后,输入将被发送到解释器。

当运行gulp term < inputFile.txt时,inputFile.txt的内容被发送到&#34; end&#34; process.stdin.on的事件,文件的内容立即发送给我的口译员进行评估。

这个场景很好地描述了我的意思&#34;终端模式&#34;。但是,我不确定这是最好的方式。

这里的一个问题是例如&#34;指令行&#34;正在打印之间&#34;开始&#34;和&#34;整理&#34; og the gulp任务:

$gulp term

[10:08:10] Using gulpfile ~/Documents/app/gulpfile.js
[10:08:10] Starting 'term'...
provide a string followed by ctrl+d
[10:08:10] Finished 'term' after 853 μs
// now waiting for input

所以我的问题是,&#34;如何提供一个有效的终端,比如我的节点应用程序的入口点?&#34;,有更多的Gulpish方式吗?提供此类&#34;终端的应用程序的任何示例,如&#34;入口点?

提前感谢您的帮助

0 个答案:

没有答案