export async function exec()

in src/util.ts [23:58]


export async function exec(moduleName: string, args: string[] = [], options: SpawnOptions = { }) {
  return new Promise<void>((ok, fail) => {

    const opts: SpawnOptions = {
      ...options,
      stdio: ['inherit', 'pipe', 'pipe'],
      shell: true,
    };
    const child = spawn(`"${process.execPath}"`, [moduleName, ...args], opts);

    const data = new Array<Buffer>();
    child.stdout?.on('data', chunk => data.push(chunk));
    child.stderr?.on('data', chunk => data.push(chunk));

    const newError = (message: string) => new Error([
      message,
      '  | ' + Buffer.concat(data).toString('utf-8').split('\n').filter(x => x).join('\n  | '),
      '  +----------------------------------------------------------------------------------',
      `  | Command: ${moduleName} ${args.join(' ')}`,
      `  | Workdir: ${path.resolve(options.cwd ?? '.')}`,
      '  +----------------------------------------------------------------------------------',
    ].join('\n'));

    child.once('error', err => {
      throw newError(`jsii compilation failed. error: ${err.message}`);
    });

    child.once('exit', code => {
      if (code === 0) {
        return ok();
      } else {
        return fail(newError(`jsii compilation failed with non-zero exit code: ${code}`));
      }
    });
  });
}