By just looking at the title, I feel like there are a lot of things going on under the hood. And it’s true, these are the things that we should know because it will make our life much easier. I mean, by a lot!

Create and Publish an NPM package — Conventional Method

This is an NPM package that I wrote to check the authenticity of URLs and images in a page.

Normally, when I finished developing and ready for a release. I will commit it to GitHub, then publish to NPMJS — risking the fact that the package may not work or missing some elements.

Now with the help of Test Driven Development methods, Continuous Integration, and Continuous Deployment. I won’t have to worry that my code will not work anymore. All that I need to do is to make a commit to Github. After that, everything is being taken care of.

The status of my package shows that everything is working well

Test Driven Development

I guess that you may have heard about this software development process. In short, this type of development relies on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only. If you want to read more about Test Driven Development, there is a good read on Wikipedia.

I didn’t start my package with this method, but these are the steps that I should begin with. I’m not gonna to the very detail of how to start and publish a whole new project. If you want to see the complete code, please visit urls-checker repository.

Start a new project

npm init -y

2. Add requirement packages

npm install mocha chai --save-dev

3. Write some test cases and run test

And yes, you have to write the test case as if your method already exists and is fully working.

// test/urls.js 'use strict'; const { urlsChecker } = require('../index');

const expect = require('chai').expect;

it('Get URLs result', async () => {

let results = await urlsChecker('

expect(results.ok.length).to.be.above(0);

});

}) describe('Urls Checker Test:', () => {it('Get URLs result', async () => {let results = await urlsChecker(' https://google.ca' );expect(results.ok.length).to.be.above(0);});})

Of course, It will fail if you run it, and it’s the point.

mocha --timeout 10000 --exit

4. Write the methods to pass the test case

It’s too long and complicated, please refer it to the repository :P

5. Add CI/CD configuration (Travis)

Now, it is the main part. Normally, after finishing the project we will publish it to NPMJS. It’s super easy to push your package.



npm publish npm login // Create an account if you don't havenpm publish

However, with the help of CI/CD, every time you commit to GitHub, your code will be checked and if it passes. Your package will be published automatically. So says goodbye to npm publish ;)

Continuous Integration with Travis

There are a lot of tools that can help you with CI/CD stuff such as Jenkins, Travis CI, Bamboo, Gitlab CI… In this post, I will try Travis. What we need to do is a Github account and a Travis account. Actually, you will log in to Travis through your Github account. So you will have all of your public projects listed in Travis dashboard.

You can choose to enable or disable the integration with any projects

In order for your project to take advantage of the Continuous Integration from Travis, you have to add a configuration file for it. This is a NodeJS project, the version of Node is 8.

// .travis.yml

language: node_js

node_js:

- '8'

After that, you can add a build status badge to your readme.md file.

// Replace the username and project name with yours [![Build Status](https://travis-ci.org/dalenguyen/urls-checker.svg?branch=master)](https://travis-ci.org/dalenguyen/urls-checker)

From now, every time you push to Github. Travis will run and test your project, so make sure that you have your test cases ready and add the test script to package.json

// package.json "scripts": {

"test": "mocha --timeout 10000 --exit"

},

Continuous Deployment to NPMJS

The final step is to make sure that your package will be published to NPMJS if everything is passed. In order to do that we need to edit travis’ configuration.

You can do it by running from travis cli:

travis setup npm

Or manually add it to .travis.yml file

// .travis.yml ...

deploy:

provider: npm

email: "YOUR_EMAIL_ADDRESS"

api_key: "YOUR_AUTH_TOKEN"

If you don’t have travis cli on your machine, please check the Travis instruction page. This is how you install on MacOS.

brew install travis

In order to get the api_key, you need to create new token from your account:

https://www.npmjs.com/settings/your-username/tokens/

However, please don’t put your token on a public site. Follow this step to secure it.

travis encrypt your-npm-token --add deploy.api_key

After that, my .travis.yml is being updated



language: node_js

node_js:

- '8'

deploy:

provider: npm

email:

api_key:

secure: BSjy5UrLZOgrTnaA...

on:

branch: master

tags: true // .travis.ymllanguage: node_jsnode_js:- '8'deploy:provider: npmemail: dungnq@itbox4vn.com api_key:secure: BSjy5UrLZOgrTnaA...on:branch: mastertags: true

You can remove tags: true, because it will only deploy to NPMJS only when you commit with a tag.

git push --tags

Start to deploy

From now, my package is safe and sound and always up to date on Github and NPMJS. Hope you will find this article helpful ;)