1
1
import { inject , injectable } from "@needle-di/core" ;
2
2
import {
3
+ NodeFactory ,
3
4
NodeRegistry ,
4
5
NSIDPattern ,
5
6
NSIDPatternResolver ,
@@ -10,6 +11,7 @@ import { emptyDir, ensureFile, exists } from "@std/fs";
10
11
import { Command } from "@cliffy/command" ;
11
12
import * as inputTypes from "./types.ts" ;
12
13
import * as fmt from "@std/fmt/colors" ;
14
+ import { z } from "zod" ;
13
15
14
16
@injectable ( )
15
17
export class FileSystem {
@@ -30,16 +32,48 @@ export class FileSystem {
30
32
}
31
33
}
32
34
35
+ const Manifest = z . object ( {
36
+ lexicons : z . array ( z . string ( ) ) ,
37
+ } ) ;
38
+ type Manifest = z . infer < typeof Manifest > ;
39
+
33
40
@injectable ( )
34
41
class ResolutionsDir {
35
42
constructor ( private fs = inject ( FileSystem ) ) { }
36
43
37
- async writeResolution ( resolution : Extract < Resolution , { success : true } > ) {
44
+ getRoot ( ) {
38
45
const manifestDir = this . fs . cwd ( ) ;
39
46
if ( ! manifestDir ) {
40
47
throw new Error ( "Could not determine manifest directory" ) ;
41
48
}
42
49
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
+
43
77
const path = `${ manifestDir } /lexicons/${
44
78
resolution . nsid . segments . join (
45
79
"/" ,
@@ -59,9 +93,9 @@ export type CommandDescriptor = {
59
93
@injectable ( )
60
94
export class FetchCommand implements CommandDescriptor {
61
95
constructor (
62
- private fs = inject ( FileSystem ) ,
63
96
private registry = inject ( NodeRegistry ) ,
64
97
private resolutionsDir = inject ( ResolutionsDir ) ,
98
+ private nodeFactory = inject ( NodeFactory ) ,
65
99
) { }
66
100
67
101
name = "fetch" ;
@@ -70,19 +104,14 @@ export class FetchCommand implements CommandDescriptor {
70
104
. action ( ( ) => this . #action( ) ) ;
71
105
72
106
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 ) ) ,
81
110
) ;
82
111
83
- await emptyDir ( ` ${ manifestDir } /lexicons` ) ;
112
+ await emptyDir ( this . resolutionsDir . getRoot ( ) + " /lexicons" ) ;
84
113
85
- for await ( const resolution of this . registry . resolve ( nsids ) ) {
114
+ for await ( const resolution of this . registry . resolve ( roots ) ) {
86
115
if ( ! resolution . success ) {
87
116
console . error ( "failed to resolve " , resolution . errorCode ) ;
88
117
continue ;
0 commit comments