소스 검색

add --filename-conflict-action switch

Gildas 5 년 전
부모
커밋
73c1adc66e
2개의 변경된 파일20개의 추가작업 그리고 9개의 파일을 삭제
  1. 3 0
      cli/args.js
  2. 17 9
      cli/single-file-cli-api.js

+ 3 - 0
cli/args.js

@@ -47,6 +47,7 @@ const args = require("yargs")
 		"compress-HTML": true,
 		"dump-content": false,
 		"filename-template": "{page-title} ({date-iso} {time-locale}).html",
+		"filename-conflict-action": "uniquify",
 		"filename-replacement-character": "_",
 		"group-duplicate-images": true,
 		"http-header": [],
@@ -138,6 +139,8 @@ const args = require("yargs")
 	.string("error-file")
 	.options("filename-template", { description: "Template used to generate the output filename (see help page of the extension for more info)" })
 	.string("filename-template")
+	.options("filename-conflict-action", { description: "Action when the filename is conflicting with existing one on the filesystem. The possible values are \"uniquify\" (default), \"overwrite\" and \"skip\"" })
+	.string("filename-conflict-action")
 	.options("filename-replacement-character", { description: "The character used for replacing invalid characters in filenames" })
 	.string("filename-replacement-character")
 	.string("filename-replacement-character")

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

@@ -203,12 +203,16 @@ async function capturePage(options) {
 	try {
 		const pageData = await backend.getPageData(options);
 		if (options.output) {
-			fs.writeFileSync(getFilename(options.output), pageData.content);
+			const filename = getFilename(pageData.output, options);
+			if (filename) {
+				fs.writeFileSync(options.output, pageData.content);
+			}
 		} else {
-			if (options.filenameTemplate && pageData.filename) {
-				fs.writeFileSync(getFilename(pageData.filename), pageData.content);
-			} 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);
 			}
 		}
 		return pageData;
@@ -222,19 +226,23 @@ async function capturePage(options) {
 	}
 }
 
-function getFilename(filename, index = 1) {
+function getFilename(filename, options, index = 1) {
 	let newFilename = filename;
-	if (index > 1) {
+	if (options.filenameConflictAction == "overwrite") {
+		return filename;
+	} else if (options.filenameConflictAction == "uniquify" && index > 1) {
 		const regExpMatchExtension = /(\.[^.]+)$/;
 		const matchExtension = newFilename.match(regExpMatchExtension);
 		if (matchExtension && matchExtension[1]) {
-			newFilename = newFilename.replace(regExpMatchExtension, " - " + index + matchExtension[1]);
+			newFilename = newFilename.replace(regExpMatchExtension, " (" + index + matchExtension[1]) + ")";
 		} else {
-			newFilename += " - " + index;
+			newFilename += " (" + index + ")";
 		}
 	}
 	if (fs.existsSync(newFilename)) {
-		return getFilename(filename, index + 1);
+		if (options.filenameConflictAction != "skip") {
+			return getFilename(filename, options, index + 1);
+		}
 	} else {
 		return newFilename;
 	}