Skip to content

Commit a9eb4f2

Browse files
Fix broken add command (#4)
* specify type of nsids * Remove unused import * Encapsulate and parse manifest properly --------- Co-authored-by: Tom Sherman <[email protected]>
1 parent eb38449 commit a9eb4f2

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

deno.lock

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/deno.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@needle-di/core": "npm:@needle-di/core@^0.11.2",
1010
"@cliffy/command": "jsr:@cliffy/[email protected]",
1111
"@std/fs": "jsr:@std/fs@^1.0.13",
12-
"@std/fmt": "jsr:@std/fmt@^1.0.5"
12+
"@std/fmt": "jsr:@std/fmt@^1.0.5",
13+
"zod": "npm:zod@^3.24.2"
1314
}
1415
}

packages/cli/src/commands.ts

+41-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { inject, injectable } from "@needle-di/core";
22
import {
3+
NodeFactory,
34
NodeRegistry,
45
NSIDPattern,
56
NSIDPatternResolver,
@@ -10,6 +11,7 @@ import { emptyDir, ensureFile, exists } from "@std/fs";
1011
import { Command } from "@cliffy/command";
1112
import * as inputTypes from "./types.ts";
1213
import * as fmt from "@std/fmt/colors";
14+
import { z } from "zod";
1315

1416
@injectable()
1517
export class FileSystem {
@@ -30,16 +32,48 @@ export class FileSystem {
3032
}
3133
}
3234

35+
const Manifest = z.object({
36+
lexicons: z.array(z.string()),
37+
});
38+
type Manifest = z.infer<typeof Manifest>;
39+
3340
@injectable()
3441
class ResolutionsDir {
3542
constructor(private fs = inject(FileSystem)) {}
3643

37-
async writeResolution(resolution: Extract<Resolution, { success: true }>) {
44+
getRoot() {
3845
const manifestDir = this.fs.cwd();
3946
if (!manifestDir) {
4047
throw new Error("Could not determine manifest directory");
4148
}
4249

50+
return manifestDir;
51+
}
52+
53+
// TODO: Configure this
54+
async getManifest() {
55+
const manifestDir = this.getRoot();
56+
57+
const manifestPath = manifestDir + "/lexicons.json";
58+
59+
return (await this.fs.exists(manifestPath))
60+
? Manifest.parse(JSON.parse(await this.fs.readText(manifestPath)))
61+
: { lexicons: [] };
62+
}
63+
64+
async writeManifest(manifest: Manifest) {
65+
const manifestDir = this.fs.cwd();
66+
if (!manifestDir) {
67+
throw new Error("Could not determine manifest directory");
68+
}
69+
70+
const manifestPath = manifestDir + "/lexicons.json";
71+
await this.fs.writeText(manifestPath, JSON.stringify(manifest, null, 2));
72+
}
73+
74+
async writeResolution(resolution: Extract<Resolution, { success: true }>) {
75+
const manifestDir = this.getRoot();
76+
4377
const path = `${manifestDir}/lexicons/${
4478
resolution.nsid.segments.join(
4579
"/",
@@ -59,9 +93,9 @@ export type CommandDescriptor = {
5993
@injectable()
6094
export class FetchCommand implements CommandDescriptor {
6195
constructor(
62-
private fs = inject(FileSystem),
6396
private registry = inject(NodeRegistry),
6497
private resolutionsDir = inject(ResolutionsDir),
98+
private nodeFactory = inject(NodeFactory),
6599
) {}
66100

67101
name = "fetch";
@@ -70,19 +104,14 @@ export class FetchCommand implements CommandDescriptor {
70104
.action(() => this.#action());
71105

72106
async #action() {
73-
// TODO: Configure this
74-
const manifestDir = this.fs.cwd();
75-
if (!manifestDir) {
76-
throw new Error("Could not determine manifest directory");
77-
}
78-
const manifestPath = manifestDir + "/lexicons.json";
79-
const nsids = JSON.parse(await this.fs.readText(manifestPath)).lexicons.map(
80-
(nsid: string) => NSID.parse(nsid),
107+
const manifest = await this.resolutionsDir.getManifest();
108+
const roots = manifest.lexicons.map(
109+
(nsid: string) => this.nodeFactory.create(NSID.parse(nsid)),
81110
);
82111

83-
await emptyDir(`${manifestDir}/lexicons`);
112+
await emptyDir(this.resolutionsDir.getRoot() + "/lexicons");
84113

85-
for await (const resolution of this.registry.resolve(nsids)) {
114+
for await (const resolution of this.registry.resolve(roots)) {
86115
if (!resolution.success) {
87116
console.error("failed to resolve ", resolution.errorCode);
88117
continue;

0 commit comments

Comments
 (0)