1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.Item = void 0;
|
7 |
|
8 | var fs = _interopRequireWildcard(require("fs-extra"));
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _sarcastic = _interopRequireDefault(require("sarcastic"));
|
13 |
|
14 | var _constants = require("./constants");
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
19 |
|
20 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
21 |
|
22 | let itemsByPath = {};
|
23 |
|
24 | class Item {
|
25 | constructor(filePath, contents) {
|
26 | _defineProperty(this, "_contents", void 0);
|
27 |
|
28 | _defineProperty(this, "_stringifiedSavedJson", void 0);
|
29 |
|
30 | _defineProperty(this, "path", void 0);
|
31 |
|
32 | _defineProperty(this, "directory", void 0);
|
33 |
|
34 | _defineProperty(this, "json", void 0);
|
35 |
|
36 | _defineProperty(this, "_config", void 0);
|
37 |
|
38 | this.json = (0, _sarcastic.default)(JSON.parse(contents), _sarcastic.default.object);
|
39 | this._stringifiedSavedJson = JSON.stringify(this.json, null, 2);
|
40 | this._contents = contents;
|
41 | this.path = filePath;
|
42 | this.directory = _path.default.dirname(filePath);
|
43 | this._config = this.json[_constants.PKG_JSON_CONFIG_FIELD] || {};
|
44 |
|
45 | if (itemsByPath[this.path] === undefined) {
|
46 | itemsByPath[this.path] = new Set();
|
47 | }
|
48 |
|
49 | itemsByPath[this.path].add(this);
|
50 | }
|
51 |
|
52 | updater(json) {
|
53 | this.json = json;
|
54 | }
|
55 |
|
56 | async refresh() {
|
57 | let contents = await fs.readFile(this.path, "utf-8");
|
58 | let json = (0, _sarcastic.default)(JSON.parse(contents), _sarcastic.default.object);
|
59 |
|
60 | for (let item of itemsByPath[this.path]) {
|
61 | item.updater(json);
|
62 | }
|
63 | }
|
64 |
|
65 | async save() {
|
66 | if (Object.keys(this._config).length) {
|
67 | this.json[_constants.PKG_JSON_CONFIG_FIELD] = this._config;
|
68 | } else {
|
69 | delete this.json[_constants.PKG_JSON_CONFIG_FIELD];
|
70 | }
|
71 |
|
72 | let stringified = JSON.stringify(this.json, null, 2);
|
73 |
|
74 | if (stringified !== this._stringifiedSavedJson) {
|
75 | await fs.writeFile(this.path, JSON.stringify(this.json, null, 2) + "\n");
|
76 | this._config = this.json[_constants.PKG_JSON_CONFIG_FIELD] || {};
|
77 |
|
78 | for (let item of itemsByPath[this.path]) {
|
79 | item.updater(this.json);
|
80 | }
|
81 |
|
82 | this._stringifiedSavedJson = stringified;
|
83 | return true;
|
84 | }
|
85 |
|
86 | return false;
|
87 | }
|
88 |
|
89 | }
|
90 |
|
91 | exports.Item = Item; |
\ | No newline at end of file |