Skip to content

Dynamic Importer を廃止した#24

Merged
massan02 merged 4 commits intomainfrom
ulxsth/deprecate-dynamic-importer
Mar 28, 2025
Merged

Dynamic Importer を廃止した#24
massan02 merged 4 commits intomainfrom
ulxsth/deprecate-dynamic-importer

Conversation

@ulxsth
Copy link
Contributor

@ulxsth ulxsth commented Mar 28, 2025

Details

commands 内を捜査してコマンドを実行する Dynamic Importer を廃止し、switch による対応を追加しました。

なにがうれしいの

コマンドのエントリファイルとしての可読性が向上します。
具体的には、各コマンドと実装ファイルが1-1で紐づきます。

const commandsDirPath = path.join(__dirname, "commands");
  const commandsDir = fs
    .readdirSync(commandsDirPath)
    .filter((file) => file.endsWith(".js"));

  for (const file of commandsDir) {
    if (file.replace(".js", "") !== commandName) {
      continue;
    }

    // * ESM Module まわりが any を使わざるを得ないので、ESLint 側の制限を緩めてます
    const commandsPath = path.join(commandsDirPath, file);
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    const commandModule = await import(commandsPath);
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
    const command: Command = commandModule.default;

    if (typeof command.exec === "function") {
      command.exec(argv);
      return;
    }
  }

  switch (commandName) {
    case "hello":
      helloCommand.exec(argv);
      break;

    case "add":
      addCommand.exec(argv);
      break;

    case "log":
      logCommand.exec(argv);
      break;

    default:
      console.log("存在するコマンドを入力してください ><");
  }

また、「default でヘルプを呼び出す」みたいな変則的な実装を埋め込めるようになりました。

Debug

mygit に実装された各コマンドを実行し、実装が呼び出せているかをチェックしてください。
現状 add hello log があります

@massan02
Copy link
Contributor

動作確認済み

@massan02 massan02 merged commit 32d42ee into main Mar 28, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants