paralayer 1.0.6 → 1.1.1
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.d.ts +1 -1
- package/dist/bin.js +23 -18
- package/dist/chunk-BES3GT56.js +195 -0
- package/dist/paralayer.d.ts +12 -8
- package/dist/paralayer.js +10 -222
- package/package.json +10 -9
- package/src/bin.ts +32 -0
- package/src/paralayer.ts +276 -0
- package/bin.js +0 -3
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -7
package/dist/bin.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/bin.js
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
paralayer
|
|
4
|
+
} from "./chunk-BES3GT56.js";
|
|
5
|
+
|
|
6
|
+
// src/bin.ts
|
|
7
|
+
import minimist from "minimist";
|
|
8
|
+
var argv = minimist(process.argv.slice(2), {
|
|
9
|
+
string: ["defaultLayerName"],
|
|
10
|
+
boolean: ["watch", "globalize"],
|
|
11
|
+
default: { watch: false, globalize: false, defaultLayerName: null }
|
|
7
12
|
});
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
var paths = argv._;
|
|
14
|
+
var input = paths.slice(0, -1);
|
|
15
|
+
var output = paths.at(-1);
|
|
11
16
|
if (input.length === 0) {
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
console.error("[paralayer] Input directory is not provided");
|
|
18
|
+
process.exit(1);
|
|
14
19
|
}
|
|
15
20
|
if (!output) {
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
console.error("[paralayer] Output directory is not provided");
|
|
22
|
+
process.exit(1);
|
|
18
23
|
}
|
|
19
24
|
await paralayer({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
input,
|
|
26
|
+
output,
|
|
27
|
+
watch: argv.watch,
|
|
28
|
+
globalize: argv.globalize,
|
|
29
|
+
defaultLayerName: argv.defaultLayerName
|
|
25
30
|
});
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// src/paralayer.ts
|
|
2
|
+
import { Queue, safe, Unit } from "@eposlabs/utils";
|
|
3
|
+
import { watch } from "chokidar";
|
|
4
|
+
import { mkdir, readFile, rm, writeFile } from "fs/promises";
|
|
5
|
+
import { basename, extname, join, relative } from "path";
|
|
6
|
+
var Paralayer = class extends Unit {
|
|
7
|
+
files = {};
|
|
8
|
+
options;
|
|
9
|
+
started = false;
|
|
10
|
+
ready = false;
|
|
11
|
+
ready$ = Promise.withResolvers();
|
|
12
|
+
queue = new Queue();
|
|
13
|
+
extensions = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx"]);
|
|
14
|
+
previousLayers = [];
|
|
15
|
+
watcher = null;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
super();
|
|
18
|
+
this.options = options;
|
|
19
|
+
}
|
|
20
|
+
async start() {
|
|
21
|
+
if (this.started) return;
|
|
22
|
+
this.started = true;
|
|
23
|
+
await safe(rm(this.options.output, { recursive: true }));
|
|
24
|
+
this.watcher = watch(this.options.input);
|
|
25
|
+
this.watcher.on("all", this.onAll);
|
|
26
|
+
this.watcher.on("ready", this.onReady);
|
|
27
|
+
await this.ready$.promise;
|
|
28
|
+
await this.queue.run(() => this.build());
|
|
29
|
+
if (!this.options.watch) await this.watcher.close();
|
|
30
|
+
}
|
|
31
|
+
async readSetupJs() {
|
|
32
|
+
const setupJsPath = join(this.options.output, "setup.js");
|
|
33
|
+
return await readFile(setupJsPath, "utf-8");
|
|
34
|
+
}
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// HANDLERS
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
onAll = async (event, path) => {
|
|
39
|
+
if (!["add", "change", "unlink"].includes(event)) return;
|
|
40
|
+
const ext = extname(path);
|
|
41
|
+
if (!this.extensions.has(ext)) return;
|
|
42
|
+
if (!this.getLayer(path)) return;
|
|
43
|
+
if (!this.ready) {
|
|
44
|
+
this.files[path] = null;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (event === "unlink") {
|
|
48
|
+
delete this.files[path];
|
|
49
|
+
await this.queue.run(() => this.build());
|
|
50
|
+
}
|
|
51
|
+
if (event === "add" || event === "change") {
|
|
52
|
+
this.files[path] = null;
|
|
53
|
+
await this.queue.run(() => this.build());
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
onReady = async () => {
|
|
57
|
+
this.ready = true;
|
|
58
|
+
this.ready$.resolve();
|
|
59
|
+
};
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// BUILD
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
async build() {
|
|
64
|
+
const paths = Object.keys(this.files);
|
|
65
|
+
if (paths.length === 0) return;
|
|
66
|
+
const pathsByLayers = Object.groupBy(paths, (path) => this.getLayer(path));
|
|
67
|
+
const allLayers = Object.keys(pathsByLayers);
|
|
68
|
+
await mkdir(this.options.output, { recursive: true });
|
|
69
|
+
await Promise.all(
|
|
70
|
+
paths.map(async (path) => {
|
|
71
|
+
if (this.files[path]) return;
|
|
72
|
+
const content = await readFile(path, "utf-8");
|
|
73
|
+
const names = this.extractExportedClassNames(content);
|
|
74
|
+
this.files[path] = { content, names };
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
for (const layer in pathsByLayers) {
|
|
78
|
+
const layerFile = join(this.options.output, `layer.${layer}.ts`);
|
|
79
|
+
const layerPaths = pathsByLayers[layer].toSorted();
|
|
80
|
+
const layerContent = this.generateLayerContent(layer, layerPaths);
|
|
81
|
+
await this.write(layerFile, layerContent);
|
|
82
|
+
if (this.isTopLayer(layer)) {
|
|
83
|
+
const indexFile = join(this.options.output, `index.${layer}.ts`);
|
|
84
|
+
const indexContent = this.generateIndexContent(layer, allLayers);
|
|
85
|
+
await this.write(indexFile, indexContent);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const removedLayers = this.previousLayers.filter((l) => !allLayers.includes(l));
|
|
89
|
+
this.previousLayers = allLayers;
|
|
90
|
+
for (const layer of removedLayers) {
|
|
91
|
+
const layerFile = join(this.options.output, `layer.${layer}.ts`);
|
|
92
|
+
await rm(layerFile);
|
|
93
|
+
if (this.isTopLayer(layer)) {
|
|
94
|
+
const indexFile = join(this.options.output, `index.${layer}.ts`);
|
|
95
|
+
await rm(indexFile);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const setupFile = join(this.options.output, "setup.js");
|
|
99
|
+
const setupContent = this.generateSetupContent(allLayers);
|
|
100
|
+
await this.write(setupFile, setupContent);
|
|
101
|
+
}
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
// HELPERS
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
extractExportedClassNames(content) {
|
|
106
|
+
if (content.includes("paralayer-ignore")) return [];
|
|
107
|
+
return content.split("export class ").slice(1).map((part) => part.split(" ")[0].split("<")[0]);
|
|
108
|
+
}
|
|
109
|
+
generateLayerContent(layer, layerPaths) {
|
|
110
|
+
const $LayerName = this.getLayerName(layer, "$Pascal");
|
|
111
|
+
const $layerName = this.getLayerName(layer, "$camel");
|
|
112
|
+
const allNames = layerPaths.flatMap((path) => this.files[path]?.names ?? []);
|
|
113
|
+
const imports = layerPaths.map((path) => {
|
|
114
|
+
const file = this.files[path];
|
|
115
|
+
if (!file) return "";
|
|
116
|
+
if (file.names.length === 0) return "";
|
|
117
|
+
const names = file.names;
|
|
118
|
+
const types = file.names.map((name) => `type ${name} as ${name}Type`);
|
|
119
|
+
const relativePath = relative(this.options.output, path);
|
|
120
|
+
return `import { ${[...names, ...types].join(", ")} } from '${relativePath}'`;
|
|
121
|
+
}).filter(Boolean);
|
|
122
|
+
const assign = [`Object.assign(${$layerName}, {`, ...allNames.map((name) => ` ${name},`), `})`];
|
|
123
|
+
const globals = [
|
|
124
|
+
`declare global {`,
|
|
125
|
+
` var ${$layerName}: ${$LayerName}`,
|
|
126
|
+
``,
|
|
127
|
+
` interface ${$LayerName} {`,
|
|
128
|
+
...allNames.map((name) => ` ${name}: typeof ${name}`),
|
|
129
|
+
` }`,
|
|
130
|
+
``,
|
|
131
|
+
` namespace ${$layerName} {`,
|
|
132
|
+
...allNames.map((name) => ` export type ${name} = ${name}Type`),
|
|
133
|
+
` }`,
|
|
134
|
+
`}`
|
|
135
|
+
];
|
|
136
|
+
return [...imports, "", ...assign, "", ...globals, ""].join("\n");
|
|
137
|
+
}
|
|
138
|
+
generateIndexContent(topLayer, allLayers) {
|
|
139
|
+
const imports = allLayers.filter((layer) => layer.includes(topLayer)).sort((layer1, layer2) => {
|
|
140
|
+
if (layer1.length !== layer2.length) return layer2.length - layer1.length;
|
|
141
|
+
return layer1.localeCompare(layer2);
|
|
142
|
+
}).map((layer) => `import './layer.${layer}.ts'`);
|
|
143
|
+
return [...imports].join("\n");
|
|
144
|
+
}
|
|
145
|
+
generateSetupContent(allLayers) {
|
|
146
|
+
const layers = allLayers.toSorted((layer1, layer2) => {
|
|
147
|
+
if (layer1.length !== layer2.length) return layer1.length - layer2.length;
|
|
148
|
+
return layer1.localeCompare(layer2);
|
|
149
|
+
});
|
|
150
|
+
const vars = layers.map((layer) => {
|
|
151
|
+
const $layerName = this.getLayerName(layer, "$camel");
|
|
152
|
+
if (this.options.globalize) return `globalThis.${$layerName} = {}`;
|
|
153
|
+
return `const ${$layerName} = {}`;
|
|
154
|
+
});
|
|
155
|
+
return [...vars].join("\n");
|
|
156
|
+
}
|
|
157
|
+
getLayer(path) {
|
|
158
|
+
const name = basename(path);
|
|
159
|
+
const layer = name.split(".").slice(1, -1).sort().join(".");
|
|
160
|
+
if (layer) return layer;
|
|
161
|
+
return this.options.defaultLayerName ?? "";
|
|
162
|
+
}
|
|
163
|
+
getLayerName(layer, style) {
|
|
164
|
+
const LayerName = layer.split(".").map(this.capitalize).join("");
|
|
165
|
+
if (style === "$camel") return `$${this.decapitalize(LayerName)}`;
|
|
166
|
+
if (style === "$Pascal") return `$${LayerName}`;
|
|
167
|
+
throw this.never;
|
|
168
|
+
}
|
|
169
|
+
capitalize(string) {
|
|
170
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
171
|
+
}
|
|
172
|
+
decapitalize(string) {
|
|
173
|
+
return string.charAt(0).toLowerCase() + string.slice(1);
|
|
174
|
+
}
|
|
175
|
+
isTopLayer(layer) {
|
|
176
|
+
return !layer.includes(".");
|
|
177
|
+
}
|
|
178
|
+
async write(path, content) {
|
|
179
|
+
const [prevContent] = await safe(() => readFile(path, "utf-8"));
|
|
180
|
+
if (content === prevContent) return;
|
|
181
|
+
await writeFile(path, content, "utf-8");
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
async function paralayer(options) {
|
|
185
|
+
const pl = new Paralayer(options);
|
|
186
|
+
await pl.start();
|
|
187
|
+
return await pl.readSetupJs();
|
|
188
|
+
}
|
|
189
|
+
var paralayer_default = paralayer;
|
|
190
|
+
|
|
191
|
+
export {
|
|
192
|
+
Paralayer,
|
|
193
|
+
paralayer,
|
|
194
|
+
paralayer_default
|
|
195
|
+
};
|
package/dist/paralayer.d.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Unit } from '@eposlabs/utils';
|
|
2
|
+
|
|
3
|
+
type DirPath = string;
|
|
4
|
+
type File = {
|
|
4
5
|
content: string;
|
|
5
6
|
names: string[];
|
|
6
7
|
};
|
|
7
|
-
|
|
8
|
+
type Options = {
|
|
8
9
|
input: DirPath | DirPath[];
|
|
9
10
|
output: DirPath;
|
|
10
11
|
watch?: boolean;
|
|
11
|
-
/** Default layer name. If a file name does not have layer tags, default name will be used. */
|
|
12
|
-
default?: string | null;
|
|
13
12
|
/** Whether the layer variables should be exposed globally. */
|
|
14
13
|
globalize?: boolean;
|
|
14
|
+
/** If a file name does not have layer tags, default layer name will be used. */
|
|
15
|
+
defaultLayerName?: string | null;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
+
declare class Paralayer extends Unit {
|
|
17
18
|
private files;
|
|
18
19
|
private options;
|
|
19
20
|
private started;
|
|
@@ -24,7 +25,7 @@ export declare class Paralayer extends $utils.Unit {
|
|
|
24
25
|
private previousLayers;
|
|
25
26
|
private watcher;
|
|
26
27
|
constructor(options: Options);
|
|
27
|
-
start
|
|
28
|
+
start(): Promise<void>;
|
|
28
29
|
readSetupJs(): Promise<string>;
|
|
29
30
|
private onAll;
|
|
30
31
|
private onReady;
|
|
@@ -40,3 +41,6 @@ export declare class Paralayer extends $utils.Unit {
|
|
|
40
41
|
private isTopLayer;
|
|
41
42
|
private write;
|
|
42
43
|
}
|
|
44
|
+
declare function paralayer(options: Options): Promise<string>;
|
|
45
|
+
|
|
46
|
+
export { type DirPath, type File, type Options, Paralayer, paralayer as default, paralayer };
|
package/dist/paralayer.js
CHANGED
|
@@ -1,222 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
queue = new $utils.Queue();
|
|
12
|
-
extensions = new Set(['.ts', '.tsx', '.js', '.jsx']);
|
|
13
|
-
previousLayers = [];
|
|
14
|
-
watcher = null;
|
|
15
|
-
constructor(options) {
|
|
16
|
-
super();
|
|
17
|
-
this.options = options;
|
|
18
|
-
}
|
|
19
|
-
start = async () => {
|
|
20
|
-
if (this.started)
|
|
21
|
-
return;
|
|
22
|
-
this.started = true;
|
|
23
|
-
await $utils.safe($fs.rm(this.options.output, { recursive: true }));
|
|
24
|
-
this.watcher = $chokidar.watch(this.options.input);
|
|
25
|
-
this.watcher.on('all', this.onAll);
|
|
26
|
-
this.watcher.on('ready', this.onReady);
|
|
27
|
-
await this.ready$.promise;
|
|
28
|
-
await this.queue.run(() => this.build());
|
|
29
|
-
if (!this.options.watch)
|
|
30
|
-
await this.watcher.close();
|
|
31
|
-
};
|
|
32
|
-
async readSetupJs() {
|
|
33
|
-
const setupJsPath = $path.join(this.options.output, 'setup.js');
|
|
34
|
-
return await $fs.readFile(setupJsPath, 'utf-8');
|
|
35
|
-
}
|
|
36
|
-
// ---------------------------------------------------------------------------
|
|
37
|
-
// HANDLERS
|
|
38
|
-
// ---------------------------------------------------------------------------
|
|
39
|
-
onAll = async (event, path) => {
|
|
40
|
-
// Process only file events
|
|
41
|
-
if (!['add', 'change', 'unlink'].includes(event))
|
|
42
|
-
return;
|
|
43
|
-
// Not supported file extension? -> Ignore
|
|
44
|
-
const ext = $path.extname(path);
|
|
45
|
-
if (!this.extensions.has(ext))
|
|
46
|
-
return;
|
|
47
|
-
// No layer in the file name? -> Ignore
|
|
48
|
-
if (!this.getLayer(path))
|
|
49
|
-
return;
|
|
50
|
-
// Initial scan? -> Just register file
|
|
51
|
-
if (!this.ready) {
|
|
52
|
-
this.files[path] = null;
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
// File removed? -> Remove and rebuild
|
|
56
|
-
if (event === 'unlink') {
|
|
57
|
-
delete this.files[path];
|
|
58
|
-
await this.queue.run(() => this.build());
|
|
59
|
-
}
|
|
60
|
-
// File added/changed? -> Reset its content and rebuild
|
|
61
|
-
if (event === 'add' || event === 'change') {
|
|
62
|
-
this.files[path] = null;
|
|
63
|
-
await this.queue.run(() => this.build());
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
onReady = async () => {
|
|
67
|
-
this.ready = true;
|
|
68
|
-
this.ready$.resolve();
|
|
69
|
-
};
|
|
70
|
-
// ---------------------------------------------------------------------------
|
|
71
|
-
// BUILD
|
|
72
|
-
// ---------------------------------------------------------------------------
|
|
73
|
-
async build() {
|
|
74
|
-
// Group file paths by layers
|
|
75
|
-
const paths = Object.keys(this.files);
|
|
76
|
-
if (paths.length === 0)
|
|
77
|
-
return;
|
|
78
|
-
const pathsByLayers = Object.groupBy(paths, path => this.getLayer(path));
|
|
79
|
-
const allLayers = Object.keys(pathsByLayers);
|
|
80
|
-
// Ensure output directory exists
|
|
81
|
-
await $fs.mkdir(this.options.output, { recursive: true });
|
|
82
|
-
// Ensure all files are read
|
|
83
|
-
await Promise.all(paths.map(async (path) => {
|
|
84
|
-
if (this.files[path])
|
|
85
|
-
return;
|
|
86
|
-
const content = await $fs.readFile(path, 'utf-8');
|
|
87
|
-
const names = this.extractExportedClassNames(content);
|
|
88
|
-
this.files[path] = { content, names };
|
|
89
|
-
}));
|
|
90
|
-
// Create layer files
|
|
91
|
-
for (const layer in pathsByLayers) {
|
|
92
|
-
// Generate layer.[layer].ts
|
|
93
|
-
const layerFile = $path.join(this.options.output, `layer.${layer}.ts`);
|
|
94
|
-
const layerPaths = pathsByLayers[layer].toSorted();
|
|
95
|
-
const layerContent = this.generateLayerContent(layer, layerPaths);
|
|
96
|
-
await this.write(layerFile, layerContent);
|
|
97
|
-
// Top layer? -> Generate index.[layer].ts
|
|
98
|
-
if (this.isTopLayer(layer)) {
|
|
99
|
-
const indexFile = $path.join(this.options.output, `index.${layer}.ts`);
|
|
100
|
-
const indexContent = this.generateIndexContent(layer, allLayers);
|
|
101
|
-
await this.write(indexFile, indexContent);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
// Determine removed layers
|
|
105
|
-
const removedLayers = this.previousLayers.filter(l => !allLayers.includes(l));
|
|
106
|
-
this.previousLayers = allLayers;
|
|
107
|
-
// Delete files of removed layers
|
|
108
|
-
for (const layer of removedLayers) {
|
|
109
|
-
// Delete layer file
|
|
110
|
-
const layerFile = $path.join(this.options.output, `layer.${layer}.ts`);
|
|
111
|
-
await $fs.rm(layerFile);
|
|
112
|
-
// Top layer? -> Remove index file
|
|
113
|
-
if (this.isTopLayer(layer)) {
|
|
114
|
-
const indexFile = $path.join(this.options.output, `index.${layer}.ts`);
|
|
115
|
-
await $fs.rm(indexFile);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// Generate setup.js file
|
|
119
|
-
const setupFile = $path.join(this.options.output, 'setup.js');
|
|
120
|
-
const setupContent = this.generateSetupContent(allLayers);
|
|
121
|
-
await this.write(setupFile, setupContent);
|
|
122
|
-
}
|
|
123
|
-
// ---------------------------------------------------------------------------
|
|
124
|
-
// HELPERS
|
|
125
|
-
// ---------------------------------------------------------------------------
|
|
126
|
-
extractExportedClassNames(content) {
|
|
127
|
-
if (content.includes('paralayer-ignore'))
|
|
128
|
-
return [];
|
|
129
|
-
return content
|
|
130
|
-
.split('export class ')
|
|
131
|
-
.slice(1)
|
|
132
|
-
.map(part => part.split(' ')[0].split('<')[0]);
|
|
133
|
-
}
|
|
134
|
-
generateLayerContent(layer, layerPaths) {
|
|
135
|
-
const $LayerName = this.getLayerName(layer, '$Pascal');
|
|
136
|
-
const $layerName = this.getLayerName(layer, '$camel');
|
|
137
|
-
const allNames = layerPaths.flatMap(path => this.files[path]?.names ?? []);
|
|
138
|
-
const imports = layerPaths
|
|
139
|
-
.map(path => {
|
|
140
|
-
const file = this.files[path];
|
|
141
|
-
if (!file)
|
|
142
|
-
return '';
|
|
143
|
-
if (file.names.length === 0)
|
|
144
|
-
return '';
|
|
145
|
-
const names = file.names;
|
|
146
|
-
const types = file.names.map(name => `type ${name} as ${name}Type`);
|
|
147
|
-
const relativePath = $path.relative(this.options.output, path);
|
|
148
|
-
return `import { ${[...names, ...types].join(', ')} } from '${relativePath}'`;
|
|
149
|
-
})
|
|
150
|
-
.filter(Boolean);
|
|
151
|
-
const assign = [`Object.assign(${$layerName}, {`, ...allNames.map(name => ` ${name},`), `})`];
|
|
152
|
-
const globals = [
|
|
153
|
-
`declare global {`,
|
|
154
|
-
` var ${$layerName}: ${$LayerName}`,
|
|
155
|
-
``,
|
|
156
|
-
` interface ${$LayerName} {`,
|
|
157
|
-
...allNames.map(name => ` ${name}: typeof ${name}`),
|
|
158
|
-
` }`,
|
|
159
|
-
``,
|
|
160
|
-
` namespace ${$layerName} {`,
|
|
161
|
-
...allNames.map(name => ` export type ${name} = ${name}Type`),
|
|
162
|
-
` }`,
|
|
163
|
-
`}`,
|
|
164
|
-
];
|
|
165
|
-
return [...imports, '', ...assign, '', ...globals, ''].join('\n');
|
|
166
|
-
}
|
|
167
|
-
generateIndexContent(topLayer, allLayers) {
|
|
168
|
-
const imports = allLayers
|
|
169
|
-
.filter(layer => layer.includes(topLayer))
|
|
170
|
-
.sort((layer1, layer2) => {
|
|
171
|
-
if (layer1.length !== layer2.length)
|
|
172
|
-
return layer2.length - layer1.length;
|
|
173
|
-
return layer1.localeCompare(layer2);
|
|
174
|
-
})
|
|
175
|
-
.map(layer => `import './layer.${layer}.ts'`);
|
|
176
|
-
return [...imports].join('\n');
|
|
177
|
-
}
|
|
178
|
-
generateSetupContent(allLayers) {
|
|
179
|
-
const layers = allLayers.toSorted((layer1, layer2) => {
|
|
180
|
-
if (layer1.length !== layer2.length)
|
|
181
|
-
return layer1.length - layer2.length;
|
|
182
|
-
return layer1.localeCompare(layer2);
|
|
183
|
-
});
|
|
184
|
-
const vars = layers.map(layer => {
|
|
185
|
-
const $layerName = this.getLayerName(layer, '$camel');
|
|
186
|
-
if (this.options.globalize)
|
|
187
|
-
return `globalThis.${$layerName} = {}`;
|
|
188
|
-
return `const ${$layerName} = {}`;
|
|
189
|
-
});
|
|
190
|
-
return [...vars].join('\n');
|
|
191
|
-
}
|
|
192
|
-
getLayer(path) {
|
|
193
|
-
const name = $path.basename(path);
|
|
194
|
-
const layer = name.split('.').slice(1, -1).sort().join('.');
|
|
195
|
-
if (layer)
|
|
196
|
-
return layer;
|
|
197
|
-
return this.options.default ?? '';
|
|
198
|
-
}
|
|
199
|
-
getLayerName(layer, style) {
|
|
200
|
-
const LayerName = layer.split('.').map(this.capitalize).join('');
|
|
201
|
-
if (style === '$camel')
|
|
202
|
-
return `$${this.decapitalize(LayerName)}`;
|
|
203
|
-
if (style === '$Pascal')
|
|
204
|
-
return `$${LayerName}`;
|
|
205
|
-
throw this.never;
|
|
206
|
-
}
|
|
207
|
-
capitalize(string) {
|
|
208
|
-
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
209
|
-
}
|
|
210
|
-
decapitalize(string) {
|
|
211
|
-
return string.charAt(0).toLowerCase() + string.slice(1);
|
|
212
|
-
}
|
|
213
|
-
isTopLayer(layer) {
|
|
214
|
-
return !layer.includes('.');
|
|
215
|
-
}
|
|
216
|
-
async write(path, content) {
|
|
217
|
-
const [prevContent] = await $utils.safe(() => $fs.readFile(path, 'utf-8'));
|
|
218
|
-
if (content === prevContent)
|
|
219
|
-
return;
|
|
220
|
-
await $fs.writeFile(path, content, 'utf-8');
|
|
221
|
-
}
|
|
222
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Paralayer,
|
|
3
|
+
paralayer,
|
|
4
|
+
paralayer_default
|
|
5
|
+
} from "./chunk-BES3GT56.js";
|
|
6
|
+
export {
|
|
7
|
+
Paralayer,
|
|
8
|
+
paralayer_default as default,
|
|
9
|
+
paralayer
|
|
10
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "paralayer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "imkost",
|
|
@@ -10,26 +10,27 @@
|
|
|
10
10
|
"vite-plugin"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
|
-
"
|
|
14
|
-
"build": "rm -rf dist && tsc",
|
|
13
|
+
"build": "tsup src --format esm --dts --clean",
|
|
15
14
|
"lint": "tsc --noEmit",
|
|
16
15
|
"release": "sh -c 'npm version ${1:-patch} && npm run build && npm publish' --"
|
|
17
16
|
},
|
|
18
17
|
"bin": {
|
|
19
|
-
"paralayer": "bin.
|
|
18
|
+
"paralayer": "./src/bin.ts"
|
|
20
19
|
},
|
|
21
20
|
"exports": {
|
|
22
21
|
".": {
|
|
23
|
-
"
|
|
24
|
-
|
|
22
|
+
"import": "./dist/paralayer.js"
|
|
23
|
+
},
|
|
24
|
+
"./ts": {
|
|
25
|
+
"import": "./src/paralayer.ts"
|
|
25
26
|
}
|
|
26
27
|
},
|
|
27
28
|
"files": [
|
|
28
|
-
"
|
|
29
|
-
"
|
|
29
|
+
"src",
|
|
30
|
+
"dist"
|
|
30
31
|
],
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@eposlabs/utils": "^1.
|
|
33
|
+
"@eposlabs/utils": "^1.1.1",
|
|
33
34
|
"chokidar": "^4.0.3",
|
|
34
35
|
"minimist": "^1.2.8"
|
|
35
36
|
},
|
package/src/bin.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import minimist from 'minimist'
|
|
4
|
+
import { paralayer } from './paralayer.ts'
|
|
5
|
+
|
|
6
|
+
const argv = minimist(process.argv.slice(2), {
|
|
7
|
+
string: ['defaultLayerName'],
|
|
8
|
+
boolean: ['watch', 'globalize'],
|
|
9
|
+
default: { watch: false, globalize: false, defaultLayerName: null },
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const paths = argv._
|
|
13
|
+
const input = paths.slice(0, -1)
|
|
14
|
+
const output = paths.at(-1)
|
|
15
|
+
|
|
16
|
+
if (input.length === 0) {
|
|
17
|
+
console.error('[paralayer] Input directory is not provided')
|
|
18
|
+
process.exit(1)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!output) {
|
|
22
|
+
console.error('[paralayer] Output directory is not provided')
|
|
23
|
+
process.exit(1)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
await paralayer({
|
|
27
|
+
input: input,
|
|
28
|
+
output: output,
|
|
29
|
+
watch: argv.watch,
|
|
30
|
+
globalize: argv.globalize,
|
|
31
|
+
defaultLayerName: argv.defaultLayerName,
|
|
32
|
+
})
|
package/src/paralayer.ts
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { Queue, safe, Unit } from '@eposlabs/utils'
|
|
2
|
+
import type { FSWatcher } from 'chokidar'
|
|
3
|
+
import { watch } from 'chokidar'
|
|
4
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
5
|
+
import { basename, extname, join, relative } from 'node:path'
|
|
6
|
+
|
|
7
|
+
export type DirPath = string
|
|
8
|
+
|
|
9
|
+
export type File = {
|
|
10
|
+
content: string
|
|
11
|
+
names: string[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Options = {
|
|
15
|
+
input: DirPath | DirPath[]
|
|
16
|
+
output: DirPath
|
|
17
|
+
watch?: boolean
|
|
18
|
+
/** Whether the layer variables should be exposed globally. */
|
|
19
|
+
globalize?: boolean
|
|
20
|
+
/** If a file name does not have layer tags, default layer name will be used. */
|
|
21
|
+
defaultLayerName?: string | null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Paralayer extends Unit {
|
|
25
|
+
private files: { [path: string]: File | null } = {}
|
|
26
|
+
private options: Options
|
|
27
|
+
private started = false
|
|
28
|
+
private ready = false
|
|
29
|
+
private ready$ = Promise.withResolvers<void>()
|
|
30
|
+
private queue = new Queue()
|
|
31
|
+
private extensions = new Set(['.ts', '.tsx', '.js', '.jsx'])
|
|
32
|
+
private previousLayers: string[] = []
|
|
33
|
+
private watcher: FSWatcher | null = null
|
|
34
|
+
|
|
35
|
+
constructor(options: Options) {
|
|
36
|
+
super()
|
|
37
|
+
this.options = options
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async start() {
|
|
41
|
+
if (this.started) return
|
|
42
|
+
this.started = true
|
|
43
|
+
|
|
44
|
+
await safe(rm(this.options.output, { recursive: true }))
|
|
45
|
+
|
|
46
|
+
this.watcher = watch(this.options.input)
|
|
47
|
+
this.watcher.on('all', this.onAll)
|
|
48
|
+
this.watcher.on('ready', this.onReady)
|
|
49
|
+
|
|
50
|
+
await this.ready$.promise
|
|
51
|
+
await this.queue.run(() => this.build())
|
|
52
|
+
if (!this.options.watch) await this.watcher.close()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async readSetupJs() {
|
|
56
|
+
const setupJsPath = join(this.options.output, 'setup.js')
|
|
57
|
+
return await readFile(setupJsPath, 'utf-8')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// HANDLERS
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
private onAll = async (event: string, path: string) => {
|
|
65
|
+
// Process only file events
|
|
66
|
+
if (!['add', 'change', 'unlink'].includes(event)) return
|
|
67
|
+
|
|
68
|
+
// Not supported file extension? -> Ignore
|
|
69
|
+
const ext = extname(path)
|
|
70
|
+
if (!this.extensions.has(ext)) return
|
|
71
|
+
|
|
72
|
+
// No layer in the file name? -> Ignore
|
|
73
|
+
if (!this.getLayer(path)) return
|
|
74
|
+
|
|
75
|
+
// Initial scan? -> Just register file
|
|
76
|
+
if (!this.ready) {
|
|
77
|
+
this.files[path] = null
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// File removed? -> Remove and rebuild
|
|
82
|
+
if (event === 'unlink') {
|
|
83
|
+
delete this.files[path]
|
|
84
|
+
await this.queue.run(() => this.build())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// File added/changed? -> Reset its content and rebuild
|
|
88
|
+
if (event === 'add' || event === 'change') {
|
|
89
|
+
this.files[path] = null
|
|
90
|
+
await this.queue.run(() => this.build())
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private onReady = async () => {
|
|
95
|
+
this.ready = true
|
|
96
|
+
this.ready$.resolve()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// BUILD
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
|
|
103
|
+
private async build() {
|
|
104
|
+
// Group file paths by layers
|
|
105
|
+
const paths = Object.keys(this.files)
|
|
106
|
+
if (paths.length === 0) return
|
|
107
|
+
const pathsByLayers = Object.groupBy(paths, path => this.getLayer(path))
|
|
108
|
+
const allLayers = Object.keys(pathsByLayers)
|
|
109
|
+
|
|
110
|
+
// Ensure output directory exists
|
|
111
|
+
await mkdir(this.options.output, { recursive: true })
|
|
112
|
+
|
|
113
|
+
// Ensure all files are read
|
|
114
|
+
await Promise.all(
|
|
115
|
+
paths.map(async path => {
|
|
116
|
+
if (this.files[path]) return
|
|
117
|
+
const content = await readFile(path, 'utf-8')
|
|
118
|
+
const names = this.extractExportedClassNames(content)
|
|
119
|
+
this.files[path] = { content, names }
|
|
120
|
+
}),
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
// Create layer files
|
|
124
|
+
for (const layer in pathsByLayers) {
|
|
125
|
+
// Generate layer.[layer].ts
|
|
126
|
+
const layerFile = join(this.options.output, `layer.${layer}.ts`)
|
|
127
|
+
const layerPaths = pathsByLayers[layer]!.toSorted()
|
|
128
|
+
const layerContent = this.generateLayerContent(layer, layerPaths)
|
|
129
|
+
await this.write(layerFile, layerContent)
|
|
130
|
+
|
|
131
|
+
// Top layer? -> Generate index.[layer].ts
|
|
132
|
+
if (this.isTopLayer(layer)) {
|
|
133
|
+
const indexFile = join(this.options.output, `index.${layer}.ts`)
|
|
134
|
+
const indexContent = this.generateIndexContent(layer, allLayers)
|
|
135
|
+
await this.write(indexFile, indexContent)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Determine removed layers
|
|
140
|
+
const removedLayers = this.previousLayers.filter(l => !allLayers.includes(l))
|
|
141
|
+
this.previousLayers = allLayers
|
|
142
|
+
|
|
143
|
+
// Delete files of removed layers
|
|
144
|
+
for (const layer of removedLayers) {
|
|
145
|
+
// Delete layer file
|
|
146
|
+
const layerFile = join(this.options.output, `layer.${layer}.ts`)
|
|
147
|
+
await rm(layerFile)
|
|
148
|
+
|
|
149
|
+
// Top layer? -> Remove index file
|
|
150
|
+
if (this.isTopLayer(layer)) {
|
|
151
|
+
const indexFile = join(this.options.output, `index.${layer}.ts`)
|
|
152
|
+
await rm(indexFile)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Generate setup.js file
|
|
157
|
+
const setupFile = join(this.options.output, 'setup.js')
|
|
158
|
+
const setupContent = this.generateSetupContent(allLayers)
|
|
159
|
+
await this.write(setupFile, setupContent)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
// HELPERS
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
private extractExportedClassNames(content: string) {
|
|
167
|
+
if (content.includes('paralayer-ignore')) return []
|
|
168
|
+
return content
|
|
169
|
+
.split('export class ')
|
|
170
|
+
.slice(1)
|
|
171
|
+
.map(part => part.split(' ')[0].split('<')[0])
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private generateLayerContent(layer: string, layerPaths: string[]) {
|
|
175
|
+
const $LayerName = this.getLayerName(layer, '$Pascal')
|
|
176
|
+
const $layerName = this.getLayerName(layer, '$camel')
|
|
177
|
+
const allNames = layerPaths.flatMap(path => this.files[path]?.names ?? [])
|
|
178
|
+
|
|
179
|
+
const imports = layerPaths
|
|
180
|
+
.map(path => {
|
|
181
|
+
const file = this.files[path]
|
|
182
|
+
if (!file) return ''
|
|
183
|
+
if (file.names.length === 0) return ''
|
|
184
|
+
const names = file.names
|
|
185
|
+
const types = file.names.map(name => `type ${name} as ${name}Type`)
|
|
186
|
+
const relativePath = relative(this.options.output, path)
|
|
187
|
+
return `import { ${[...names, ...types].join(', ')} } from '${relativePath}'`
|
|
188
|
+
})
|
|
189
|
+
.filter(Boolean)
|
|
190
|
+
|
|
191
|
+
const assign = [`Object.assign(${$layerName}, {`, ...allNames.map(name => ` ${name},`), `})`]
|
|
192
|
+
|
|
193
|
+
const globals = [
|
|
194
|
+
`declare global {`,
|
|
195
|
+
` var ${$layerName}: ${$LayerName}`,
|
|
196
|
+
``,
|
|
197
|
+
` interface ${$LayerName} {`,
|
|
198
|
+
...allNames.map(name => ` ${name}: typeof ${name}`),
|
|
199
|
+
` }`,
|
|
200
|
+
``,
|
|
201
|
+
` namespace ${$layerName} {`,
|
|
202
|
+
...allNames.map(name => ` export type ${name} = ${name}Type`),
|
|
203
|
+
` }`,
|
|
204
|
+
`}`,
|
|
205
|
+
]
|
|
206
|
+
|
|
207
|
+
return [...imports, '', ...assign, '', ...globals, ''].join('\n')
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private generateIndexContent(topLayer: string, allLayers: string[]) {
|
|
211
|
+
const imports = allLayers
|
|
212
|
+
.filter(layer => layer.includes(topLayer))
|
|
213
|
+
.sort((layer1, layer2) => {
|
|
214
|
+
if (layer1.length !== layer2.length) return layer2.length - layer1.length
|
|
215
|
+
return layer1.localeCompare(layer2)
|
|
216
|
+
})
|
|
217
|
+
.map(layer => `import './layer.${layer}.ts'`)
|
|
218
|
+
|
|
219
|
+
return [...imports].join('\n')
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private generateSetupContent(allLayers: string[]) {
|
|
223
|
+
const layers = allLayers.toSorted((layer1, layer2) => {
|
|
224
|
+
if (layer1.length !== layer2.length) return layer1.length - layer2.length
|
|
225
|
+
return layer1.localeCompare(layer2)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
const vars = layers.map(layer => {
|
|
229
|
+
const $layerName = this.getLayerName(layer, '$camel')
|
|
230
|
+
if (this.options.globalize) return `globalThis.${$layerName} = {}`
|
|
231
|
+
return `const ${$layerName} = {}`
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
return [...vars].join('\n')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private getLayer(path: string) {
|
|
238
|
+
const name = basename(path)
|
|
239
|
+
const layer = name.split('.').slice(1, -1).sort().join('.')
|
|
240
|
+
if (layer) return layer
|
|
241
|
+
return this.options.defaultLayerName ?? ''
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private getLayerName(layer: string, style: '$camel' | '$Pascal') {
|
|
245
|
+
const LayerName = layer.split('.').map(this.capitalize).join('')
|
|
246
|
+
if (style === '$camel') return `$${this.decapitalize(LayerName)}`
|
|
247
|
+
if (style === '$Pascal') return `$${LayerName}`
|
|
248
|
+
throw this.never
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private capitalize(string: string) {
|
|
252
|
+
return string.charAt(0).toUpperCase() + string.slice(1)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private decapitalize(string: string) {
|
|
256
|
+
return string.charAt(0).toLowerCase() + string.slice(1)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private isTopLayer(layer: string) {
|
|
260
|
+
return !layer.includes('.')
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private async write(path: string, content: string) {
|
|
264
|
+
const [prevContent] = await safe(() => readFile(path, 'utf-8'))
|
|
265
|
+
if (content === prevContent) return
|
|
266
|
+
await writeFile(path, content, 'utf-8')
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function paralayer(options: Options) {
|
|
271
|
+
const pl = new Paralayer(options)
|
|
272
|
+
await pl.start()
|
|
273
|
+
return await pl.readSetupJs()
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export default paralayer
|
package/bin.js
DELETED
package/dist/index.d.ts
DELETED