Skip to content

Commit

Permalink
feat: create @amazeelabs/save-webpage
Browse files Browse the repository at this point in the history
  • Loading branch information
Leksat committed Jan 19, 2024
1 parent 92c42d2 commit 6d41cb1
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/npm/@amazeelabs/save-webpage/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: ['@amazeelabs/eslint-config'],
root: true,
};
2 changes: 2 additions & 0 deletions packages/npm/@amazeelabs/save-webpage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
node_modules
1 change: 1 addition & 0 deletions packages/npm/@amazeelabs/save-webpage/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!build
1 change: 1 addition & 0 deletions packages/npm/@amazeelabs/save-webpage/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@amazeelabs/prettier-config"
27 changes: 27 additions & 0 deletions packages/npm/@amazeelabs/save-webpage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@amazeelabs/save-webpage",
"version": "1.0.0",
"description": "Saves a webpage with all resources.",
"type": "module",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"private": false,
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "rm -rf build && tsc",
"test:static": "tsc --noEmit && eslint '**/*.{ts,tsx,js,jsx}' --ignore-path='./.gitignore'"
},
"dependencies": {
"website-scraper": "^5.3.1"
},
"devDependencies": {
"@amazeelabs/eslint-config": "1.4.43",
"@amazeelabs/prettier-config": "1.1.3",
"@types/website-scraper": "^1.2.10",
"typescript": "5.3.3"
},
"keywords": [],
"author": "AmazeeLabs <[email protected]>"
}
57 changes: 57 additions & 0 deletions packages/npm/@amazeelabs/save-webpage/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as scrape from 'website-scraper';

export async function saveWebpage(args: {
/**
* URL of the target webpage.
*/
url: string;
/**
* Replaces the actual webpage content.
*/
content?: string;
/**
* Path to directory where downloaded files will be saved. Must not exist.
*/
directory: string;
}): Promise<void> {
await scrape({
directory: args.directory,
urls: [args.url],
plugins: args.content ? [new ReplaceContentPlugin(args.content)] : [],
});
}

// Replaces the first request response with given content.
class ReplaceContentPlugin {
isFirstRequest = true;

constructor(public firstRequestResponse: string) {}

apply(
registerAction: (
event: 'afterResponse',
handler: <T>(args: { response: T }) => Promise<
| T
| {
body: string;
encoding: 'utf8';
metadata: {};
}
>,
) => void,
): void {
registerAction('afterResponse', async ({ response }) => {
if (this.isFirstRequest) {
this.isFirstRequest = false;
return {
body: this.firstRequestResponse,
encoding: 'utf8',
metadata: {},
};
} else {
this.isFirstRequest = false;
return response;
}
});
}
}
12 changes: 12 additions & 0 deletions packages/npm/@amazeelabs/save-webpage/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"declaration": true,
"target": "esnext",
"module": "esnext",
"strict": true,
"skipLibCheck": true,
"outDir": "build"
},
"include": ["src"],
"$schema": "https://json.schemastore.org/tsconfig"
}
146 changes: 144 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d41cb1

Please sign in to comment.