-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.ts
executable file
·204 lines (179 loc) · 5.88 KB
/
index.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
import { parseArgs } from 'util'
import { exec } from 'child_process'
import jsdom from 'jsdom'
import { Browser, HTMLAnchorElement } from 'happy-dom'
// const browser = new Browser()
const { JSDOM } = jsdom
const buffer = new Set()
const __dirname = new URL('.', import.meta.url).pathname
const { values, positionals } = parseArgs({
args: Bun.argv,
options: {
/** Base URL, should be the `baseUrl` of the Docusaurus instance (e.g. https://docusaurus.io/docs/) */
'url': {
type: 'string',
short: 'u',
},
/** CSS selector to find the link of the next page */
'selector': {
type: 'string',
short: 's',
},
/** Working directory. Default to `./pdf` */
'dest': {
type: 'string',
short: 'd',
default: './pdf',
},
/** Change default list output filename */
'file': {
type: 'string',
short: 'f',
},
/** Change PDF output filename */
'output': {
type: 'string',
short: 'o',
},
/** Include passed URL in generated PDF */
'include-index': {
type: 'boolean',
},
/** Prepend additional pages, split with comma */
'prepend': {
type: 'string',
},
/** Append additional pages, split with comma */
'append': {
type: 'string',
},
/** Additional options for Prince. ie. `--prince-args="--page-size='210mm 297mm'"` or `--prince-args "\\-\\-page\\-size='210mm 297mm'"` */
'prince-args': {
type: 'string',
},
/** Use external Prince docker image to generate PDF. See https://github.com/sparanoid/docker-prince for more info */
'prince-docker': {
type: 'boolean',
},
/** Fetch list without generating PDF */
'list-only': {
type: 'boolean',
},
/** Generate PDF without fetching list. Ensure list exists */
'pdf-only': {
type: 'boolean',
},
/** Specify the cookie with the domain part, e.g. `--cookie="token=123456; domain=example.com;"` */
'cookie': {
type: 'string',
},
},
strict: true,
allowPositionals: true,
})
const url = values.url ? values.url.replace(/\/$/, '') : 'https://dev.openbayes.com/docs'
const parsedUrl = new URL(url)
const baseUrl = parsedUrl.origin
const scope = parsedUrl.pathname
const scopeName =
scope !== '/' ? `-${scope.replace(/\/$/g, '').replace(/^\//g, '').replace(/\//g, '-')}` : ''
const dest = values.dest
const listFile = values.file || `${dest}/${parsedUrl.hostname}${scopeName}.txt`
const pdfFile = values.output || `${dest}/${parsedUrl.hostname}${scopeName}.pdf`
const fetchOptions = {}
function execute(cmd: string): Promise<{
stdout: string
stderr: string
}> {
const s = (b: string) => String(b).trim()
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) return reject(error)
resolve({ stdout: s(stdout), stderr: s(stderr) })
})
})
}
async function generatePdf(list: string, filename: string, cookie?: string) {
console.log(`Generating PDF ${filename}`)
const args = values['prince-args'] || ''
const cookieArg = cookie ? `--cookie "${cookie}"` : ''
const princeCmd = values['prince-args']
? `docker run --rm -i -v ${__dirname}:/config sparanoid/prince --no-warn-css --style=/config/print.css ${cookieArg} --input-list=/config/${list} -o /config/${filename} ${args}`
: `prince --no-warn-css --style=${__dirname}print.css ${cookieArg} --input-list=${list} -o ${filename} ${args}`
console.log(`Executing command: ${princeCmd}`)
// TODO: https://github.com/oven-sh/bun/issues/9747
// await $`${princeCmd}`
await execute(princeCmd)
.then((resp) => {
console.log(resp.stdout)
console.log(`Done`)
})
.catch((err) => {
throw new Error(err)
})
}
async function requestPage(url: string) {
try {
const resp = await fetch(url, fetchOptions)
const body = await resp.text()
// TODO: https://github.com/capricorn86/happy-dom/issues/1415
// const page = browser.newPage()
// page.url = url
// page.content = body
// const dom = page.mainFrame
const dom = new JSDOM(body).window
const nextLinkEl = dom.document.body.querySelector(
values.selector || '.pagination-nav__link--next'
)
// TODO: jsdom does not have bultin DOM types.
// Use `nextLinkEl instanceof HTMLAnchorElement` instead for happy-dom
const nextLink = nextLinkEl && 'href' in nextLinkEl && `${baseUrl}${nextLinkEl.href}`
const cycle = buffer.has(nextLink)
if (!cycle && nextLink) {
const nextLink = `${baseUrl}${nextLinkEl.href}`
console.log(`Got link: ${nextLink}`)
buffer.add(nextLink)
requestPage(nextLink)
} else {
if (cycle) {
console.log(`Pagination cycle detected on ${url}`)
} else {
console.log('No next link found!')
}
if (values.append) {
values.append.split(',').forEach(async (item) => {
const url = item.match(/^https?:\/\//) ? item : `${baseUrl}${scope}${item}`
buffer.add(url)
console.log(`Got link: ${url} [append]`)
})
}
if (buffer.size > 0) {
console.log(`Writing buffer (${buffer.size} links) to ${listFile}`)
await Bun.write(listFile, [...buffer].join('\n'))
if (!values['list-only']) {
generatePdf(listFile, pdfFile, values.cookie)
}
} else {
console.log('No buffer to write!')
}
}
} catch (err) {
console.error('Error fetching page:', err)
}
}
if (values['pdf-only']) {
generatePdf(listFile, pdfFile, values.cookie)
} else {
if (values.prepend) {
values.prepend.split(',').map((item) => {
const url = item.match(/^https?:\/\//) ? item : `${baseUrl}${scope}${item}`
buffer.add(url)
console.log(`Got link: ${url} [prepend]`)
})
}
if (values['include-index']) {
console.log(`Got link: ${baseUrl}${scope} [index]`)
buffer.add(`${baseUrl}${scope}`)
}
requestPage(`${baseUrl}${scope}`)
}