Command-Line app with Node.js

JavaScript has grown massively recently with the introduction of Node.js, and it serves as a language for Front-End, Server-side api, desktop app, and even for mobile app. It’s a kind of universal language every platform understands.

So, it’s no wonder that you build command-line tools with node.js. Recently, at Just-Eat, I had to upload a custom ZenDesk app via ZenDesk api and wanted to build a script that would do the job. So my choice was node.js with command.js.

Packages you need

With command.js, you can build command-line arguments in a very clear way.

const program = require('commander');
const request = require('request');
const fs = require('fs');

console.log('ex) node zen i <subdomain> <token>\n');

  .description('ZenDial Installer');

  .command('install <subdomain> <token>')
  .description('to install ZenDial')
  .action((subdomain, token) => {
    install({subdomain, token});

One tricky bit was to add basic authentication with formData. I’ve spend 20-30 minutes to figure it out. It turned out that I had to specify the endpoint url in {} object.

const install = (params) => {

  const user = '';
  const password = params.token;
  const fileStream = fs.createReadStream('');

  console.log('uploading the file...'){
    url: 'https://' + params.subdomain + '',
    formData: { uploaded_data: fileStream },
    auth: { 'username': user, 'password': password }
  }, function(err, res, body) {
    if (err) {
      console.error('upload failed:', err);

    console.log('file uploaded')

    const uploadId = JSON.parse(body).id;
    console.log('upload id: ' + uploadId){
      url: 'https://' + params.subdomain + '',
      form: {
        name: 'Zendial v2',
        short_description: 'Zendesk-to-liveops integration app',
        upload_id: uploadId
      json: true,
      auth: { 'username': user, 'password': password }
    }, function (err, res, body) {
        if (err) console.log(err);



Command-Line app with Node.js

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s