Skip to content

WIP draft of a graphviz / mermaid generator #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface Options {
cache: 'local' | 'github' | 'none';
failureMode: FailureMode;
agent: Agent;
graph: boolean;
}

export const getOptions = (): Result<Options> => {
Expand Down Expand Up @@ -201,7 +202,7 @@ export const getOptions = (): Result<Options> => {
function getArgvOptions(
script: ScriptReference,
agent: Agent
): Pick<Options, 'watch' | 'extraArgs'> {
): Pick<Options, 'watch' | 'extraArgs' | 'graph'> {
// The way command-line arguments are handled in npm, yarn, and pnpm are all
// different. Our goal here is for `<agent> --watch -- --extra` to behave the
// same in all agents.
Expand All @@ -219,6 +220,7 @@ function getArgvOptions(
return {
watch: process.env['npm_config_watch'] !== undefined,
extraArgs: process.argv.slice(2),
graph: true,
};
}
case 'yarnClassic': {
Expand Down Expand Up @@ -355,7 +357,7 @@ function findRemainingArgsFromNpmConfigArgv(
*/
function parseRemainingArgs(
args: string[]
): Pick<Options, 'watch' | 'extraArgs'> {
): Pick<Options, 'watch' | 'extraArgs' | 'graph'> {
let watch = false;
let extraArgs: string[] = [];
const unrecognized = [];
Expand All @@ -381,5 +383,6 @@ function parseRemainingArgs(
return {
watch,
extraArgs,
graph: true,
};
}
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ const run = async (): Promise<Result<void, Failure[]>> => {
if (!config.ok) {
return config;
}
if (options.graph) {
const {Graph} = await import('./graph.js');
const graph = new Graph(config.value);
await graph.generate();
return {ok: true, value: undefined};
}
console.log('\n\n\n\n\nOH NO!\n\n\n\n\n\n');
const executor = new Executor(
config.value,
logger,
Expand Down
88 changes: 88 additions & 0 deletions src/graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {ScriptConfig} from './config.js';
import {relative} from 'path';

interface Generator {
addEdge(from: string, to: string): void;
done(): void;
}

class GraphViz implements Generator {
constructor() {
console.log('digraph {');
}

addEdge(from: string, to: string) {
console.log(` "${from}" -> "${to}";`);
}

done() {
console.log('}');
}
}

class Mermaid implements Generator {
#nextId = 0;
readonly #labelToId = new Map<string, number>();
constructor() {
console.log('graph TD');
}

addEdge(from: string, to: string) {
console.log(` ${this.#format(from)} --> ${this.#format(to)}`);
}

#format(label: string) {
let id = this.#labelToId.get(label);
if (id === undefined) {
id = this.#nextId++;
this.#labelToId.set(label, id);
// TODO: properly escape the label
return `${id}[${label}]`;
}
return String(id);
}

done() {}
}

export class Graph {
readonly #config: ScriptConfig;
readonly #cwd = process.cwd();
readonly #generator: Generator;
constructor(config: ScriptConfig, kind: 'mermaid' | 'graphviz' = 'mermaid') {
this.#config = config;
if (kind === 'mermaid') {
this.#generator = new Mermaid();
} else {
this.#generator = new GraphViz();
}
}

generate() {
const seen = new Set<ScriptConfig>();
const queue = [this.#config];
let current;
while ((current = queue.pop())) {
if (seen.has(current)) {
continue;
}
seen.add(current);
for (const dependency of current.dependencies) {
this.#generator.addEdge(
this.#formatConfig(current),
this.#formatConfig(dependency.config)
);
queue.push(dependency.config);
}
}
this.#generator.done();
}

#formatConfig(config: ScriptConfig) {
const rel = relative(this.#cwd, config.packageDir);
if (rel === '') {
return config.name;
}
return `${rel}:${config.name}`;
}
}