FlamingTempura / bibtex-tidy

Cleaner and Formatter for BibTeX files
https://flamingtempura.github.io/bibtex-tidy/
MIT License
832 stars 63 forks source link

Option `--backup` is not enabled by default #395

Closed arkivm closed 1 year ago

arkivm commented 1 year ago

In CLI mode, the option --backup gets activated only if the option is explicitly passed.

Expected behavior: This should happen automatically as it is marked as default.

The default option gets set in normalizeOptions which gets called only from tidy. However, the normalized options are not visible in the start function.

This crude band-aid fix solves it for me.

diff --git a/src/cli.ts b/src/cli.ts
index 05f4cd1..5004a21 100755
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -41,21 +41,22 @@ async function start(): Promise<void> {
        console.error = () => undefined;
    }
    console.log('Tidying...');
+   const options_ = normalizeOptions(options);
    for (const inputFile of inputFiles) {
        const isStdIO = inputFile === '-';
        const bibtex = isStdIO
            ? await readStdin()
            : readFileSync(inputFile, 'utf8');
-       const result = tidy(bibtex, options);
+       const result = tidy(bibtex, options_);
        for (const warning of result.warnings) {
            console.error(`${warning.code}: ${warning.message}`);
        }
        console.log(`Done. Successfully tidied ${result.count} entries.`);
-       if (options.merge) {
+       if (options_.merge) {
            const dupes = result.warnings.filter((w) => w.code === 'DUPLICATE_ENTRY');
            console.log(`${dupes.length} entries merged`);
        }
-       if (options.backup && !isStdIO) {
+       if (options_.backup && !isStdIO) {
            writeFileSync(`${inputFile}.original`, bibtex, 'utf8');
        }
        if (isStdIO) {
diff --git a/src/index.ts b/src/index.ts
index e777854..06b9c39 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -20,9 +20,7 @@ export type BibTeXTidyResult = {
    count: number;
 };

-export function tidy(input: string, options_: Options = {}): BibTeXTidyResult {
-   const options = normalizeOptions(options_);
-
+export function tidy(input: string, options): BibTeXTidyResult {
    input = convertCRLF(input);

    // Parse the bibtex and retrieve the items (includes comments, entries, strings, preambles)
FlamingTempura commented 1 year ago

Thanks for raising this. This should be fixed in v1.10.1. Executing bibtex-tidy <file> should create the file <file>.original.bib.

Note that bibtex-tidy v2 will work slightly differently. You will be able to specify a different input and output, which hopefully precludes the need for backups. The v2 behaviour can be enabled using the --v2 flag - e.g. bibtex-tidy --v2 references.bib -o tidied.bib.