pmcf 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/model.mjs +62 -73
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/model.mjs CHANGED
@@ -4,6 +4,7 @@ import { join } from "node:path";
4
4
  export class Base {
5
5
  owner;
6
6
  name;
7
+ description;
7
8
 
8
9
  static get typeName() {
9
10
  return "base";
@@ -17,6 +18,10 @@ export class Base {
17
18
  return "**/" + this.typeFileName;
18
19
  }
19
20
 
21
+ static refinedType(data) {
22
+ return this;
23
+ }
24
+
20
25
  static baseName(name) {
21
26
  if (!name) {
22
27
  return undefined;
@@ -31,8 +36,14 @@ export class Base {
31
36
 
32
37
  constructor(owner, data) {
33
38
  this.owner = owner;
34
- if (data?.name) {
35
- this.name = data.name;
39
+
40
+ if (data) {
41
+ if (data.name) {
42
+ this.name = data.name;
43
+ }
44
+ if (data.description) {
45
+ this.description = data.description;
46
+ }
36
47
  }
37
48
  }
38
49
 
@@ -54,6 +65,15 @@ export class Base {
54
65
  return this.owner.network;
55
66
  }
56
67
 
68
+ #directory;
69
+ set directory(directory) {
70
+ this.#directory = directory;
71
+ }
72
+
73
+ get directory() {
74
+ return this.#directory || this.name;
75
+ }
76
+
57
77
  expand(object) {
58
78
  if (typeof object === "string") {
59
79
  return object.replaceAll(/\$\{([^\}]*)\}/g, (match, m1) => {
@@ -79,7 +99,9 @@ export class Base {
79
99
  toJSON() {
80
100
  return {
81
101
  name: this.name,
82
- owner: this.owner.name
102
+ directory: this.directory,
103
+ owner: this.owner.name,
104
+ description: this.description
83
105
  };
84
106
  }
85
107
  }
@@ -92,7 +114,6 @@ export class World {
92
114
  directory;
93
115
  #byName = new Map();
94
116
 
95
- /** @typedef {Map<string,Location>} */ #locations = new Map();
96
117
  /** @typedef {Map<string,Host>} */ #hosts = new Map();
97
118
 
98
119
  constructor(directory) {
@@ -104,7 +125,7 @@ export class World {
104
125
  }
105
126
 
106
127
  async load() {
107
- for (const type of Object.values(World.types)) {
128
+ for (let type of Object.values(World.types)) {
108
129
  for await (const name of glob(type.fileNameGlob, {
109
130
  cwd: this.directory
110
131
  })) {
@@ -114,8 +135,12 @@ export class World {
114
135
  await readFile(join(this.directory, name), "utf8")
115
136
  );
116
137
 
117
- data.directory = baseName;
118
138
  data.name = baseName;
139
+
140
+ const t2 = type.refinedType(data);
141
+ if (t2) {
142
+ type = t2;
143
+ }
119
144
  const object = new type(this, data);
120
145
  this.#byName.set(data.name, object);
121
146
  }
@@ -124,34 +149,27 @@ export class World {
124
149
  }
125
150
 
126
151
  async named(name) {
152
+ await this.load();
127
153
  return this.#byName.get(name);
128
154
  }
129
155
 
130
156
  async *locations() {
131
- if (this.#locations.size > 0) {
132
- for (const location of this.#locations.values()) {
133
- yield location;
134
- }
135
- }
157
+ await this.load();
136
158
 
137
- for await (const name of glob(Location.fileNameGlob, {
138
- cwd: this.directory
139
- })) {
140
- yield this.location(name);
159
+ for (const object of this.#byName.values()) {
160
+ if (object instanceof Location) {
161
+ yield object;
162
+ }
141
163
  }
142
164
  }
143
165
 
144
166
  async *hosts() {
145
- if (this.#hosts.size > 0) {
146
- for (const host of this.#hosts.values()) {
147
- yield host;
148
- }
149
- }
167
+ await this.load();
150
168
 
151
- for await (const name of glob(Host.fileNameGlob, {
152
- cwd: this.directory
153
- })) {
154
- yield this.host(name);
169
+ for (const object of this.#byName.values()) {
170
+ if (object instanceof Host) {
171
+ yield object;
172
+ }
155
173
  }
156
174
  }
157
175
 
@@ -162,49 +180,14 @@ export class World {
162
180
  }
163
181
 
164
182
  async location(name) {
165
- name = Location.baseName(name);
166
- if (name === undefined) {
167
- return undefined;
168
- }
169
-
170
- let location = this.#locations.get(name);
171
- if (location) {
172
- return location;
173
- }
174
-
175
- const directory = join(this.directory, name);
176
- try {
177
- const data = JSON.parse(
178
- await readFile(join(directory, Location.typeFileName), "utf8")
179
- );
180
-
181
- data.directory = directory;
182
- data.name = name;
183
-
184
- location = new Location(this, data);
185
- } catch {} // TODO
186
- this.#locations.set(name, location);
187
- return location;
183
+ await this.load();
184
+ return this.#byName.get(Location.baseName(name));
188
185
  }
189
186
 
190
187
  async host(name) {
191
- name = Host.baseName(name);
192
-
193
- if (name === undefined) {
194
- return undefined;
195
- }
196
-
197
- let host = this.#hosts.get(name);
198
- if (host) {
199
- return host;
200
- }
201
-
202
- const directory = join(this.directory, name);
203
- const data = JSON.parse(
204
- await readFile(join(directory, Host.typeFileName), "utf8")
205
- );
206
-
207
- data.directory = directory;
188
+ await this.load();
189
+ return this.#byName.get(Host.baseName(name));
190
+ /*
208
191
 
209
192
  if (!data.name) {
210
193
  data.name = name;
@@ -225,11 +208,7 @@ export class World {
225
208
  if (data.extends) {
226
209
  data.extends = await Promise.all(data.extends.map(e => this.host(e)));
227
210
  }
228
-
229
- host = new (data.name.indexOf("model/") >= 0 ? Model : Host)(this, data);
230
-
231
- this.#hosts.set(name, host);
232
- return host;
211
+ */
233
212
  }
234
213
 
235
214
  async *subnets() {
@@ -248,7 +227,6 @@ export class World {
248
227
  }
249
228
 
250
229
  export class Host extends Base {
251
- directory;
252
230
  networkInterfaces = {};
253
231
  services = {};
254
232
  postinstall = [];
@@ -266,6 +244,14 @@ export class Host extends Base {
266
244
  return "host";
267
245
  }
268
246
 
247
+ static refinedType(data) {
248
+ if (data.name?.indexOf("model/") >= 0) {
249
+ return Model;
250
+ }
251
+
252
+ return this;
253
+ }
254
+
269
255
  constructor(owner, data) {
270
256
  super(owner, data);
271
257
 
@@ -413,7 +399,6 @@ export class Host extends Base {
413
399
  ...super.toJSON(),
414
400
  ...Object.fromEntries(
415
401
  [
416
- "directory",
417
402
  "location",
418
403
  "model",
419
404
  "os",
@@ -438,7 +423,6 @@ export class Host extends Base {
438
423
  export class Model extends Host {}
439
424
 
440
425
  export class Location extends Base {
441
- directory;
442
426
  domain;
443
427
  dns;
444
428
  #administratorEmail;
@@ -608,6 +592,8 @@ export class Location extends Base {
608
592
  export class Network extends Base {
609
593
  #hosts = new Map();
610
594
  kind;
595
+ scope;
596
+ metric;
611
597
  ipv4;
612
598
  ipv4_netmask;
613
599
  subnet;
@@ -650,7 +636,10 @@ export class Network extends Base {
650
636
  toJSON() {
651
637
  return {
652
638
  ...super.toJSON(),
653
- kind: this.kind
639
+ kind: this.kind,
640
+ ipv4: this.ipv4,
641
+ scope: this.scope,
642
+ metric: this.metric
654
643
  };
655
644
  }
656
645
  }