From b8adee7f7ca589cde2c25caeb0ee628f792b9a8f Mon Sep 17 00:00:00 2001 From: Karthik Prakash <116057817+skoriop@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:35:55 +0530 Subject: [PATCH] feat: add `--no-clobber` option to CLI (#3025) Co-authored-by: skoriop Co-authored-by: Steven --- bin/main.js | 9 +++++++++ man/marked.1.md | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/main.js b/bin/main.js index ddc32fb8..97ac522c 100644 --- a/bin/main.js +++ b/bin/main.js @@ -62,6 +62,7 @@ export async function main(nodeProcess) { let tokens; let config; let opt; + let noclobber; function getArg() { let arg = argv.shift(); @@ -114,6 +115,10 @@ export async function main(nodeProcess) { case '--config': config = argv.shift(); break; + case '-n': + case '--no-clobber': + noclobber = true; + break; case '-h': case '--help': return await help(); @@ -216,6 +221,10 @@ export async function main(nodeProcess) { : await marked.parse(data, options); if (output) { + if (noclobber && await fileExists(output)) { + nodeProcess.stderr.write('marked: output file \'' + output + '\' already exists, disable the \'-n\' / \'--no-clobber\' flag to overwrite\n'); + nodeProcess.exit(1); + } return await writeFile(output, html); } diff --git a/man/marked.1.md b/man/marked.1.md index 34d89513..d5b94a45 100644 --- a/man/marked.1.md +++ b/man/marked.1.md @@ -2,7 +2,7 @@ ## SYNOPSIS -`marked` [`-o` ] [`-i` ] [`-s` ] [`-c` ] [`--help`] [`--version`] [`--tokens`] [`--pedantic`] [`--gfm`] [`--breaks`] [`--no-etc...`] [`--silent`] [filename] +`marked` [`-o` ] [`-i` ] [`-s` ] [`-c` ] [`--help`] [`--version`] [`--tokens`] [`--no-clobber`] [`--pedantic`] [`--gfm`] [`--breaks`] [`--no-etc...`] [`--silent`] [filename] ## DESCRIPTION @@ -45,6 +45,9 @@ Specify config file to use instead of the default `~/.marked.json` or `~/.marked * -t, --tokens Output a token list instead of html. +* -n, --no-clobber +Do not overwrite `output` if it exists. + * --pedantic Conform to obscure parts of markdown.pl as much as possible. Don't fix original markdown bugs.