# @everymundo/runner

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

Latest version **1.1.2** (published 2019-05-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @everymundo/runner
pnpm add @everymundo/runner
yarn add @everymundo/runner
bun add @everymundo/runner
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2019-05-28 |
| First published | 2018-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Santana |
| Maintainers | alexandprivate, anahiem, avivero93, ballester, caviola, christopherepm, danielsan, davidxl, devrchancay, eduardomundo, emdavidmoreno, erlin, ernestocarrasco, everymundo-admin, front10devs, gallegogt, hdelcastillo, herlin, hugopalomares, jonmorazav, joseivansandoya, karinfdez, mvcruzata, neilgoldman, raymer, yartiles |
| Keywords | node, runner, run, test |

## Links

- npm: https://www.npmjs.com/package/@everymundo/runner
- Repository: https://github.com/EveryMundo/runner
- Homepage: https://github.com/EveryMundo/runner#readme
- Issues: https://github.com/EveryMundo/runner/issues
- npm.io page: https://npm.io/package/@everymundo/runner

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2019-05-28
- 1.1.1 — 2019-05-28
- 1.1.0 — 2018-02-01
- 1.0.0 — 2018-01-28

## README

# @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

```sh
npm install @everymundo/runner
```

## Example
Let's say you have a node.js file with a small simple http server
```js
// 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:
```sh
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';

```js
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 ...
```js
// 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')`;

---
_Source: https://npm.io/package/@everymundo/runner · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
