소스 검색

feat: add `--no-clobber` option to CLI (#3025)

Co-authored-by: skoriop <karthikp.ivy@gmail.com>
Co-authored-by: Steven <steven@ceriously.com>
Karthik Prakash 1 년 전
부모
커밋
b8adee7f7c
2개의 변경된 파일13개의 추가작업 그리고 1개의 파일을 삭제
  1. 9 0
      bin/main.js
  2. 4 1
      man/marked.1.md

+ 9 - 0
bin/main.js

@@ -62,6 +62,7 @@ export async function main(nodeProcess) {
     let tokens;
     let tokens;
     let config;
     let config;
     let opt;
     let opt;
+    let noclobber;
 
 
     function getArg() {
     function getArg() {
       let arg = argv.shift();
       let arg = argv.shift();
@@ -114,6 +115,10 @@ export async function main(nodeProcess) {
         case '--config':
         case '--config':
           config = argv.shift();
           config = argv.shift();
           break;
           break;
+        case '-n':
+        case '--no-clobber':
+          noclobber = true;
+          break;
         case '-h':
         case '-h':
         case '--help':
         case '--help':
           return await help();
           return await help();
@@ -216,6 +221,10 @@ export async function main(nodeProcess) {
       : await marked.parse(data, options);
       : await marked.parse(data, options);
 
 
     if (output) {
     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);
       return await writeFile(output, html);
     }
     }
 
 

+ 4 - 1
man/marked.1.md

@@ -2,7 +2,7 @@
 
 
 ## SYNOPSIS
 ## SYNOPSIS
 
 
-`marked` [`-o` <output file>] [`-i` <input file>] [`-s` <markdown string>] [`-c` <config file>] [`--help`] [`--version`] [`--tokens`] [`--pedantic`] [`--gfm`] [`--breaks`] [`--no-etc...`] [`--silent`] [filename]
+`marked` [`-o` <output file>] [`-i` <input file>] [`-s` <markdown string>] [`-c` <config file>] [`--help`] [`--version`] [`--tokens`] [`--no-clobber`] [`--pedantic`] [`--gfm`] [`--breaks`] [`--no-etc...`] [`--silent`] [filename]
 
 
 ## DESCRIPTION
 ## DESCRIPTION
 
 
@@ -45,6 +45,9 @@ Specify config file to use instead of the default `~/.marked.json` or `~/.marked
 * -t, --tokens
 * -t, --tokens
 Output a token list instead of html.
 Output a token list instead of html.
 
 
+* -n, --no-clobber
+Do not overwrite `output` if it exists.
+
 * --pedantic
 * --pedantic
 Conform to obscure parts of markdown.pl as much as possible.
 Conform to obscure parts of markdown.pl as much as possible.
 Don't fix original markdown bugs.
 Don't fix original markdown bugs.