TypeScript产生错误

时间:2018-07-18 18:24:38

标签: node.js typescript async-await

我正在将一些bash脚本移植到TypeScript。它们大多数是对系统工具的顺序调用,例如curl,tar,mkdir等。

这可能是新手错误,但需要帮助来找出此一文件TypeScript代码中的spawn / exec调用出了什么问题:

#!/sbin/ts-node

import * as child from 'child_process';
const { promisify } = require('util');

const exec = promisify(child.exec);

const basePkgDir = process.env.HOME + '/cross/pkg';
const baseTmpDir = process.env.HOME + '/cross/tmp';

class Formula {
    private pkgDir: string;
    private tmpDir: string;

    constructor(public name: string, public version: string, public url: string) {
        this.url = url.replace(/\${pkgver}/gi, version)

        const pkgName = name + '-' + version;
        this.pkgDir = basePkgDir + '/' + pkgName;
        this.tmpDir = basePkgDir + '/' + pkgName;
    }

    private async run(cmd: string) {
        await exec(cmd, {cwd: this.tmpDir}, (error: Error, stdout: string, stderr: string) => {
            console.log(stdout);    
            if (error)
                console.log(error);  
            if (stderr)
                console.log(stderr)
        });
    }

    private async createDirs() {
        this.run(`mkdir -p ${this.pkgDir}`);
        this.run(`mkdir -p ${this.tmpDir}`);
    }

    private async download() {
        this.run(`curl -LJO ${this.url}`);
    }

    public async process() {
        await this.createDirs();
        await this.download();
    }
}


async function main() {
    const xz = new Formula('xz', '5.2.3', 'https://github.com/xz-mirror/xz/archive/v${pkgver}.tar.gz');
    await xz.process();
}

main().then(
    text => {
        console.log('Done.');
    },
    err => {
        // Deal with the fact the chain failed
        console.log(err)
    }
);

运行脚本会出现以下错误:

{ Error: spawn /bin/sh ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:229:19)
    at onErrorNT (internal/child_process.js:406:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at Object.<anonymous> (/usr/lib/node_modules/ts-node/src/bin.ts:147:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn /bin/sh',
  path: '/bin/sh',
  spawnargs: [ '-c', 'mkdir -p /home/alex/cross/pkg/xz-5.2.3' ],
  cmd: 'mkdir -p /home/alex/cross/pkg/xz-5.2.3' }

{ Error: spawn /bin/sh ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:229:19)
    at onErrorNT (internal/child_process.js:406:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at Object.<anonymous> (/usr/lib/node_modules/ts-node/src/bin.ts:147:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn /bin/sh',
  path: '/bin/sh',
  spawnargs: [ '-c', 'mkdir -p /home/alex/cross/pkg/xz-5.2.3' ],
  cmd: 'mkdir -p /home/alex/cross/pkg/xz-5.2.3' }
Done.

{ Error: spawn /bin/sh ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:229:19)
    at onErrorNT (internal/child_process.js:406:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at Object.<anonymous> (/usr/lib/node_modules/ts-node/src/bin.ts:147:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn /bin/sh',
  path: '/bin/sh',
  spawnargs:
   [ '-c',
     'curl -LJO https://github.com/xz-mirror/xz/archive/v5.2.3.tar.gz' ],
  cmd:
   'curl -LJO https://github.com/xz-mirror/xz/archive/v5.2.3.tar.gz' }

我怀疑这里使用async/awaitpromisify的方式有问题。

2 个答案:

答案 0 :(得分:1)

结果是,由于您的cwd不存在,导致了错误。 只需更改为{cwd: '/'}即可使程序在createDirs()处成功。

另请参阅https://github.com/nodejs/node/issues/9644#issuecomment-281613121


我进行了以下更改以使其起作用:

1)更改了shebang行#!/usr/bin/env ts-node
2)tsconfig.json

{
  "compilerOptions": {
    "lib": [ "es2015" ]
  }
}

答案 1 :(得分:0)

知道了,原因是{cwd: this.tmpDir}在命令运行时tmpDir不存在。

如果在异步/等待,执行/生成方面还有其他潜在的错误或建议,请射击。