UNPKG

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