当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Readable.setEncoding方法代码示例

本文整理汇总了TypeScript中stream.Readable.setEncoding方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Readable.setEncoding方法的具体用法?TypeScript Readable.setEncoding怎么用?TypeScript Readable.setEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stream.Readable的用法示例。


在下文中一共展示了Readable.setEncoding方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: run

 run(): void {
     let cmd = this.taskExecParams.cmd;
     let stdin = this.taskExecParams.stdin;
     let instream: stream.Readable = null;
     let pid:number = null;
     let stdout = '';
     let stderr = '';
     let raisedError = "";
     if (stdin && stdin.length > 0) {
         if (stdin.length >= 1 && stdin.substr(0,1) === '@') {   // stdin string begins with '@' => a file path
             let stdinFile = stdin.substr(1);
             instream = fs.createReadStream(stdinFile, {encoding: 'utf8'});
         } else {
             instream = new stream.Readable();
             instream.setEncoding("utf8");
             instream.push(stdin);
             instream.push(null);
         }
     }
     if (instream) {
         instream.on("error", (err: any) => {    // stdin stream has some kind of error (maybe input file does not exist)
             if (err.syscall && err.path)
                 raisedError = "error " + err.syscall + " " + err.path;
             else
                 raisedError = JSON.stringify(err);
             treeKill(pid, 'SIGKILL');   // kill the child process tree
         });
     }
     let env: any;
     if (this.taskExecParams.envJSON) {
         try {env = JSON.parse(this.taskExecParams.envJSON);}catch(e) {}
     }
     let child = exec(cmd, {maxBuffer: 20000 * 1024, env});
     if (instream && child.stdin) instream.pipe(child.stdin);
     pid = child.pid;
     this.emit('started', pid);
     child.stdout.setEncoding('utf8');
     child.stderr.setEncoding('utf8');
     child.stdout.on('data', (data:string) => {
         stdout += data;
     });
     child.stderr.on('data', (data:string) => {
         stderr += data;
     });
     child.on('close', (code: number, signal: string) => {
         let result: ITaskExecResult = {
             pid
             ,retCode: code
             ,stdout: (stdout.length > 0 ? stdout : null)
             ,stderr: (stderr.length > 0 ? stderr : (raisedError ? raisedError : null))
         };
         this.emit('finished', result);
     });
 }
开发者ID:wchang28,项目名称:node-grid-2,代码行数:54,代码来源:taskRunner.ts

示例2: test

test('rewrites /node_modules references, 1 dir nested', async (t) => {
  const filePath = '/demo/index.html';
  const beforeStream = new Readable();
  beforeStream.push('<script src="../node_modules/other-package/file.html">');
  beforeStream.push(null);
  beforeStream.setEncoding('utf8');

  const expected = '<script src="/other-package/file.html">';

  const actualStream = beforeStream.pipe(new HTMLRewriter({}, filePath));
  t.is(await getStream(actualStream), expected);
});
开发者ID:customelements,项目名称:v2,代码行数:12,代码来源:html-rewriter-test.ts

示例3: Promise

 return new Promise(resolve => {
   stdin
     .setEncoding('utf8')
     .on('readable', () => {
       let current
       while ((current = stdin.read())) {
         out += current
       }
     })
     .on('end', () => resolve(out.trim()))
   setTimeout(() => {
     if (!out.trim()) {
       resolve(out.trim())
     }
   }, 1000)
 })
开发者ID:jaredallard,项目名称:nexe,代码行数:16,代码来源:bundle.ts


注:本文中的stream.Readable.setEncoding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。