Skip to content

Commit 611556d

Browse files
committed
Add download page logic
1 parent 72d497d commit 611556d

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

bin/page-loader.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env node
2+
3+
import { program } from 'commander'
4+
import downloadPage from '../src/index.js'
5+
6+
program
7+
.name('Page loader')
8+
.description('Page loader utility.')
9+
.version('1.0.0')
10+
.arguments('<url>')
11+
.option('-o, --output [dir]', 'output dir', process.cwd())
12+
.action((url, options) => {
13+
downloadPage(url, options.output)
14+
.then(filePath => console.log(filePath))
15+
})
16+
17+
program.parse()

bin/pageloader.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/getPage.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import axios from 'axios'
2+
3+
const getPage = url => axios.get(url).then(response => response.data)
4+
5+
export default getPage

src/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import path from 'path'
2+
import fsp from 'fs/promises'
3+
import getPage from './getPage.js'
4+
5+
const getAbsolutePath = dirpath => path.resolve(process.cwd(), dirpath)
6+
7+
const generateFileName = (url) => {
8+
const href = new URL(url)
9+
return (href.hostname + href.pathname).replace(/[^a-zA-Z0-9]/g, '-') + '.html'
10+
}
11+
12+
const downloadPage = (url, outputDir) => {
13+
const fileName = generateFileName(url)
14+
const filePath = path.join(getAbsolutePath(outputDir), fileName)
15+
16+
return getPage(url)
17+
.then(pageData => fsp.writeFile(filePath, pageData)
18+
.then(() => filePath))
19+
.catch((err) => {
20+
throw new Error(`Error: ${err.message}`)
21+
})
22+
}
23+
24+
export default downloadPage

0 commit comments

Comments
 (0)