sc2: add graph subcommand (#1811)

Summary:
Paired with @decentralion.

Test Plan:
Follow the test plan for #1810, then additionally run

```
(cd /tmp/test-instance && node "$OLDPWD/bin/sc2.js" graph)
```

and note that the `output/graphs/...` directory has a graph JSON file.

wchargin-branch: cli2-graph
This commit is contained in:
William Chargin 2020-05-28 19:01:03 -07:00 committed by GitHub
parent 80c3c38282
commit 8d88ea937b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

55
src/cli2/graph.js Normal file
View File

@ -0,0 +1,55 @@
// @flow
import fs from "fs-extra";
import sortBy from "lodash.sortby";
import stringify from "json-stable-stringify";
import {join as pathJoin} from "path";
import {CascadingReferenceDetector} from "../core/references/cascadingReferenceDetector";
import type {Command} from "./command";
import {
makePluginDir,
loadInstanceConfig,
pluginDirectoryContext,
} from "./common";
import {toJSON as weightedGraphToJSON} from "../core/weightedGraph";
function die(std, message) {
std.err("fatal: " + message);
return 1;
}
const graphCommand: Command = async (args, std) => {
if (args.length !== 0) {
die(std, "usage: sourcecred graph");
}
const baseDir = process.cwd();
const config = await loadInstanceConfig(baseDir);
const graphOutputPrefix = ["output", "graphs"];
const rd = await buildReferenceDetector(baseDir, config);
for (const [name, plugin] of config.bundledPlugins) {
const dirContext = pluginDirectoryContext(baseDir, name);
const graph = await plugin.graph(dirContext, rd);
const serializedGraph = stringify(weightedGraphToJSON(graph));
const outputDir = makePluginDir(baseDir, graphOutputPrefix, name);
const outputPath = pathJoin(outputDir, "graph.json");
await fs.writeFile(outputPath, serializedGraph);
}
return 0;
};
async function buildReferenceDetector(baseDir, config) {
const rds = [];
for (const [name, plugin] of sortBy(
[...config.bundledPlugins],
([k, _]) => k
)) {
const dirContext = pluginDirectoryContext(baseDir, name);
const rd = await plugin.referenceDetector(dirContext);
rds.push(rd);
}
return new CascadingReferenceDetector(rds);
}
export default graphCommand;

View File

@ -3,6 +3,7 @@
import type {Command} from "./command";
import load from "./load";
import graph from "./graph";
const sourcecred: Command = async (args, std) => {
if (args.length === 0) {
@ -12,6 +13,8 @@ const sourcecred: Command = async (args, std) => {
switch (args[0]) {
case "load":
return load(args.slice(1), std);
case "graph":
return graph(args.slice(1), std);
default:
std.err("fatal: unknown command: " + JSON.stringify(args[0]));
return 1;