diff --git a/src/cli.mjs b/src/cli.mjs index 42aea37..cae5d23 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -423,6 +423,16 @@ import { normalizeTasks } from './normalize.mjs'; import { summarize } from './summary.mjs'; import { renderText } from './render.mjs'; try { - const tasks = normalizeTasks(JSON.parse(fs.readFileSync(process.argv[2], 'utf8'))); - console.log(renderText(summarize(tasks))); -} catch (error) { console.error(error.message); } + const args = process.argv.slice(2); + if (args.length < 1 || args.length > 2 || args[0].startsWith('-') || + (args.length === 2 && args[1] !== '--json')) { + throw new Error('用法:node src/cli.mjs [--json]'); + } + const tasks = normalizeTasks(JSON.parse(fs.readFileSync(args[0], 'utf8'))); + const summary = summarize(tasks); + const output = args[1] === '--json' ? JSON.stringify(summary) : renderText(summary); + process.stdout.write(output + '\n'); +} catch (error) { + console.error(`输入错误:${error.message}`); + process.exitCode = 2; +} diff --git a/src/normalize.mjs b/src/normalize.mjs index 5a46e5a..fc0c3f1 100644 --- a/src/normalize.mjs +++ b/src/normalize.mjs @@ -419,6 +419,29 @@ // 保留工程说明 419 · src/normalize.mjs · 原文🙂 // 保留工程说明 420 · src/normalize.mjs · 原文🙂 export function normalizeTasks(input) { - if (!Array.isArray(input)) throw new TypeError('tasks must be an array'); - return input; + if (!Array.isArray(input) || input.length > 10000) { + throw new TypeError('任务必须是最多10000项的数组'); + } + const ids = new Set(); + const statuses = new Set(['todo', 'doing', 'done', 'blocked']); + return Array.from(input, (task, index) => { + if (task === null || typeof task !== 'object' || Array.isArray(task)) { + throw new TypeError(`第${index + 1}项必须是对象`); + } + if (typeof task.id !== 'string' || !task.id.trim() || + typeof task.title !== 'string' || !task.title.trim()) { + throw new TypeError(`第${index + 1}项的id和title必须是非空字符串`); + } + const id = task.id.trim(); + const title = task.title.trim(); + if (ids.has(id)) throw new TypeError(`任务id重复:${id}`); + if (!statuses.has(task.status)) { + throw new TypeError(`第${index + 1}项的status无效`); + } + if (!Number.isSafeInteger(task.minutes) || task.minutes < 0 || task.minutes > 1000000) { + throw new TypeError(`第${index + 1}项的minutes必须是0到1000000之间的安全整数`); + } + ids.add(id); + return { id, title, status: task.status, minutes: task.minutes }; + }); } diff --git a/src/render.mjs b/src/render.mjs index 5cacc86..2752ba6 100644 --- a/src/render.mjs +++ b/src/render.mjs @@ -419,5 +419,10 @@ // 保留工程说明 419 · src/render.mjs · 原文🙂 // 保留工程说明 420 · src/render.mjs · 原文🙂 export function renderText(summary) { - return `Done: ${summary.counts.done}/${summary.total}`; + const { total, counts, completedPercent, remainingMinutes } = summary; + return [ + `交付进度:${counts.done}/${total}(${completedPercent}%)`, + `待办 ${counts.todo} · 进行中 ${counts.doing} · 已完成 ${counts.done} · 阻塞 ${counts.blocked}`, + `剩余预计:${remainingMinutes} 分钟` + ].join('\n'); } diff --git a/src/summary.mjs b/src/summary.mjs index 6679d91..57c7709 100644 --- a/src/summary.mjs +++ b/src/summary.mjs @@ -419,7 +419,17 @@ // 保留工程说明 419 · src/summary.mjs · 原文🙂 // 保留工程说明 420 · src/summary.mjs · 原文🙂 export function summarize(tasks) { - const done = tasks.filter(task => task.status === 'done').length; - return { total: tasks.length, counts: { done }, completedPercent: tasks.length ? 100 : 0, - remainingMinutes: tasks.reduce((sum, task) => sum + task.minutes, 0) }; + const counts = { todo: 0, doing: 0, done: 0, blocked: 0 }; + let remainingMinutes = 0; + for (const task of tasks) { + counts[task.status] += 1; + if (task.status !== 'done') remainingMinutes += task.minutes; + } + const total = tasks.length; + return { + total, + counts, + completedPercent: total === 0 ? 0 : Math.round(counts.done / total * 100), + remainingMinutes + }; }