index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var Module = require("module");
  3. var fs = require("fs");
  4. var FILENAME = ".roadrunner.json";
  5. var data = {};
  6. var wrap = function (fn) {
  7. return function (filename) {
  8. filename = filename || FILENAME;
  9. return fn(filename);
  10. };
  11. };
  12. exports.save = wrap(function (filename) {
  13. exports.set('realpath', Module._realpathCache);
  14. exports.set('path', Module._pathCache);
  15. fs.writeFileSync(filename, JSON.stringify(data, null, " "));
  16. });
  17. exports.load = wrap(function (filename) {
  18. if (!fs.existsSync(filename)) return;
  19. try {
  20. data = JSON.parse(fs.readFileSync(filename));
  21. } catch (err) {
  22. return;
  23. }
  24. Module._pathCache = exports.get('path');
  25. Module._realpathCache = exports.get('realpath');
  26. });
  27. exports.setup = function () {
  28. process.on("exit", exports.save);
  29. var sigint = function () {
  30. process.removeListener("SIGINT", sigint);
  31. exports.save();
  32. process.kill(process.pid, "SIGINT");
  33. };
  34. process.on("SIGINT", sigint);
  35. };
  36. exports.get = function (key) {
  37. return data[key] = data[key] || {};
  38. };
  39. exports.set = function (key, val) {
  40. return data[key] = val;
  41. };