Reorganize the README checklist: fold the standalone Ex-command task into earlier tasks, give the outline panel its own bindings (g o / Ctrl+Shift+O), and add a dedicated debugging task with breakpoint, stepping, and fault-finding drills anchored to real lines in the training app. Annotate the training app sources and tests with Chinese practice hints (multi-cursor, macro, text-object, and breakpoint spots). Drop the redundant root tsconfig, since training/tsconfig.json already covers src and tests, and remove the stale LEADERBOARD.md left over from the deleted challenge mode.
84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
// ════════════════════════════════════════════════════════════
|
||
// cli —— 命令行入口(实际可运行)
|
||
// npm start 显示用法
|
||
// npm start -- list 商品目录
|
||
// npm start -- order apple 3 cheese 1
|
||
// npm start -- report 汇总报表
|
||
// npm start -- stock 库存快照
|
||
// ════════════════════════════════════════════════════════════
|
||
|
||
import { listProducts } from "./catalog";
|
||
import { createOrder, listOrders, shipOrder } from "./orders";
|
||
import { reportSummary } from "./reporting";
|
||
import { stockReport } from "./inventory";
|
||
|
||
function printUsage(): void {
|
||
console.log(
|
||
[
|
||
"Usage:",
|
||
" list 商品目录",
|
||
" order <id> <qty>… 下单(可多行,如 apple 3 milk 1)",
|
||
" ship <id> 发货",
|
||
" report 汇总报表",
|
||
" demo 内置示例:下两单 → 发货 → 报表",
|
||
" stock 库存快照",
|
||
].join("\n"),
|
||
);
|
||
}
|
||
|
||
function parseOrderArgs(args: string[]) {
|
||
const pairs: Array<{ productId: string; quantity: number }> = [];
|
||
for (let i = 0; i + 1 < args.length; i += 2) {
|
||
pairs.push({ productId: args[i], quantity: Number(args[i + 1]) });
|
||
}
|
||
if (pairs.length === 0 || pairs.some((p) => !Number.isFinite(p.quantity) || p.quantity <= 0)) {
|
||
throw new Error("order 参数需成对出现:order <商品> <数量> …");
|
||
}
|
||
return pairs;
|
||
}
|
||
|
||
function main(argv: string[]): void {
|
||
const [command, ...rest] = argv;
|
||
switch (command) {
|
||
case "list":
|
||
console.log(listProducts().join("\n"));
|
||
break;
|
||
case "order": {
|
||
const order = createOrder(parseOrderArgs(rest));
|
||
console.log(
|
||
`order #${order.id} subtotal $${order.subtotal.toFixed(2)} tax $${order.tax.toFixed(2)} total $${order.total.toFixed(2)}`,
|
||
);
|
||
break;
|
||
}
|
||
case "ship": {
|
||
const id = Number(rest[0]);
|
||
shipOrder(id);
|
||
console.log(`order #${id} shipped`);
|
||
break;
|
||
}
|
||
// 任务11:demo = 两单 → 发货 → 报表;调试时启动脚本参数就填 demo
|
||
case "demo": {
|
||
const one = createOrder([
|
||
{ productId: "apple", quantity: 12 },
|
||
{ productId: "milk", quantity: 2 },
|
||
]);
|
||
const two = createOrder([{ productId: "cheese", quantity: 6 }]);
|
||
shipOrder(one.id);
|
||
console.log(`#${one.id} ${listOrders()[0].lines.length} lines`);
|
||
console.log(`#${two.id} ${listOrders()[1].lines.length} lines`);
|
||
console.log(reportSummary().join("\n"));
|
||
break;
|
||
}
|
||
case "report":
|
||
console.log(reportSummary().join("\n"));
|
||
break;
|
||
case "stock":
|
||
console.log(stockReport().join("\n"));
|
||
break;
|
||
default:
|
||
printUsage();
|
||
}
|
||
}
|
||
|
||
main(process.argv.slice(2));
|