child_processを使ってVimを起動する

コード

const { spawn } = require('child_process');

const subprocess = spawn(
  'sh',
  [
    '-c',
    'vim'
  ], {
      stdio: ['inherit', 'inherit', 'inherit']
  }
);

// 2秒後に終了させる
setTimeout(() => {
  subprocess.kill(); // Does not terminate the Node.js process in the shell.
}, 2000);

sh -c

 -c     Take  the  first  argument as a
              command to execute, rather than
              reading  commands from a script
              or standard input.  If any fur-
              ther  arguments  are given, the
              first one is  assigned  to  $0,
              rather  than  being  used  as a
              positional parameter.

最初の引数をスクリプトもしくは標準入力から読むのではなく、コマンドとして実行するために取る。 もしさらに他に引数が与えられた場合、最初の引数は $0 に割り当てられ、位置パラメーターとして使用されるのでない。

options stdio: ['inherit', 'inherit', 'inherit']

標準IOを以下のように指定しているのと同等

  • stdin = process.stdin
  • stdout = process.stdout
  • stderr = process.stderr

Note

child_process.spawn(command[, args][, options])

nodejs.org