65

A universal task runner to run them all

 5 years ago
source link: https://www.tuicool.com/articles/hit/AF7jQ33
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Today we’ll talk about a miscomfort that has been echoing in my head since a while ago: Task runners fatigue .

We’ve come accross Grunt, Gradle, Gulp, NPM scripts, then rediscovered makefiles, only to reconsider shell scripts. I tend to switch projects many times a day, every one of them was made with a different trendy task runner and most of my time I will be attempting to run npm run watch , then try gulp watch just to realize that make info will tell me to use make dev .

Repeat this process all across the day and you are in front of a productivity killer.

In this article I’m going to explain how I save time and try to put neurons to work where they belong.

Gulp

After the rise of Grunt, Gulp was the first JS task runner that I took in seriously.

The good:

  • Totally customizable behavior
  • Wealthy ecosystem of plugins
  • Platform agnostic

The bad:

  • In addition to the global gulp command, you also need to install a local gulp in your project.
  • Using just gulp brings a new package.json file and the beloved node_modules folder with 253 dependencies and 1946 files. Even if your project doesn’t use Node.
  • Starting gulp is usually slow
  • Stuck in version 3.9.1 since a long time ago

NPM run scripts

But then, we saw that we could encapsulate simple commands on our package.json without the need of installing any task runner. Sweet, let’s move our commands there!

The good:

  • No additional dependencies
  • Platform agnostic

The bad:

  • Task definitions need to fit in one line of a JSON file, so only simple commands will do
  • Complex operations are inviable
  • It is still slow to start running

Makefiles

One day, a JSConf speaker with C/C++ background wondered “Why can’t we use the venerable dev tools from the Unix ecosystem to bundle our apps?” . After a couple of articles about make as a task runner, I got convinced and switched to the good old makefiles.

The good:

  • Native. Commands start so fast!
  • Will usually be installed in your dev box
  • The tool is battle tested

The bad:

  • It can do the job, but make was designed to help write compilation rules instead of task automation

Taskfiles

A couple of years ago I saw the ligh with an article that brought me back to the starting point. Which I totally recommend, by the way:

In short, “Why don’t we use shell scripts if they were designed to run complex tasks and provide what we need nowadays”?

The good:

  • Actions are totally customizable
  • They start instantly
  • Manage complex dependencies, launch parallel jobs, synchronize when done, etc.
  • Pass parameters to tasks

The bad:

  • Developers only use scripts for concrete tasks, never as task runners
  • Bash/sh restricts to Linux/Unix/Cygwin environments
  • In non-English keyboards it is inconvenient to type  ./taskfile every time vs. make or npm run

The solution

That’s the reason why I couldn’t help but developing a task runner to run them all.

What does it do?

> You execute run <taskname>

  • It looks for existing task config files and attempts to run the task on the most specific tool available (will not use package.json if it finds a gulpfile)
  • If the executable is not installed, it will let you get it installed
  • Then gulp <taskname>  , npm run <taskname>  , make <taskname> or sh taskfile <taskname> is launched
  • Any additional arguments are forwarded to the actual task runner

You execute run

  • Without any parameters, it lists all the available commands

You execute run --create <runner>

  • Generate a template config file for any of the above

Let’s get it installed:

[sudo] npm install -g runner-cli

Some examples

There is a gulpfile

You type run + Enter:

UVJzeiI.png!web

You don’t need to think/read/search too much. You don’t need to open gulpfile.js.

You want to compile, so you run run build . Period.

v6NbEvm.png!web

Easy, short and straight.

You have a gulpfile, but nothing else

7nuaErQ.png!web

Select to install Gulp

FVjaimm.png!web

Zero waste of brain. Zero wast of time. No package.json. No init. No defaults. No install. Run.

You have an empty folder

Let’s create a taskfile from a template:

Ar2iqm.png!web

What’s in taskfile ?

2Yv6jyA.png!web

Here is a task file, with three tasks (build, styles and media). build will launch styles and media , first sequentially and then in parallel. Also note that media can receive parameters, so we can encapsulate similar tasks into one function and act accordingly.

Here is what happens when we run build :

AbQnu2a.png!web

Now you can code tasks into a powerful script and get them to run in no time.

Wrap up

Productivity gain calculation

Based on my biased habits, I may run among 5 to 40 commands an hour. If on average, my thinking and typing are ~2 seconds quicker without the task runner fatigue, and without the gulp/npm latency:

  • 22.5 executions per hour
  • 8 hours a day, 22 days a month, 11 months a year

This is, 24.2 hours ! I have been burning ~3 working days of my life every year, which I could have spent by the beach with my girlfriend.

You may be asking: “Well, isn’t this a trirvial tool to write?”. And my answer is “Why didn’t someone bring such trivial tool before?”

If you think the tool would be great with an extra feature, feel free to fork it from https://github.com/ledfusion/runner-cli and submit a pull request.

As always, if you liked it, clap, comment, share and smile !

Happy task running!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK