-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path_novel_conv_fast.ts
257 lines (208 loc) · 5.32 KB
/
_novel_conv_fast.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
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
/**
* Created by user on 2018/5/21/021.
*/
import * as crossSpawn from 'cross-spawn-extra';
import * as path from 'path';
import gitDiffIDNovelID, { gitDiffStagedFile } from '../lib/git';
import { freeGC, trimTxtLine } from '../lib/util';
import ProjectConfig from '../project.config';
import Promise = require('bluebird');
import * as fs from 'fs-extra';
import { array_unique } from '../lib/func';
import novelInfo, { mdconf_parse, IMdconfMeta } from 'node-novel-info';
import * as yargs from 'yargs';
import { console } from 'debug-color2';
import prettyuse = require('prettyuse');
import * as novelGlobby from 'node-novel-globby';
import * as iconv from 'iconv-jschardet';
import { tw2cn_min, cn2tw_min, tableCn2TwDebug, tableTw2CnDebug } from 'cjk-conv/lib/zh/convert/min';
import escapeGlob = require('glob-escape');
import { do_cn2tw_min } from './lib/conv';
import novelText from 'novel-text';
import { contextEmpty, loadFileAutoDecode } from '../lib/fs/load';
import { sortTree } from 'node-novel-globby/lib/glob-sort';
import { fixGlobBug, sortTreeUnique } from './lib/util';
import { defaultPatternsExclude } from 'node-novel-globby/lib/options';
let cli = yargs
.option('unsafe', {
boolean: true,
})
.argv
;
let arr_ids = gitDiffIDNovelID(ProjectConfig.dist_novel_root);
let _cache_file = path.join(
ProjectConfig.temp_root,
path.basename(__filename) + '.json',
);
if (arr_ids.length == 1)
{
fs.outputJSONSync(_cache_file, arr_ids);
}
else if (0)
{
throw new Error();
}
else if (arr_ids.length == 0 && fs.existsSync(_cache_file))
{
arr_ids = fs.readJSONSync(_cache_file);
console.info(`使用上次執行的目錄`, arr_ids);
}
Promise
.mapSeries(arr_ids, async function ({
pathMain,
novelID,
})
{
let cwd_path = path.join(ProjectConfig.dist_novel_root, pathMain, novelID);
let files = gitDiffStagedFile(cwd_path);
let meta: IMdconfMeta;
if (fs.existsSync(path.join(cwd_path, 'README.md')))
{
meta = await fs.readFile(path.join(cwd_path, 'README.md'))
.then(mdconf_parse)
}
else if (fs.existsSync(path.join(cwd_path, 'meta.md')))
{
meta = await fs.readFile(path.join(cwd_path, 'meta.md'))
.then(mdconf_parse)
}
if (!meta)
{
throw new Error([pathMain, novelID].join())
}
let globby_patterns: string[];
let globby_options: novelGlobby.IOptions = {
cwd: cwd_path,
useDefaultPatternsExclude: true,
absolute: true,
};
{
[globby_patterns, globby_options] = novelGlobby.getOptions(globby_patterns, globby_options);
}
let _last_empty: string[] = [];
let TIME_LABEL = novelID;
let STAT_CACHE = {
all: 0,
file: 0,
changed: 0,
skip: 0,
error: 0,
};
console.info(pathMain, novelID);
console.time(TIME_LABEL);
console.log(files);
globby_options.nonull = true;
let do_cn2tw_min_options: Parameters<typeof do_cn2tw_min>[1] = {};
if (cli.unsafe)
{
do_cn2tw_min_options.safe = false;
}
let ls = await Promise
.mapSeries(novelGlobby
.globbyASync([
...escapeGlob(files),
'!**/*.md',
'!*.md',
], globby_options)
.tap(async function (ls)
{
})
.then(novelGlobby.returnGlobList)
.then(ls => {
let ls2 = fixGlobBug(files, {
cwd: cwd_path,
exclude: [
...defaultPatternsExclude,
'!**/*.md',
'!*.md',
]
})
.map(v => path.join(globby_options.cwd, v))
;
ls = sortTreeUnique([...ls, ...ls2]);
return ls
})
, async function (file, index, len)
{
let ext = '.txt';
let name = path.basename(file, ext);
let file_dir = path.relative(cwd_path, path.dirname(file));
let currentFile = path.join(file_dir, name);
const idfile = currentFile;
let _t_old: string = await loadFileAutoDecode(file, {
idfile,
});
if (contextEmpty(_t_old))
{
_last_empty.push(currentFile);
//console.gray(currentFile, '此檔案無內容');
STAT_CACHE.skip++;
return idfile;
}
else
{
if (_last_empty.length)
{
_last_empty
.forEach(function (currentFile)
{
console.red(currentFile, '此檔案無內容');
})
;
_last_empty = [];
}
}
// @ts-ignore
_t_old = _t_old.toString();
let _t = trimTxtLine(do_cn2tw_min(novelText.toStr(_t_old), do_cn2tw_min_options));
let changed = _t != _t_old;
if (!_t.replace(/\xa0\s+ /g, ''))
{
STAT_CACHE.error++;
console
.yellow
.bgRed(currentFile, index, len)
;
await fs.outputFile(file, '');
}
else if (changed)
{
console[changed ? 'log' : 'red'](currentFile, index, len);
await fs.outputFile(file, _t);
}
else
{
console[changed ? 'log' : 'red'](currentFile, index, len);
}
if (changed)
{
STAT_CACHE.changed++;
}
return currentFile;
})
.then(function (ls)
{
if (_last_empty.length)
{
_last_empty
.forEach(function (currentFile)
{
console.grey(currentFile, '此檔案無內容');
})
;
_last_empty = [];
}
STAT_CACHE.all = ls.length;
STAT_CACHE.file = STAT_CACHE.all - STAT_CACHE.skip;
return ls;
})
.tap(function ()
{
console.debug(prettyuse());
console.debug(STAT_CACHE);
})
;
console.timeEnd(TIME_LABEL);
console.success(pathMain, novelID);
})
;