-
Notifications
You must be signed in to change notification settings - Fork 19
/
mod.ts
182 lines (160 loc) · 5.27 KB
/
mod.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
import { colors } from "./deps.ts";
import { Progress, SilentProgress } from "./progress.ts";
import { importUrls } from "./search.ts";
import { fragment, Semver, semver } from "./semver.ts";
import { lookup, REGISTRIES, RegistryCtor, RegistryUrl } from "./registry.ts";
// FIXME we should catch ctrl-c etc. and write back the original deps.ts
export async function udd(
filename: string,
options: UddOptions,
): Promise<UddResult[]> {
const u = new Udd(filename, options);
return await u.run();
}
export interface UddOptions {
// don't permanently edit files
dryRun?: boolean;
// don't print progress messages
quiet?: boolean;
// if this function errors then the update is reverted
test?: () => Promise<void>;
_registries?: RegistryCtor[];
}
export interface UddResult {
initUrl: string;
initVersion: string;
message?: string;
success?: boolean;
}
export class Udd {
private filename: string;
private test: () => Promise<void>;
private options: UddOptions;
private progress: Progress;
private registries: RegistryCtor[];
constructor(
filename: string,
options: UddOptions,
) {
this.filename = filename;
this.options = options;
this.registries = options._registries || REGISTRIES;
// deno-lint-ignore require-await
this.test = options.test || (async () => undefined);
this.progress = options.quiet ? new SilentProgress(1) : new Progress(1);
}
async content(): Promise<string> {
const decoder = new TextDecoder();
return decoder.decode(await Deno.readFile(this.filename));
}
async run(): Promise<UddResult[]> {
const content: string = await this.content();
const urls: string[] = importUrls(content, this.registries);
this.progress.n = urls.length;
// from a url we need to extract the current version
const results: UddResult[] = [];
for (const [i, u] of urls.entries()) {
this.progress.step = i;
const v = lookup(u, this.registries);
if (v !== undefined) {
results.push(await this.update(v!));
}
}
return results;
}
async update(
url: RegistryUrl,
): Promise<UddResult> {
const initUrl: string = url.url;
const initVersion: string = url.version();
let newFragmentToken: string | undefined = undefined;
await this.progress.log(`Looking for releases: ${url.url}`);
const versions = await url.all();
// for now, let's pick the most recent!
let newVersion = versions[0];
// FIXME warn that the version modifier is moved to a fragment...
// if the version includes a modifier we move it to the fragment
if (initVersion[0].match(/^[\~\^\=\<]/) && !url.url.includes("#")) {
newFragmentToken = initVersion[0];
url.url = `${url.at(initVersion.slice(1)).url}#${newFragmentToken}`;
}
try {
new Semver(url.version());
} catch (_) {
// The version string is a non-semver string like a branch name.
await this.progress.log(`Skip updating: ${url.url}`);
return { initUrl, initVersion };
}
// if we pass a fragment with semver
let filter: ((other: Semver) => boolean) | undefined = undefined;
try {
filter = fragment(url.url, url.version());
} catch (e) {
if (e instanceof SyntaxError) {
return {
initUrl,
initVersion,
success: false,
message: e.message,
};
} else {
throw e;
}
}
// potentially we can shortcut if fragment is #=${url.version()}...
if (filter !== undefined) {
const compatible: string[] = versions.map(semver).filter((x) =>
x !== undefined
).map((x) => x!).filter(filter).map((x) => x.version);
if (compatible.length === 0) {
return {
initUrl,
initVersion,
success: false,
message: "no compatible version found",
};
}
newVersion = compatible[0];
}
if (url.version() === newVersion && newFragmentToken === undefined) {
await this.progress.log(`Using latest: ${url.url}`);
return { initUrl, initVersion };
}
let failed = false;
if (!this.options.dryRun) {
await this.progress.log(`Attempting update: ${url.url} -> ${newVersion}`);
failed = await this.maybeReplace(url, newVersion, initUrl);
const msg = failed ? "failed" : "successful";
await this.progress.log(`Update ${msg}: ${url.url} -> ${newVersion}`);
}
const maybeFragment = newFragmentToken === undefined
? ""
: `#${newFragmentToken}`;
return {
initUrl,
initVersion,
message: newVersion + colors.yellow(maybeFragment),
success: !failed,
};
}
// Note: we pass initUrl because it may have been modified with fragments :(
async maybeReplace(
url: RegistryUrl,
newVersion: string,
initUrl: string,
): Promise<boolean> {
const newUrl = url.at(newVersion).url;
await this.replace(initUrl, newUrl);
const failed = await this.test().then((_) => false).catch((_) => true);
if (failed) {
await this.replace(newUrl, initUrl);
}
return failed;
}
async replace(left: string, right: string) {
const content = await this.content();
const newContent = content.split(left).join(right);
const encoder = new TextEncoder();
await Deno.writeFile(this.filename, encoder.encode(newContent));
}
}