diff --git a/src/cli.mjs b/src/cli.mjs index 42aea37..fcdf606 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] || args[0].startsWith('--') || + (args.length === 2 && args[1] !== '--json')) { + throw new TypeError('用法: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) { + process.exitCode = 2; + process.stderr.write('输入错误:' + error.message + '\n'); +} diff --git a/src/normalize.mjs b/src/normalize.mjs index 5a46e5a..0c57eef 100644 --- a/src/normalize.mjs +++ b/src/normalize.mjs @@ -419,6 +419,30 @@ // 保留工程说明 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 states = new Set(['todo', 'doing', 'done', 'blocked']); + const tasks = []; + for (const task of input) { + if (task === null || typeof task !== 'object' || Array.isArray(task)) { + throw new TypeError('每项任务必须是对象'); + } + if (typeof task.id !== 'string' || typeof task.title !== 'string') { + throw new TypeError('任务id和title必须是非空字符串'); + } + const id = task.id.trim(); + const title = task.title.trim(); + if (!id || !title) throw new TypeError('任务id和title不能为空'); + if (ids.has(id)) throw new TypeError('任务id不能重复'); + const { status, minutes } = task; + if (!states.has(status)) throw new TypeError('任务状态必须是todo、doing、done或blocked'); + if (!Number.isSafeInteger(minutes) || minutes < 0 || minutes > 1000000) { + throw new TypeError('预计分钟数必须是0到1000000之间的安全整数'); + } + ids.add(id); + tasks.push({ id, title, status, minutes }); + } + return tasks; } 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..af3a52d 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 total = tasks.length; + 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; + } + return { + total, + counts, + completedPercent: total === 0 ? 0 : Math.round(counts.done / total * 100), + remainingMinutes + }; }