All Articles

Getting Started with NodeCG: Cleaning up the Environment

Last time I talked about getting Parcel and some other tools set-up so that we can get started developing widgets with NodeCG. Iā€™m not going to cover NodeCG widgets quite yet, but if you want to learn how you can keep your brand new development environment in a clean stateā€¦ then Iā€™ve got you covered today!

As I mentioned last time, you can see me apply these steps in order in this GitHub repository.

Alright, letā€™s get started! šŸ˜

Catching code and format smells with ESLint + Prettier

Our dev environment is setupā€”why go further? Well, sometimes we write code that is innocuous now but could be problematic in the future, or we might have other people working with us and itā€™s useful to have a consistent style or format.

  • ESLint can help us with part of this. We can use it to find and fix JavaScript (or in our case, TypeScript) problems
  • Prettier is an opinionated code formatter so that we donā€™t have to think about how we format our code

Letā€™s set those up!

ESLint

Installing ESLint is straightforward, but depends a lot on how you want to configure it. All you need to do is npm install --save-dev eslint then npx eslint --init to go through the wizard. I opted to pick the standard JS style but there are other styles available.

If you run eslint now (npx eslint "src/**"), it may warn you about some potential problems in the code weā€™ve written (oh no!). Some of these problems are self-imposed, but others might feel unfixable, for example:

React must be in scope.

h is defined but never used.

How do we handle these?! šŸ˜²

While our bundling process is aware of these variables, eslint is not. To fix this, weā€™re going to npm install --save-dev eslint-config-standard-preact1 and make a small changes to our .eslintrc file.

"extends": [
    "plugin:react/recommended",
    "standard",
    "standard-preact"          // No need to add prefix; eslint is smart like that
],

ā€¦And if you run eslint again, the errors should hopefully go away. If they donā€™t, itā€™s possible that we missed something. Hopefully thereā€™s enough information to resolve the issues if so šŸ¤ž

Prettier

That leaves Prettier. Honestly, if itā€™s just one person working on the project, adding this might be a bit of overkill, but itā€™s easy to add and leaves code in a consistent style so Iā€™d still recommend it (and it works for multiple different file types).

Installation is pretty easy, and once youā€™ve done that, you can run npx prettier --write . to format all support files in your project!

ā€¦But donā€™t do that just yet! Prettier and ESLint may not agree on formattingā€”running Prettier could create style-based linting errors šŸ˜±

So weā€™ll need to fix that! Fortunately, Prettier has some documentation on a few different ways you can resolve this depending on your setup. In our case, we can go the recommended route and npm install --save-dev eslint-config-prettier and add it to the end of our .eslintrc ā€œextendsā€ as before:

"extends": [
    "plugin:react/recommended",
    "standard",
    "standard-preact",
    "prettier"                 // Again, no prefix necessary
],

More details on the eslint-config-prettier plugin are here. It will disable several rules but thatā€™s worth it to make sure that the two programs donā€™t fight.

And voila, weā€™ve made our environment a little bit cleaner! āœØ

ā€¦But can we do more? šŸ¤”šŸ’­

Optional: Integrating with Atom

Itā€™s great that we have these tools, but running them manually still kind of sucks. Manual tools are tools that are not run very often.

There are a few different ways to fix this.

  • Git hooks: automatically run our tools before committing. Prettier even has some documentation on a few different ways of doing this including a tool called Husky which is intended to make git hooks easier.
  • Automatically run tools in your IDE: As long as you arenā€™t running notepad, you can probably set this up
  • Build toolkits (e.g. Gulp, Grunt, possibly even webpack or Parcel, CI): Have the tools run as part of the build process!

Heck, you can probably implement a bunch of these simultaneouslyā€”which also reduces the likelihood of it being missedā€”but weā€™re going to focus on setting it up in the IDE for now. šŸ”

I use Atom, so most of the following steps will be specific to it, but Iā€™m sure similar plugins or settings are available in most IDEs.

A quick rundown of the packages I installed, and the settings I had to change:

  • linter: This allows any sort of linting in the Atom UI. However, it needs specific linters installed to workā€¦
  • linter-eslint: Allows us to use eslint with linter! Atom will look for a version of eslint in the project, and if it canā€™t find one, will use a version that it is prepackeged with.
    • By default, it doesnā€™t know to lint .ts and .tsx files. To fix that, youā€™ll need to go into Atomā€™s settings > Packages > linter-eslint, and under itā€™s settings add source.ts, source.tsx to the list of scopes.
  • prettier-atom: Surprise! This will allow Atom to run Prettier.
    • Most settings you can leave as is, but Iā€™d strongly suggest setting it to ā€œFormat on saveā€ and also ā€œOnly format if a Prettier config is foundā€. This avoids the issue of formatting other files in Atom unexpectedly for projects not using Prettier.
  • atom-typescript: This lets Atom know about Typescript files so it can syntax highlight correctly and provide other useful typing hints

Thereā€™s also one last one which is super unnecessary for an individual project, but Iā€™m including for completeness because I think itā€™s a cool idea to avoid arguments in groups: EditorConfig. šŸ¤“

In a nutshell, EditorConfig is another way of maintaining consistent code styles, but instead of doing it after the fact, like Prettier, it does so while youā€™re coding. Itā€™s mostly for things like indentation amounts and type (tabs vs spaces), character sets, and line endings. Itā€™s a little less necessary since weā€™re using Prettier, but a useful tool to add to your toolkit (and is supported by many IDEs).

To get it to work in Atom, we just need to install editorconfig and add a basic file to our project at the root. I picked these settings fairly arbitrarily based on my preferences:

[*]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 2
max_line_length = 80

And thatā€™s it! We now have a really tidy setup for our NodeCG development! I hope it wonā€™t be too long before I can talk more about how to actually develop widgets, but I wanted to show you all how you can set things up cleanly forā€¦ well, most front end projects! šŸ„³


  1. eslint-config-standard-preact is quite old at this point (~4 years since a new version was published), so there may be better fixes for this problemā€¦ Iā€™m just not aware of better alternatives at the moment.ā†©