paralayer 1.7.0 → 1.9.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.
package/dist/bin.js CHANGED
@@ -4,7 +4,7 @@
4
4
  import minimist from "minimist";
5
5
 
6
6
  // src/paralayer.ts
7
- import { Queue, safe, Unit } from "@eposlabs/utils";
7
+ import { Queue, Unit, safe } from "@eposlabs/utils";
8
8
  import { watch } from "chokidar";
9
9
  import { mkdir, readFile, rm, writeFile } from "fs/promises";
10
10
  import { basename, extname, join, relative } from "path";
@@ -112,8 +112,8 @@ var Paralayer = class extends Unit {
112
112
  return content.split("export class ").slice(1).map((part) => part.split(" ")[0].split("<")[0]);
113
113
  }
114
114
  generateLayerContent(layer, layerPaths) {
115
- const $LayerName = this.getLayerName(layer, "$Pascal");
116
- const $layerName = this.getLayerName(layer, "$camel");
115
+ const layerName = this.getLayerName(layer, "camel");
116
+ const LayerName = this.getLayerName(layer, "Pascal");
117
117
  const allNames = layerPaths.flatMap((path) => this.files[path]?.names ?? []);
118
118
  const imports = layerPaths.map((path) => {
119
119
  const file = this.files[path];
@@ -124,16 +124,26 @@ var Paralayer = class extends Unit {
124
124
  const relativePath = relative(this.options.output, path);
125
125
  return `import { ${[...names, ...types].join(", ")} } from '${relativePath}'`;
126
126
  }).filter(Boolean);
127
- const assign = [`Object.assign(${$layerName}, {`, ...allNames.map((name) => ` ${name},`), `})`];
127
+ const assign = [`Object.assign(${layerName}, {`, ...allNames.map((name) => ` ${name},`), `})`];
128
+ let extendPascal = "";
129
+ let extendCamel = "";
130
+ if (this.options.globalLayerName && layer !== this.options.globalLayerName) {
131
+ extendPascal = `extends ${this.getLayerName(this.options.globalLayerName, "Pascal")} `;
132
+ extendCamel = `extends ${this.getLayerName(this.options.globalLayerName, "camel")} `;
133
+ }
128
134
  const globals = [
129
135
  `declare global {`,
130
- ` var ${$layerName}: ${$LayerName}`,
136
+ ` var ${layerName}: ${LayerName}`,
131
137
  ``,
132
- ` interface ${$LayerName} {`,
138
+ ` interface ${LayerName} ${extendPascal}{`,
133
139
  ...allNames.map((name) => ` ${name}: typeof ${name}`),
134
140
  ` }`,
135
141
  ``,
136
- ` namespace ${$layerName} {`,
142
+ ` interface ${layerName} ${extendCamel}{`,
143
+ ...allNames.map((name) => ` ${name}: ${name}`),
144
+ ` }`,
145
+ ``,
146
+ ` namespace ${layerName} {`,
137
147
  ...allNames.map((name) => ` export type ${name} = ${name}Type`),
138
148
  ` }`,
139
149
  `}`
@@ -145,6 +155,9 @@ var Paralayer = class extends Unit {
145
155
  if (layer1.length !== layer2.length) return layer2.length - layer1.length;
146
156
  return layer1.localeCompare(layer2);
147
157
  }).map((layer) => `import './layer.${layer}.ts'`);
158
+ if (this.options.globalLayerName && topLayer !== this.options.globalLayerName) {
159
+ imports.unshift(`import './layer.${this.options.globalLayerName}.ts'`);
160
+ }
148
161
  return [...imports].join("\n");
149
162
  }
150
163
  generateSetupContent(allLayers) {
@@ -153,9 +166,9 @@ var Paralayer = class extends Unit {
153
166
  return layer1.localeCompare(layer2);
154
167
  });
155
168
  const vars = layers.map((layer) => {
156
- const $layerName = this.getLayerName(layer, "$camel");
157
- if (this.options.globalize) return `globalThis.${$layerName} = {}`;
158
- return `const ${$layerName} = {}`;
169
+ const layerName = this.getLayerName(layer, "camel");
170
+ if (this.options.globalize) return `globalThis.${layerName} = {}`;
171
+ return `const ${layerName} = {}`;
159
172
  });
160
173
  return [...vars].join("\n");
161
174
  }
@@ -167,8 +180,8 @@ var Paralayer = class extends Unit {
167
180
  }
168
181
  getLayerName(layer, style) {
169
182
  const LayerName = layer.split(".").map(this.capitalize).join("");
170
- if (style === "$camel") return `$${this.decapitalize(LayerName)}`;
171
- if (style === "$Pascal") return `$${LayerName}`;
183
+ if (style === "camel") return this.decapitalize(LayerName);
184
+ if (style === "Pascal") return LayerName;
172
185
  throw this.never;
173
186
  }
174
187
  capitalize(string) {
@@ -11,6 +11,8 @@ type Options = {
11
11
  watch?: boolean;
12
12
  /** Whether the layer variables should be exposed globally. */
13
13
  globalize?: boolean;
14
+ /** If provided, other layers will extend this layer. */
15
+ globalLayerName?: string | null;
14
16
  /** If a file name does not have layer tags, default layer name will be used. */
15
17
  defaultLayerName?: string | null;
16
18
  };
package/dist/paralayer.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/paralayer.ts
2
- import { Queue, safe, Unit } from "@eposlabs/utils";
2
+ import { Queue, Unit, safe } from "@eposlabs/utils";
3
3
  import { watch } from "chokidar";
4
4
  import { mkdir, readFile, rm, writeFile } from "fs/promises";
5
5
  import { basename, extname, join, relative } from "path";
@@ -107,8 +107,8 @@ var Paralayer = class extends Unit {
107
107
  return content.split("export class ").slice(1).map((part) => part.split(" ")[0].split("<")[0]);
108
108
  }
109
109
  generateLayerContent(layer, layerPaths) {
110
- const $LayerName = this.getLayerName(layer, "$Pascal");
111
- const $layerName = this.getLayerName(layer, "$camel");
110
+ const layerName = this.getLayerName(layer, "camel");
111
+ const LayerName = this.getLayerName(layer, "Pascal");
112
112
  const allNames = layerPaths.flatMap((path) => this.files[path]?.names ?? []);
113
113
  const imports = layerPaths.map((path) => {
114
114
  const file = this.files[path];
@@ -119,16 +119,26 @@ var Paralayer = class extends Unit {
119
119
  const relativePath = relative(this.options.output, path);
120
120
  return `import { ${[...names, ...types].join(", ")} } from '${relativePath}'`;
121
121
  }).filter(Boolean);
122
- const assign = [`Object.assign(${$layerName}, {`, ...allNames.map((name) => ` ${name},`), `})`];
122
+ const assign = [`Object.assign(${layerName}, {`, ...allNames.map((name) => ` ${name},`), `})`];
123
+ let extendPascal = "";
124
+ let extendCamel = "";
125
+ if (this.options.globalLayerName && layer !== this.options.globalLayerName) {
126
+ extendPascal = `extends ${this.getLayerName(this.options.globalLayerName, "Pascal")} `;
127
+ extendCamel = `extends ${this.getLayerName(this.options.globalLayerName, "camel")} `;
128
+ }
123
129
  const globals = [
124
130
  `declare global {`,
125
- ` var ${$layerName}: ${$LayerName}`,
131
+ ` var ${layerName}: ${LayerName}`,
126
132
  ``,
127
- ` interface ${$LayerName} {`,
133
+ ` interface ${LayerName} ${extendPascal}{`,
128
134
  ...allNames.map((name) => ` ${name}: typeof ${name}`),
129
135
  ` }`,
130
136
  ``,
131
- ` namespace ${$layerName} {`,
137
+ ` interface ${layerName} ${extendCamel}{`,
138
+ ...allNames.map((name) => ` ${name}: ${name}`),
139
+ ` }`,
140
+ ``,
141
+ ` namespace ${layerName} {`,
132
142
  ...allNames.map((name) => ` export type ${name} = ${name}Type`),
133
143
  ` }`,
134
144
  `}`
@@ -140,6 +150,9 @@ var Paralayer = class extends Unit {
140
150
  if (layer1.length !== layer2.length) return layer2.length - layer1.length;
141
151
  return layer1.localeCompare(layer2);
142
152
  }).map((layer) => `import './layer.${layer}.ts'`);
153
+ if (this.options.globalLayerName && topLayer !== this.options.globalLayerName) {
154
+ imports.unshift(`import './layer.${this.options.globalLayerName}.ts'`);
155
+ }
143
156
  return [...imports].join("\n");
144
157
  }
145
158
  generateSetupContent(allLayers) {
@@ -148,9 +161,9 @@ var Paralayer = class extends Unit {
148
161
  return layer1.localeCompare(layer2);
149
162
  });
150
163
  const vars = layers.map((layer) => {
151
- const $layerName = this.getLayerName(layer, "$camel");
152
- if (this.options.globalize) return `globalThis.${$layerName} = {}`;
153
- return `const ${$layerName} = {}`;
164
+ const layerName = this.getLayerName(layer, "camel");
165
+ if (this.options.globalize) return `globalThis.${layerName} = {}`;
166
+ return `const ${layerName} = {}`;
154
167
  });
155
168
  return [...vars].join("\n");
156
169
  }
@@ -162,8 +175,8 @@ var Paralayer = class extends Unit {
162
175
  }
163
176
  getLayerName(layer, style) {
164
177
  const LayerName = layer.split(".").map(this.capitalize).join("");
165
- if (style === "$camel") return `$${this.decapitalize(LayerName)}`;
166
- if (style === "$Pascal") return `$${LayerName}`;
178
+ if (style === "camel") return this.decapitalize(LayerName);
179
+ if (style === "Pascal") return LayerName;
167
180
  throw this.never;
168
181
  }
169
182
  capitalize(string) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "paralayer",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "imkost",
package/src/paralayer.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Queue, safe, Unit } from '@eposlabs/utils'
1
+ import { Queue, Unit, safe } from '@eposlabs/utils'
2
2
  import type { FSWatcher } from 'chokidar'
3
3
  import { watch } from 'chokidar'
4
4
  import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
@@ -17,6 +17,8 @@ export type Options = {
17
17
  watch?: boolean
18
18
  /** Whether the layer variables should be exposed globally. */
19
19
  globalize?: boolean
20
+ /** If provided, other layers will extend this layer. */
21
+ globalLayerName?: string | null
20
22
  /** If a file name does not have layer tags, default layer name will be used. */
21
23
  defaultLayerName?: string | null
22
24
  }
@@ -172,8 +174,8 @@ export class Paralayer extends Unit {
172
174
  }
173
175
 
174
176
  private generateLayerContent(layer: string, layerPaths: string[]) {
175
- const $LayerName = this.getLayerName(layer, '$Pascal')
176
- const $layerName = this.getLayerName(layer, '$camel')
177
+ const layerName = this.getLayerName(layer, 'camel')
178
+ const LayerName = this.getLayerName(layer, 'Pascal')
177
179
  const allNames = layerPaths.flatMap(path => this.files[path]?.names ?? [])
178
180
 
179
181
  const imports = layerPaths
@@ -188,17 +190,28 @@ export class Paralayer extends Unit {
188
190
  })
189
191
  .filter(Boolean)
190
192
 
191
- const assign = [`Object.assign(${$layerName}, {`, ...allNames.map(name => ` ${name},`), `})`]
193
+ const assign = [`Object.assign(${layerName}, {`, ...allNames.map(name => ` ${name},`), `})`]
194
+
195
+ let extendPascal = ''
196
+ let extendCamel = ''
197
+ if (this.options.globalLayerName && layer !== this.options.globalLayerName) {
198
+ extendPascal = `extends ${this.getLayerName(this.options.globalLayerName, 'Pascal')} `
199
+ extendCamel = `extends ${this.getLayerName(this.options.globalLayerName, 'camel')} `
200
+ }
192
201
 
193
202
  const globals = [
194
203
  `declare global {`,
195
- ` var ${$layerName}: ${$LayerName}`,
204
+ ` var ${layerName}: ${LayerName}`,
196
205
  ``,
197
- ` interface ${$LayerName} {`,
206
+ ` interface ${LayerName} ${extendPascal}{`,
198
207
  ...allNames.map(name => ` ${name}: typeof ${name}`),
199
208
  ` }`,
200
209
  ``,
201
- ` namespace ${$layerName} {`,
210
+ ` interface ${layerName} ${extendCamel}{`,
211
+ ...allNames.map(name => ` ${name}: ${name}`),
212
+ ` }`,
213
+ ``,
214
+ ` namespace ${layerName} {`,
202
215
  ...allNames.map(name => ` export type ${name} = ${name}Type`),
203
216
  ` }`,
204
217
  `}`,
@@ -216,6 +229,10 @@ export class Paralayer extends Unit {
216
229
  })
217
230
  .map(layer => `import './layer.${layer}.ts'`)
218
231
 
232
+ if (this.options.globalLayerName && topLayer !== this.options.globalLayerName) {
233
+ imports.unshift(`import './layer.${this.options.globalLayerName}.ts'`)
234
+ }
235
+
219
236
  return [...imports].join('\n')
220
237
  }
221
238
 
@@ -226,9 +243,9 @@ export class Paralayer extends Unit {
226
243
  })
227
244
 
228
245
  const vars = layers.map(layer => {
229
- const $layerName = this.getLayerName(layer, '$camel')
230
- if (this.options.globalize) return `globalThis.${$layerName} = {}`
231
- return `const ${$layerName} = {}`
246
+ const layerName = this.getLayerName(layer, 'camel')
247
+ if (this.options.globalize) return `globalThis.${layerName} = {}`
248
+ return `const ${layerName} = {}`
232
249
  })
233
250
 
234
251
  return [...vars].join('\n')
@@ -241,10 +258,10 @@ export class Paralayer extends Unit {
241
258
  return this.options.defaultLayerName ?? ''
242
259
  }
243
260
 
244
- private getLayerName(layer: string, style: '$camel' | '$Pascal') {
261
+ private getLayerName(layer: string, style: 'camel' | 'Pascal') {
245
262
  const LayerName = layer.split('.').map(this.capitalize).join('')
246
- if (style === '$camel') return `$${this.decapitalize(LayerName)}`
247
- if (style === '$Pascal') return `$${LayerName}`
263
+ if (style === 'camel') return this.decapitalize(LayerName)
264
+ if (style === 'Pascal') return LayerName
248
265
  throw this.never
249
266
  }
250
267