-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.ts
48 lines (42 loc) · 1.43 KB
/
version.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
import { $, ansi } from "./deps.ts";
/** `VERSION` managed by https://deno.land/x/publish */
export const VERSION = "0.2.0";
export const MODULE = "dis";
/** `prepublish` will be invoked before publish */
export async function prepublish(version: string) {
for await (const file of $.fs.expandGlob("./*.{md,ts}")) {
if (file.isFile) await bump(file.path, version);
}
}
/** `postpublish` will be invoked after publish */
export function postpublish(version: string) {
console.log(
ansi.brightGreen(
` ✓ published ${ansi.bold(ansi.underline(`dis@${version}`))}`,
),
);
}
export async function bump(path: string, version: string, prev = VERSION) {
const filename = $.path.basename(path);
const _dirname = $.path.dirname(path);
try {
const TAG = "VERSION";
const PLACEHOLDERS = `\{${TAG}\}|\{\{${TAG}\}\}|\$${TAG}|${VERSION}`;
const SPECIFIER_RE = new RegExp(
`(?<=(?:^|[/"'\s])${MODULE}[@])(${PLACEHOLDERS}|[^/"'\s]+)(?=$|[/"'\s])`,
"mig",
);
let content = await Deno.readTextFile(path);
if (SPECIFIER_RE.test(content)) {
content = content.replaceAll(SPECIFIER_RE, version),
await Deno.writeTextFile(path, content).catch(console.error);
}
} catch (error) {
console.error(
ansi.bgRed(" FAILED "),
`⚠︎ Unable to bump ${ansi.underline(ansi.red(filename))} from ${
ansi.underline(prev)
} to ${ansi.bold(version)}!\n\n${error}`,
);
}
}