From eb2cb12d27fe421bc6fa9894239201cdb7753226 Mon Sep 17 00:00:00 2001 From: Daniel Lamando Date: Sat, 29 Jan 2022 23:41:53 +0100 Subject: [PATCH] feat: Show graph for file:// + optional isolation --- feat/dependencies-of/cli.ts | 16 ++++++++++++++-- lib/module-map.ts | 1 + lib/module-registries.ts | 25 ++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/feat/dependencies-of/cli.ts b/feat/dependencies-of/cli.ts index ca065d5..67bb362 100755 --- a/feat/dependencies-of/cli.ts +++ b/feat/dependencies-of/cli.ts @@ -5,12 +5,24 @@ const flags = Flags.parse(Deno.args, { alias: { output: ['o'], }, + boolean: [ + 'isolate-std', + 'isolate-files', + ], }); if (flags._.length !== 1) { console.error(`usage: cli.ts [-o output.{png,svg,jpg,jpeg}]`); Deno.exit(4); } +const graphParams = new URLSearchParams(); +if (flags['isolate-std']) { + graphParams.set('std', 'isolate'); +} +if (flags['isolate-files']) { + graphParams.set('files', 'isolate'); +} + import { computeGraph, renderGraph } from "./compute.ts"; const modUrl = `${flags._[0]}`; @@ -25,10 +37,10 @@ if (flags.output) { const dotProc = await renderGraph(modUrl, [ `-o${flags.output}`, `-T${ext}`, - ], new URLSearchParams()); + ], graphParams); console.log(await dotProc.captureAllTextOutput()); } else { - const dotText = await computeGraph(modUrl, new URLSearchParams(), 'dot'); + const dotText = await computeGraph(modUrl, graphParams, 'dot'); console.log(dotText); } diff --git a/lib/module-map.ts b/lib/module-map.ts index 4ecaecc..e435da1 100644 --- a/lib/module-map.ts +++ b/lib/module-map.ts @@ -15,6 +15,7 @@ export class ModuleMap { ) { this.registryOpts = { mainModule: rootNode.specifier, + isolateFiles: this.args.get('files') === 'isolate', isolateStd: this.args.get('std') === 'isolate', } diff --git a/lib/module-registries.ts b/lib/module-registries.ts index ef30b94..365e3af 100644 --- a/lib/module-registries.ts +++ b/lib/module-registries.ts @@ -2,13 +2,18 @@ import type { CodeModule } from "./types.ts"; export interface RegistryOpts { mainModule: string; + isolateFiles?: boolean; isolateStd?: boolean; }; export function determineModuleBase(fullUrl: string, opts: RegistryOpts): string { const url = new URL(fullUrl); const parts = fullUrl.split('/'); - if (url.protocol === 'file:') return 'file://'; + if (url.protocol === 'file:') { + if (opts.isolateFiles) return fullUrl; + if (url.pathname.endsWith('deps.ts')) return fullUrl; + return new URL('.', url).toString(); + } if (url.protocol !== 'https:') return fullUrl; switch (url.host) { case 'deno.land': @@ -83,6 +88,24 @@ export function determineModuleBase(fullUrl: string, opts: RegistryOpts): string export function determineModuleLabel(module: CodeModule, opts: RegistryOpts): string[] { const url = new URL(module.base); const parts = module.base.split('/'); + if (url.protocol === 'file:') { + const mainDir = new URL('.', opts.mainModule).toString(); + const thisDir = module.base; + if (thisDir.startsWith(mainDir)) { + return [`./${thisDir.slice(mainDir.length)}`]; + } + const dirNames = mainDir.split('/'); + dirNames.pop(); // trailing slash + let steps = 0; + while (dirNames.length > 5 && ++steps && dirNames.pop()) { + const joined = dirNames.join('/'); + if (thisDir.startsWith(joined+'/')) { + const walkUp = new Array(steps).fill('..').join('/'); + return [`${walkUp}/${thisDir.slice(joined.length+1)}`]; + } + } + return [thisDir]; + } if (url.protocol !== 'https:') return [module.base]; switch (url.host) { case 'deno.land': {