@pagepocket/cli 0.6.3 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/cli.js +62 -18
- package/dist/stages/prepare-output.js +0 -2
- package/dist/stages/write-snapshot.js +3 -1
- package/dist/utils/with-spinner.js +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
package/dist/cli.js
CHANGED
|
@@ -12,41 +12,72 @@ const chalk_1 = __importDefault(require("chalk"));
|
|
|
12
12
|
const prepare_output_1 = require("./stages/prepare-output");
|
|
13
13
|
const write_snapshot_1 = require("./stages/write-snapshot");
|
|
14
14
|
const with_spinner_1 = require("./utils/with-spinner");
|
|
15
|
+
const MAX_STATUS_URL_LENGTH = 80;
|
|
16
|
+
const formatStatusUrl = (url) => {
|
|
17
|
+
if (url.length <= MAX_STATUS_URL_LENGTH) {
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
return `${url.slice(0, MAX_STATUS_URL_LENGTH - 3)}...`;
|
|
21
|
+
};
|
|
15
22
|
class PagepocketCommand extends core_1.Command {
|
|
16
23
|
async run() {
|
|
17
24
|
const { args, flags } = await this.parse(PagepocketCommand);
|
|
18
25
|
const targetUrl = args.url;
|
|
19
26
|
const outputFlag = flags.output ? flags.output.trim() : undefined;
|
|
27
|
+
const overwrite = flags.overwrite === true;
|
|
20
28
|
const triggerActions = (flags.action ?? []);
|
|
21
29
|
const runtime = flags.runtime;
|
|
30
|
+
const timeoutMs = typeof flags.timeout === "number" ? flags.timeout : undefined;
|
|
31
|
+
const maxDurationMs = typeof flags.maxDuration === "number" ? flags.maxDuration : undefined;
|
|
22
32
|
const headers = {
|
|
23
33
|
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
|
24
34
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
|
25
35
|
"accept-language": "en-US,en;q=0.9",
|
|
26
36
|
referer: targetUrl
|
|
27
37
|
};
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
const interceptor = runtime === "puppeteer"
|
|
39
|
+
? new puppeteer_adapter_1.PuppeteerAdapter({
|
|
40
|
+
// gotoOptions: { waitUntil: "networkidle2" },
|
|
41
|
+
triggerActions
|
|
42
|
+
})
|
|
43
|
+
: new lighterceptor_adapter_1.LighterceptorAdapter({ headers, triggerActions });
|
|
44
|
+
const pagepocket = lib_1.PagePocket.fromURL(targetUrl);
|
|
45
|
+
const snapshot = await (0, with_spinner_1.withSpinner)(async (spinner) => {
|
|
46
|
+
const eventsTask = (async () => {
|
|
47
|
+
for await (const event of pagepocket.interceptedRequestEvents()) {
|
|
48
|
+
if (event.type === "request") {
|
|
49
|
+
spinner.text = `Freezing page (${formatStatusUrl(event.url)})`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
try {
|
|
54
|
+
return await pagepocket.capture({
|
|
55
|
+
interceptor,
|
|
56
|
+
timeoutMs,
|
|
57
|
+
maxDurationMs,
|
|
58
|
+
blacklist: [...lib_1.ga, ...lib_1.sw_iframe, ...lib_1.ns]
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
try {
|
|
63
|
+
await eventsTask;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// Ignore event stream errors during shutdown.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}, "Freezing page");
|
|
70
|
+
const { outputDir } = await (0, with_spinner_1.withSpinner)((_spinner) => (0, prepare_output_1.prepareOutputDir)(snapshot.title ?? "snapshot", outputFlag), "Preparing output directory");
|
|
71
|
+
const writeResult = await (0, with_spinner_1.withSpinner)(async (_spinner) => {
|
|
72
|
+
return (0, write_snapshot_1.writeSnapshotFiles)({
|
|
44
73
|
outputDir,
|
|
45
|
-
snapshot
|
|
74
|
+
snapshot,
|
|
75
|
+
overwrite
|
|
46
76
|
});
|
|
47
77
|
}, "Writing snapshot files");
|
|
78
|
+
const resolvedOutputDir = writeResult?.outputDir ?? outputDir;
|
|
48
79
|
this.log(chalk_1.default.green("All done! Snapshot created."));
|
|
49
|
-
this.log(`Snapshot saved to ${chalk_1.default.cyan(
|
|
80
|
+
this.log(`Snapshot saved to ${chalk_1.default.cyan(resolvedOutputDir)}`);
|
|
50
81
|
process.exit();
|
|
51
82
|
}
|
|
52
83
|
}
|
|
@@ -65,6 +96,10 @@ PagepocketCommand.flags = {
|
|
|
65
96
|
char: "o",
|
|
66
97
|
description: "Output path for the snapshot HTML file"
|
|
67
98
|
}),
|
|
99
|
+
overwrite: core_1.Flags.boolean({
|
|
100
|
+
description: "Overwrite existing output directory instead of suffixing",
|
|
101
|
+
default: false
|
|
102
|
+
}),
|
|
68
103
|
action: core_1.Flags.string({
|
|
69
104
|
char: "a",
|
|
70
105
|
description: "Trigger action to run during/after capture (repeatable). Use -a multiple times to run multiple actions.",
|
|
@@ -76,6 +111,15 @@ PagepocketCommand.flags = {
|
|
|
76
111
|
description: "Interceptor runtime to use",
|
|
77
112
|
options: ["lighterceptor", "puppeteer"],
|
|
78
113
|
default: "lighterceptor"
|
|
114
|
+
}),
|
|
115
|
+
timeout: core_1.Flags.integer({
|
|
116
|
+
char: "t",
|
|
117
|
+
description: "Network idle duration in milliseconds before capture stops",
|
|
118
|
+
default: 5000
|
|
119
|
+
}),
|
|
120
|
+
maxDuration: core_1.Flags.integer({
|
|
121
|
+
description: "Hard max capture duration in milliseconds",
|
|
122
|
+
default: 60000
|
|
79
123
|
})
|
|
80
124
|
};
|
|
81
125
|
exports.default = PagepocketCommand;
|
|
@@ -4,14 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.prepareOutputDir = void 0;
|
|
7
|
-
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
8
|
const filename_1 = require("../lib/filename");
|
|
10
9
|
const prepareOutputDir = async (title, outputFlag) => {
|
|
11
10
|
const safeTitle = (0, filename_1.safeFilename)(title || "snapshot");
|
|
12
11
|
const baseDir = outputFlag ? node_path_1.default.resolve(outputFlag) : process.cwd();
|
|
13
12
|
const outputDir = node_path_1.default.join(baseDir, safeTitle);
|
|
14
|
-
await promises_1.default.mkdir(outputDir, { recursive: true });
|
|
15
13
|
return {
|
|
16
14
|
safeTitle,
|
|
17
15
|
outputDir
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.writeSnapshotFiles = void 0;
|
|
4
4
|
const writeSnapshotFiles = async (input) => {
|
|
5
|
-
|
|
5
|
+
return input.snapshot.toDirectory(input.outputDir, {
|
|
6
|
+
overwrite: input.overwrite
|
|
7
|
+
});
|
|
6
8
|
};
|
|
7
9
|
exports.writeSnapshotFiles = writeSnapshotFiles;
|
|
@@ -8,7 +8,7 @@ const ora_1 = __importDefault(require("ora"));
|
|
|
8
8
|
const withSpinner = async (run, spinnerText) => {
|
|
9
9
|
const spinner = (0, ora_1.default)(spinnerText).start();
|
|
10
10
|
try {
|
|
11
|
-
const result = await run();
|
|
11
|
+
const result = await run(spinner);
|
|
12
12
|
spinner.succeed(spinnerText.replace(/\.$/, "") || spinnerText);
|
|
13
13
|
return result;
|
|
14
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pagepocket/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "CLI for capturing offline snapshots of web pages.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"@oclif/core": "^4.0.9",
|
|
17
17
|
"chalk": "^4.1.2",
|
|
18
18
|
"ora": "^9.0.0",
|
|
19
|
-
"@pagepocket/lib": "0.
|
|
20
|
-
"@pagepocket/interceptor": "0.
|
|
21
|
-
"@pagepocket/
|
|
22
|
-
"@pagepocket/
|
|
19
|
+
"@pagepocket/lib": "0.7.1",
|
|
20
|
+
"@pagepocket/interceptor": "0.7.1",
|
|
21
|
+
"@pagepocket/puppeteer-adapter": "0.7.1",
|
|
22
|
+
"@pagepocket/lighterceptor-adapter": "0.7.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^20.11.30",
|