Skip to content

krakenjs/grabthar

Repository files navigation

build status code coverage npm version apache license

Do it live

Because npm installing in production every 30 seconds is a great idea, right? ...right?

Quick Start

npm install --save @krakenjs/grabthar

Examples

Hot deploy and serve up static files:

import { poll } from "@krakenjs/grabthar";

let watcher = poll({
  name: "my-live-updating-module",
});

app.get("/foo.js", async function handleRequest(req, res) {
  const { modulePath } = await watcher.get();
  res.sendFile(`${modulePath}/dist/foo.js`);
});

Or if you're feeling really brave, hot deploy and require new code:

import { poll } from "@krakenjs/grabthar";

let watcher = poll({
  name: "my-live-updating-module",
});

app.get("/api/foo", async function handleRequest(req, res) {
  const { getFoo } = await watcher.import();
  res.json(getFoo());
});

Deploying

By default grabthar will use the current latest tag of your chosen module, from npm.

So, to deploy and activate new code, just:

npm version patch;
npm publish;

This will automatically set the latest tag to the latest version.

Deploying and activating in different steps

To separate out the deployment and activation of new code, you can make use of different npm dist-tags:

import { poll } from "@krakenjs/grabthar";

let watcher = poll({
  name: "my-live-updating-module",
  tags: ["latest", "release"],
});

app.get("/foo.js", async function handleRequest(req, res) {
  const { modulePath } = await watcher.get("release");
  res.sendFile(`${modulePath}/dist/foo.js`);
});

To deploy:

npm version patch;
npm publish;

To activate:

npm dist-tag add [email protected] release

grabthar will monitor and install anything passed in tags, but the activated version will only change when you set a new release dist tag.

Rolling back to old versions

Just change the dist-tag to whatever version you want to roll back to:

npm dist-tag add [email protected] latest

or:

npm dist-tag add [email protected] release

What else will watcher.get() return?

const {
  // The root directory where the module is installed, e.g.
  // /Users/zippy/__live_modules__/my-live-updating-module_1.3.53
  moduleRoot,

  // The full path to the node_modules installed, e.g.
  // /Users/zippy/__live_modules__/my-live-updating-module_1.3.53/node_modules/
  nodeModulesPath,

  // The full path to your module, e.g.
  // /Users/zippy/__live_modules__/my-live-updating-module_1.3.53/node_modules/my-live-updating-module
  modulePath,

  // The semver version of your module that is currently installed and activated, e.g.
  // 1.3.53
  version,

  // A map of the dependencies of your module, e.g.
  // { foo: '1.2.3', bar: '0.45.2' }
  dependencies,
} = await watcher.get();

How can I cancel a watcher?

watcher.cancel();

Tests

  • Run the tests:

    npm test

About

Hot install and activation of npm modules

Resources

License

Stars

Watchers

Forks

Packages

No packages published