UNPKG

2.96 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 if (typeof b !== "function" && b !== null)
11 throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12 extendStatics(d, b);
13 function __() { this.constructor = d; }
14 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.create = exports.Store = void 0;
19var events_1 = require("events");
20var path = require("path");
21var vinylFile = require("vinyl-file");
22var File = require("vinyl");
23var stream_1 = require("stream");
24function createFile(filepath) {
25 return new File({
26 cwd: process.cwd(),
27 base: process.cwd(),
28 path: filepath,
29 contents: null
30 });
31}
32var Store = /** @class */ (function (_super) {
33 __extends(Store, _super);
34 function Store() {
35 var _this = _super !== null && _super.apply(this, arguments) || this;
36 _this.store = {};
37 return _this;
38 }
39 Store.prototype.load = function (filepath) {
40 var file;
41 try {
42 file = vinylFile.readSync(filepath);
43 }
44 catch (err) {
45 file = createFile(filepath);
46 }
47 this.store[filepath] = file;
48 return file;
49 };
50 Store.prototype.get = function (filepath) {
51 filepath = path.resolve(filepath);
52 return this.store[filepath] || this.load(filepath);
53 };
54 Store.prototype.existsInMemory = function (filepath) {
55 filepath = path.resolve(filepath);
56 return !!this.store[filepath];
57 };
58 Store.prototype.add = function (file) {
59 this.store[file.path] = file;
60 this.emit('change', file.path);
61 return this;
62 };
63 Store.prototype.each = function (onEach) {
64 var _this = this;
65 Object.keys(this.store).forEach(function (key, index) {
66 onEach(_this.store[key], index);
67 });
68 return this;
69 };
70 Store.prototype.all = function () {
71 return Object.values(this.store);
72 };
73 Store.prototype.stream = function () {
74 var _this = this;
75 var stream = new stream_1.PassThrough({ objectMode: true, autoDestroy: true });
76 setImmediate(function () {
77 _this.each(function (file) { return stream.write(file); });
78 stream.end();
79 });
80 return stream;
81 };
82 return Store;
83}(events_1.EventEmitter));
84exports.Store = Store;
85function create() {
86 return new Store();
87}
88exports.create = create;
89;