Skip to content

Commit d35d513

Browse files
committed
Code refactoring
1 parent 2ccb468 commit d35d513

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/downloadFiles.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@ import Listr from 'listr'
88
const log = debug('page-loader')
99

1010
const tasks = ($, links, url, filesDirPath, selector, attr) => new Listr(links
11-
.filter((link) => {
11+
.map((link) => {
1212
const absLink = new URL(link, url)
1313
if (absLink.hostname === new URL(url).hostname) {
14-
return link
14+
return {
15+
title: `${absLink.href}`,
16+
task: () => downloadFile($, link, absLink, filesDirPath, selector, attr),
17+
}
1518
}
19+
else return null
1620
})
17-
.map((link) => {
18-
const absLink = new URL(link, url)
19-
return {
20-
title: `${absLink.href}`,
21-
task: () => downloadFile($, link, absLink, filesDirPath, selector, attr),
22-
}
23-
}), {
21+
.filter(link => link !== null && link !== undefined), {
2422
concurrent: true,
2523
exitOnError: false,
2624
})
2725

2826
const downloadFile = ($, link, absLink, filesDirPath, selector, attr) => {
29-
const fileName = path.extname(absLink.pathname)
30-
? generateName(absLink.href).replace(/-(?=[a-zA-Z0-9]+$)/, '.')
31-
: generateName(absLink.href) + '.html'
27+
const fileName = generateName(absLink.href)
3228
const filePath = path.join(filesDirPath, fileName)
3329

3430
log('download file %s', absLink.href)

src/generateName.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import path from 'path'
2+
13
const generateName = (url) => {
24
const href = new URL(url)
3-
return (href.hostname + href.pathname).replace(/[^a-zA-Z0-9]/g, '-')
5+
const baseName = (href.hostname + href.pathname).replace(/[^a-zA-Z0-9]/g, '-')
6+
return path.extname(href.pathname)
7+
? baseName.replace(/-(?=[a-zA-Z0-9]+$)/, '.')
8+
: baseName + '.html'
49
}
510

611
export default generateName

src/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from 'path'
22
import fsp from 'fs/promises'
33
import * as cheerio from 'cheerio'
4-
import * as prettier from 'prettier'
54
import debug from 'debug'
65
import getData from './getData.js'
76
import downloadFiles from './downloadFiles.js'
@@ -15,9 +14,9 @@ const downloadPage = (url, outputDir = process.cwd()) => {
1514
log('download page %s to %s', url, outputDir)
1615

1716
const absOutputDir = getAbsolutePath(outputDir)
18-
const fileName = generateName(url) + '.html'
17+
const fileName = generateName(url)
1918
const filePath = path.join(absOutputDir, fileName)
20-
const filesDirName = generateName(url) + '_files'
19+
const filesDirName = generateName(url).replace('.html', '_files')
2120
const filesDirPath = path.join(absOutputDir, filesDirName)
2221

2322
return fsp.access(absOutputDir)
@@ -37,12 +36,8 @@ const downloadPage = (url, outputDir = process.cwd()) => {
3736
.then(() => $)
3837
})
3938
.then(($) => {
40-
log('files downloaded, format HTML')
41-
return prettier.format($.html(), { parser: 'html' })
42-
})
43-
.then((formattedHtml) => {
4439
log('write to file %s', filePath)
45-
return fsp.writeFile(filePath, formattedHtml)
40+
return fsp.writeFile(filePath, $.html())
4641
})
4742
.then(() => {
4843
log('done, output file is %s', filePath)
@@ -56,6 +51,9 @@ const downloadPage = (url, outputDir = process.cwd()) => {
5651
if (err.code === 'EACCES') {
5752
throw new Error(`ERROR: No access to output directory - ${outputDir}`)
5853
}
54+
if (err.message.startsWith('Cant get ')) {
55+
throw err
56+
}
5957
throw new Error(`ERROR: ${err.message}`)
6058
})
6159
}

0 commit comments

Comments
 (0)