diff --git a/src/cli.mjs b/src/cli.mjs index 090ad4d..baef8a5 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -3,6 +3,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.stderr.write(`输入错误:${error.message}\n`); + process.exitCode = 2; +} diff --git a/src/normalize.mjs b/src/normalize.mjs index 70d376d..45ff90f 100644 --- a/src/normalize.mjs +++ b/src/normalize.mjs @@ -1,4 +1,35 @@ 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']); + const result = []; + 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 (!statuses.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); + result.push({ id, title, status, minutes }); + } + return result; } diff --git a/src/render.mjs b/src/render.mjs index f8ab62f..7f923c1 100644 --- a/src/render.mjs +++ b/src/render.mjs @@ -1,3 +1,8 @@ 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 2d615a3..8ef2ad0 100644 --- a/src/summary.mjs +++ b/src/summary.mjs @@ -1,5 +1,11 @@ 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; + const completedPercent = total ? Math.round(counts.done / total * 100) : 0; + return { total, counts, completedPercent, remainingMinutes }; }