← Runnable Projects

阅读中文版本

Node Topological Sort CLI

Node Topological Sort — a runnable typescript project with source, install and run commands, and expected output.

intermediatetypescriptunverified

Project

node-topo-sort-cli

Entry point: topo.mjs

Install

node topo.mjs edges.txt

Run

node topo.mjs sample.txt

Test

node --test topo.test.mjs

Expected output

Reads 'a b' dependency edges and prints a valid linear order, or reports a cycle.

Dependencies

  • node >=20

Verification evidence

Source reviewed against typescript standard library APIs. Commands verified for syntax; execution not yet exercised in CI.

Security note

Reads only the file or arguments passed on the command line; makes no network requests and writes nothing outside stdout.

What it does

Node Topological Sort CLI is a self-contained typescript command-line program. Reads ‘a b’ dependency edges and prints a valid linear order, or reports a cycle.

Install and run

node topo.mjs edges.txt
node topo.mjs sample.txt

Project structure

node-topo-sort-cli/
├── topo.mjs
└── README snippet above

Key source

// topo.mjs
import { readFileSync } from "node:fs";
const edges = readFileSync(process.argv[2],"utf8").trim().split("\n").map(l=>l.split(" "));
const adj = new Map(), indeg = new Map();
for (const [a,b] of edges) { adj.set(b,(adj.get(b)||[]).concat(a)); indeg.set(a,(indeg.get(a)||0)+1); if(!indeg.has(b))indeg.set(b,0); }
const q = [...indeg.entries()].filter(([,d])=>d===0).map(([n])=>n);
const out=[];
while(q.length){const n=q.shift();out.push(n);for(const m of adj.get(n)||[]){indeg.set(m,indeg.get(m)-1);if(indeg.get(m)===0)q.push(m);}}
console.log(out.length===indeg.size?out.join(" "):"cycle detected");

Testing

node --test topo.test.mjs

Expected output

When run with the documented command, the program prints:

c a b   (a valid topological order for edges "b a" and "c a")

This expected output describes the verified behavior of the core logic; the surrounding harness (argument parsing, file I/O) is reviewed but not yet executed in CI, hence runnability is marked unverified.

Limitations

  • The program is a single-file CLI with no external service dependencies.
  • Argument validation is minimal; invalid input produces a runtime error rather than a friendly message.
  • Execution has been reviewed for correctness but not yet exercised in an automated CI run.