Skip to content

Commit 7fcd164

Browse files
committed
Add logging to primary logic functions
1 parent 9503c30 commit 7fcd164

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

src/downloadFiles.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
import path from 'path'
22
import fsp from 'fs/promises'
3+
import debug from 'debug'
34
import getData from './getData.js'
45
import generateName from './generateName.js'
56

7+
const log = debug('page-loader')
8+
69
const downloadFiles = ($, url, filesDirPath, selector, attr) => {
710
const links = $(selector).map((i, tag) => $(tag).attr(attr)).get()
811

912
if (links.length === 0) {
13+
log('no %s links', selector)
1014
return Promise.resolve()
1115
}
1216

17+
log(`download %ss`, selector)
1318
return fsp.mkdir(filesDirPath, { recursive: true })
1419
.then(() => Promise.all(links.map((link) => {
1520
const absLink = new URL(link, url)
1621
if (absLink.hostname !== new URL(url).hostname) {
22+
log('skip external link %s', absLink.href)
1723
return Promise.resolve()
1824
}
1925
const fileName = path.extname(absLink.pathname)
2026
? generateName(absLink.href).replace(/-(?=[a-zA-Z0-9]+$)/, '.')
2127
: generateName(absLink.href) + '.html'
2228
const filePath = path.join(filesDirPath, fileName)
2329

30+
log('download file %s', absLink.href)
2431
return getData(absLink.href)
25-
.then(data => fsp.writeFile(filePath, data))
32+
.then((data) => {
33+
log('write to file %s', filePath)
34+
return fsp.writeFile(filePath, data)
35+
})
2636
.then(() => {
37+
const newLink = path.join(path.basename(filesDirPath), fileName)
38+
log('change link from %s to %s in HTML', link, newLink)
2739
$(selector).each((i, tag) => {
2840
if ($(tag).attr(attr) === link) {
29-
$(tag).attr(attr, path.join(path.basename(filesDirPath), fileName))
41+
$(tag).attr(attr, newLink)
3042
}
3143
})
3244
})

src/getData.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
import axios from 'axios'
2+
import axiosDebugLog from 'axios-debug-log'
3+
import debug from 'debug'
4+
5+
const log = debug('page-loader')
6+
7+
axiosDebugLog({
8+
debug: log,
9+
request: (debug, config) => {
10+
debug('Request with ' + config.headers['content-type'], config.url)
11+
},
12+
response: (debug, response) => {
13+
debug(
14+
'Response with ' + response.headers['content-type'],
15+
'from ' + response.config.url,
16+
'status ' + response.status,
17+
)
18+
},
19+
error: (debug, error) => {
20+
debug(`Error: ${error.message}`)
21+
},
22+
})
223

324
const getData = url => axios
425
.get(url, { responseType: 'arraybuffer' })

src/index.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,55 @@ import path from 'path'
22
import fsp from 'fs/promises'
33
import * as cheerio from 'cheerio'
44
import * as prettier from 'prettier'
5+
import debug from 'debug'
56
import getData from './getData.js'
67
import downloadFiles from './downloadFiles.js'
78
import generateName from './generateName.js'
89

10+
const log = debug('page-loader')
11+
912
const getAbsolutePath = dirpath => path.resolve(process.cwd(), dirpath)
1013

1114
const downloadPage = (url, outputDir) => {
15+
log('download page %s to %s', url, outputDir)
16+
17+
const absOutputDir = getAbsolutePath(outputDir)
1218
const fileName = generateName(url) + '.html'
13-
const filePath = path.join(getAbsolutePath(outputDir), fileName)
19+
const filePath = path.join(absOutputDir, fileName)
1420
const filesDirName = generateName(url) + '_files'
15-
const filesDirPath = path.join(getAbsolutePath(outputDir), filesDirName)
16-
let $
21+
const filesDirPath = path.join(absOutputDir, filesDirName)
1722

18-
return getData(url)
23+
return fsp.access(absOutputDir)
24+
.then(() => {
25+
log('output dir exists')
26+
return getData(url)
27+
})
1928
.then((html) => {
20-
$ = cheerio.load(html)
29+
log('HTML downloaded')
30+
const $ = cheerio.load(html)
2131

2232
return Promise.all([
2333
downloadFiles($, url, filesDirPath, 'img', 'src'),
2434
downloadFiles($, url, filesDirPath, 'link', 'href'),
2535
downloadFiles($, url, filesDirPath, 'script', 'src'),
2636
])
37+
.then(() => $)
38+
})
39+
.then(($) => {
40+
log('files downloaded, format HTML')
41+
return prettier.format($.html(), { parser: 'html' })
42+
})
43+
.then((formattedHtml) => {
44+
log('write to file %s', filePath)
45+
return fsp.writeFile(filePath, formattedHtml)
46+
})
47+
.then(() => {
48+
log('done, output file is %s', filePath)
49+
return filePath
2750
})
28-
.then(() => prettier.format($.html(), { parser: 'html' }))
29-
.then(formattedHtml => fsp.writeFile(filePath, formattedHtml))
30-
.then(() => filePath)
3151
.catch((err) => {
3252
// console.error('Full error:', err)
53+
log('error: %s', err.message)
3354
throw new Error(`Error: ${err.message}`)
3455
})
3556
}

0 commit comments

Comments
 (0)