Tools: Automating Digital Cleanup: A Deep Dive into the File Archiver CLI

Tools: Automating Digital Cleanup: A Deep Dive into the File Archiver CLI

Source: Dev.to

What is File Archiver CLI? ## How to Use It ## Under the Hood: Architecture and Tech Stack ## The Archiving Workflow ## Enterprise-Ready: Testing and Documentation ## Conclusion If you manage servers, applications, or even just a cluttered local drive, you know how quickly files can pile up. Log files, old reports, and daily backups can rapidly consume valuable disk space. Enter File Archiver CLI, an open-source utility designed to automate the process of compressing and cleaning up old files. Here is a closer look at what this tool is, how it works under the hood, and how it can be used to keep your directories pristine. Authored by rpi1337, File Archiver CLI is a command-line interface application that targets a specific source folder, identifies files that are older than a specified number of months, and archives them into a single destination folder. By default, it outputs the archived files in a .zip format, though the architecture is typed to support extensions like .tar.gz. The tool is built on Node.js. To get started, you simply install the dependencies and run the start script. File Archiver CLI is written in TypeScript and compiles down to ES5. It leverages several robust NPM packages: The codebase relies heavily on Object-Oriented principles, using Singleton classes to manage distinct responsibilities: When you run the tool via the CLI, Program.main() parses the arguments and triggers FileArchiver to execute a strict workflow: Reliability is key when writing scripts that intentionally delete files. The author built this CLI keeping testability in mind. Using the Jasmine testing framework alongside jasmine-auto-spies and stream-mock, the core logic is completely unit-tested. For example, the test suite verifies that the original files are deleted only after the zip compression successfully occurs. Furthermore, the project ships with complete HTML documentation generated via TypeDoc. This makes the codebase highly accessible to contributors who might want to extend its capabilities. Whether you need a ready-to-use tool to clear out old logs or a well-structured open-source project to study TypeScript and Node.js Streams, File Archiver CLI is an excellent repository to explore. It efficiently bridges the gap between simple ad-hoc file system scripts and robust software architecture. This project is distributed under the GNU General Public License v3.0. https://github.com/arpad1337/file-archiver Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK: npm install npm start sourceFolder [--format zip] [--months months] destinationFolder Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: npm install npm start sourceFolder [--format zip] [--months months] destinationFolder COMMAND_BLOCK: npm install npm start sourceFolder [--format zip] [--months months] destinationFolder - sourceFolder: The directory you want to clean up. - --format: The compression format (e.g., zip). - --months: An integer representing how many months old a file must be to get archived. - destinationFolder: Where the final archive will be saved. - moment: For precise date math to determine file age. - fs-extra: For enhanced file system operations. - archiver: A streaming interface for generating the archives. - uuid4: To generate unique names for temporary directories. - FileStorage: Handles interacting with the file system. It reads directories, checks file metadata (mtime), copies files, and deletes directories recursively. - ZipCompressor: Wraps the archiver library to stream the contents of a directory into a highly compressed (level 9) ZIP file. - FileArchiver: The orchestrator. It calculates the cutoff date using the provided month integer and coordinates the other classes. - Filter: It reads the source folder and filters out any files modified more recently than the cutoff date. - Isolate: It copies the qualifying files into a temporary folder inside /tmp/, uniquely named using uuid4. - Compress: The ZipCompressor points to this temporary folder and pipes its contents into a newly created ZIP file located in your specified destination folder. - Clean Up: Once compression is successfully resolved, it recursively deletes the temporary folder and systematically removes the original targeted files from the source directory.