Skip to content

Commit bef0365

Browse files
committed
feat: make downloading other resources
1 parent 958813c commit bef0365

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

__tests__/page-loader.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('downloads html', () => {
2828
html = await readFile('just.html').then(trimHtml);
2929
});
3030

31-
beforeEach(() => {
31+
beforeEach(async () => {
3232
nock(/ru\.hexlet\.io/)
3333
.get(/\/courses/)
3434
.reply(200, html);
@@ -46,7 +46,8 @@ describe('downloads html', () => {
4646
});
4747

4848
test('to the current working directory, if the destFolder parameter is not defined', async () => {
49-
process.cwd = jest.fn(() => tmpFolder);
49+
const cwdMock = jest.spyOn(process, 'cwd');
50+
cwdMock.mockImplementation(() => tmpFolder);
5051

5152
const returnValue = await loadPage('https://ru.hexlet.io/courses');
5253

@@ -56,6 +57,8 @@ describe('downloads html', () => {
5657

5758
expect(trimHtml(actualContent)).toBe(trimHtml(html));
5859
expect(returnValue.filepath).toBe(expectedFilePath);
60+
61+
cwdMock.mockRestore();
5962
});
6063
});
6164

@@ -119,7 +122,7 @@ describe('downloads local resources', () => {
119122
const cssPath = path.join(
120123
tmpFolder,
121124
'ru-hexlet-io-courses_files',
122-
'ru-hexlet-io-courses_files/ru-hexlet-io-assets-application.css',
125+
'ru-hexlet-io-assets-application.css',
123126
);
124127

125128
const actualCss = await fs.readFile(cssPath, 'utf-8');
@@ -131,7 +134,7 @@ describe('downloads local resources', () => {
131134
const scriptPath = path.join(
132135
tmpFolder,
133136
'ru-hexlet-io-courses_files',
134-
'ru-hexlet-io-courses_files/ru-hexlet-io-packs-js-runtime.js',
137+
'ru-hexlet-io-packs-js-runtime.js',
135138
);
136139

137140
const actualScript = await fs.readFile(scriptPath, 'utf-8');

cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { loadPage } from './page-loader';
3+
import { loadPage } from './page-loader.js';
44
import { program } from 'commander';
55

66
program.option('-o, --output <path>', 'output path');

page-loader.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createWriteStream } from 'fs';
88

99
let $;
1010

11-
class PageLoader {
11+
export class PageLoader {
1212
#url;
1313
#destFolder;
1414
#resourceDist;
@@ -24,7 +24,8 @@ class PageLoader {
2424
$ = cheerio.load(html);
2525

2626
await this.#createResourceDir();
27-
await this.#loadImages();
27+
await this.#loadResources();
28+
// await this.#loadImages();
2829

2930
await fsc.writeFile(filepath, $);
3031

@@ -50,11 +51,23 @@ class PageLoader {
5051
return { filepath, html };
5152
}
5253

53-
async #loadImages() {
54+
async #loadResources() {
55+
const $links = $('link');
5456
const $images = $('img');
55-
await Promise.allSettled($images.map((_, img) => this.#loadResource(img)));
57+
const $scripts = $('script');
58+
59+
const promises = [$links, $images, $scripts].flatMap(($elements) =>
60+
$elements.toArray().map((el) => this.#loadResource(el)),
61+
);
62+
63+
await Promise.allSettled(promises);
5664
}
5765

66+
// async #loadImages() {
67+
// const $images = $('img');
68+
// await Promise.allSettled($images.map((_, img) => this.#loadResource(img)));
69+
// }
70+
5871
#getUrlAttr(element) {
5972
return element.name === 'link' ? 'href' : 'src';
6073
}
@@ -68,7 +81,7 @@ class PageLoader {
6881
const resourceUrl = new URL(element.attribs[urlAttr], this.#url.href);
6982

7083
if (!this.#isResourceLocal(resourceUrl)) {
71-
return;
84+
return Promise.resolve();
7285
}
7386

7487
let resp;
@@ -115,11 +128,6 @@ class PageLoader {
115128
}
116129
}
117130

118-
const loadPage = (url, destFolder) => {
131+
export const loadPage = (url, destFolder) => {
119132
return new PageLoader(url, destFolder).load();
120133
};
121-
122-
module.exports = {
123-
PageLoader,
124-
loadPage,
125-
};

0 commit comments

Comments
 (0)