-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
72 lines (70 loc) · 1.87 KB
/
cli.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
// @ts-check
const path = require('path');
const fs = require('fs-extra');
const klaw = require('klaw');
const { execSync } = require('child_process');
const { convertFiles } = require('./index');
const scanDir = async (
/** @type {string} */ dirPath,
options = {
depthLimit: -1,
}
) => {
const fileList = [];
console.log('正在扫描当前文件夹下的word文档...');
for await (const word of klaw(dirPath, {
filter: (/** @type {string} */ filePath) => {
if (
!path.basename(filePath).startsWith('~$') &&
['.doc', '.docx'].includes(path.extname(filePath))
) {
return true;
}
if (
fs.statSync(filePath).isDirectory() &&
!filePath.includes('node_modules')
) {
return true;
}
return false;
},
...options,
})) {
if (word.stats.isFile()) {
fileList.push(word.path);
}
}
return fileList;
};
(async ({ outputDirPath = path.resolve(process.cwd(), 'output') } = {}) => {
const fileList = await scanDir('.');
if (fileList.length === 0) {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', process.exit.bind(process, 0));
console.log('没有发现word文档,按任意键退出');
return;
}
await convertFiles(fileList, outputDirPath);
console.log(
`执行完成,已处理${fileList.length}个文件,结果已放在${outputDirPath}目录下,是否打开目录? (Y/N)`
);
process.stdin.setRawMode(true);
process.stdin.resume();
let keyLock = false;
process.stdin.on('data', async (data) => {
if (keyLock) {
return;
}
keyLock = true;
if (data.toString().toLocaleLowerCase() === 'y') {
console.log(`正在打开${outputDirPath}`);
try {
execSync(`explorer.exe ${outputDirPath}`);
} catch (error) {
//
}
}
process.exit(0);
});
})();