-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (119 loc) · 3.68 KB
/
index.ts
File metadata and controls
128 lines (119 loc) · 3.68 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
const startTick = Date.now();
import * as fs from 'fs';
import * as path from 'path';
import * as argv from 'argv';
import {execute} from './src/works';
import {Env} from './src/env';
function printHelp() {
console.error('Usage :');
argv.help();
}
// gen version
import * as pkg from './package.json';
argv.version(pkg.version);
const ParamSrcTTF = 'src-ttf';
const ParamOutTTF = 'out-ttf';
const ParamEncoding = 'encoding';
const ParamFilters = 'list-filters';
const ParamFileList = 'file-list';
// gen type
argv.type('encode', function(v) {
if (Env.supportEncode.has(v))
return v;
throw `ERROR : encoding "${v}" not support.`;
});
// gen args
argv.option([
{
name: ParamSrcTTF,
short: 's',
type: 'path',
description:'Origin ttf file to extra',
example: '${path_relative_to_cwd}/origin.ttf',
},
{
name: ParamOutTTF,
short: 'o',
type: 'path',
description:'Output ttf file',
example: '${path_relative_to_cwd}/out.ttf',
},
{
name: ParamEncoding,
short: 'e',
type: 'encode',
description:'file encode(ascii|utf8|utf16le|ucs2|latin1)',
example: 'utf8',
},
{
name: ParamFilters,
short: 'l',
type: 'list,string',
description:'Input search filters relative to de ${cwd} path. Multi input split with ";". If the path first character is "!" means exclude that path',
example: 'src/**/*.txt,!test/**/*',
},
{
name: ParamFileList,
short: 'f',
type: 'list,string',
description:'Input src files. split with ";"',
example: 'src/**/*.txt,!test/**/*',
},
]);
function resolvePath(s: string): string {
if (!s) return '';
if (path.isAbsolute(s)) return s;
return path.join(Env.sRootDir, s);
}
function main(): number {
const args = argv.run(process.argv);
const srcTTF = resolvePath(args.options[ParamSrcTTF]);
if (!fs.existsSync(srcTTF)) {
console.error(`ERROR : src ttf file ${srcTTF} not exist!`);
printHelp();
return -1;
}
const outTTF = resolvePath(args.options[ParamOutTTF]);
if (!outTTF) {
console.error(`ERROR : output ttf file must be set!`);
printHelp();
return -3;
}
if (args.options[ParamEncoding]) {
Env.setDefaultEncoding(args.options[ParamEncoding]);
}
const filters = new Array<string>();
const origin_filters: Array<string> = args.options[ParamFilters];
if (origin_filters) {
for (const l of origin_filters) {
const sp = l.trim().split(';');
for (let s of sp) {
s = s.trim();
if (s == '') continue;
filters.push(s);
}
}
}
const origin_filelist: Array<string> = args.options[ParamFileList];
const filelist = new Array<string>();
for (const l of origin_filelist) {
const sp = l.trim().split(';');
for (let s of sp) {
s = s.trim();
if (s == '') continue;
filelist.push(s);
}
}
if (filelist.length <= 0 && filters.length <= 0) {
console.error(`ERROR : one of [list-filters (-l)] or [file-list (-f)] must be set!`);
printHelp();
return -2;
}
return execute(srcTTF, outTTF, filters, filelist);
}
// execute main
const ret = main();
if (ret == 0) {
console.log(`total use time : ${(Date.now() - startTick) / 1000}s`);
}
process.exit(ret);