@everymundo/runner

1.1.2 • Public • Published

@everymundo/runner

Runs a given script only if it is the main one called with node.

This module/package aims to help organizing main node scripts and to help with testing them as well.

Installation

npm install @everymundo/runner

Example

Let's say you have a node.js file with a small simple http server

// server.js
const http = require('http');
const logr = require('@everymundo/simple-logr');
const APP_PORT = Math.abs(process.env.APP_PORT) || 8008;

const requestHandler = (req, res) => {
  logr.debug(req.url);
  res.end('Hello Node.js Server!');
};

const server = http.createServer(requestHandler);

server.listen(APP_PORT, (err) => {
  if (err) {
    logr.error('something bad happened', err);
    throw err;
  }

  logr.info(`server runnin on port ${APP_PORT}`);
});

It is a regular working code, and you can put it to work by just running:

node server.js

But it is not really easy to test it. As soon as this file is loaded by your test 'test/server.test';

require('../server.test');

That will automatically put the server to run, and that is not an ideal effect when you only want to test it.

Here comes the runner

By slightly changing the code ...

// server.js
const { run } = require('@everymundo/runner');
const http = require('http');
const logr = require('@everymundo/simple-logr');
const APP_PORT = Math.abs(process.env.APP_PORT) || 8008;

const requestHandler = (req, res) => {
  logr.debug(req.url);
  res.end('Hello Node.js Server!');
};

const server = http.createServer(requestHandler);
const initialize = () => server.listen(APP_PORT, (err) => {
  if (err) {
    logr.error('something bad happened', err);
    throw err;
  }

  logr.info(`server runnin on port ${APP_PORT}`);
});

run(__filename, initialize);

... we can guarantee that it will only run its functionality when used by running node server.js but not when loading the file via require('server.js');

Readme

Keywords

Package Sidebar

Install

npm i @everymundo/runner

Weekly Downloads

1

Version

1.1.2

License

MIT

Unpacked Size

6.56 kB

Total Files

5

Last publish

Collaborators

  • a11y-dev
  • jsalvadorpp
  • lindolo25
  • double2-us
  • ceciliagalarza
  • carla-villegas
  • eblez
  • mjremedios1985
  • mtmorell88
  • a11y_automation2
  • ramses83
  • raydel
  • alex-orga
  • estebanpablo89
  • yartiles
  • jonmorazav
  • everymundo-admin
  • ballester
  • danielsan
  • halain
  • avivero93
  • anahiem
  • herlin
  • karinfdez
  • erlin
  • front10devs
  • hdelcastillo
  • dcuevacem
  • rodneyem
  • ortega.dc98
  • pedroleon917
  • kaylingw
  • lazjramos
  • daniuska-em