jopi-toolkit 3.0.5
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/README.md +6 -0
- package/dist/jk_app/common.d.ts +40 -0
- package/dist/jk_app/common.js +313 -0
- package/dist/jk_app/common.js.map +1 -0
- package/dist/jk_app/index.d.ts +1 -0
- package/dist/jk_app/index.js +4 -0
- package/dist/jk_app/index.js.map +1 -0
- package/dist/jk_app/jBundler_ifBrowser.d.ts +1 -0
- package/dist/jk_app/jBundler_ifBrowser.js +3 -0
- package/dist/jk_app/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_app/jBundler_ifServer.d.ts +1 -0
- package/dist/jk_app/jBundler_ifServer.js +9 -0
- package/dist/jk_app/jBundler_ifServer.js.map +1 -0
- package/dist/jk_appResolver/common.d.ts +40 -0
- package/dist/jk_appResolver/common.js +306 -0
- package/dist/jk_appResolver/common.js.map +1 -0
- package/dist/jk_appResolver/index.d.ts +30 -0
- package/dist/jk_appResolver/index.js +276 -0
- package/dist/jk_appResolver/index.js.map +1 -0
- package/dist/jk_appResolver/jBundler_ifBrowser.d.ts +1 -0
- package/dist/jk_appResolver/jBundler_ifBrowser.js +3 -0
- package/dist/jk_appResolver/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_appResolver/jBundler_ifServer.d.ts +1 -0
- package/dist/jk_appResolver/jBundler_ifServer.js +9 -0
- package/dist/jk_appResolver/jBundler_ifServer.js.map +1 -0
- package/dist/jk_compress/index.d.ts +1 -0
- package/dist/jk_compress/index.js +2 -0
- package/dist/jk_compress/index.js.map +1 -0
- package/dist/jk_compress/jBundler_ifBrowser.d.ts +2 -0
- package/dist/jk_compress/jBundler_ifBrowser.js +3 -0
- package/dist/jk_compress/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_compress/jBundler_ifServer.d.ts +5 -0
- package/dist/jk_compress/jBundler_ifServer.js +11 -0
- package/dist/jk_compress/jBundler_ifServer.js.map +1 -0
- package/dist/jk_crypto/index.d.ts +1 -0
- package/dist/jk_crypto/index.js +2 -0
- package/dist/jk_crypto/index.js.map +1 -0
- package/dist/jk_crypto/jBundler_ifBrowser.d.ts +2 -0
- package/dist/jk_crypto/jBundler_ifBrowser.js +3 -0
- package/dist/jk_crypto/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_crypto/jBundler_ifServer.d.ts +5 -0
- package/dist/jk_crypto/jBundler_ifServer.js +17 -0
- package/dist/jk_crypto/jBundler_ifServer.js.map +1 -0
- package/dist/jk_events/index.d.ts +35 -0
- package/dist/jk_events/index.js +134 -0
- package/dist/jk_events/index.js.map +1 -0
- package/dist/jk_fs/common.d.ts +16 -0
- package/dist/jk_fs/common.js +2 -0
- package/dist/jk_fs/common.js.map +1 -0
- package/dist/jk_fs/index.d.ts +2 -0
- package/dist/jk_fs/index.js +3 -0
- package/dist/jk_fs/index.js.map +1 -0
- package/dist/jk_fs/jBundler_ifBrowser.d.ts +41 -0
- package/dist/jk_fs/jBundler_ifBrowser.js +88 -0
- package/dist/jk_fs/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_fs/jBundler_ifServer.d.ts +48 -0
- package/dist/jk_fs/jBundler_ifServer.js +286 -0
- package/dist/jk_fs/jBundler_ifServer.js.map +1 -0
- package/dist/jk_linker/TypeComposite.d.ts +12 -0
- package/dist/jk_linker/TypeComposite.js +112 -0
- package/dist/jk_linker/TypeComposite.js.map +1 -0
- package/dist/jk_linker/arobaseType_List.d.ts +44 -0
- package/dist/jk_linker/arobaseType_List.js +183 -0
- package/dist/jk_linker/arobaseType_List.js.map +1 -0
- package/dist/jk_linker/arobaseTypes.d.ts +45 -0
- package/dist/jk_linker/arobaseTypes.js +181 -0
- package/dist/jk_linker/arobaseTypes.js.map +1 -0
- package/dist/jk_linker/binding.d.ts +1 -0
- package/dist/jk_linker/binding.js +13 -0
- package/dist/jk_linker/binding.js.map +1 -0
- package/dist/jk_linker/engine.d.ts +119 -0
- package/dist/jk_linker/engine.js +553 -0
- package/dist/jk_linker/engine.js.map +1 -0
- package/dist/jk_linker/index.d.ts +1 -0
- package/dist/jk_linker/index.js +2 -0
- package/dist/jk_linker/index.js.map +1 -0
- package/dist/jk_linker/install.d.ts +4 -0
- package/dist/jk_linker/install.js +44 -0
- package/dist/jk_linker/install.js.map +1 -0
- package/dist/jk_linker/jBundler_ifServer.d.ts +3 -0
- package/dist/jk_linker/jBundler_ifServer.js +4 -0
- package/dist/jk_linker/jBundler_ifServer.js.map +1 -0
- package/dist/jk_linker/modulesInitProcessor.d.ts +7 -0
- package/dist/jk_linker/modulesInitProcessor.js +36 -0
- package/dist/jk_linker/modulesInitProcessor.js.map +1 -0
- package/dist/jk_linker/typeChunks.d.ts +15 -0
- package/dist/jk_linker/typeChunks.js +42 -0
- package/dist/jk_linker/typeChunks.js.map +1 -0
- package/dist/jk_linker/typeComposites.d.ts +13 -0
- package/dist/jk_linker/typeComposites.js +121 -0
- package/dist/jk_linker/typeComposites.js.map +1 -0
- package/dist/jk_linker/typeEvents.d.ts +5 -0
- package/dist/jk_linker/typeEvents.js +29 -0
- package/dist/jk_linker/typeEvents.js.map +1 -0
- package/dist/jk_linker/typeList.d.ts +30 -0
- package/dist/jk_linker/typeList.js +144 -0
- package/dist/jk_linker/typeList.js.map +1 -0
- package/dist/jk_linker/typeListeners.d.ts +21 -0
- package/dist/jk_linker/typeListeners.js +188 -0
- package/dist/jk_linker/typeListeners.js.map +1 -0
- package/dist/jk_linker/typeReplaces.d.ts +8 -0
- package/dist/jk_linker/typeReplaces.js +41 -0
- package/dist/jk_linker/typeReplaces.js.map +1 -0
- package/dist/jk_os/index.d.ts +1 -0
- package/dist/jk_os/index.js +2 -0
- package/dist/jk_os/index.js.map +1 -0
- package/dist/jk_os/jBundler_ifBrowser.d.ts +3 -0
- package/dist/jk_os/jBundler_ifBrowser.js +10 -0
- package/dist/jk_os/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_os/jBundler_ifServer.d.ts +2 -0
- package/dist/jk_os/jBundler_ifServer.js +64 -0
- package/dist/jk_os/jBundler_ifServer.js.map +1 -0
- package/dist/jk_process/index.d.ts +1 -0
- package/dist/jk_process/index.js +2 -0
- package/dist/jk_process/index.js.map +1 -0
- package/dist/jk_process/jBundler_ifBrowser.d.ts +4 -0
- package/dist/jk_process/jBundler_ifBrowser.js +21 -0
- package/dist/jk_process/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_process/jBundler_ifServer.d.ts +4 -0
- package/dist/jk_process/jBundler_ifServer.js +5 -0
- package/dist/jk_process/jBundler_ifServer.js.map +1 -0
- package/dist/jk_registry/index.d.ts +11 -0
- package/dist/jk_registry/index.js +36 -0
- package/dist/jk_registry/index.js.map +1 -0
- package/dist/jk_schemas/index.d.ts +118 -0
- package/dist/jk_schemas/index.js +220 -0
- package/dist/jk_schemas/index.js.map +1 -0
- package/dist/jk_streams/index.d.ts +1 -0
- package/dist/jk_streams/index.js +6 -0
- package/dist/jk_streams/index.js.map +1 -0
- package/dist/jk_term/index.d.ts +51 -0
- package/dist/jk_term/index.js +102 -0
- package/dist/jk_term/index.js.map +1 -0
- package/dist/jk_thread/common.d.ts +3 -0
- package/dist/jk_thread/common.js +8 -0
- package/dist/jk_thread/common.js.map +1 -0
- package/dist/jk_thread/index.d.ts +2 -0
- package/dist/jk_thread/index.js +3 -0
- package/dist/jk_thread/index.js.map +1 -0
- package/dist/jk_thread/jBundler_ifBrowser.d.ts +6 -0
- package/dist/jk_thread/jBundler_ifBrowser.js +15 -0
- package/dist/jk_thread/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_thread/jBundler_ifServer.d.ts +6 -0
- package/dist/jk_thread/jBundler_ifServer.js +22 -0
- package/dist/jk_thread/jBundler_ifServer.js.map +1 -0
- package/dist/jk_timer/index.d.ts +26 -0
- package/dist/jk_timer/index.js +100 -0
- package/dist/jk_timer/index.js.map +1 -0
- package/dist/jk_tools/index.d.ts +20 -0
- package/dist/jk_tools/index.js +72 -0
- package/dist/jk_tools/index.js.map +1 -0
- package/dist/jk_translate/index.d.ts +21 -0
- package/dist/jk_translate/index.js +56 -0
- package/dist/jk_translate/index.js.map +1 -0
- package/dist/jk_webSocket/common.d.ts +2 -0
- package/dist/jk_webSocket/common.js +7 -0
- package/dist/jk_webSocket/common.js.map +1 -0
- package/dist/jk_webSocket/index.d.ts +2 -0
- package/dist/jk_webSocket/index.js +3 -0
- package/dist/jk_webSocket/index.js.map +1 -0
- package/dist/jk_webSocket/jBundler_ifBrowser.ts.d.ts +3 -0
- package/dist/jk_webSocket/jBundler_ifBrowser.ts.js +14 -0
- package/dist/jk_webSocket/jBundler_ifBrowser.ts.js.map +1 -0
- package/dist/jk_webSocket/jBundler_ifServer.d.ts +7 -0
- package/dist/jk_webSocket/jBundler_ifServer.js +41 -0
- package/dist/jk_webSocket/jBundler_ifServer.js.map +1 -0
- package/dist/jk_what/index.d.ts +1 -0
- package/dist/jk_what/index.js +2 -0
- package/dist/jk_what/index.js.map +1 -0
- package/dist/jk_what/jBundler_ifBrowser.d.ts +5 -0
- package/dist/jk_what/jBundler_ifBrowser.js +6 -0
- package/dist/jk_what/jBundler_ifBrowser.js.map +1 -0
- package/dist/jk_what/jBundler_ifServer.d.ts +5 -0
- package/dist/jk_what/jBundler_ifServer.js +6 -0
- package/dist/jk_what/jBundler_ifServer.js.map +1 -0
- package/package.json +100 -0
- package/src/jk_app/common.js +442 -0
- package/src/jk_app/common.ts +389 -0
- package/src/jk_app/index.js +3 -0
- package/src/jk_app/index.ts +4 -0
- package/src/jk_app/jBundler_ifBrowser.ts +2 -0
- package/src/jk_app/jBundler_ifServer.js +8 -0
- package/src/jk_app/jBundler_ifServer.ts +11 -0
- package/src/jk_compress/index.js +1 -0
- package/src/jk_compress/index.ts +1 -0
- package/src/jk_compress/jBundler_ifBrowser.ts +2 -0
- package/src/jk_compress/jBundler_ifServer.js +10 -0
- package/src/jk_compress/jBundler_ifServer.ts +16 -0
- package/src/jk_crypto/index.js +1 -0
- package/src/jk_crypto/index.ts +1 -0
- package/src/jk_crypto/jBundler_ifBrowser.ts +2 -0
- package/src/jk_crypto/jBundler_ifServer.ts +21 -0
- package/src/jk_events/index.js +203 -0
- package/src/jk_events/index.ts +168 -0
- package/src/jk_fs/common.js +1 -0
- package/src/jk_fs/common.ts +20 -0
- package/src/jk_fs/index.js +2 -0
- package/src/jk_fs/index.ts +2 -0
- package/src/jk_fs/jBundler_ifBrowser.ts +116 -0
- package/src/jk_fs/jBundler_ifServer.ts +308 -0
- package/src/jk_os/index.js +1 -0
- package/src/jk_os/index.ts +1 -0
- package/src/jk_os/jBundler_ifBrowser.ts +11 -0
- package/src/jk_os/jBundler_ifServer.js +132 -0
- package/src/jk_os/jBundler_ifServer.ts +63 -0
- package/src/jk_process/index.ts +1 -0
- package/src/jk_process/jBundler_ifBrowser.ts +20 -0
- package/src/jk_process/jBundler_ifServer.ts +4 -0
- package/src/jk_schemas/TODO.txt +7 -0
- package/src/jk_schemas/index.js +248 -0
- package/src/jk_schemas/index.ts +416 -0
- package/src/jk_streams/index.ts +6 -0
- package/src/jk_term/index.js +146 -0
- package/src/jk_term/index.ts +131 -0
- package/src/jk_thread/common.ts +9 -0
- package/src/jk_thread/index.js +2 -0
- package/src/jk_thread/index.ts +2 -0
- package/src/jk_thread/jBundler_ifBrowser.ts +18 -0
- package/src/jk_thread/jBundler_ifServer.js +21 -0
- package/src/jk_thread/jBundler_ifServer.ts +28 -0
- package/src/jk_timer/index.js +155 -0
- package/src/jk_timer/index.ts +150 -0
- package/src/jk_tools/index.js +82 -0
- package/src/jk_tools/index.ts +82 -0
- package/src/jk_translate/index.js +56 -0
- package/src/jk_translate/index.ts +85 -0
- package/src/jk_webSocket/common.js +6 -0
- package/src/jk_webSocket/common.ts +7 -0
- package/src/jk_webSocket/index.js +2 -0
- package/src/jk_webSocket/index.ts +2 -0
- package/src/jk_webSocket/jBundler_ifBrowser.ts.ts +16 -0
- package/src/jk_webSocket/jBundler_ifServer.js +40 -0
- package/src/jk_webSocket/jBundler_ifServer.ts +57 -0
- package/src/jk_what/index.js +1 -0
- package/src/jk_what/index.ts +1 -0
- package/src/jk_what/jBundler_ifBrowser.ts +5 -0
- package/src/jk_what/jBundler_ifServer.ts +5 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
3
|
+
import * as jk_thread from "jopi-toolkit/jk_thread";
|
|
4
|
+
import * as jk_fs from "jopi-toolkit/jk_fs";
|
|
5
|
+
import * as jk_timer from "jopi-toolkit/jk_timer";
|
|
6
|
+
import {isBunJS, isNodeJS} from "jopi-toolkit/jk_what";
|
|
7
|
+
import {isUsingWorker} from "jopi-toolkit/jk_thread";
|
|
8
|
+
|
|
9
|
+
export type Listener = ()=>void|Promise<void>;
|
|
10
|
+
|
|
11
|
+
async function execListeners(listeners: Listener[]) {
|
|
12
|
+
const list = [...listeners];
|
|
13
|
+
listeners.splice(0);
|
|
14
|
+
|
|
15
|
+
for (const listener of list) {
|
|
16
|
+
try {
|
|
17
|
+
const res = listener();
|
|
18
|
+
if (res instanceof Promise) await res;
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.error(e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//region Life cycle
|
|
27
|
+
|
|
28
|
+
declare global {
|
|
29
|
+
var jopiHotReload: HotReloadType;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const gOnServerSideReady: Listener[] = [];
|
|
33
|
+
const gOnAppExiting: Listener[] = [];
|
|
34
|
+
const gOnAppExited: Listener[] = [];
|
|
35
|
+
const gOnAppStart: Listener[] = [];
|
|
36
|
+
let gIsServerSideReady = !(isNodeJS || isBunJS);
|
|
37
|
+
|
|
38
|
+
let gIsHotReload = globalThis.jopiHotReload !== undefined;
|
|
39
|
+
let gIsAppStarted = false;
|
|
40
|
+
|
|
41
|
+
export interface HotReloadType {
|
|
42
|
+
onHotReload: Listener[];
|
|
43
|
+
memory: { [key: string]: any };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (gIsHotReload) {
|
|
47
|
+
execListeners(globalThis.jopiHotReload.onHotReload).then();
|
|
48
|
+
} else {
|
|
49
|
+
globalThis.jopiHotReload = {
|
|
50
|
+
onHotReload: [],
|
|
51
|
+
memory: {}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const gOnHotReload = globalThis.jopiHotReload.onHotReload;
|
|
56
|
+
const gMemory = globalThis.jopiHotReload.memory;
|
|
57
|
+
|
|
58
|
+
export function onServerSideReady(listener: Listener) {
|
|
59
|
+
if (gIsServerSideReady) listener();
|
|
60
|
+
else gOnServerSideReady.push(listener);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function waitServerSideReady() {
|
|
64
|
+
if (gIsServerSideReady) {
|
|
65
|
+
return Promise.resolve();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new Promise<void>(r => {
|
|
69
|
+
onServerSideReady(r);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function declareServerSideReady() {
|
|
74
|
+
gIsServerSideReady = true;
|
|
75
|
+
await execListeners(gOnServerSideReady);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function onAppStart(listener: Listener) {
|
|
79
|
+
if (gIsAppStarted) listener();
|
|
80
|
+
else gOnAppStart.push(listener);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function onAppExiting(listener: Listener) {
|
|
84
|
+
if (gIsExited) listener();
|
|
85
|
+
else gOnAppExiting.push(listener);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function onAppExited(listener: Listener) {
|
|
89
|
+
if (gIsExited) listener();
|
|
90
|
+
else gOnAppExited.push(listener);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function declareAppStarted() {
|
|
94
|
+
gIsAppStarted = true;
|
|
95
|
+
await execListeners(gOnAppStart);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export async function declareAppExiting() {
|
|
99
|
+
if (gIsExited) return;
|
|
100
|
+
gIsExited = true;
|
|
101
|
+
|
|
102
|
+
if (isUsingWorker()) {
|
|
103
|
+
// Wait 1 sec, which allows the worker to correctly initialize.
|
|
104
|
+
await jk_timer.tick(1000);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
gIsAppStarted = false;
|
|
108
|
+
|
|
109
|
+
await execListeners(gOnAppExiting);
|
|
110
|
+
|
|
111
|
+
if (isUsingWorker()) {
|
|
112
|
+
// Allows to worker to correctly stop their activity.
|
|
113
|
+
await jk_timer.tick(100);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (!jk_thread.isMainThread) {
|
|
117
|
+
// Allows to worker to correctly stop their activity.
|
|
118
|
+
await jk_timer.tick(50);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
await execListeners(gOnAppExited);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function executeApp(app: Listener) {
|
|
125
|
+
await waitServerSideReady();
|
|
126
|
+
declareAppStarted();
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const res = app();
|
|
130
|
+
if (res instanceof Promise) await res;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
declareAppExiting();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//endregion
|
|
138
|
+
|
|
139
|
+
//region Hot-reload
|
|
140
|
+
|
|
141
|
+
export function onHotReload(listener: Listener) {
|
|
142
|
+
gOnHotReload.push(listener);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function keepOnHotReload<T>(key: string, provider: ()=>T): T {
|
|
146
|
+
let current = gMemory[key];
|
|
147
|
+
if (current!==undefined) return current;
|
|
148
|
+
return gMemory[key] = provider();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function clearHotReloadKey(key: string) {
|
|
152
|
+
delete(gMemory[key]);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//endregion
|
|
156
|
+
|
|
157
|
+
//region Temp dir
|
|
158
|
+
|
|
159
|
+
export function getTempDir(): string {
|
|
160
|
+
if (!gTempDir) {
|
|
161
|
+
gTempDir = jk_fs.resolve(process.cwd(), "temp")!;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return gTempDir;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let gIsExited = false;
|
|
168
|
+
let gTempDir: string|undefined;
|
|
169
|
+
|
|
170
|
+
//endregion
|
|
171
|
+
|
|
172
|
+
//region Resolving
|
|
173
|
+
|
|
174
|
+
export function findNodePackageDir(packageName: string, useLinuxPathFormat: boolean = true): string|undefined {
|
|
175
|
+
let currentDir = jk_fs.dirname(findPackageJson());
|
|
176
|
+
|
|
177
|
+
while (true) {
|
|
178
|
+
const packagePath = jk_fs.join(currentDir, 'node_modules', packageName);
|
|
179
|
+
|
|
180
|
+
if (jk_fs.isDirectorySync(packagePath)) {
|
|
181
|
+
if (useLinuxPathFormat) return jk_fs.win32ToLinuxPath(packagePath);
|
|
182
|
+
return packagePath;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const parentDir = jk_fs.dirname(currentDir);
|
|
186
|
+
|
|
187
|
+
// Reached root directory
|
|
188
|
+
if (parentDir === currentDir) {
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
currentDir = parentDir;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function requireNodePackageDir(packageName: string, useLinuxPathFormat: boolean = true): string {
|
|
199
|
+
let pkgDir = findNodePackageDir(packageName, useLinuxPathFormat);
|
|
200
|
+
if (!pkgDir) throw new Error("Package '" + packageName + "' not found");
|
|
201
|
+
return pkgDir;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function findPackageJsonDir(searchFromDir = getCodeSourceDirHint()): string {
|
|
205
|
+
let pkgJsonPath = findPackageJson(searchFromDir);
|
|
206
|
+
return jk_fs.dirname(pkgJsonPath);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function findPackageJson(searchFromDir = getCodeSourceDirHint()): string {
|
|
210
|
+
if (!searchFromDir && (gPackageJsonPath!==undefined)) return gPackageJsonPath;
|
|
211
|
+
|
|
212
|
+
let currentDir = searchFromDir;
|
|
213
|
+
|
|
214
|
+
while (true) {
|
|
215
|
+
const packagePath = jk_fs.join(currentDir, 'package.json');
|
|
216
|
+
|
|
217
|
+
if (jk_fs.isFileSync(packagePath)) return gPackageJsonPath = packagePath;
|
|
218
|
+
|
|
219
|
+
const parentDir = jk_fs.dirname(currentDir);
|
|
220
|
+
|
|
221
|
+
// Reached root directory
|
|
222
|
+
if (parentDir === currentDir) break;
|
|
223
|
+
|
|
224
|
+
currentDir = parentDir;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
throw "No package.json found."
|
|
228
|
+
}
|
|
229
|
+
//
|
|
230
|
+
let gPackageJsonPath: string|undefined;
|
|
231
|
+
|
|
232
|
+
export function setApplicationMainFile(applicationMainFile: string) {
|
|
233
|
+
gApplicationMainFile = applicationMainFile;
|
|
234
|
+
gCodeSourceDirHint = jk_fs.dirname(applicationMainFile);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function getApplicationMainFile(): string|undefined {
|
|
238
|
+
return gApplicationMainFile;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function getCodeSourceDirHint() {
|
|
242
|
+
if (!gCodeSourceDirHint) return process.cwd();
|
|
243
|
+
return gCodeSourceDirHint;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function getSourceCodeDir(): string {
|
|
247
|
+
if (gSourceCodeDir) return gSourceCodeDir;
|
|
248
|
+
|
|
249
|
+
let pkgJsonPath = findPackageJson();
|
|
250
|
+
let dirName = jk_fs.join(jk_fs.dirname(pkgJsonPath), "src");
|
|
251
|
+
|
|
252
|
+
if (jk_fs.isDirectorySync(dirName)) {
|
|
253
|
+
return gSourceCodeDir = dirName;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return gSourceCodeDir = jk_fs.dirname(pkgJsonPath);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function getCompiledCodeDir(): string {
|
|
260
|
+
if (gCompiledSourcesDir) return gCompiledSourcesDir;
|
|
261
|
+
const sourceCodeDir = getSourceCodeDir();
|
|
262
|
+
|
|
263
|
+
if (!sourceCodeDir.endsWith("src")) {
|
|
264
|
+
return gCompiledSourcesDir = sourceCodeDir;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// This means that it's Bun.js, and it directly uses the TypeScript version.
|
|
268
|
+
if (gApplicationMainFile && isBunJS && gApplicationMainFile.startsWith(sourceCodeDir)) {
|
|
269
|
+
return gCompiledSourcesDir = sourceCodeDir;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
let pkgJsonPath = findPackageJson();
|
|
273
|
+
|
|
274
|
+
let rootDir = jk_fs.dirname(pkgJsonPath);
|
|
275
|
+
|
|
276
|
+
for (let toTest of ["dist", "build", "out"]) {
|
|
277
|
+
if (jk_fs.isDirectorySync(jk_fs.join(rootDir, toTest))) {
|
|
278
|
+
return gCompiledSourcesDir = jk_fs.join(rootDir, toTest);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// No output dir? Assume we compiled on the same dir.
|
|
283
|
+
return rootDir;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function getCompiledFilePathFor(sourceFilePath: string): string {
|
|
287
|
+
const compiledCodeDir = getCompiledCodeDir();
|
|
288
|
+
const sourceCodeDir = getSourceCodeDir();
|
|
289
|
+
|
|
290
|
+
if (!sourceFilePath.startsWith(sourceCodeDir)) {
|
|
291
|
+
throw new Error("jopi-loader - The source file must be in the source code directory: " + sourceFilePath);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
let filePath = sourceFilePath.substring(sourceCodeDir.length);
|
|
295
|
+
|
|
296
|
+
if (isNodeJS && !filePath.endsWith(".js")) {
|
|
297
|
+
let idx = filePath.lastIndexOf(".");
|
|
298
|
+
if (idx !== -1) filePath = filePath.substring(0, idx) + ".js";
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return jk_fs.join(compiledCodeDir, filePath);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function getSourcesCodePathFor(compiledFilePath: string): string {
|
|
305
|
+
const compiledCodeDir = getCompiledCodeDir();
|
|
306
|
+
const sourceCodeDir = getSourceCodeDir();
|
|
307
|
+
|
|
308
|
+
if (!compiledFilePath.startsWith(compiledCodeDir)) {
|
|
309
|
+
throw new Error("jopi-loader - The compiled file must be in the compiled code directory: " + compiledFilePath);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
let filePath = jk_fs.join(sourceCodeDir, compiledFilePath.substring(compiledCodeDir.length));
|
|
313
|
+
|
|
314
|
+
let idx = filePath.lastIndexOf(".");
|
|
315
|
+
if (idx !== -1) filePath = filePath.substring(0, idx);
|
|
316
|
+
|
|
317
|
+
if (jk_fs.isFileSync(filePath + ".tsx")) {
|
|
318
|
+
return filePath + ".tsx";
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (jk_fs.isFileSync(filePath + ".ts")) {
|
|
322
|
+
return filePath + ".ts";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return filePath + ".js";
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function requireSourceOf(scriptPath: string): string {
|
|
329
|
+
let src = searchSourceOf(scriptPath);
|
|
330
|
+
if (!src) throw new Error("Cannot find source of " + scriptPath);
|
|
331
|
+
return src;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Search the source of the component if it's a JavaScript and not a TypeScript.
|
|
336
|
+
* Why? Because EsBuild doesn't work well on already transpiled code.
|
|
337
|
+
*/
|
|
338
|
+
export function searchSourceOf(scriptPath: string): string|undefined {
|
|
339
|
+
function tryResolve(filePath: string, outDir: string) {
|
|
340
|
+
let out = jk_fs.sep + outDir + jk_fs.sep;
|
|
341
|
+
let idx = filePath.lastIndexOf(out);
|
|
342
|
+
|
|
343
|
+
if (idx !== -1) {
|
|
344
|
+
filePath = filePath.slice(0, idx) + jk_fs.sep + "src" + jk_fs.sep + filePath.slice(idx + out.length);
|
|
345
|
+
if (jk_fs.isFileSync(filePath)) return filePath;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let scriptExt = jk_fs.extname(scriptPath);
|
|
352
|
+
|
|
353
|
+
if ((scriptExt===".ts") || (scriptExt===".tsx")) {
|
|
354
|
+
// Is already the source.
|
|
355
|
+
return scriptPath;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const originalScriptPath = scriptPath;
|
|
359
|
+
let isJavascript = (scriptPath.endsWith(".js")||(scriptPath.endsWith(".jsx")));
|
|
360
|
+
|
|
361
|
+
if (isJavascript) {
|
|
362
|
+
// Remove his extension.
|
|
363
|
+
scriptPath = scriptPath.slice(0, -scriptExt.length);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
let tryDirs = ["dist", "build"];
|
|
367
|
+
|
|
368
|
+
for (let toTry of tryDirs) {
|
|
369
|
+
if (isJavascript) {
|
|
370
|
+
let found = tryResolve(scriptPath + ".tsx", toTry);
|
|
371
|
+
if (found) return found;
|
|
372
|
+
|
|
373
|
+
found = tryResolve(scriptPath + ".ts", toTry);
|
|
374
|
+
if (found) return found;
|
|
375
|
+
} else {
|
|
376
|
+
let found = tryResolve(scriptPath, toTry);
|
|
377
|
+
if (found) return found;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return originalScriptPath;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
let gSourceCodeDir: string|undefined;
|
|
385
|
+
let gCodeSourceDirHint: string|undefined;
|
|
386
|
+
let gApplicationMainFile: string|undefined;
|
|
387
|
+
let gCompiledSourcesDir: string|undefined;
|
|
388
|
+
|
|
389
|
+
//endregion
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { declareServerSideReady, declareAppExiting } from "./common.ts";
|
|
2
|
+
declareServerSideReady().then();
|
|
3
|
+
export function init() {
|
|
4
|
+
// When the program exits gracefully
|
|
5
|
+
process.on('exit', function () { return declareAppExiting(); });
|
|
6
|
+
// When the user wants to terminate the program (CTRL+C)
|
|
7
|
+
process.on('SIGINT', function () { return declareAppExiting(); });
|
|
8
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {declareServerSideReady, declareAppExiting} from "./common.ts";
|
|
2
|
+
|
|
3
|
+
declareServerSideReady().then();
|
|
4
|
+
|
|
5
|
+
export function init() {
|
|
6
|
+
// When the program exits gracefully
|
|
7
|
+
process.on('exit', () => declareAppExiting());
|
|
8
|
+
|
|
9
|
+
// When the user wants to terminate the program (CTRL+C)
|
|
10
|
+
process.on('SIGINT', () => declareAppExiting());
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./jBundler_ifServer.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./jBundler_ifServer.ts";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { gunzipSync as n_gunzipSync, gzipSync as n_gzipSync } from "node:zlib";
|
|
2
|
+
import { isNodeJS } from "jopi-toolkit/jk_what";
|
|
3
|
+
function node_gunzipSync(data) {
|
|
4
|
+
return n_gunzipSync(Buffer.from(data));
|
|
5
|
+
}
|
|
6
|
+
function node_gzipSync(data) {
|
|
7
|
+
return n_gzipSync(Buffer.from(data));
|
|
8
|
+
}
|
|
9
|
+
export var gunzipSync = isNodeJS ? node_gunzipSync : Bun.gunzipSync;
|
|
10
|
+
export var gzipSync = isNodeJS ? node_gzipSync : Bun.gzipSync;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {gunzipSync as n_gunzipSync, gzipSync as n_gzipSync} from "node:zlib";
|
|
2
|
+
import {isNodeJS} from "jopi-toolkit/jk_what";
|
|
3
|
+
|
|
4
|
+
type t_gunzipSync = (data: Uint8Array|string) => Buffer;
|
|
5
|
+
type t_gzipSync = (data: Uint8Array|string) => Buffer;
|
|
6
|
+
|
|
7
|
+
function node_gunzipSync(data: Uint8Array|string): Buffer {
|
|
8
|
+
return n_gunzipSync(Buffer.from(data));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function node_gzipSync(data: Uint8Array|string): Buffer {
|
|
12
|
+
return n_gzipSync(Buffer.from(data));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const gunzipSync: t_gunzipSync = isNodeJS ? node_gunzipSync : (Bun.gunzipSync as t_gunzipSync);
|
|
16
|
+
export const gzipSync: t_gzipSync = isNodeJS ? node_gzipSync : (Bun.gzipSync as t_gzipSync);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./jBundler_ifServer.ts";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./jBundler_ifServer.ts";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import {isNodeJS} from "jopi-toolkit/jk_what";
|
|
3
|
+
|
|
4
|
+
function node_md5(text: string): string {
|
|
5
|
+
return crypto.createHash('md5').update(text).digest('hex');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function node_fastHash(text: string): string {
|
|
9
|
+
return crypto.createHash('sha256').update(text).digest('hex');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function bun_fastHash(text: string): string {
|
|
13
|
+
return Bun.hash(text).toString();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function bun_md5(text: string): string {
|
|
17
|
+
return Bun.MD5.hash(text, "hex");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const md5 = isNodeJS ? node_md5 : bun_md5;
|
|
21
|
+
export const fastHash = isNodeJS ? node_fastHash : bun_fastHash;
|