Skip to content

Commit

Permalink
feat(mod)!: add cwd options to collect
Browse files Browse the repository at this point in the history
BRAKING CHANGE: `findImportMap` option is abandoned. `collect` automatically
finds `deno.json` or `deno.jsonc` in the current working directory or its
parent directories.
  • Loading branch information
hasundue committed Mar 5, 2024
1 parent 2243937 commit 4590c06
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
1 change: 0 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ async function collectUpdates(
const updates = await Promise.all(
entrypoints.map(async (entrypoint) =>
await collect(entrypoint, {
findImportMap: options.importMap === undefined,
ignore: options.ignore
? (dep) => options.ignore!.some((it) => dep.name.includes(it))
: undefined,
Expand Down
2 changes: 1 addition & 1 deletion lib/file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ WriteTextFileStub.create(fs);
async function test(path: string, name = toName(path)) {
const updates = await DependencyUpdate.collect(
new URL(path, import.meta.url),
{ findImportMap: true },
{ cwd: new URL(dirname(path), import.meta.url) },
);
const results = associateByFile(updates);

Expand Down
22 changes: 13 additions & 9 deletions lib/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { distinct } from "./std/collections.ts";
import { dirname, fromFileUrl } from "./std/path.ts";
import { fromFileUrl } from "./std/path.ts";
import {
createGraph,
type CreateGraphOptions,
Expand Down Expand Up @@ -63,9 +63,18 @@ class DenoGraph {
}

export interface CollectOptions {
/**
* The working directory to resolve relative paths.
* If not specified, the current working directory is used.
* At present, this option is only used to find the import map.
*
* @example "/path/to/project"
*/
cwd?: string | URL;
/**
* The path to the import map used to resolve dependencies.
* If not specified, deno.json or deno.jsonc in the root directory of the module is used.
* If not specified, molt will automatically find deno.json or deno.jsonc
* in the current working directory or parent directories.
*
* @example
* ```ts
Expand All @@ -76,10 +85,6 @@ 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 @@ -120,9 +125,7 @@ export async function collect(
const urls = froms.map((path) => toUrl(path));

const importMapPath = options.importMap ??
(options.findImportMap
? await findFileUp(dirname(urls[0]), "deno.json", "deno.jsonc")
: undefined);
await findFileUp(options.cwd ?? Deno.cwd(), "deno.json", "deno.jsonc");

const importMap = importMapPath
? await tryReadFromJson(toUrl(importMapPath))
Expand Down Expand Up @@ -157,6 +160,7 @@ const load: NonNullable<CreateGraphOptions["load"]> = async (
switch (url.protocol) {
case "node:":
case "npm:":
case "jsr:":
return {
kind: "external",
specifier,
Expand Down
35 changes: 24 additions & 11 deletions lib/update_test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { dirname } from "./std/path.ts";
import { assertEquals, assertThrows } from "./std/assert.ts";
import { filterKeys } from "./std/collections.ts";
import { basename } from "./std/path.ts";
import { assertSnapshot } from "./testing.ts";
import { LatestSemVerStub } from "./testing.ts";
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), {
findImportMap: true,
});
for (const update of updates) {
await assertUpdateSnapshot(t, update);
}
});
function test(
path: string,
name = basename(path),
variation?: string,
) {
Deno.test(
"collect - " + (variation ? `${name} - ${variation}` : name),
async (t) => {
const updates = await collect(new URL(path, import.meta.url), {
cwd: new URL(dirname(path), import.meta.url),
});
for (const update of updates) {
await assertUpdateSnapshot(t, update);
}
},
);
}

async function assertUpdateSnapshot(
t: Deno.TestContext,
Expand Down Expand Up @@ -45,8 +54,12 @@ for await (
new URL("../test/data/" + testCase.name, import.meta.url),
)
) {
if (entry.isFile && entry.name === "mod.ts") {
test(`../test/data/${testCase.name}/mod.ts`, testCase.name);
if (entry.isFile && ["mod.ts", "deno.json"].includes(entry.name)) {
test(
`../test/data/${testCase.name}/${entry.name}`,
testCase.name,
entry.name,
);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions test/snapshots/update_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ snapshot[`collect - multiple_imports.ts 3`] = `
}
`;

snapshot[`collect - import_map_referred 1`] = `
snapshot[`collect - import_map_referred - mod.ts 1`] = `
{
code: {
span: {
Expand Down Expand Up @@ -180,7 +180,7 @@ snapshot[`collect - updated_import_and_outdated_export.ts 1`] = `
}
`;

snapshot[`collect - import_map 1`] = `
snapshot[`collect - import_map - mod.ts 1`] = `
{
code: {
span: {
Expand Down Expand Up @@ -210,7 +210,7 @@ snapshot[`collect - import_map 1`] = `
}
`;

snapshot[`collect - import_map 2`] = `
snapshot[`collect - import_map - mod.ts 2`] = `
{
code: {
span: {
Expand Down Expand Up @@ -240,7 +240,7 @@ snapshot[`collect - import_map 2`] = `
}
`;

snapshot[`collect - import_map 3`] = `
snapshot[`collect - import_map - mod.ts 3`] = `
{
code: {
span: {
Expand Down Expand Up @@ -360,7 +360,7 @@ snapshot[`collect - import_and_export.ts 2`] = `
}
`;

snapshot[`collect - import_map_no_resolve 1`] = `
snapshot[`collect - import_map_no_resolve - mod.ts 1`] = `
{
code: {
span: {
Expand Down Expand Up @@ -390,7 +390,7 @@ snapshot[`collect - import_map_no_resolve 1`] = `
}
`;

snapshot[`collect - relative_import 1`] = `
snapshot[`collect - relative_import - mod.ts 1`] = `
{
code: {
span: {
Expand Down Expand Up @@ -450,7 +450,7 @@ snapshot[`collect - updated_and_outdated.ts 1`] = `
}
`;

snapshot[`collect - multiple_modules 1`] = `
snapshot[`collect - multiple_modules - mod.ts 1`] = `
{
code: {
span: {
Expand Down Expand Up @@ -480,7 +480,7 @@ snapshot[`collect - multiple_modules 1`] = `
}
`;

snapshot[`collect - multiple_modules 2`] = `
snapshot[`collect - multiple_modules - mod.ts 2`] = `
{
code: {
span: {
Expand Down Expand Up @@ -510,7 +510,7 @@ snapshot[`collect - multiple_modules 2`] = `
}
`;

snapshot[`collect - multiple_modules 3`] = `
snapshot[`collect - multiple_modules - mod.ts 3`] = `
{
code: {
span: {
Expand Down Expand Up @@ -540,7 +540,7 @@ snapshot[`collect - multiple_modules 3`] = `
}
`;

snapshot[`collect - multiple_modules 4`] = `
snapshot[`collect - multiple_modules - mod.ts 4`] = `
{
code: {
span: {
Expand Down

0 comments on commit 4590c06

Please sign in to comment.