• How I automated my job with Node.js
  • Shaun Michael Stone
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: geniusq1981
  • Proofread by: Starriers

You know there are a lot of tedious tasks you have to do at work: update configuration files, copy and paste files, update Jira tasks.

Over time, these tasks add up. This was the case when I worked for an online gaming company in 2016. Building configurable templates for games is a very rewarding part of game development, but about 70% of my time is spent copying these game templates and deploying these repackaged implementations.

What is a Reskin?

Reskin defines it as using the same game mechanics, screen and element positioning, but changing purely visual aesthetics such as color and material resources. So, in a simple game like “Rock Paper Scissors,” we created a template with the following basic material resources.

After we create one of these reskin, we can change the different material resources. If you look at games like Candy Crush or Angry Birds, there are many different versions of the same game. There are usually separate releases for Halloween, Christmas or Easter. From a business perspective, this makes perfect sense. Now… Let’s go back to our implementation. Each game shares the same JavaScript file, but loads JSON files with different content and resource paths. The result is?

I and other developers have a lot of schedules to work on every day, and my first thought was “a lot of things can be automated.” Every time I build a new game, I have to do the following:

  1. Git pull template repositories to ensure they are up to date;
  2. Create a new branch from the main branch – identified by the Jira task ID;
  3. Make copies of the templates I need to build;
  4. Run gulp;
  5. Update the contents of the config.json file. This involves resource paths, headings, paragraphs, and data service requests;
  6. Compile locally and check if it matches the content required by the task requirements document;
  7. Check with the designer if they are happy with the result;
  8. Merge to main branch and continue to next branch;
  9. Update the status of Jira tasks and make comments;
  10. Organize and repeat the process.

To me, it feels more like an administrative job than an actual development job. I had been exposed to Bash scripts in a previous role and created scripts based on them to reduce the amount of work I had to do. One script updates the template and creates a new branch, while the other performs a COMMIT and merges the project into development and production.

It takes three to ten minutes to create a project manually. Deployment may take five to ten minutes. These vary depending on the complexity of the game, and can take anywhere from ten minutes to half a day. Scripts can help, but it still takes a lot of time to update content or track down missing information.

It’s not enough to write code to shorten the time. I need to think about better ways to handle our workflow so THAT I can make better use of these scripts. Move the content out of the document, decompose it into relevant custom fields, and move it into the Jira task. Instead of sending a link address for a resource on a common server, it is more practical to set up a content Delivery Network (CDN) repository that contains the URL for the development and production of the resource.

Jira API

Things like this can take a while to see results, but our processes do improve over time. I did some research on the API of Jira, our project management tool, and made some requests for the Jira task I was working on. I collected a lot of valuable data. This data was so valuable that I decided to integrate it into my Bash script so that I could read it from the Jira task and leave a message to the person in charge when the task was complete.

Go from Bash to Node

The Bash script is fine, but it won’t work if someone is working on Windows. After doing some research, I decided to use JavaScript to wrap the entire process into a custom build tool. I call it Mason. It changes everything.

CLI

When you use Git in a terminal, you’ll notice that it has a very friendly command line interface. If you make a typo or type a wrong command, it politely suggests what to type. A library called COMMANDER, one of many I’ve used, uses the same behavior.

Consider the following simplified code example. It is booting the command line interface (CLI) application.

src/mason.js

#! /usr/bin/env node

const mason = require('commander');
const { version } = require('./package.json');
const console = require('console');

// commands
const create = require('./commands/create');
const setup = require('./commands/setup');

mason
    .version(version);

mason
    .command('setup [env]')
    .description('run setup commands for all envs')
    .action(setup);

mason
    .command('create <ticketId>')
    .description('creates a new game')
    .action(create);

mason
    .command(The '*')
    .action(() => {
        mason.help();
    });

mason.parse(process.argv);

if(! mason.args.length) { mason.help(); }Copy the code

Using NPM, you can run a link in package.json that creates a global alias.

."bin": {
  "mason": "src/mason.js"},...Copy the code

When I run NPM link in the root directory of the project.

npm link
Copy the code

It will provide me with a Mason command that I can call. So whenever I call Mason from the terminal, it runs the Mason.js script. All the tasks are done in this Mason command, which I use every day to build the game. The time I saved was unbelievable.

As you can see below — in the example I had in mind at the time — I passed a Jira task number as a parameter to the command. This will access the Jira API and get all the information I need to update the game. It then proceeds to compile and deploy the project. Then I’ll post a comment, @the person in charge and the designer, to let them know it’s done.

$ mason create GS-234 ... calling Jira API ... OK! got values! . creating a new branch from master called'GS-234'. updating templates repository ... copying from template'pick-from-three'. injecting values into config JSON ... building project ... deploying game ... Perfect! Here is the live link http://www.fake-studio.com/game/fire-water-earth ... Posted comment'Hey [~ben.smith], this has been released. Does the design look okay? [~jamie.lane]' on Jira.
Copy the code

All in just a few keys!

I was so pleased with the whole project that I decided to rewrite a better version in a book I just published called Automating with Node.js.

  • Color printing: amzn.eu/aA0cSnu
  • The Kindle: amzn.eu/dVSykv1
  • Kobo: www.kobo.com/gb/en/ebook…
  • Leanpub: Leanpub.com/automatingw…
  • Google Play: Play.google.com/store/books…

The book is divided into two parts:

Part 1

The first part is a collection of methods, or an instruction building block as a single global command. They can be used in your everyday work, or they can be called at any time purely for convenience to speed up your workflow.

Part 2

The second part is a walkthrough of creating cross-platform compilation tools from scratch. Each script implements a specific task, all wrapped up by the main command, usually the name of the project.

The project in the book is called Nobot (no-bot), and it’s based on a little cartoon robot. I hope you enjoy it and learn something from it.

I know that every business situation and process is different, but you should find something, even if it’s a small one, that will make your day in the office a lot easier.

Spend more time developing and less time managing.

Thanks for reading! If you liked it, please give us a like below. 👏

In every aspect of software/hardware video, please check out my YouTube channel: www.youtube.com/channel/UCK…

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.