paralayer 1.7.0 → 1.8.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
  `}`
@@ -153,9 +163,9 @@ var Paralayer = class extends Unit {
153
163
  return layer1.localeCompare(layer2);
154
164
  });
155
165
  const vars = layers.map((layer) => {
156
- const $layerName = this.getLayerName(layer, "$camel");
157
- if (this.options.globalize) return `globalThis.${$layerName} = {}`;
158
- return `const ${$layerName} = {}`;
166
+ const layerName = this.getLayerName(layer, "camel");
167
+ if (this.options.globalize) return `globalThis.${layerName} = {}`;
168
+ return `const ${layerName} = {}`;
159
169
  });
160
170
  return [...vars].join("\n");
161
171
  }
@@ -167,8 +177,8 @@ var Paralayer = class extends Unit {
167
177
  }
168
178
  getLayerName(layer, style) {
169
179
  const LayerName = layer.split(".").map(this.capitalize).join("");
170
- if (style === "$camel") return `$${this.decapitalize(LayerName)}`;
171
- if (style === "$Pascal") return `$${LayerName}`;
180
+ if (style === "camel") return this.decapitalize(LayerName);
181
+ if (style === "Pascal") return LayerName;
172
182
  throw this.never;
173
183
  }
174
184
  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
  `}`
@@ -148,9 +158,9 @@ var Paralayer = class extends Unit {
148
158
  return layer1.localeCompare(layer2);
149
159
  });
150
160
  const vars = layers.map((layer) => {
151
- const $layerName = this.getLayerName(layer, "$camel");
152
- if (this.options.globalize) return `globalThis.${$layerName} = {}`;
153
- return `const ${$layerName} = {}`;
161
+ const layerName = this.getLayerName(layer, "camel");
162
+ if (this.options.globalize) return `globalThis.${layerName} = {}`;
163
+ return `const ${layerName} = {}`;
154
164
  });
155
165
  return [...vars].join("\n");
156
166
  }
@@ -162,8 +172,8 @@ var Paralayer = class extends Unit {
162
172
  }
163
173
  getLayerName(layer, style) {
164
174
  const LayerName = layer.split(".").map(this.capitalize).join("");
165
- if (style === "$camel") return `$${this.decapitalize(LayerName)}`;
166
- if (style === "$Pascal") return `$${LayerName}`;
175
+ if (style === "camel") return this.decapitalize(LayerName);
176
+ if (style === "Pascal") return LayerName;
167
177
  throw this.never;
168
178
  }
169
179
  capitalize(string) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "paralayer",
3
- "version": "1.7.0",
3
+ "version": "1.8.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
  `}`,
@@ -226,9 +239,9 @@ export class Paralayer extends Unit {
226
239
  })
227
240
 
228
241
  const vars = layers.map(layer => {
229
- const $layerName = this.getLayerName(layer, '$camel')
230
- if (this.options.globalize) return `globalThis.${$layerName} = {}`
231
- return `const ${$layerName} = {}`
242
+ const layerName = this.getLayerName(layer, 'camel')
243
+ if (this.options.globalize) return `globalThis.${layerName} = {}`
244
+ return `const ${layerName} = {}`
232
245
  })
233
246
 
234
247
  return [...vars].join('\n')
@@ -241,10 +254,10 @@ export class Paralayer extends Unit {
241
254
  return this.options.defaultLayerName ?? ''
242
255
  }
243
256
 
244
- private getLayerName(layer: string, style: '$camel' | '$Pascal') {
257
+ private getLayerName(layer: string, style: 'camel' | 'Pascal') {
245
258
  const LayerName = layer.split('.').map(this.capitalize).join('')
246
- if (style === '$camel') return `$${this.decapitalize(LayerName)}`
247
- if (style === '$Pascal') return `$${LayerName}`
259
+ if (style === 'camel') return this.decapitalize(LayerName)
260
+ if (style === 'Pascal') return LayerName
248
261
  throw this.never
249
262
  }
250
263