Skip to content

Commit

Permalink
sc2: add graph subcommand (sourcecred#1811)
Browse files Browse the repository at this point in the history
Summary:
Paired with @decentralion.

Test Plan:
Follow the test plan for sourcecred#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
  • Loading branch information
wchargin authored and META-DREAMER committed Jun 18, 2020
1 parent cb2ea7f commit 0fadd98
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/cli2/graph.js
@@ -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;
3 changes: 3 additions & 0 deletions src/cli2/sourcecred.js
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 0fadd98

Please sign in to comment.