Răsfoiți Sursa

fix support of directories

Gildas 5 ani în urmă
părinte
comite
359a237cfb
2 a modificat fișierele cu 14 adăugiri și 10 ștergeri
  1. 2 1
      cli/args.js
  2. 12 9
      cli/single-file-cli-api.js

+ 2 - 1
cli/args.js

@@ -30,6 +30,7 @@ const args = require("yargs")
 		yargs.positional("output", { description: "Output filename", type: "string" });
 	})
 	.default({
+		"background-save": true,
 		"back-end": "puppeteer",
 		"browser-headless": true,
 		"browser-executable-path": "",
@@ -198,7 +199,7 @@ const args = require("yargs")
 	.options("web-driver-executable-path", { description: "Path to Selenium WebDriver executable (webdriver-gecko, webdriver-chromium)" })
 	.string("web-driver-executable-path")
 	.options("output-directory", { description: "Path to where to save files, this path must exist." })
-	.string("output-directory")	
+	.string("output-directory")
 	.argv;
 args.compressCSS = args.compressCss;
 args.compressHTML = args.compressHtml;

+ 12 - 9
cli/single-file-cli-api.js

@@ -24,6 +24,7 @@
 /* global require, module, URL */
 
 const fs = require("fs");
+const path = require("path");
 const VALID_URL_TEST = /^(https?|file):\/\//;
 
 const STATE_PROCESSING = "processing";
@@ -201,19 +202,21 @@ function getHostURL(url) {
 
 async function capturePage(options) {
 	try {
+		let filename;
 		const pageData = await backend.getPageData(options);
 		if (options.output) {
-			const filename = getFilename(options.output, options);
-			if (filename) {
-				fs.writeFileSync(filename, pageData.content);
-			}
+			filename = getFilename(options.output, options);
+		} else if (options.dumpContent) {
+			console.log(pageData.content); // eslint-disable-line no-console
 		} else {
-			const filename = getFilename(pageData.filename, options);
-			if (options.dumpContent) {
-				console.log(pageData.content); // eslint-disable-line no-console
-			} else if (filename) {
-				fs.writeFileSync(filename, pageData.content);
+			filename = getFilename(pageData.filename, options);
+		}
+		if (filename) {
+			const dirname = path.dirname(filename);
+			if (dirname) {
+				fs.mkdirSync(dirname, { recursive: true });
 			}
+			fs.writeFileSync(filename, pageData.content);
 		}
 		return pageData;
 	} catch (error) {