Skip to content

Commit 45409fe

Browse files
committed
chore(lib): Prepare for more configurabilitty
1 parent c1b6de7 commit 45409fe

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

.github/workflows/deno-ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ jobs:
5050
- name: Check entrypoint
5151
run: time deno cache server.ts
5252

53+
- name: Run any tests
54+
run: time deno test
55+
5356
# Push image to GitHub Packages.
5457
# See also https://docs.docker.com/docker-hub/builds/
5558
push:

lib/module-map.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,28 @@ import * as registries from "./module-registries.ts";
55

66
export class ModuleMap {
77
modules = new Map<string,CodeModule>();
8-
mainModule: CodeModule | null = null;
9-
mainFile: string | null = null;
8+
mainModule: CodeModule;
9+
registryOpts: registries.RegistryOpts;
1010

1111
constructor(
1212
public args: URLSearchParams,
1313
public redirects: Record<string,string>,
14+
public rootNode: DenoModule,
1415
) {
15-
this.isolateStd = this.args.get('std') === 'isolate';
16+
this.registryOpts = {
17+
mainModule: rootNode.specifier,
18+
isolateStd: this.args.get('std') === 'isolate',
19+
}
20+
21+
this.mainModule = this.grabModFor(
22+
rootNode.specifier,
23+
rootNode.error ? '#error' : undefined);
1624
}
17-
isolateStd: boolean;
1825

1926
grabModFor(url: string, fragment: string = '') {
2027
const wireUrl = url.split('#')[0];
2128
const actualUrl = this.redirects[wireUrl] || wireUrl;
22-
const base = registries.determineModuleBase(actualUrl, this.isolateStd);
29+
const base = registries.determineModuleBase(actualUrl, this.registryOpts);
2330
let moduleInfo = this.modules.get(base + fragment);
2431
if (!moduleInfo) {
2532
moduleInfo = {
@@ -79,7 +86,7 @@ export class ModuleMap {
7986
for (const module of this.modules.values()) {
8087
modules[module.base+module.fragment] = {
8188
moduleDeps: Array.from(module.deps).map(x => x.base+x.fragment),
82-
labelText: registries.determineModuleLabel(module, this.isolateStd),
89+
labelText: registries.determineModuleLabel(module, this.registryOpts),
8390
totalSize: module.totalSize,
8491
fileCount: module.files.length,
8592
errors: module.errors,
@@ -96,7 +103,7 @@ export class ModuleMap {
96103
for (const module of this.modules.values()) {
97104
// console.log(module.base, Array.from(module.deps.values()).map(x => x.base));
98105

99-
const labels = registries.determineModuleLabel(module, this.isolateStd);
106+
const labels = registries.determineModuleLabel(module, this.registryOpts);
100107
if (module.errors) {
101108
labels.unshift(`${module.errors.length} FAILED IMPORTS FROM:`);
102109
for (const err of module.errors) {
@@ -171,17 +178,13 @@ export class ModuleMap {
171178
}
172179

173180
export function processDenoInfo(data: DenoInfo, args?: URLSearchParams) {
174-
const map = new ModuleMap(args ?? new URLSearchParams, data.redirects);
175-
176181
// TODO: when are there multiple roots?
177182
const roots = data.roots.map(x => data.redirects[x] || x);
178183
const rootNode = data.modules.find(x => roots.includes(x.specifier));
179184
if (!rootNode) throw new Error(
180185
`I didn't find a root node in the Deno graph! This is a module-visualizer bug.`);
181186

182-
map.mainModule = map.grabModFor(rootNode.specifier, rootNode.error ? '#error' : undefined);
183-
map.mainFile = rootNode.specifier;
184-
187+
const map = new ModuleMap(args ?? new URLSearchParams, data.redirects, rootNode);
185188
for (const info of data.modules) {
186189
map.addFile(info.specifier, info, data);
187190
}

lib/module-registries.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import type { CodeModule } from "./types.ts";
22

3-
export function determineModuleBase(fullUrl: string, isolateStd: boolean): string {
3+
export interface RegistryOpts {
4+
mainModule: string;
5+
isolateStd?: boolean;
6+
};
7+
8+
export function determineModuleBase(fullUrl: string, opts: RegistryOpts): string {
49
const url = new URL(fullUrl);
510
const parts = fullUrl.split('/');
611
if (url.protocol === 'file:') return 'file://';
712
if (url.protocol !== 'https:') return fullUrl;
813
switch (url.host) {
914
case 'deno.land':
10-
if (parts[3].startsWith('std') && !isolateStd) return parts.slice(0, 4).join('/');
15+
if (parts[3].startsWith('std') && !opts.isolateStd) return parts.slice(0, 4).join('/');
1116
return parts.slice(0, 5).join('/');
1217
case 'cdn.deno.land':
13-
if (parts[3] === 'std' && isolateStd) return parts.slice(0, 8).join('/');
18+
if (parts[3] === 'std' && opts.isolateStd) return parts.slice(0, 8).join('/');
1419
return parts.slice(0, 6).join('/');
1520
case 'crux.land':
1621
if (parts.length == 4) return `${url.origin}/${parts[3]}`;
@@ -75,22 +80,22 @@ export function determineModuleBase(fullUrl: string, isolateStd: boolean): strin
7580
return fullUrl;
7681
}
7782

78-
export function determineModuleLabel(module: CodeModule, isolateStd: boolean): string[] {
83+
export function determineModuleLabel(module: CodeModule, opts: RegistryOpts): string[] {
7984
const url = new URL(module.base);
8085
const parts = module.base.split('/');
8186
if (url.protocol !== 'https:') return [module.base];
8287
switch (url.host) {
8388
case 'deno.land': {
8489
let extra = new Array<string>();
85-
if (parts[3].startsWith('std') && !isolateStd) {
90+
if (parts[3].startsWith('std') && !opts.isolateStd) {
8691
const folders = new Set(module.files.map(x => x.url.split('/')[4]));
8792
extra = Array.from(folders).map(x => ` • /${x}`);
8893
}
8994
return ['/'+parts.slice(3).join('/'), ...extra];
9095
}
9196
case 'cdn.deno.land': {
9297
let extra = new Array<string>();
93-
if (parts[3] === 'std' && !isolateStd) {
98+
if (parts[3] === 'std' && !opts.isolateStd) {
9499
const folders = new Set(module.files.map(x => x.url.split('/')[7]));
95100
extra = Array.from(folders).map(x => ` • /${x}`);
96101
}

lib/module-registries_test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
33

44
Deno.test('gist', () => {
55
const gistBase = 'https://gist.githubusercontent.com/danopia/d8b92fdbaa146133dac74a248e62d761/raw/bf5074703f24fee4c2f08577908115f2a6ffff6a';
6-
const gistUrl = `${gistBase}/repro.ts`;
6+
const mainModule = `${gistBase}/repro.ts`;
7+
8+
assertEquals(determineModuleBase(mainModule, { mainModule }), gistBase);
79

8-
assertEquals(determineModuleBase(gistUrl, false), gistBase);
910
assertEquals(determineModuleLabel({
1011
base: gistBase, fragment: '',
1112
deps: new Set(), depsUnversioned: new Set(),
1213
files: [], totalSize: 0,
13-
}, false), [
14+
}, { mainModule }), [
1415
"gist: danopia/d8b92fdbaa146133dac74a248e62d761",
1516
" @ bf5074703f24fee4c2f08577908115f2a6ffff6a",
1617
]);

0 commit comments

Comments
 (0)