Monday, 17 August 2020

rmby - Remove files asynchronously with a fluent interface

Hi everyone!TLDR: A zero-dependency Node.js library with a fluent interface for removing files asynchronously by certain aspectsI always wanted to know how the process of library development works and how to develop my own library that I could publish on NPM. Recently I had a use case on one of my side-projects where I had to delete files on a certain folder that are older than X hours using a cronjob. The library that I was using back then couldn't handle hours, just minutes and it only worked synchronously. I decided to ditch the library and develop the feature by myself.Then I realised that it would be the perfect moment to provide a library that could handle this use case and also provide more filter criteria's for more sophisticated removal use cases. And that's how "rmby" ("remove by") was born.The goal of rmby is to provide a fluent interface that is easy to use and guides the user when creating a remove query.Since this is my first library I would also appreciate any advice from people that are more experienced in library development and could share some tips on what to watch out for :)GitHub: https://github.com/yduman/rmbyNPM: https://www.npmjs.com/package/rmbyNow some examples:UsageThe RemoveFiles class is all you need. You can navigate yourself through the API by chaining methods, since the API provides a fluent interface.```js // JavaScript const { RemoveFiles } = require("rmby");// TypeScript import { RemoveFiles } from "rmby"; ```Remove Files By TimeFiles can be removed by a time difference in milliseconds, seconds, minutes or hours. The time difference is always checked against the current time.js // Remove all files that are older than 12 hours async () => await new RemoveFiles().from("/some/path/to/dir").byTime().inHours().olderThan(12).run();Remove Files By NameFiles can be removed regarding its name without considering the file extension. You can remove files that match exactly, start with, end with, or include the name that you provide.js // Remove all files that start with "React" async () => await new RemoveFiles().from("/some/path/to/dir").byName().thatStartsWith("React").run();Remove Files By ExtensionFiles can be removed regarding their file extension. You can remove files that match exactly with the extension you provide.js // Remove all .log files async () => await new RemoveFiles().from("/some/path/to/dir").byExtension(".log").run();Remove Files By CombinationFiles can be removed by combining the available filters. Therefore you can create more specific filters for your remove use case.js // Remove all JS files that start with "f" and are older than 12 hours async () => await new RemoveFiles() .from("/some/path/to/dir") .byName() .thatStartsWith("f") .and() .byExtension(".js") .and() .byTime() .inHours() .olderThan(12) .run();

Submitted August 17, 2020 at 04:18PM by prox76

No comments:

Post a Comment