UNPKG

7.21 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13 return new (P || (P = Promise))(function (resolve, reject) {
14 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
17 step((generator = generator.apply(thisArg, _arguments || [])).next());
18 });
19};
20var __generator = (this && this.__generator) || function (thisArg, body) {
21 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
22 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
23 function verb(n) { return function (v) { return step([n, v]); }; }
24 function step(op) {
25 if (f) throw new TypeError("Generator is already executing.");
26 while (_) try {
27 if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
28 if (y = 0, t) op = [0, t.value];
29 switch (op[0]) {
30 case 0: case 1: t = op; break;
31 case 4: _.label++; return { value: op[1], done: false };
32 case 5: _.label++; y = op[1]; op = [0]; continue;
33 case 7: op = _.ops.pop(); _.trys.pop(); continue;
34 default:
35 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
36 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
37 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
38 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
39 if (t[2]) _.ops.pop();
40 _.trys.pop(); continue;
41 }
42 op = body.call(thisArg, _);
43 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
44 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
45 }
46};
47Object.defineProperty(exports, "__esModule", { value: true });
48var loader_remote_1 = require("@chix/loader-remote");
49var fs = require("fs");
50var util = require("util");
51var util_1 = require("./util");
52var readFile = util.promisify(fs.readFile);
53var syncer;
54function nop() { }
55/**
56 * Loads and saves definitions from/to the local cache file.
57 */
58var FSLoader = /** @class */ (function (_super) {
59 __extends(FSLoader, _super);
60 function FSLoader(opts) {
61 if (opts === void 0) { opts = {}; }
62 var _this = _super.call(this) || this;
63 _this.opts = {};
64 _this._cacheFile = './.x.cache';
65 _this.opts.cache = Boolean(opts.cache);
66 _this.opts.purge = Boolean(opts.purge);
67 // purge the cache file
68 if (_this.opts.purge) {
69 try {
70 fs.unlinkSync(_this._cacheFile);
71 _this.emit('purgeCache', {
72 file: _this._cacheFile
73 });
74 }
75 catch (error) {
76 nop();
77 }
78 }
79 if (_this.opts.cache && !_this.opts.purge) {
80 if (syncer) {
81 process.removeListener('exit', syncer);
82 }
83 else {
84 // re-used
85 process.on('SIGINT', function () {
86 process.exit();
87 });
88 }
89 syncer = _this.syncState.bind(_this);
90 process.on('exit', syncer);
91 }
92 return _this;
93 }
94 /**
95 *
96 * Loads all definitions from the database.
97 * Any missing pieces will be loaded by remote Loader.
98 *
99 * If you use `update` all definitions in the database will be refreshed.
100 *
101 * @param {Object} flow
102 * @param {Boolean} update
103 */
104 FSLoader.prototype.load = function (flow, update) {
105 if (update === void 0) { update = false; }
106 return __awaiter(this, void 0, void 0, function () {
107 var content, cache, error_1;
108 return __generator(this, function (_a) {
109 switch (_a.label) {
110 case 0:
111 if (!this.opts.cache) return [3 /*break*/, 4];
112 _a.label = 1;
113 case 1:
114 _a.trys.push([1, 3, , 4]);
115 return [4 /*yield*/, readFile(this._cacheFile, 'utf8')];
116 case 2:
117 content = _a.sent();
118 cache = JSON.parse(content);
119 this.setDependencies(cache.dependencies);
120 this.setNodeDefinitions(cache.nodeDefinitions);
121 this.emit('loadCache', {
122 file: this._cacheFile,
123 cache: cache
124 });
125 // Start the remote loader, It will not load any known definitions.
126 return [2 /*return*/, _super.prototype.load.call(this, flow, update)];
127 case 3:
128 error_1 = _a.sent();
129 if (error_1.code !== 'ENOENT') {
130 throw Error(error_1);
131 }
132 return [3 /*break*/, 4];
133 case 4: return [2 /*return*/, _super.prototype.load.call(this, flow, update)];
134 }
135 });
136 });
137 };
138 /**
139 *
140 * Loads a local file
141 *
142 * @param {NodeDefinitionLocationFS} location
143 */
144 FSLoader.prototype.loadFile = function (location) {
145 return __awaiter(this, void 0, void 0, function () {
146 return __generator(this, function (_a) {
147 this.emit('loadFile', { path: location.path });
148 return [2 /*return*/, util_1.loadFile(location)];
149 });
150 });
151 };
152 /**
153 * Saves the internal state to disk
154 */
155 FSLoader.prototype.syncState = function () {
156 // save internal state to disk
157 fs.writeFileSync(this._cacheFile, JSON.stringify({
158 dependencies: this.getDependencies(),
159 nodeDefinitions: this.getNodeDefinitions()
160 }, null, 2));
161 this.emit('writeCache', {
162 file: this._cacheFile
163 });
164 };
165 return FSLoader;
166}(loader_remote_1.RemoteLoader));
167exports.FSLoader = FSLoader;
168//# sourceMappingURL=fs.js.map
\No newline at end of file