Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/plugins/save-resource-to-fs-plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import fs from 'fs-extra';
import fs from 'fs';
import { outputFile } from '../utils/fs.js';

class SaveResourceToFileSystemPlugin {
apply (registerAction) {
Expand All @@ -20,13 +21,13 @@ class SaveResourceToFileSystemPlugin {
registerAction('saveResource', async ({resource}) => {
const filename = path.join(absoluteDirectoryPath, resource.getFilename());
const text = resource.getText();
await fs.outputFile(filename, text, { encoding: resource.getEncoding() });
await outputFile(filename, text, resource.getEncoding());
loadedResources.push(resource);
});

registerAction('error', async () => {
if (loadedResources.length > 0) {
await fs.remove(absoluteDirectoryPath);
await fs.promises.rm(absoluteDirectoryPath, {force: true, recursive: true});
}
});
}
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from 'path';
import fs from 'fs/promises';

async function outputFile (file, data, encoding) {
const dir = path.dirname(file);
await fs.mkdir(dir, { recursive: true});

return fs.writeFile(file, data, { encoding: encoding });
}

export {
outputFile
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"css-url-parser": "^1.0.0",
"debug": "^4.3.1",
"filenamify": "^7.0.0",
"fs-extra": "^11.1.0",
"got": "^14.4.7",
"normalize-url": "^9.0.0",
"p-queue": "^9.0.0",
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/e2e-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scrape from 'website-scraper';
import fs from 'fs-extra';
import fs from 'fs';
import * as chai from 'chai';

import { readFile } from 'fs/promises';
Expand All @@ -11,7 +11,8 @@ chai.should();

describe('E2E', function() {
before(function() {
fs.emptyDirSync(resultDirname);
fs.rmSync(resultDirname, {recursive: true, force: true});
fs.mkdirSync(resultDirname, {recursive: true});
});

after(function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/base/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import * as cheerio from 'cheerio';
import scrape from 'website-scraper';
import Resource from '../../../lib/resource.js';
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('Functional: base', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/base/check-it-works.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as chai from 'chai';
chai.should();
import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/base/.tmp2';
Expand All @@ -17,7 +17,7 @@ describe('Functional: check it works', function() {
afterEach(function () {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should work with promise', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/binary-resources/images.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import * as cheerio from 'cheerio';
import scrape from 'website-scraper';

Expand Down Expand Up @@ -31,7 +31,7 @@ describe('Functional: images', () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

beforeEach(() => {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/callbacks/callbacks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import sinon from 'sinon';
import scrape from 'website-scraper';

Expand All @@ -19,7 +19,7 @@ describe('Functional: onResourceSaved and onResourceError callbacks in plugin',
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should call onResourceSaved callback and onResourceError callback if ignoreErrors = true', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/circular-dependencies/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional circular dependencies', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should correctly load files with circular dependency', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/css-handling/css-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/css-handling/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: css handling', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should correctly handle css files, style tags and style attributes and ignore css-like text inside common html tags', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/data-url/data-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/data-url/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: data urls handling', function () {
afterEach(function () {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should correctly handle html files with data urls in attributes', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/error-handling/error-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import sinon from 'sinon';
import scrape from 'website-scraper';
import Scraper from '../../../lib/scraper.js';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('Functional error handling', function() {
afterEach(function () {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

describe('FS Error', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/html-entities/html-entities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/html-entities/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: html entities', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should decode all html-entities found in html files and not encode entities from css file', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/html-id-href/html-id-href.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/html-id-href/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional html id href', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should ignore same-file paths and update other-file paths', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/max-depth/max-depth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/max-depth/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: maxDepth and maxRecursiveDepth ', () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should filter out all resources by depth > maxDepth', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/recursive/recursive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/recursive/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional recursive downloading', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should follow anchors if recursive flag is set', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/redirect/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import sinon from 'sinon';
import scrape from 'website-scraper';
import Scraper from '../../../lib/scraper.js';
Expand All @@ -21,7 +21,7 @@ describe('Functional redirects', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should follow redirects and save resource once if it has different urls', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/request-concurrency/.tmp';
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Functional concurrent requests', function() {
afterEach(function () {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should have maximum concurrent requests == requestConcurrency option', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/req-res-customizations-after-response/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: afterResponse action in plugin', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should skip downloading resource if afterResponse returns null', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/req-res-customizations-request/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional: customize request options with plugin', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should use options from request property if no beforeRequest actions', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/resource-saver/resource-saver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import sinon from 'sinon';
import scrape from 'website-scraper';

Expand All @@ -20,7 +20,7 @@ describe('Functional: plugin for saving resources', () => {
afterEach(() => {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

let saveResourceStub, handleErrorStub, saveResourcePlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ chai.should();

import '../../utils/assertions.js';
import nock from 'nock';
import fs from 'fs-extra';
import fs from 'fs';
import scrape from 'website-scraper';

const testDirname = './test/functional/resource-without-ext/.tmp';
Expand All @@ -19,7 +19,7 @@ describe('Functional resources without extensions', function() {
afterEach(function() {
nock.cleanAll();
nock.enableNetConnect();
fs.removeSync(testDirname);
fs.rmSync(testDirname, {recursive: true, force: true});
});

it('should load resources without extensions with correct type and wrap with extensions', function () {
Expand Down
Loading