Skip to content

Commit

Permalink
fix(cli): ensure updates displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jul 26, 2024
1 parent e86edb7 commit 1b4c7a7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { collect } from "@molt/core";
import type { Update } from "@molt/core/types";

import { printChangelog } from "./src/changelog.ts";
import { findConfig, findLock } from "./src/files.ts";
import { findConfig, findLock, findSource } from "./src/files.ts";
import { runTasks } from "./src/tasks.ts";
import { print, printRefs } from "./src/updates.ts";

Expand Down Expand Up @@ -53,6 +53,8 @@ main.action(async function (options, ...source) {
? undefined
: options.lock ?? await findLock();

source = source.length ? source : config ? [] : await findSource();

if (options.dryRun) {
const paths = [config, lock, ...source].filter((it) => it != null);
await import("./src/mock.ts").then((m) => m.mock(paths));
Expand Down
19 changes: 19 additions & 0 deletions cli/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ describe("CLI", () => {
`,
);
});

it("should find updates in modules with `--no-config`", async () => {
const { stderr, stdout } = await molt("--no-config");
assertEquals(
stdout,
dedent`
📦 @conventional-commits/parser 0.4.0 → 0.4.1
📦 @luca/flag 1.0.0 → 1.0.1
📦 deno.land/std 0.222.0 → 0.224.0
`,
);
assertEquals(
stderr,
dedent`
Collecting dependencies
Fetching updates
`,
);
});
});
22 changes: 20 additions & 2 deletions cli/src/files.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { exists } from "@std/fs";
import { parse } from "@std/jsonc";

export async function findConfig() {
if (await exists("deno.json")) {
if (await exists("deno.json") && await hasImports("deno.json")) {
return "deno.json";
} else if (await exists("deno.jsonc")) {
}
if (await exists("deno.jsonc") && await hasImports("deno.jsonc")) {
return "deno.jsonc";
}
}

async function hasImports(config: string): Promise<boolean> {
if (!config) return false;
const jsonc = parse(await Deno.readTextFile(config));
return jsonc !== null && typeof jsonc === "object" && "imports" in jsonc;
}

export async function findLock() {
if (await exists("deno.lock")) {
return "deno.lock";
}
}

export async function findSource() {
const source: string[] = [];
for await (const entry of Deno.readDir(".")) {
if (entry.isFile && entry.name.endsWith(".ts")) {
source.push(entry.name);
}
}
return source;
};
26 changes: 18 additions & 8 deletions cli/src/updates.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { colors } from "@cliffy/ansi";
import type { Update } from "@molt/core/types";
import * as SemVer from "@std/semver";

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

export function print(update: Update) {
const { constraint, lock } = update;
let output = `📦 ${bold(update.dep.name)}`;

if (constraint && !lock) {
output += cyan(` ${constraint.from}${constraint.to}`);
return output;
}
if (lock) {
output += yellow(` ${lock.from}${lock.to}`);
const versions = constraint && SemVer.tryParse(constraint.to)
? constraint
: lock;

if (versions) {
output += yellow(` ${versions.from}${versions.to}`);
}
if (constraint && constraint.to !== lock?.to) {
output += cyan(` (${constraint.from}${constraint.to})`);

const ranges = constraint && !SemVer.tryParse(constraint.to)
? constraint
: undefined;

if (ranges) {
output += " ";
if (versions) output += cyan("(");
output += cyan(`${ranges.from}${ranges.to}`);
if (versions) output += cyan(")");
}

console.log(output);
}

Expand Down
32 changes: 31 additions & 1 deletion core/bumps_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { get } from "./bumps.ts";
import { parse } from "./specs.ts";

describe("bump", () => {
it("should determine the version bump for a package", () => {
it("should bump a constrianted jsr dep with a lock", () => {
assertEquals(
get(
{ ...parse("jsr:@std/jsonc@^0.222.1"), locked: "0.222.1" },
Expand All @@ -13,4 +13,34 @@ describe("bump", () => {
{ constraint: "^0.224.0", lock: "0.224.3" },
);
});

it("should bump a constrianted jsr dep without a lock", () => {
assertEquals(
get(
parse("jsr:@std/jsonc@^0.222.1"),
{ latest: "1.0.0-rc.3", released: "0.224.3" },
),
{ constraint: "^0.224.0" },
);
});

it("should bump a fixed jsr dep with a lock", () => {
assertEquals(
get(
{ ...parse("jsr:@std/jsonc@0.222.1"), locked: "0.222.1" },
{ latest: "1.0.0-rc.3", released: "0.224.3" },
),
{ constraint: "0.224.3", lock: "0.224.3" },
);
});

it("should bump a fixed jsr dep without a lock", () => {
assertEquals(
get(
parse("jsr:@std/jsonc@0.222.1"),
{ latest: "1.0.0-rc.3", released: "0.224.3" },
),
{ constraint: "0.224.3" },
);
});
});
8 changes: 8 additions & 0 deletions core/updates_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("get", () => {
});
});

it("should get an update to a fixed jsr dep", async () => {
const dep = parse("jsr:@molt/core@0.18.0");
assertEquals(await get(dep), {
released: "0.18.5",
latest,
});
});

it("should get an update to a jsr dep", async () => {
const dep = parse("jsr:@molt/core@^0.18.0");
assertEquals(await get(dep), {
Expand Down

0 comments on commit 1b4c7a7

Please sign in to comment.