How I recreated the Snap from Avengers: Infinity War with JavaScript

Vinicius Marchesin Araujo
4 min readMar 8, 2019

⚠️ WARNING ⚠️

This article contains spoilers about the movie Avengers: Infinity War. If you already watched it or don’t care about these things, you’re free to go ahead.

You should’ve gone for the head, Thor.

The upcoming movie Avengers: Endgame is one of the most anticipated movies of 2019. It’ll bring an end to the plot of Thanos and a conclusion to the war that’s being teased for almost a decade now.

The previous movie, Avengers: Infinity War, presented Thanos and his quest to obtain the Infinity Stones in order to achieve his ultimate goal: wipe out half of the sentient beings in the universe.

If you watched the movie then you know what I’m talking about. If you didn’t, this is all you need to know to understand this article. This isn’t an article about the movie, so I just need to explain the concept behind the code so it doesn’t look silly (it still does anyway). Thanos is evil (allegedly), he gathers incredible power and then deletes half the people (alien people as well) in the universe. Why? I’ll leave the reasoning for the movie, it’s good and you should watch.

The package

Before I explain the code, you can try it for yourself. Why would you? I don’t know, but you can.

You need nodejs and npm to run it. Install it as a global dependency and run it using the CLI.

Just a few things before we proceed:

  1. You understand this deletes your files permanently. Don’t use it on sensitive information.
  2. Don’t use this.
  3. Make sure you’re in the correct directory before running this, or else you’ll lose a bunch of stuff.
  4. Seriously, don’t use this.
  5. You should use Linux, or another Unix based system.
  6. SERIOUSLY, DON’T DO IT.

If you follow these rules you’ll be fine.

The code

I’m using ES6 features, but that’s not the goal of this article. If you don’t understand the syntax leave a comment and I’ll explain it to you.

The code is minimal, and doesn’t not use any dependencies. Well, I’ve used two dependencies to make it run as a CLI app, but the file deleting doesn’t use external dependencies. I won’t go deep into the CLI options, if you’re interested check the npm package page.

You can follow along using the project’s Github page to check the code as I explain.

I use the child_process module from Node to spawn child processes in the OS. For that I use the spawn method. It takes a command and a list of arguments then asynchronously spawns a process running the command.

I run find $path -type f , which returns all filenames (with the absolute path) under the $path directory (by default it’s the current directory that called the program). The result is a binary Buffer, which is read using the readline module. This module takes a readable stream of binary data and outputs each line over a listener as text. I grab each line and push it to an array.

Using the listener result.on('exit') I resolve the Promise, returning all the filenames inside the array.

Resumed version of the code. Find the original here.

The code above is the main part of the package, as it retrieves all the filenames. All that’s left is deleting!

The entry point of the program is below:

Resumed version of the code. Find the original here.

We get all the filenames from our spawn.js process and shuffle the result. This will ensure that we get a pseudo random order inside the array. We simply drop half of the files, and delete the other half from disk using the fs module.

After that our disk has now achieved true balance. There’s plenty of resources left for all the files that survived!

Perfectly balanced, as all things should be. — Thanos

The actual package has some extra features, such as:

  • Logging the files that were deleted;
  • An option to preserve git files, so you can push the balanced code to your repo;
  • An option to exclude files that match a RegEx from being deleted, and
  • A usage guide

Did you like it? Are you going to use it? Please don’t. I don’t know why I did this.

Leave a clap and follow me for other projects like this.

--

--

Vinicius Marchesin Araujo

Front-end developer and dolphin impersonator. Currently working as a JavaScript Developer at @Socialbakers in Prague. | https://vmarches.in