forked from lordgiotto/google-font-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·299 lines (266 loc) · 9.09 KB
/
cli.js
File metadata and controls
executable file
·299 lines (266 loc) · 9.09 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env node
// @ts-check
"use strict";
/**
* @typedef {import('./lib/types').GoogleFontListInstance} GoogleFontListInstance
* @typedef {import('./lib/types').GoogleFontInstance} GoogleFontInstance
* @typedef {import('./lib/types').FontResult} FontResult
*/
const { Command } = require("commander");
const pc = require("picocolors");
const ncp = require("copy-paste-win32fix");
const GoogleFontList = require("./lib/google-font-list");
const pjson = require("./package.json");
/** @type {any} */
const fontList = new GoogleFontList();
const program = new Command();
program.option("--refresh-cache", "Refresh the cached Google font list");
/**
* Split comma-separated font family arguments into an array
* @param {string[]} familyArgs - Array of family arguments
* @returns {string[]} Array of trimmed family names
*/
const splitFamilies = (familyArgs) =>
familyArgs
.join(" ")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
/**
* Promisified wrapper for getFontByName
* @param {string} term - Font family name to search
* @returns {Promise<any>} Filtered font list
*/
const getFontByNameAsync = (term) =>
new Promise((resolve, reject) => {
fontList.getFontByName(term, (/** @type {Error | null} */ err, /** @type {any} */ filtered) => {
if (err) return reject(err);
resolve(filtered);
});
});
/**
* Helper to wrap fontList initialization in a Promise
* @param {boolean} [refreshCache=false] - Whether to force refresh the cache
* @returns {Promise<void>}
*/
const ensureFontsLoaded = async (refreshCache = false) => {
if (refreshCache) {
fontList.loaded = false;
console.log(pc.bold(pc.blue("\nRefreshing Google Font List cache...\n")));
}
if (fontList.loaded) return;
try {
await fontList.load(refreshCache);
} catch (err) {
console.error(pc.bold(pc.red("Error loading font list!")));
console.error(pc.red(err.toString()));
process.exit(1);
}
};
program.version(pjson.version);
program
.command("search [family...]")
.description("Search for a font family")
.action(async (family) => {
const refresh = program.opts().refreshCache;
await ensureFontsLoaded(refresh);
const term = family ? family.join(" ") : "";
fontList.searchFontByName(term, printFontList);
});
program
.command("download <family...>")
.description("Download a font family")
.option("-d, --dest <folder>", "Specify destination folder")
.option("-v, --variants <variants>", "Variants separated by comma")
.option("--ttf", "Download TTF format (default)")
.option("--woff2", "Download WOFF2 format")
.action(async (family, options) => {
const refresh = program.opts().refreshCache;
const variants = options.variants ? options.variants.split(",") : false;
const format = options.woff2 ? "woff2" : "ttf";
const families = splitFamilies(family);
try {
await ensureFontsLoaded(refresh);
/** @type {FontResult[]} */
let allResults = [];
let successCount = 0;
let failCount = 0;
for (const term of families) {
try {
const filteredList = await getFontByNameAsync(term);
if (filteredList.data.length !== 1) {
handleMatchError("Download", term, null);
failCount++;
continue;
}
const font = filteredList.getFirst();
if (!font) {
handleMatchError("Download", term, null);
failCount++;
continue;
}
const result = await font.saveAtAsync(variants, options.dest, format);
allResults = allResults.concat(result);
successCount++;
} catch (err) {
handleMatchError("Download", term, /** @type {Error} */ (err));
failCount++;
}
}
if (allResults.length > 0) {
printResult(null, allResults);
}
// If all operations failed, exit with error
if (failCount > 0 && successCount === 0) {
console.error(pc.red(pc.bold(`\nAll ${failCount} font download(s) failed.`)));
process.exit(1);
}
// Report partial failures
if (failCount > 0 && successCount > 0) {
console.log(pc.yellow(`\n${successCount} font(s) downloaded successfully, ${failCount} failed.`));
}
} catch (err) {
console.error(pc.red(/** @type {Error} */ (err).toString()));
process.exit(1);
}
});
program
.command("install <family...>")
.description("Install a font family to the system")
.option("-v, --variants <variants>", "Variants separated by comma")
.action(async (family, options) => {
const refresh = program.opts().refreshCache;
const variants = options.variants ? options.variants.split(",") : false;
const families = splitFamilies(family);
try {
await ensureFontsLoaded(refresh);
/** @type {FontResult[]} */
let allResults = [];
let successCount = 0;
let failCount = 0;
for (const term of families) {
try {
const filteredList = await getFontByNameAsync(term);
if (filteredList.data.length !== 1) {
handleMatchError("Installation", term, null);
failCount++;
continue;
}
const font = filteredList.getFirst();
if (!font) {
handleMatchError("Installation", term, null);
failCount++;
continue;
}
const result = await font.installAsync(variants);
allResults = allResults.concat(result);
successCount++;
} catch (err) {
handleMatchError("Installation", term, /** @type {Error} */ (err));
failCount++;
}
}
if (allResults.length > 0) {
printResult(null, allResults);
}
// If all operations failed, exit with error
if (failCount > 0 && successCount === 0) {
console.error(pc.red(pc.bold(`\nAll ${failCount} font installation(s) failed.`)));
process.exit(1);
}
// Report partial failures
if (failCount > 0 && successCount > 0) {
console.log(pc.yellow(`\n${successCount} font(s) installed successfully, ${failCount} failed.`));
}
} catch (err) {
console.error(pc.red(/** @type {Error} */ (err).toString()));
process.exit(1);
}
});
program
.command("copy <family...>")
.description("Copy Google Fonts stylesheet link to clipboard")
.option("-v, --variants <variants>", "Variants separated by comma")
.action(async (family, options) => {
const refresh = program.opts().refreshCache;
await ensureFontsLoaded(refresh);
const term = family.join(" ");
const variants = options.variants ? options.variants.split(",") : false;
fontList.getFontByName(term, (/** @type {Error | null} */ err, /** @type {any} */ filteredList) => {
if (err || filteredList.data.length !== 1) {
handleMatchError("Copy", term, err);
return;
}
const font = filteredList.getFirst();
if (!font) return;
const url = variants
? `${font.getCssUrl()}:${variants.join(",")}`
: font.getCssUrl();
ncp.copy(url, () => {
console.log(pc.green(`"${term}" CSS URL copied to clipboard.`));
});
});
});
program.parse(process.argv);
// Handle empty commands
if (program.args.length === 0) {
program.help();
}
/**
* Output Helpers
*/
/**
* Handle and display match errors for font operations
* @param {string} action - Action being performed (e.g., "Download", "Install")
* @param {string} term - Font family name that failed
* @param {Error | null} err - Error object or null
* @returns {void}
*/
function handleMatchError(action, term, err) {
if (err) {
console.error(pc.red(err.toString()));
} else {
console.log(
pc.bold(pc.red(`${action} failed: unable to find font family "${term}"`))
);
fontList.searchFontByName(term, printFontList);
}
}
/**
* Print a list of fonts to the console
* @param {Error | null} err - Error object or null
* @param {GoogleFontListInstance} list - Font list to display
* @param {string} [message="Search results for:"] - Header message
* @returns {void}
*/
function printFontList(err, list, message = "Search results for:") {
if (err) return console.error(pc.red(err.toString()));
if (list.data.length === 0) {
return console.log(pc.red(`No results found for: ${list._filterTerm}`));
}
console.log(pc.green(`${message} "${pc.bold(pc.blue(list._filterTerm))}"\n`));
list.data.forEach((el) => {
console.log(pc.bold(pc.blue(` * ${el.family}`)));
console.log(` Category: ${el.getCategory()}`);
console.log(` Variants: ${el.getVariants().join(", ")}`);
console.log(` CSS Url: ${el.getCssUrl()}\n`);
});
}
/**
* Print font operation results to the console
* @param {Error | null} err - Error object or null
* @param {FontResult[]} result - Array of font results to display
* @returns {void}
*/
function printResult(err, result) {
if (err) return console.error(pc.red(err.toString()));
console.log("");
result.forEach((el) => {
console.log(
pc.green(
`${pc.bold(el.family)} variant ${pc.bold(el.variant)} processed: ${pc.underline(el.path)}`
)
);
});
console.log("");
}