DEV Community

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

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.

What is File Archiver CLI?

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.

How to Use It

The tool is built on Node.js. To get started, you simply install the dependencies and run the start script.

The basic syntax is:

npm install
npm start sourceFolder [--format zip] [--months months] destinationFolder

Enter fullscreen mode Exit fullscreen mode
  • 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.

Under the Hood: Architecture and Tech Stack

File Archiver CLI is written in TypeScript and compiles down to ES5. It leverages several robust NPM packages:

  • 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.

The codebase relies heavily on Object-Oriented principles, using Singleton classes to manage distinct responsibilities:

  1. FileStorage: Handles interacting with the file system. It reads directories, checks file metadata (mtime), copies files, and deletes directories recursively.
  2. ZipCompressor: Wraps the archiver library to stream the contents of a directory into a highly compressed (level 9) ZIP file.
  3. FileArchiver: The orchestrator. It calculates the cutoff date using the provided month integer and coordinates the other classes.

The Archiving Workflow

When you run the tool via the CLI, Program.main() parses the arguments and triggers FileArchiver to execute a strict workflow:

  1. Filter: It reads the source folder and filters out any files modified more recently than the cutoff date.
  2. Isolate: It copies the qualifying files into a temporary folder inside /tmp/, uniquely named using uuid4.
  3. Compress: The ZipCompressor points to this temporary folder and pipes its contents into a newly created ZIP file located in your specified destination folder.
  4. Clean Up: Once compression is successfully resolved, it recursively deletes the temporary folder and systematically removes the original targeted files from the source directory.

Enterprise-Ready: Testing and Documentation

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.

Conclusion

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

Top comments (0)