forked from renyunkang/yuque-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
69 lines (54 loc) · 2.09 KB
/
main.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
import puppeteer from 'puppeteer';
import { exit } from 'process';
import fs from 'fs';
import path from 'path';
import { autoLogin } from './src/login.js';
import { getAllBooks } from './src/toc.js';
import { exportMarkDownFiles } from './src/export.js';
// import { printDirectoryTree } from './src/toc.js';
let color = {
byNum: (mess, fgNum) => {
mess = mess || '';
fgNum = fgNum === undefined ? 31 : fgNum;
return '\u001b[' + fgNum + 'm' + mess + '\u001b[39m';
},
black: (mess) => color.byNum(mess, 30),
red: (mess) => color.byNum(mess, 31),
green: (mess) => color.byNum(mess, 32),
yellow: (mess) => color.byNum(mess, 33),
blue: (mess) => color.byNum(mess, 34),
magenta: (mess) => color.byNum(mess, 35),
cyan: (mess) => color.byNum(mess, 36),
white: (mess) => color.byNum(mess, 37)
};
async function run() {
if (!process.env.EXPORT_PATH) {
const outputDir = path.join(process.cwd(), 'output');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
process.env.EXPORT_PATH = outputDir;
console.log(`The environment variable EXPORT_PATH is not set, so the default ${outputDir} is used as the export path.`)
}
// const page = await BrowserPage.getInstance();
const browser = await puppeteer.launch({ headless: true,
// Mac指定Chromium的安装位置
executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium',
// executablePath: 'chromium'
}); // true:not show browser
const page = await browser.newPage();
// 检查是否存在 cookie 文件
await autoLogin(page)
console.log(color.green("Login successfully!"))
console.log()
console.log("获取全部知识库信息 Get book stacks ...")
// 这里可以传入指定的知识库 ID
// https://www.yuque.com/api/mine/book_stacks 接口获取
const booksId = '';
const books = await getAllBooks(page,booksId)
// console.log(books)
console.log("Start export all books ...")
await exportMarkDownFiles(page, books)
browser.close()
};
run();