-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 2.1 KB
/
index.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
import axios from 'axios';
import fs from 'fs/promises';
import path from 'path';
import debug from 'debug';
import {
getOutputName, loadLocalAssets, updateAttributes, parse,
} from './src/utils.js';
// import 'axios-debug-log';
const logger = debug('page-loader');
/**
* @description Load pages by given URL and keeps them in the dir
* @param {String} url
* @param {String} dirpath
*/
export default (url, dirpath) => {
const outputDirname = getOutputName(url, '_files');
const outputFilename = getOutputName(url, '.html');
logger(`Formation of dirname where loaded pages are kept: ${outputDirname}`);
logger(`Formation of filename with loaded main page: ${outputFilename}`);
let markup;
let links;
let absoluteDirpath;
let absoluteFilepath;
return axios.get(url)
.then(({ data }) => {
logger(`Request to ${url} to load data`);
markup = data;
links = parse(data, url);
logger('Parse data of local resourses');
absoluteDirpath = path.join(dirpath, outputDirname);
logger(`Make a directory where load pages are kept: ${absoluteDirpath}`);
return fs.mkdir(absoluteDirpath);
})
.then(() => {
absoluteFilepath = path.join(absoluteDirpath, outputFilename);
logger(`Save home page file: ${absoluteFilepath}`);
return fs.writeFile(absoluteFilepath, markup, 'utf-8');
})
.then(() => loadLocalAssets(links))
.then((responses) => Promise.all(responses.map(({ config, data }) => {
const extension = path.extname(config.url);
const sourceName = getOutputName(config.url, extension);
const absoluteSourcePath = path.join(absoluteDirpath, sourceName);
logger(`Save local resourse file: ${absoluteSourcePath}`);
return fs.writeFile(absoluteSourcePath, data, 'utf-8');
})))
.then(() => {
const updatedHTML = updateAttributes(markup, links, outputDirname);
logger('Update home page file with new names for local resourses');
return fs.writeFile(absoluteFilepath, updatedHTML, 'utf-8');
})
.then(() => console.log(`Page was successfuly downloaded into '${outputDirname}/${outputFilename}'`));
};