-
Notifications
You must be signed in to change notification settings - Fork 2
/
repository.ts
207 lines (190 loc) · 5.36 KB
/
repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Config, validateConfig } from "./config.ts";
import { Module } from "./module.ts";
import * as path from "./vendor/https/deno.land/std/path/mod.ts";
import { sprintf } from "./vendor/https/deno.land/std/fmt/printf.ts";
import { createURL } from "./net.ts";
const vendorDirectoryPath = "vendor";
export type Repository = {
removeModule(moduleProtocol: string, modulePath: string): Promise<void>;
addLink(
moduleProtocol: string,
modulePath: string,
moduleVersion: string,
filePath: string,
hasDefaultExport: boolean,
): Promise<void>;
removeLink(
moduleProtocol: string,
modulePath: string,
filePath: string,
): Promise<void>;
addAlias(
moduleProtocol: string,
modulePath: string,
filePath: string,
aliasPath: string,
hasDefaultExport: boolean,
): Promise<void>;
removeAlias(aliasPath: string): Promise<void>;
updateLink(
moduleProtocol: string,
modulePath: string,
moduleVersion: string,
filePath: string,
hasDefaultExport: boolean,
): Promise<void>;
loadConfig(): Promise<Config>;
saveConfig(config: Config): void;
};
const dec = new TextDecoder("utf-8");
const enc = new TextEncoder();
export class StorageRepository {
constructor(private filePath: string) {}
async removeModule(
moduleProtocol: string,
modulePath: string,
): Promise<void> {
const dp = path.join(
vendorDirectoryPath,
moduleProtocol,
modulePath,
);
try {
await Deno.remove(dp, { recursive: true });
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
throw new Error(`module already removed: ${dp}`);
} else {
throw new Error(`failed to remove directory: ${dp}, ${e}`);
}
}
}
async addLink(
moduleProtocol: string,
modulePath: string,
moduleVersion: string,
filePath: string,
hasDefaultExport: boolean,
): Promise<void> {
const directoryPath = path.dirname(filePath);
const fp = path.join(
vendorDirectoryPath,
moduleProtocol,
modulePath,
filePath,
);
const dp = path.join(
vendorDirectoryPath,
moduleProtocol,
modulePath,
directoryPath,
);
const url = createURL(moduleProtocol, modulePath, moduleVersion, filePath);
let script = sprintf('export * from "%s";\n', url);
if (hasDefaultExport) {
script += sprintf('export { default } from "%s";\n', url);
}
// create directories and file
await Deno.mkdir(dp, { recursive: true });
await Deno.writeFile(fp, enc.encode(script));
}
async removeLink(
moduleProtocol: string,
modulePath: string,
filePath: string,
): Promise<void> {
const fp = path.join(
vendorDirectoryPath,
moduleProtocol,
modulePath,
filePath,
);
try {
await Deno.remove(fp, { recursive: false });
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
throw new Error(`link already removed: ${fp}`);
} else {
throw new Error(`failed to remove directory: ${fp}, ${e}`);
}
}
}
async addAlias(
moduleProtocol: string,
modulePath: string,
filePath: string,
aliasPath: string,
hasDefaultExport: boolean,
): Promise<void> {
const aliasDirectoryPath = path.dirname(aliasPath);
const fp = path.join(
vendorDirectoryPath,
aliasPath,
);
const dp = path.join(
vendorDirectoryPath,
aliasDirectoryPath,
);
const aliasTargetPath = `./${moduleProtocol}/${modulePath}${filePath}`;
let script = sprintf('export * from "%s";\n', aliasTargetPath);
if (hasDefaultExport) {
script += sprintf('export { default } from "%s";\n', aliasTargetPath);
}
// create directories and file
await Deno.mkdir(dp, { recursive: true });
await Deno.writeFile(fp, enc.encode(script));
}
async removeAlias(aliasPath: string): Promise<void> {
const fp = path.join(
vendorDirectoryPath,
aliasPath,
);
try {
await Deno.remove(fp, { recursive: false });
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
throw new Error(`alias already removed: ${fp}`);
} else {
throw new Error(`failed to remove alias: ${fp}, ${e}`);
}
}
}
async updateLink(
moduleProtocol: string,
modulePath: string,
moduleVersion: string,
filePath: string,
hasDefaultExport: boolean,
): Promise<void> {
const fp = path.join(
vendorDirectoryPath,
moduleProtocol,
modulePath,
filePath,
);
const url = createURL(moduleProtocol, modulePath, moduleVersion, filePath);
let script = sprintf('export * from "%s";\n', url);
if (hasDefaultExport) {
script += sprintf('export { default } from "%s";\n', url);
}
await Deno.writeFile(fp, enc.encode(script));
}
async loadConfig(): Promise<Config> {
const jsonBody = dec.decode(await Deno.readFile(this.filePath));
const configObj = JSON.parse(jsonBody);
if (!configObj.aliases) {
configObj.aliases = {};
}
const config = configObj as Config;
// throws error
validateConfig(config);
config.modules = config.modules.map(
(mod) => new Module(mod.protocol, mod.path, mod.version, mod.files),
);
return config;
}
async saveConfig(config: Config) {
const jsonBody = JSON.stringify(config, undefined, 2);
await Deno.writeFile(this.filePath, enc.encode(jsonBody));
}
}