This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
cmd.js
executable file
·166 lines (148 loc) · 5.38 KB
/
cmd.js
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
#!/usr/bin/env node
const argv = require( "minimist" )( process.argv.slice(2) );
const chalk = require("chalk");
const getStdin = require("get-stdin");
const GlyphHanger = require( "./src/GlyphHanger" );
const GlyphHangerWhitelist = require( "./src/GlyphHangerWhitelist" );
const GlyphHangerSubset = require( "./src/GlyphHangerSubset" );
const GlyphHangerFontFace = require( "./src/GlyphHangerFontFace" );
const MultipleSpiderPigs = require( "./src/MultipleUrlSpiderPig" );
const debug = require( "debug" )( "glyphhanger:cli" );
var whitelist = new GlyphHangerWhitelist( argv.w || argv.whitelist, {
US_ASCII: argv.US_ASCII,
LATIN: argv.LATIN
});
var gh = new GlyphHanger();
gh.setUnicodesOutput( argv.string );
gh.setWhitelist( whitelist );
gh.setSubset( argv.subset );
gh.setJson( argv.json );
gh.setClassName( argv.classname );
gh.setFamilies( argv.family );
gh.setTimeout( argv.timeout );
gh.setVisibilityCheck( argv.onlyVisible );
gh.setCSSSelector( argv.cssSelector );
if( argv.jsdom ) {
gh.setEnvironmentJSDOM();
}
var subset = new GlyphHangerSubset();
subset.setOutputDirectory(argv.output);
if( argv.formats ) {
subset.setFormats( argv.formats );
}
if( argv.subset ) {
subset.setFontFilesGlob( argv.subset );
}
var fontface = new GlyphHangerFontFace();
fontface.setFamilies( argv.family );
fontface.setCSSOutput( argv.css );
if( argv.subset ) {
fontface.setSubset( subset );
}
// glyphhanger --version
// glyphhanger --help
// glyphhanger
// glyphhanger http://localhost/
// glyphhanger http://localhost/ --timeout (change timeout, in ms, default is 30000)
// glyphhanger http://localhost/ http://localhost2/ (multiple urls are welcome)
// glyphhanger http://localhost/ --spider (limit default 10)
// glyphhanger http://localhost/ --spider-limit (limit default 10)
// glyphhanger http://localhost/ --spider-limit=0 (no limit)
// glyphhanger http://localhost/ --spider-limit=1 (limit 1)
// glyphhanger --whitelist=ABCD (convert characters to unicode range)
// glyphhanger --US_ASCII (convert characters to unicode range)
// glyphhanger --US_ASCII --whitelist=ABCD (convert characters to unicode range)
// glyphhanger --LATIN
// glyphhanger --subset=*.ttf (file format conversion)
// glyphhanger --subset=*.ttf --whitelist=ABCD (reduce to whitelist characters)
// glyphhanger --subset=*.ttf --US_ASCII (reduce to US_ASCII characters)
// glyphhanger --subset=*.ttf --US_ASCII --whitelist=ABCD (reduce to US_ASCII union with whitelist)
// glyphhanger --family='My Serif' (outputs results for specific family)
// glyphhanger --family='My Serif' --css (outputs results for specific family with a font-face block)
// glyphhanger --subset=*.ttf --family='My Serif' (subset group of fonts to results for specific family)
// glyphhanger --subset=*.ttf --family='My Serif' -css (subset group of fonts to results for specific family and a font-face block)
// glyphhanger --subset=*.ttf --output=dist/ (change the output directory for subset files)
(async function() {
let standardInput = (await getStdin()).trim();
gh.setStandardInput(standardInput);
if( argv.version ) {
var pkg = require( "./package.json" );
console.log( pkg.version );
} else if( argv.help ) {
gh.outputHelp();
} else if( argv._ && argv._.length || standardInput ) {
// Spider
try {
if( argv.spider || argv[ 'spider-limit' ] || argv[ 'spider-limit' ] === 0 ) {
let sp = new MultipleSpiderPigs();
sp.setLimit(argv[ 'spider-limit' ]);
await sp.fetchUrls( argv._ );
let urls = sp.getUrlsWithLimit();
await sp.finish();
debug( "Urls (after limit): %o", urls );
await gh.fetchUrls( urls );
} else {
await gh.fetchUrls( argv._ );
}
} catch(e) {
console.log(chalk.red("GlyphHanger Fetch Error: "), e);
process.exitCode = 1;
}
gh.output();
try {
fontface.setUnicodeRange( gh.getUnicodeRange() );
fontface.writeCSSFiles();
} catch(e) {
console.log(chalk.red("GlyphHangerFontFace Error: "), e);
process.exitCode = 1;
}
try {
if( argv.subset ) {
subset.subsetAll( gh.getUnicodeRange() );
}
} catch(e) {
console.log(chalk.red("GlyphHangerSubset Error: "), e);
process.exitCode = 1;
}
try {
fontface.output();
} catch(e) {
console.log(chalk.red("GlyphHangerFontFace Error: "), e);
process.exitCode = 1;
}
} else { // not using URLs
if( argv.subset ) {
gh.output();
try {
// --subset with or without --whitelist
subset.subsetAll( !whitelist.isEmpty() ? whitelist.getWhitelistAsUnicodes() : whitelist.getUniversalRangeAsUnicodes() );
} catch(e) {
process.exitCode = 1;
console.log(chalk.red("GlyphHangerSubset Error: "), e);
}
try {
if(!whitelist.isEmpty()) {
fontface.setUnicodeRange( whitelist.getWhitelistAsUnicodes());
}
fontface.writeCSSFiles();
fontface.output();
} catch(e) {
process.exitCode = 1;
console.log(chalk.red("GlyphHangerFontFace Error: "), e);
}
} else if( !whitelist.isEmpty() ) {
// not subsetting, just output the code points (can convert whitelist string to code points)
gh.outputUnicodes();
try {
fontface.setUnicodeRange( whitelist.getWhitelistAsUnicodes());
fontface.writeCSSFiles();
fontface.output();
} catch(e) {
process.exitCode = 1;
console.log(chalk.red("GlyphHangerFontFace Error: "), e);
}
} else {
gh.outputHelp();
}
}
})();