forked from sourcecred/sourcecred
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sc2: add graph subcommand (sourcecred#1811)
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
1 parent
cb2ea7f
commit 0fadd98
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters