DEV Community

Cover image for I built modulix. it does regenerate your codebase.
Arif Aydın
Arif Aydın

Posted on Edited on

I built modulix. it does regenerate your codebase.

Selling a product to one customer is simple. The moment a second customer says "we don't want the billing module, but we do want stock", things get messy. Teams usually pick one of three paths:

  1. A branch per customer. Fine for a week, then every fix has to be carried across five branches.
  2. if (features.billing) sprinkled everywhere. The disabled module's code still ships, and its tables still sit in the database.
  3. Delete things by hand before delivery. It works once. The second time, nobody remembers how it was done.

modulix is an open-source CLI I wrote to avoid all three: you mark modules inside the source code and switch them on or off with one command. It's on npm as modulix.

the idea: tags as comments

modulix adds no language-specific syntax. You wrap the code that belongs to a module between a start and an end tag, written in that language's own comment style:

// @auth users-table
const authService = new AuthService();
setupAuthListeners(authService);
// @!auth users-table
Enter fullscreen mode Exit fullscreen mode

@auth users-table opens the block, @!auth users-table closes it. auth is the module, users-table is the block. Because the tags are comments, the code compiles and runs exactly as if modulix weren't there.

The same works in HTML, CSS and SQL:

<!-- @billing invoice-panel -->
<div class="billing-container">
  <h3>Invoices</h3>
</div>
<!-- @!billing invoice-panel -->
Enter fullscreen mode Exit fullscreen mode
-- @auth users-schema
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50)
);
-- @!auth users-schema
Enter fullscreen mode Exit fullscreen mode

JavaScript, TypeScript, PHP, HTML, CSS/SCSS/LESS, Go, C# and SQL files are supported today. If a whole file belongs to a module, put @@module block on its first line: the file is deleted when the module is disabled and restored when it's enabled again.

the flow: tag, save a template, sync

Using modulix in a project takes four steps:

npm install -g modulix   # global CLI, short alias: mdl
mdl init                 # creates .modulix/config.json in the project
mdl config set includes add .php
mdl config set excludes add node_modules
Enter fullscreen mode Exit fullscreen mode

Then you declare modules and save the current state of the code as a template:

mdl modules add          # e.g. auth, billing
mdl templates add base-template
Enter fullscreen mode Exit fullscreen mode

The template is what makes this safe: when a module is disabled, its blocks are actually removed from the source. When you enable it again, the content is written back from the template. The disabled module's code is never in the delivered project, but it's never lost either.

mdl modules disable billing
mdl modules sync         # clears blocks of disabled modules, fills active ones from the template
Enter fullscreen mode Exit fullscreen mode

the database follows the same rules

Removing the code but leaving the tables is half a job. modulix can filter the template's SQL schema (for example schema.sql) by the active modules and apply it. MySQL and PostgreSQL are supported:

mdl config set db                 # interactive wizard for connection details
mdl db sync base-template         # applies the filtered schema
Enter fullscreen mode Exit fullscreen mode

This command drops the current schema and imports the filtered structure, so take a backup before running it anywhere near real data. There are commands for that:

mdl db backup create v1-db-backup
mdl db backup swap v1-db-backup   # roll back if needed
Enter fullscreen mode Exit fullscreen mode

a way back: codebase backups

Since sync rewrites files, modulix can also snapshot the filesystem:

mdl backup create initial-state
mdl backup status initial-state   # created, modified and deleted files
mdl backup swap initial-state     # restore the workspace to that point
Enter fullscreen mode Exit fullscreen mode

status shows exactly what a sync changed; taking a backup before a sync and checking it afterwards is a good habit.

where it fits, and where it doesn't

modulix works well when:

  • you ship the same product in different packages (basic, pro, enterprise),
  • a customer must not receive the code of a module they didn't buy,
  • you want the client and server folders of a monorepo kept in sync with one module list (mdl config set folder).

It is not the right tool for features that toggle at runtime (feature flags, A/B tests). modulix decides at build time, before delivery. For something that changes per user, you still want a flag system.

try it

npm install -g modulix
mdl init
Enter fullscreen mode Exit fullscreen mode

If you interest you can check my website.

The source and a full example walkthrough are on GitHub. Issues for bugs, ideas or support for another language are welcome.

Top comments (0)