forked from githubnext/monaspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.ts
executable file
·77 lines (67 loc) · 2.14 KB
/
renamer.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
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-env
import { parse, } from "https://deno.land/[email protected]/path/mod.ts";
import { walk, WalkEntry } from "https://deno.land/[email protected]/fs/mod.ts";
import { parse as parseFlags } from "https://deno.land/[email protected]/flags/mod.ts";
async function findFonts (src: string): Promise<WalkEntry[]> {
const fonts: WalkEntry[] = [];
const fontsIter = walk(src, {maxDepth: 10, includeFiles: true, includeDirs: false, match: [/\.(ttf|otf|woff|woff2)$/]});
for await (const font of fontsIter) {
fonts.push(font)
}
return fonts;
}
function getDstPath (font: WalkEntry): string {
const { name, ext, base } = parse(font.path);
if (name.match(/Frozen/)) {
// frozen fonts
return `${dst}/fonts/frozen/${name}${ext}`
}
else if (name.match(/Var(VF)?$/) ) {
// web variable fonts (woff and woff2)
if (ext.match(/\.woff2?/)) {
return `${dst}/fonts/webfonts/${name}[wght,wdth,slnt]${ext}`
}
// desktop variable fonts (ttf)
if (ext.match(/\.ttf/)) {
return `${dst}/fonts/variable/${name}[wght,wdth,slnt]${ext}`
}
} else {
// web static fonts (woff and woff2)
if (ext.match(/\.woff2?/)) {
return `${dst}/fonts/webfonts/${base}`
}
// desktop static fonts (otf)
if (ext.match(/\.otf/)) {
return `${dst}/fonts/otf/${base}`
}
}
throw new Error(`Could not find destination path for ${font.path}`);
}
const flags = parseFlags(Deno.args, {
boolean: ["verbose"],
string: ["src", "dst"],
alias: {
"src": "s",
"dst": "d",
},
default: {
verbose: false,
dst: ".", // Current directory
},
})
if (!flags.src) {
console.error("No source directory specified");
Deno.exit(1);
}
const HOME = Deno.env.get("HOME");
const src = await Deno.realPath(flags.src.replace("~", HOME!));
const dst = await Deno.realPath(flags.dst.replace("~", HOME!));
console.log("Source: ", src);
console.log("Destination: ", dst);
const fonts = await findFonts(src);
for (const f of fonts) {
const dstPath = getDstPath(f)
await Deno.mkdir(parse(dstPath).dir, {recursive: true});
await Deno.copyFile(f.path, dstPath);
console.log(dstPath)
}