Skip to content

Commit

Permalink
feat(core): give stringify an option to exclude version
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed May 22, 2024
1 parent c63b070 commit a0a2af3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
24 changes: 17 additions & 7 deletions core/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,17 @@ function addSeparator(protocol: string): string {
*/
export function stringify(
dependency: Dependency,
include: { protocol?: boolean; path?: boolean } = {},
include: { protocol?: boolean; version?: boolean; path?: boolean } = {},
): string {
include.protocol ??= true;
const header = include.protocol ? addSeparator(dependency.protocol) : "";
include = { protocol: true, version: true, path: true, ...include };

const version = dependency.version ? "@" + dependency.version : "";
const path = dependency.path;
const header = include.protocol ? addSeparator(dependency.protocol) : "";
const version = include.version
? dependency.version ? "@" + dependency.version : ""
: "";
const path = include.path ? dependency.path : "";

include.path ??= true;
return `${header}${dependency.name}${version}` + (include.path ? path : "");
return `${header}${dependency.name}${version}` + path;
}

export function hasVersionRange(
Expand Down Expand Up @@ -178,6 +179,8 @@ export async function resolveLatestVersion(
: undefined;
const cached = cache?.get(dependency.name);
if (cached) {
console.log(dependency);
console.log(cached);
return { ...cached, path: dependency.path };
}
if (cached === null) {
Expand Down Expand Up @@ -217,6 +220,13 @@ class LatestVersionCache implements Disposable {
}
}

function reparseUnversionedWithCached(
dependency: Dependency & { version: undefined },
cached: UpdatedDependency,
) {
const { name, version, path } = cached;
}

async function _resolveLatestVersion(
dependency: Dependency,
): Promise<UpdatedDependency | undefined> {
Expand Down
63 changes: 62 additions & 1 deletion core/dependency_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { afterAll, beforeAll, describe, it } from "@std/testing/bdd";
import { assertEquals, assertExists, assertObjectMatch } from "@std/assert";
import { LatestVersionStub } from "@molt/lib/testing";
import { isPreRelease, parse, resolveLatestVersion } from "./dependency.ts";
import {
isPreRelease,
parse,
resolveLatestVersion,
stringify,
} from "./dependency.ts";

describe("parse", () => {
it("deno.land/std", () =>
Expand Down Expand Up @@ -73,6 +78,62 @@ describe("parse", () => {
));
});

describe("stringify", () => {
it("full", () =>
assertEquals(
stringify({
protocol: "https:",
name: "deno.land/std",
version: "0.1.0",
path: "/version.ts",
}),
"https://deno.land/std@0.1.0/version.ts",
));

it("no version", () =>
assertEquals(
stringify({
protocol: "https:",
name: "deno.land/std",
path: "/version.ts",
}, { version: false }),
"https://deno.land/std/version.ts",
));

it("no path", () =>
assertEquals(
stringify({
protocol: "https:",
name: "deno.land/std",
version: "0.1.0",
path: "/version.ts",
}, { path: false }),
"https://deno.land/std@0.1.0",
));

it("no protocol", () =>
assertEquals(
stringify({
protocol: "https:",
name: "deno.land/std",
version: "0.1.0",
path: "/version.ts",
}, { protocol: false }),
"deno.land/std@0.1.0/version.ts",
));

it("name only", () =>
assertEquals(
stringify({
protocol: "https:",
name: "deno.land/std",
version: "0.1.0",
path: "/version.ts",
}, { protocol: false, version: false, path: false }),
"deno.land/std",
));
});

Deno.test("isPreRelease", () => {
assertEquals(
isPreRelease("0.1.0"),
Expand Down

0 comments on commit a0a2af3

Please sign in to comment.