Skip to content
This repository was archived by the owner on Mar 30, 2026. It is now read-only.

Commit ad4790a

Browse files
refactor: ensure CollieRepository always uses an absolute path
1 parent 7931411 commit ad4790a

5 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/commands/init.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function registerInitCommand(program: TopLevelCommand) {
2323

2424
// we would like to create the CollieRepository after git has been initialized
2525
// but we need it to build git
26-
const kit = new CollieRepository(directory);
26+
const kit = CollieRepository.uninitialized(directory);
2727
const logger = new Logger(kit, opts);
2828

2929
// ensure git is initialized

src/commands/kit/import.command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function registerImportCmd(program: TopLevelCommand) {
7979

8080
async function promptForKitModuleId(logger: Logger, hubRepoDir: string) {
8181
// note: the hub is a standard collie repository for the most part, so we can just parse it with the same code
82-
const repo = new CollieRepository(hubRepoDir);
82+
const repo = await CollieRepository.load(hubRepoDir);
8383
const validator = new ModelValidator(logger);
8484

8585
const moduleRepo = await KitModuleRepository.load(repo, validator, logger);

src/model/CollieRepository.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { assertEquals } from "std/testing/assert";
1+
import { assertEquals, assertNotEquals } from "std/testing/assert";
22
import { CollieRepository } from "./CollieRepository.ts";
33

44
Deno.test("relativePath calculates paths relative to repository root", () => {
5-
const sut = new CollieRepository("/tmp/test");
5+
const sut = CollieRepository.uninitialized("/tmp/test");
66

77
const result = sut.relativePath("/tmp/test/123");
88
assertEquals(result, "123");
99
});
10+
11+
Deno.test("relativePath does not work with paths that are already relative", () => {
12+
const sut = CollieRepository.uninitialized("/tmp/test");
13+
14+
const result = sut.relativePath("123");
15+
assertNotEquals(result, "123");
16+
});

src/model/CollieRepository.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import * as fs from "std/fs";
22
import * as path from "std/path";
33

44
export class CollieRepository {
5-
constructor(private readonly repoDir: string) {}
5+
private constructor(private readonly repoDir: string) {
6+
if (!path.isAbsolute(repoDir)) {
7+
throw new Error(
8+
`Tried to load CollieRepository with an invalid path. '${repoDir}' is not an absolute path.`,
9+
);
10+
}
11+
}
612

713
/**
814
* Resolve a path relative to the kit repository
@@ -11,7 +17,10 @@ export class CollieRepository {
1117
return path.resolve(this.repoDir, ...pathSegments);
1218
}
1319

14-
/** */
20+
/**
21+
* Calculate a relative path between the repoDir
22+
* @param toPath an absolute path
23+
*/
1524
relativePath(toPath: string) {
1625
return path.relative(this.repoDir, toPath);
1726
}
@@ -55,4 +64,10 @@ export class CollieRepository {
5564

5665
return new CollieRepository(absolutePath);
5766
}
67+
68+
static uninitialized(dir: string) {
69+
const absolutePath = path.resolve(dir);
70+
71+
return new CollieRepository(absolutePath);
72+
}
5873
}

src/model/schemas/ModelValidator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ModelValidator } from "./ModelValidator.ts";
66
import { assertEquals } from "std/testing/assert";
77

88
Deno.test("can validate FoundationConfig", () => {
9-
const collie = new CollieRepository("./");
9+
const collie = CollieRepository.uninitialized("./");
1010
const logger = new Logger(collie, {
1111
debug: false,
1212
verbose: false,

0 commit comments

Comments
 (0)