Skip to content

[Bug] content.pagesが空のとき html-share page add が失敗する #6

Description

@joelmitz

起きたこと

新規に作成した html-share.config.yamlcontent.pages がまだ空の、セットアップ直後の状態)に対して

html-share page add <path> --title "..."

を実行すると、最初の1件のはずが

content.pages must contain at least one page

で失敗します。YAMLファイルを手動で編集して1件足すまで、page add で最初のページを追加する手段がありません。

原因: src/cli.tsmain() が、page コマンドの分岐より先に無条件で loadConfig() を呼んでいます。

const command = process.argv[2];
if (!command || flag('--help') || flag('-h')) usage();
const config = loadConfig(option('--config'));   // ← ここで 'page' コマンドも巻き込まれて例外

if (command === 'build') { ... }
...
if (command === 'page') {
  const added = addPageToConfig(option('--config'), pagePath, option('--title'));
  ...
}

loadConfig()src/config.ts)は content.pages.length > 0 を要求します。

if (pages.length === 0) throw new Error('content.pages must contain at least one page');

しかし page add のハンドラは実際には読み込んだ config オブジェクトを使っておらず、addPageToConfig() が生YAMLを直接読み書きするだけの実装です。そのため page add 自体には不要な検証に巻き込まれ、しかも「最初のページを追加する」という唯一の手段そのものが使えなくなっています。

修正案: page コマンドの分岐を loadConfig() 呼び出しより前に移動し、config検証を経由しないようにする。

async function main(): Promise<void> {
  const command = process.argv[2];
  if (!command || flag('--help') || flag('-h')) usage();

  if (command === 'page') {
    const action = process.argv[3];
    const pagePath = process.argv[4];
    if (action !== 'add' || !pagePath) usage();
    const added = addPageToConfig(option('--config'), pagePath, option('--title'));
    console.log(JSON.stringify({ ok: true, added, path: pagePath }));
    return;
  }

  const config = loadConfig(option('--config'));
  // 以降の他コマンドは変更なし
}

自分のCloudflareバックエンド版fork(joelmitz/html-share@f1ca1f6)でこの修正と回帰テストを検証済みです。よろしければPRも出せますし、こちらのdiffを直接採用いただいても構いません。

再現手順

  1. content.pages: [] の状態で html-share.config.yaml を用意する(新規セットアップ直後を想定)
  2. html-share page add pages/demo.html --title "デモ" を実行する
  3. content.pages must contain at least one page で失敗する

バージョン

0.1.0(commit HEAD時点。cli.ts/config.tsのこの箇所は変更なく現行mainに存在)

実行環境

Node.js 22 / OS問わず(CLIロジックのみが原因)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions