Skip to content

Commit

Permalink
chore(lib): Prepare for more configurabilitty
Browse files Browse the repository at this point in the history
  • Loading branch information
danopia committed Jan 29, 2022
1 parent c1b6de7 commit 45409fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deno-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ jobs:
- name: Check entrypoint
run: time deno cache server.ts

- name: Run any tests
run: time deno test

# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:
Expand Down
27 changes: 15 additions & 12 deletions lib/module-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import * as registries from "./module-registries.ts";

export class ModuleMap {
modules = new Map<string,CodeModule>();
mainModule: CodeModule | null = null;
mainFile: string | null = null;
mainModule: CodeModule;
registryOpts: registries.RegistryOpts;

constructor(
public args: URLSearchParams,
public redirects: Record<string,string>,
public rootNode: DenoModule,
) {
this.isolateStd = this.args.get('std') === 'isolate';
this.registryOpts = {
mainModule: rootNode.specifier,
isolateStd: this.args.get('std') === 'isolate',
}

this.mainModule = this.grabModFor(
rootNode.specifier,
rootNode.error ? '#error' : undefined);
}
isolateStd: boolean;

grabModFor(url: string, fragment: string = '') {
const wireUrl = url.split('#')[0];
const actualUrl = this.redirects[wireUrl] || wireUrl;
const base = registries.determineModuleBase(actualUrl, this.isolateStd);
const base = registries.determineModuleBase(actualUrl, this.registryOpts);
let moduleInfo = this.modules.get(base + fragment);
if (!moduleInfo) {
moduleInfo = {
Expand Down Expand Up @@ -79,7 +86,7 @@ export class ModuleMap {
for (const module of this.modules.values()) {
modules[module.base+module.fragment] = {
moduleDeps: Array.from(module.deps).map(x => x.base+x.fragment),
labelText: registries.determineModuleLabel(module, this.isolateStd),
labelText: registries.determineModuleLabel(module, this.registryOpts),
totalSize: module.totalSize,
fileCount: module.files.length,
errors: module.errors,
Expand All @@ -96,7 +103,7 @@ export class ModuleMap {
for (const module of this.modules.values()) {
// console.log(module.base, Array.from(module.deps.values()).map(x => x.base));

const labels = registries.determineModuleLabel(module, this.isolateStd);
const labels = registries.determineModuleLabel(module, this.registryOpts);
if (module.errors) {
labels.unshift(`${module.errors.length} FAILED IMPORTS FROM:`);
for (const err of module.errors) {
Expand Down Expand Up @@ -171,17 +178,13 @@ export class ModuleMap {
}

export function processDenoInfo(data: DenoInfo, args?: URLSearchParams) {
const map = new ModuleMap(args ?? new URLSearchParams, data.redirects);

// TODO: when are there multiple roots?
const roots = data.roots.map(x => data.redirects[x] || x);
const rootNode = data.modules.find(x => roots.includes(x.specifier));
if (!rootNode) throw new Error(
`I didn't find a root node in the Deno graph! This is a module-visualizer bug.`);

map.mainModule = map.grabModFor(rootNode.specifier, rootNode.error ? '#error' : undefined);
map.mainFile = rootNode.specifier;

const map = new ModuleMap(args ?? new URLSearchParams, data.redirects, rootNode);
for (const info of data.modules) {
map.addFile(info.specifier, info, data);
}
Expand Down
17 changes: 11 additions & 6 deletions lib/module-registries.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import type { CodeModule } from "./types.ts";

export function determineModuleBase(fullUrl: string, isolateStd: boolean): string {
export interface RegistryOpts {
mainModule: string;
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 !== 'https:') return fullUrl;
switch (url.host) {
case 'deno.land':
if (parts[3].startsWith('std') && !isolateStd) return parts.slice(0, 4).join('/');
if (parts[3].startsWith('std') && !opts.isolateStd) return parts.slice(0, 4).join('/');
return parts.slice(0, 5).join('/');
case 'cdn.deno.land':
if (parts[3] === 'std' && isolateStd) return parts.slice(0, 8).join('/');
if (parts[3] === 'std' && opts.isolateStd) return parts.slice(0, 8).join('/');
return parts.slice(0, 6).join('/');
case 'crux.land':
if (parts.length == 4) return `${url.origin}/${parts[3]}`;
Expand Down Expand Up @@ -75,22 +80,22 @@ export function determineModuleBase(fullUrl: string, isolateStd: boolean): strin
return fullUrl;
}

export function determineModuleLabel(module: CodeModule, isolateStd: boolean): string[] {
export function determineModuleLabel(module: CodeModule, opts: RegistryOpts): string[] {
const url = new URL(module.base);
const parts = module.base.split('/');
if (url.protocol !== 'https:') return [module.base];
switch (url.host) {
case 'deno.land': {
let extra = new Array<string>();
if (parts[3].startsWith('std') && !isolateStd) {
if (parts[3].startsWith('std') && !opts.isolateStd) {
const folders = new Set(module.files.map(x => x.url.split('/')[4]));
extra = Array.from(folders).map(x => ` • /${x}`);
}
return ['/'+parts.slice(3).join('/'), ...extra];
}
case 'cdn.deno.land': {
let extra = new Array<string>();
if (parts[3] === 'std' && !isolateStd) {
if (parts[3] === 'std' && !opts.isolateStd) {
const folders = new Set(module.files.map(x => x.url.split('/')[7]));
extra = Array.from(folders).map(x => ` • /${x}`);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/module-registries_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test('gist', () => {
const gistBase = 'https://gist.githubusercontent.com/danopia/d8b92fdbaa146133dac74a248e62d761/raw/bf5074703f24fee4c2f08577908115f2a6ffff6a';
const gistUrl = `${gistBase}/repro.ts`;
const mainModule = `${gistBase}/repro.ts`;

assertEquals(determineModuleBase(mainModule, { mainModule }), gistBase);

assertEquals(determineModuleBase(gistUrl, false), gistBase);
assertEquals(determineModuleLabel({
base: gistBase, fragment: '',
deps: new Set(), depsUnversioned: new Set(),
files: [], totalSize: 0,
}, false), [
}, { mainModule }), [
"gist: danopia/d8b92fdbaa146133dac74a248e62d761",
" @ bf5074703f24fee4c2f08577908115f2a6ffff6a",
]);
Expand Down

0 comments on commit 45409fe

Please sign in to comment.