Skip to content

Instantly share code, notes, and snippets.

@WomB0ComB0
Last active April 22, 2025 16:09
Show Gist options
  • Save WomB0ComB0/e9b5f467369316a3853bbdbb5ed3d740 to your computer and use it in GitHub Desktop.
Save WomB0ComB0/e9b5f467369316a3853bbdbb5ed3d740 to your computer and use it in GitHub Desktop.
format-gitignore.ts and related files - with AI-generated descriptions
import { $, write } from "bun";
if (require.main === module) {
(async () => {
try {
const filePath = `${process.cwd()}/.gitignore`;
console.log(filePath);
const content = (await $`cat ${filePath}`.text().then(text => text.trim()
.replace(/[\u{1F600}-\u{1F64F}]/gu, "")
.replace(/[^\x00-\x7F]/g, "")
.replace(/,/g, " ")
.replace(/:/g, " ")
.replace(/=/g, " ")
.replace(/;/g, " ")))
if (!content) throw new Error("File not found");
const book: Set<string> = new Set();
for (const line of content.split("\n")) {
if (line.includes("#")) {
if (line.startsWith("#")) continue;
const [key, value] = line.split("#");
if (key.trim().length > 0) book.add(key);
if (value.trim().length > 0) book.add(value);
};
if (line.trim().length > 0) book.add(line);
}
await write(filePath, Array.from(book.values()).join("\n"));
} catch (error) {
console.error("An error occurred:", error);
throw new Error(`${error instanceof Error ? error.message : error}`);
}
})();
process.exit(0);
}

format-gitignore.ts Description

File Type: ts

Generated Description:

Analysis of format-gitignore.ts

Summary

The TypeScript file format-gitignore.ts reads a .gitignore file, cleans its content by removing emojis, non-ASCII characters, and replacing certain punctuation marks with spaces, and then parses the file line by line to create a set of unique entries. It then writes the formatted and unique entries to a new file named backup_ignore.log. The script uses Bun's $ function for shell command execution.

Key Components and Functions

  1. File Reading and Cleaning: The script reads the .gitignore file using Bun's $ command to execute cat. It then applies a series of regular expression replacements to clean the content, removing emojis, non-ASCII characters, and replacing commas, colons, equal signs, and semicolons with spaces.

  2. Parsing and Deduplication: The cleaned content is split into lines. Each line is processed:

    • Lines starting with # are treated as comments, but the text after the # is also added if it's not empty.
    • Lines containing # but not starting with it are split at the # character, and both parts (before and after #) are added if not empty.
    • Other lines are added to the set. This ensures that all unique entries, even those within comments, are preserved. A Set is used efficiently to deduplicate entries.
  3. Output Writing: Finally, the unique entries from the set are joined back together with newline characters and written to a new file named backup_ignore.log using Bun's $ to execute echo.

  4. Error Handling: A try...catch block handles potential errors during file reading or writing, providing informative error messages.

Notable Patterns and Techniques

  • Use of Bun's $: The script leverages Bun's built-in $ function for executing shell commands, simplifying the process of interacting with the operating system. This makes the code concise.

  • Regular Expression Cleaning: The use of regular expressions efficiently removes unwanted characters and formats the input data.

  • Set for Deduplication: Using a Set efficiently handles deduplication of entries in the .gitignore file.

  • Clear Error Handling: The try...catch block ensures robustness by handling potential exceptions and providing useful error messages.

  • Conditional Processing of Lines: The script demonstrates clear and concise logic for handling different types of lines (comments, regular entries).

Potential Use Cases

  • Cleaning and standardizing .gitignore files: The script can be used to clean up messy .gitignore files, removing unwanted characters and ensuring only unique entries remain.

  • Generating a backup of .gitignore: The script creates a backup of the .gitignore file after processing, which can be useful before making significant changes.

  • Programmatic management of .gitignore: The script could be integrated into a larger build process or CI/CD pipeline to automatically manage and standardize .gitignore files.

  • Data Migration/Transformation: The logic could be adapted to handle other similar types of files that need cleaning and deduplication.

Description generated on 4/22/2025, 12:04:10 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment