Closed9

nodeで対話型のシェル風コマンドツールを作る

11-1010-10011-1010-100

試しにコードを書く

touch index.mts
index.mts
import {$} from 'zx';

(async () => {
  await $`ls -la`;
})();
package.jsonに追記
{
  "scripts": {
    "start": "node --loader ts-node/esm index.mts"
  },
}
11-1010-10011-1010-100

試しに実行する

pnpm start

drwxr-xr-x   8 user  staff    256 11  7 17:45 .
drwxr-xr-x   7 user  staff    224 11  7 16:38 ..
drwxr-xr-x  10 user  staff    320 11  7 17:22 node_modules
-rw-r--r--   1 user  staff    399 11  7 17:47 package.json
-rw-r--r--   1 user  staff  26137 11  7 17:22 pnpm-lock.yaml
-rw-r--r--   1 user  staff     63 11  7 17:40 index.mts
-rw-r--r--   1 user  staff     86 11  7 17:42 tsconfig.json
11-1010-10011-1010-100

試しにコードを書いてみる

index.mts
import {$, echo} from 'zx';
import inquirer from 'inquirer';

(async () => {
  const choices = await inquirer.prompt<{userName: string}>([
    {name: 'userName', message: 'Enter your name.'},
  ]);
  echo(choices.userName);
})();
11-1010-10011-1010-100

シェル風にするなら、ループさせる

index.mts
import {$} from 'zx';
import inquirer from 'inquirer';

(async () => {
  while (true) {
    const userCommand = await inquirer.prompt<{cmd: string}>([
      {
        type: 'rawlist',
        name: 'cmd',
        message: 'choose your command',
        choices: ['ls', 'pwd', 'exit'],
      },
    ]);

    await $`${userCommand.cmd}`;
    if (userCommand.cmd === 'exit') break;
  }
})();
このスクラップは2022/11/07にクローズされました