-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
269 lines (206 loc) · 7.7 KB
/
mod.ts
File metadata and controls
269 lines (206 loc) · 7.7 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//// export
export class ChronVer {
private static readonly DATE_REGEX = /^(\d{4})\.(\d{2})\.(\d{2})$/;
private static readonly VERSION_REGEX = /^(\d{4})\.(?:0[1-9]|1[0-2])\.(?:0[1-9]|[12]\d|3[01])(?:\.(\d+))?(?:-(break|[a-zA-Z0-9-]+)(?:\.(\d+))?)?$/;
readonly changeset: number; /*** changeset number (0 if not specified) ***/
readonly day: number; /*** day component (1-31) ***/
readonly feature?: string; /*** feature name (if specified) ***/
readonly isBreaking: boolean; /*** whether this is a breaking change ***/
readonly month: number; /*** month component (1-12) ***/
readonly year: number; /*** year component ***/
constructor(version?: string) {
if (!version) {
const now = new Date();
this.changeset = 0;
this.day = now.getDate();
this.isBreaking = false;
this.month = now.getMonth() + 1;
this.year = now.getFullYear();
return;
}
const parsed = this.parseVersionString(version);
this.changeset = parsed.changeset;
this.day = parsed.day;
this.feature = parsed.feature;
this.isBreaking = parsed.isBreaking;
this.month = parsed.month;
this.year = parsed.year;
this.validate();
}
//// private methods
private getDaysInMonth(year: number, month: number): number {
return new Date(year, month, 0).getDate();
}
private parseVersionString(version: string): { changeset: number; day: number; feature?: string; isBreaking: boolean; month: number; year: number; } {
const match = version.match(ChronVer.VERSION_REGEX);
if (!match)
throw new ChronVerError(`Invalid ChronVer format: "${version}"`);
const [, yearStr, changesetStr, label, featureChangeset] = match;
const parts = version.split(".");
if (parts.length < 3)
throw new ChronVerError(`Invalid ChronVer format: "${version}"`);
const year = parseInt(yearStr, 10);
const month = parseInt(parts[1], 10);
const day = parseInt(parts[2], 10);
const isBreaking = label === "break";
let changeset = changesetStr ?
parseInt(changesetStr, 10) :
0;
let feature: string | undefined;
if (label && !isBreaking) {
feature = label;
if (!changeset) {
changeset = featureChangeset ?
parseInt(featureChangeset, 10) :
0;
}
}
return {
changeset,
day,
feature,
isBreaking,
month,
year
};
}
private validate(): void {
if (this.year < 1)
throw new ChronVerError("Year must be positive");
if (this.month < 1 || this.month > 12)
throw new ChronVerError("Month must be between 1 and 12");
const maxDays = this.getDaysInMonth(this.year, this.month);
if (this.day < 1 || this.day > maxDays)
throw new ChronVerError(`Day must be between 1 and ${maxDays} for ${this.year}-${this.month.toString().padStart(2, "0")}`);
if (this.changeset < 0)
throw new ChronVerError("Changeset cannot be negative");
if (this.feature && !/^[a-zA-Z0-9-]+$/.test(this.feature))
throw new ChronVerError("Feature name must contain only alphanumeric characters and hyphens");
}
//// public methods
compare(other: ChronVer): number {
/*** compare date components ***/
const comparisons = [
this.year - other.year,
this.month - other.month,
this.day - other.day,
this.changeset - other.changeset
];
for (const diff of comparisons) {
if (diff !== 0)
return Math.sign(diff);
}
if (this.isBreaking !== other.isBreaking)
return this.isBreaking ? 1 : -1; /*** compare breaking changes ***/
/*** at this point, changeset is zero ***/
return 0;
}
equals(other: ChronVer): boolean {
return this.compare(other) === 0;
}
getDateString(): string {
return `${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}`;
}
increment(): ChronVer {
const today = new Date();
const isToday = this.year === today.getFullYear() &&
this.month === (today.getMonth() + 1) &&
this.day === today.getDate();
if (isToday) /*** already ran today, increment changeset ***/
return new ChronVer(`${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}.${this.changeset + 1}`);
return new ChronVer();
}
isNewerThan(other: ChronVer): boolean {
return this.compare(other) > 0;
}
isOlderThan(other: ChronVer): boolean {
return this.compare(other) < 0;
}
toString(): string {
const base = `${this.year}.${this.month.toString().padStart(2, "0")}.${this.day.toString().padStart(2, "0")}`;
let result = base;
if (this.changeset > 0)
result += `.${this.changeset}`;
if (this.feature)
result += `-${this.feature}`;
if (this.isBreaking)
result += "-break";
return result;
}
//// static methods
static compare(v1: string, v2: string): number {
try {
const version1 = new ChronVer(v1);
const version2 = new ChronVer(v2);
return version1.compare(version2);
} catch(error) {
throw new ChronVerError(`Failed to compare versions: ${(error as Error).message}`);
}
}
static fromDate(date: Date, changeset: number = 0): ChronVer {
const dateStr = `${date.getFullYear()}.${(date.getMonth() + 1).toString().padStart(2, "0")}.${date.getDate().toString().padStart(2, "0")}`;
return new ChronVer(changeset > 0 ? `${dateStr}.${changeset}` : dateStr);
}
static async incrementInFile(filename: string = "package.json"): Promise<string> {
try {
const content = await Deno.readTextFile(filename);
const json = JSON.parse(content);
if (!json.version) {
json.version = new ChronVer().toString();
} else {
const currentVersion = new ChronVer(json.version);
json.version = currentVersion.increment().toString();
}
await Deno.writeTextFile(filename, JSON.stringify(json, null, 2) + "\n");
return json.version;
} catch(error) {
if ( /*** not found errors ***/
error instanceof Deno.errors.NotFound ||
(error as Error).message.includes("No such file") ||
(error as Error).message.includes("ENOENT") ||
(error as Error).message.includes("not found"))
throw new ChronVerError(`File not found: ${filename}`);
if (error instanceof SyntaxError) /*** json parsing errors ***/
throw new ChronVerError(`Invalid JSON in ${filename}: ${(error as Error).message}`);
if ((error as Error).message.includes("permission") || (error as Error).message.includes("EACCES"))
throw new ChronVerError(`Permission denied accessing ${filename}`); /*** permission errors ***/
throw new ChronVerError(`Failed to update ${filename}: ${(error as Error).message}`);
}
}
static isValid(version: string): boolean {
try {
new ChronVer(version);
return true;
} catch {
return false;
}
}
static parseVersion(version: string): { changeset: number; date: string; feature?: string; isBreaking: boolean; version: string; } | null {
if (!this.isValid(version))
return null;
try {
const chronVer = new ChronVer(version);
return {
changeset: chronVer.changeset,
date: chronVer.getDateString(),
feature: chronVer.feature,
isBreaking: chronVer.isBreaking,
version
};
} catch {
return null;
}
}
static sort(versions: string[], descending: boolean = false): string[] {
return versions.slice().sort((a, b) => {
const result = ChronVer.compare(a, b);
return descending ? -result : result;
});
}
}
export class ChronVerError extends Error {
constructor(message: string) {
super(message);
this.name = "ChronVerError";
}
}