Skip to content

Commit

Permalink
chore: fix update.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Nov 25, 2023
1 parent eb36afa commit 7cca8cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
3 changes: 2 additions & 1 deletion lib/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export interface UpdatedDependency extends Dependency {
* // -> { name: "deno.land/std", version: "0.200.0", path: "/fs/mod.ts" }
* ```
*/
export function parse(url: URL): Dependency {
export function parse(url: string | URL): Dependency {
url = new URL(url);
const protocol = url.protocol;
const body = url.hostname + url.pathname;
const semver = SemVerString.extract(url.href);
Expand Down
2 changes: 1 addition & 1 deletion lib/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const isImportMapReferrer = is.ObjectOf({
* Read an import map from the given file path or URL.
* @param url - The URL of the import map.
* @return The import map object if found.
* @throws {SyntaxError} If the import map is not a valid JSON.
* @throws {SyntaxError} If the file does not have a valid import map.
*/
export async function readFromJson(
url: string | URL,
Expand Down
52 changes: 28 additions & 24 deletions lib/update.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { distinct } from "./std/collections.ts";
import { fromFileUrl } from "./std/path.ts";
import { dirname, fromFileUrl } from "./std/path.ts";
import {
createGraph,
type CreateGraphOptions,
init as initDenoGraph,
load as defaultLoad,
type ModuleJson,
} from "./x/deno_graph.ts";
import { findFileUp, toUrl } from "./path.ts";
import { ImportMap, readFromJson } from "./import_map.ts";
import { findFileUp, toUrl, toPath } from "./path.ts";
import { ImportMap, tryReadFromJson } from "./import_map.ts";
import {
type Dependency,
parse,
Expand Down Expand Up @@ -36,14 +36,14 @@ export interface DependencyUpdate {
span: NonNullable<DependencyJson["code"]>["span"];
};
/** The URL of the module that imports the dependency. */
referrer: URL;
referrer: string;
/** Information about the import map used to resolve the dependency. */
map?: {
/** The URL of the import map used to resolve the dependency. */
source: URL;
/** The path to the import map used to resolve the dependency. */
source: string;
/** The string in the dependency specifier being replaced */
key: string;
/** The fully resolved specifier of the dependency. */
key?: string;
/** The fully resolved specifier (URI) of the dependency. */
resolved: string;
};
}
Expand Down Expand Up @@ -74,6 +74,10 @@ export interface CollectOptions {
* ```
*/
importMap?: string | URL;
/**
* If true, the import map is searched for in the parent directories of the first module specified.
*/
findImportMap?: boolean;
/**
* A function to filter out dependencies.
*
Expand Down Expand Up @@ -110,22 +114,21 @@ export async function collect(
from: string | URL | (string | URL)[],
options: CollectOptions = {},
): Promise<DependencyUpdate[]> {
const urls = [from].flat().map((path) => toUrl(path));

if (urls.length === 0) {
throw new Error("No module specified");
}
const froms = [from].flat();
const paths = froms.map((path) => toPath(path));
const urls = froms.map((path) => toUrl(path));

const importMapUrl = options.importMap
? toUrl(options.importMap)
: (urls.length === 1
? await findFileUp(urls[0], "deno.json", "deno.jsonc")
const importMapPath = options.importMap
?? (options.findImportMap
? await findFileUp(dirname(paths[0]), "deno.json", "deno.jsonc")
: undefined);

const importMap = importMapUrl ? await readFromJson(importMapUrl) : undefined;
const importMap = importMapPath
? await tryReadFromJson(toUrl(importMapPath))
: undefined;

await DenoGraph.ensureInit();
const graph = await createGraph(urls.map((url) => url.href), {
const graph = await createGraph(urls, {
load,
resolve: importMap?.resolveInner,
});
Expand All @@ -136,7 +139,7 @@ export async function collect(
m.dependencies?.map(async (dependency) => {
const update = await create(
dependency,
new URL(m.specifier),
m.specifier,
{ ...options, importMap },
);
return update ? updates.push(update) : undefined;
Expand Down Expand Up @@ -172,7 +175,7 @@ const load: NonNullable<CreateGraphOptions["load"]> = async (

async function create(
dependencyJson: DependencyJson,
referrer: URL,
referrer: string,
options?: Pick<CollectOptions, "ignore" | "only"> & {
importMap?: ImportMap;
},
Expand All @@ -184,13 +187,14 @@ async function create(
`The dependency ${dependencyJson.specifier} in ${
fromFileUrl(referrer)
} has no resolved specifier.`,
{ cause: dependencyJson },
);
}
const mapped = options?.importMap?.resolve(
dependencyJson.specifier,
referrer,
);
const dependency = parse(new URL(mapped?.resolved ?? specifier));
const dependency = parse(new URL(mapped?.value ?? specifier));
if (options?.ignore?.(dependency)) {
return;
}
Expand Down Expand Up @@ -221,8 +225,8 @@ async function create(
map: mapped
? {
source: options!.importMap!.url,
key: mapped.key!,
resolved: mapped.resolved!,
key: mapped.key,
resolved: mapped.resolved,
}
: undefined,
};
Expand Down
4 changes: 3 additions & 1 deletion lib/update_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { collect, DependencyUpdate, getVersionChange } from "./update.ts";

const test = (path: string, name = basename(path)) =>
Deno.test("collect - " + name, async (t) => {
const updates = await collect(new URL(path, import.meta.url));
const updates = await collect(new URL(path, import.meta.url), {
findImportMap: true,
});
for (const update of updates) {
await assertUpdateSnapshot(t, update);
}
Expand Down

0 comments on commit 7cca8cf

Please sign in to comment.