Skip to content

Commit

Permalink
fix(mod)!: critical bug in replacing specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Nov 3, 2023
1 parent 6ef3541 commit 86a102f
Show file tree
Hide file tree
Showing 15 changed files with 701 additions and 403 deletions.
26 changes: 13 additions & 13 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { URI } from "./lib/uri.ts";
import { DependencyUpdate } from "./lib/update.ts";
import { FileUpdate } from "./lib/file.ts";
import { GitCommitSequence } from "./lib/git.ts";
import { parseSemVer, resolveLatestURL } from "./lib/dependency.ts";
import { Dependency, parseSemVer } from "./lib/dependency.ts";

const { gray, yellow, bold, cyan } = colors;

Expand Down Expand Up @@ -150,20 +150,20 @@ function _list(updates: DependencyUpdate[]) {
console.log(`💡 Found ${updates.length > 1 ? "updates" : "an update"}:`);
const dependencies = new Map<string, DependencyUpdate[]>();
for (const u of updates) {
const list = dependencies.get(u.name) ?? [];
const list = dependencies.get(u.to.name) ?? [];
list.push(u);
dependencies.set(u.name, list);
dependencies.set(u.to.name, list);
}
for (const [name, list] of dependencies.entries()) {
console.log();
const froms = distinct(list.map((u) => u.version.from)).join(", ");
const froms = distinct(list.map((u) => u.from.version)).join(", ");
console.log(
`📦 ${bold(name)} ${yellow(froms)} => ${yellow(list[0].version.to)}`,
`📦 ${bold(name)} ${yellow(froms)} => ${yellow(list[0].to.version)}`,
);
distinct(
list.map((u) => {
const source = URI.relative(u.map?.source ?? u.referrer);
return ` ${source} ` + gray(u.version.from ?? "");
return ` ${source} ` + gray(u.from.version ?? "");
}),
).forEach((line) => console.log(line));
}
Expand All @@ -188,7 +188,7 @@ async function _write(
}
if (options?.report) {
const content = distinct(
updates.map((u) => `- ${u.name} ${u.version.from} => ${u.version.to}`),
updates.map((u) => `- ${u.to.name} ${u.from.version} => ${u.to.version}`),
).join("\n");
await Deno.writeTextFile(options.report, content);
console.log(`📄 ${options.report}`);
Expand All @@ -206,7 +206,7 @@ async function _commit(
},
) {
const commits = GitCommitSequence.from(updates, {
groupBy: (dependency) => dependency.name,
groupBy: (dependency) => dependency.to.name,
composeCommitMessage: ({ group, version }) =>
_formatPrefix(options.prefix) + `bump ${group}` +
(version?.from ? ` from ${version?.from}` : "") +
Expand Down Expand Up @@ -328,13 +328,13 @@ function _formatPrefix(prefix: string | undefined) {
return prefix ? prefix.trimEnd() + " " : "";
}

async function _version() {
async function versionCommand() {
const version = parseSemVer(import.meta.url) ??
await $.progress("Fetching version info").with(async () => {
const url = await resolveLatestURL(
new URL("https://deno.land/x/molt/cli.ts"),
const latest = await Dependency.resolveLatest(
Dependency.parse(new URL("https://deno.land/x/molt/cli.ts")),
);
return url ? parseSemVer(url.href) : undefined;
return latest ? latest.version : undefined;
}) ?? "unknown";
console.log(version);
}
Expand All @@ -348,7 +348,7 @@ const main = new Command()
.versionOption(
"-v, --version",
"Print version info.",
_version,
versionCommand,
)
.command("check", checkCommand)
.command("update", updateCommand);
Expand Down
147 changes: 140 additions & 7 deletions lib/__snapshots__/file_test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,21 +1,154 @@
export const snapshot = {};

snapshot[`writeAll > direct import 1`] = `
'import { VERSION } from "https://deno.land/std@0.205.0/version.ts";
[
{
dependencies: [
{
code: {
span: {
end: {
character: 84,
line: 0,
},
start: {
character: 29,
line: 0,
},
},
specifier: "https://deno.land/std@0.200.0/assert/assert_exists.ts",
},
from: {
name: "deno.land/std",
path: "/assert/assert_exists.ts",
scheme: "https://",
version: "0.200.0",
},
map: undefined,
referrer: "file:///home/shun/molt/test/fixtures/direct-import/lib.ts",
to: {
name: "deno.land/std",
path: "/assert/assert_exists.ts",
scheme: "https://",
version: "0.205.0",
},
},
],
kind: "module",
specifier: "file:///home/shun/molt/test/fixtures/direct-import/lib.ts",
},
{
dependencies: [
{
code: {
span: {
end: {
character: 74,
line: 1,
},
start: {
character: 28,
line: 1,
},
},
specifier: "https://deno.land/x/deno_graph@0.50.0/mod.ts",
},
from: {
name: "deno.land/x/deno_graph",
path: "/mod.ts",
scheme: "https://",
version: "0.50.0",
},
map: undefined,
referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts",
to: {
name: "deno.land/x/deno_graph",
path: "/mod.ts",
scheme: "https://",
version: "0.59.2",
},
},
{
code: {
span: {
end: {
character: 40,
line: 2,
},
start: {
character: 18,
line: 2,
},
},
specifier: "npm:node-emoji@2.0.0",
},
from: {
name: "node-emoji",
path: "",
scheme: "npm:",
version: "2.0.0",
},
map: undefined,
referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts",
to: {
name: "node-emoji",
path: "",
scheme: "npm:",
version: "2.1.0",
},
},
{
code: {
span: {
end: {
character: 84,
line: 0,
},
start: {
character: 29,
line: 0,
},
},
specifier: "https://deno.land/std@0.200.0/assert/assert_equals.ts",
},
from: {
name: "deno.land/std",
path: "/assert/assert_equals.ts",
scheme: "https://",
version: "0.200.0",
},
map: undefined,
referrer: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts",
to: {
name: "deno.land/std",
path: "/assert/assert_equals.ts",
scheme: "https://",
version: "0.205.0",
},
},
],
kind: "module",
specifier: "file:///home/shun/molt/test/fixtures/direct-import/mod.ts",
},
]
`;

snapshot[`writeAll > direct import 2`] = `
'import { assertEquals } from "https://deno.land/std@0.205.0/assert/assert_equals.ts";
import { createGraph } from "https://deno.land/x/deno_graph@0.59.2/mod.ts";
import { emojify } from "npm:node-emoji@2.1.0";
import emoji from "npm:node-emoji@2.1.0";
import { noop } from "./lib.ts";'
`;

snapshot[`writeAll > direct import 2`] = `
'import { VERSION } from "https://deno.land/std@0.205.0/version.ts";
snapshot[`writeAll > direct import 3`] = `
'import { assertExists } from "https://deno.land/std@0.205.0/assert/assert_exists.ts";
export const noop = () => {};'
`;

snapshot[`writeAll > import map 1`] = `
'{
"imports": {
"std/": "https://deno.land/std@0.200.0/",
"std/": "https://deno.land/std@0.205.0/",
"deno_graph": "https://deno.land/x/deno_graph@0.59.2/mod.ts",
"node-emoji": "npm:node-emoji@2.1.0",
"/": "./"
Expand All @@ -28,10 +161,10 @@ snapshot[`writeAll > import map 2`] = `
export const noop = () => {};'
`;

snapshot[`writeAll > import map with no resolve 1`] = `'import {} from "https://deno.land/std@0.205.0/version.ts";'`;
snapshot[`writeAll > import map with no resolve 1`] = `'import {} from "https://deno.land/std@0.205.0/testing/asserts.ts";'`;

snapshot[`writeAll > unversioned specifiers 1`] = `
'import { VERSION } from "https://deno.land/std@0.205.0/version.ts";
'import { VERSION } from "https://deno.land/std@0.205.0";
import { createGraph } from "https://deno.land/x/deno_graph@0.59.2/mod.ts";
import { emojify } from "npm:node-emoji@2.1.0";
import { noop } from "./lib.ts";'
Expand Down
Loading

0 comments on commit 86a102f

Please sign in to comment.