api: expose new bundle of select internals (#1526)

Summary:
For convenient import by scripts and Observable notebooks that want to
use SourceCred code outside its normal build system. We export a subset
of the codebase, including some core data structures and algorithms and
also some plugin metadata, but no plugin loading code.

To build, run `yarn backend` (or `yarn backend --watch`), then grab the
new `bin/api.js` file.

Test Plan:
Sample usage, with normal Node:

```javascript
const {
  core: {
    graph: {Graph, NodeAddress, EdgeAddress},
  },
} = require("./api").default;

function node(address) {
  return {
    address,
    description: "blurgh",
    timestampMs: -1,
  };
}

const g = new Graph();
g.addNode(node(NodeAddress.fromParts(["people", "alice"])));
g.addNode(node(NodeAddress.fromParts(["people", "bob"])));
g.addEdge({
  address: EdgeAddress.fromParts(["friendship"]),
  src: NodeAddress.fromParts(["people", "alice"]),
  dst: NodeAddress.fromParts(["people", "bob"]),
  timestampMs: 0,
});

console.log(require("json-stable-stringify")(g));
```

This prints a valid graph JSON object.

wchargin-branch: api-bundle
This commit is contained in:
William Chargin 2020-01-08 23:38:39 -08:00 committed by GitHub
parent 01248fb8f6
commit 6b85296e55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -27,6 +27,7 @@ module.exports = {
// point within the build directory.
backendEntryPoints: {
sourcecred: resolveApp("src/cli/main.js"),
api: resolveApp("src/api/index.js"),
//
generateGithubGraphqlFlowTypes: resolveApp(
"src/plugins/github/bin/generateGraphqlFlowTypes.js"

44
src/api/index.js Normal file
View File

@ -0,0 +1,44 @@
// @flow
import deepFreeze from "deep-freeze";
// Exports for calling SourceCred code programmatically. Both the
// structure and the contents of this API are experimental and subject
// to change.
import * as address from "../core/address";
import * as discourseAddress from "../plugins/discourse/address";
import * as discourseDeclaration from "../plugins/discourse/declaration";
import * as githubDeclaration from "../plugins/github/declaration";
import * as githubEdges from "../plugins/github/edges";
import * as githubNodes from "../plugins/github/nodes";
import * as graph from "../core/graph";
import * as graphToMarkovChain from "../core/attribution/graphToMarkovChain";
import * as markovChain from "../core/attribution/markovChain";
import * as timelineCred from "../analysis/timeline/timelineCred";
export default deepFreeze({
analysis: {
timeline: {
timelineCred,
},
},
core: {
address,
attribution: {
markovChain,
graphToMarkovChain,
},
graph,
},
plugins: {
github: {
declaration: githubDeclaration,
edges: githubEdges,
nodes: githubNodes,
},
discourse: {
address: discourseAddress,
declaration: discourseDeclaration,
},
},
});