onejs-core 2.0.3 → 2.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/bin/oj.js +129 -1
- package/definitions/globals.d.ts +27 -12
- package/dist/utils/responsive.d.ts +4 -0
- package/dist/utils/responsive.js +23 -0
- package/package.json +1 -1
- package/scripts/postcss/onejs-tw-config.cjs +3 -0
- package/tsconfig.json +1 -0
package/bin/oj.js
CHANGED
|
@@ -1,3 +1,131 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
"use strict"
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
const fs = require("fs")
|
|
5
|
+
const path = require("path")
|
|
6
|
+
const zlib = require("zlib")
|
|
7
|
+
const readline = require("readline")
|
|
8
|
+
const tar = require("tar")
|
|
9
|
+
|
|
10
|
+
/* ───────────── helpers ───────────── */
|
|
11
|
+
|
|
12
|
+
function findAssetsDir(startDir) {
|
|
13
|
+
let dir = startDir
|
|
14
|
+
const { root } = path.parse(dir)
|
|
15
|
+
|
|
16
|
+
while (dir !== root) {
|
|
17
|
+
const probe = path.join(dir, "Assets")
|
|
18
|
+
if (fs.existsSync(probe) && fs.statSync(probe).isDirectory()) return probe
|
|
19
|
+
dir = path.dirname(dir)
|
|
20
|
+
}
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function findTarball(assetsDir) {
|
|
25
|
+
const stack = [assetsDir]
|
|
26
|
+
|
|
27
|
+
while (stack.length) {
|
|
28
|
+
const curr = stack.pop()
|
|
29
|
+
for (const e of fs.readdirSync(curr, { withFileTypes: true })) {
|
|
30
|
+
const p = path.join(curr, e.name)
|
|
31
|
+
if (e.isDirectory()) stack.push(p)
|
|
32
|
+
else if (e.isFile() && e.name === "premade-uis.tgz.bytes") return p
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function askYN(prompt) {
|
|
39
|
+
return new Promise(resolve => {
|
|
40
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
41
|
+
rl.question(`${prompt} [y/N] `, ans => {
|
|
42
|
+
rl.close()
|
|
43
|
+
resolve(/^(y|yes)$/i.test(ans.trim()))
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* ───────────── main ───────────── */
|
|
49
|
+
|
|
50
|
+
(async () => {
|
|
51
|
+
const [, , cmd, target] = process.argv
|
|
52
|
+
|
|
53
|
+
if (cmd !== "add" || !target) {
|
|
54
|
+
console.error("Usage: npx oj add <all|name>")
|
|
55
|
+
process.exit(1)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const assetsDir = findAssetsDir(process.cwd()) || findAssetsDir(__dirname)
|
|
59
|
+
if (!assetsDir) {
|
|
60
|
+
console.error("Assets/ folder not found in any ancestor directory. Make sure you are in a Unity project but outside the Assets/ folder.")
|
|
61
|
+
process.exit(1)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const tarPath = findTarball(assetsDir)
|
|
65
|
+
if (!tarPath) {
|
|
66
|
+
console.error("premade-uis.tgz not found. It's included in the OneJS Asset Store package. So make sure you have that installed somewhere under the Assets/ folder.")
|
|
67
|
+
process.exit(1)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const cwd = process.cwd()
|
|
71
|
+
const compsRoot = path.join(cwd, "comps")
|
|
72
|
+
fs.mkdirSync(compsRoot, { recursive: true })
|
|
73
|
+
|
|
74
|
+
/* ── dry-run to detect conflicts ── */
|
|
75
|
+
const conflictKeys = new Set()
|
|
76
|
+
|
|
77
|
+
await tar.t({
|
|
78
|
+
file: tarPath,
|
|
79
|
+
onentry: entry => {
|
|
80
|
+
const p = entry.path
|
|
81
|
+
if (!p.startsWith("comps/")) return
|
|
82
|
+
|
|
83
|
+
if (target !== "all") {
|
|
84
|
+
const rootFile = p.split("/").length === 2
|
|
85
|
+
const inTarget = p === `comps/${target}` || p.startsWith(`comps/${target}/`)
|
|
86
|
+
if (!rootFile && !inTarget) return
|
|
87
|
+
|
|
88
|
+
const dest = path.join(cwd, p)
|
|
89
|
+
if (fs.existsSync(dest)) {
|
|
90
|
+
conflictKeys.add(rootFile ? p : `comps/${target}`)
|
|
91
|
+
}
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ── "all" mode ──
|
|
96
|
+
// Ask once per top-level folder (comps/echo, comps/casaul, ...)
|
|
97
|
+
const topLevel = p.split("/").slice(0, 2).join("/")
|
|
98
|
+
const dest = path.join(cwd, topLevel)
|
|
99
|
+
if (fs.existsSync(dest)) conflictKeys.add(topLevel)
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
for (const key of conflictKeys) {
|
|
104
|
+
const ok = await askYN(`Overwrite existing ${key}?`)
|
|
105
|
+
if (!ok) {
|
|
106
|
+
console.log("Aborted.")
|
|
107
|
+
process.exit(0)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* ── real extraction ── */
|
|
112
|
+
const filter = p => {
|
|
113
|
+
if (!p.startsWith("comps/")) return false
|
|
114
|
+
if (target === "all") return true
|
|
115
|
+
|
|
116
|
+
const rootFile = p.split("/").length === 2
|
|
117
|
+
return rootFile || p === `comps/${target}` || p.startsWith(`comps/${target}/`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
await tar.x({
|
|
121
|
+
file: tarPath,
|
|
122
|
+
cwd,
|
|
123
|
+
gzip: true,
|
|
124
|
+
filter
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
console.log("Done.")
|
|
128
|
+
})().catch(err => {
|
|
129
|
+
console.error(err)
|
|
130
|
+
process.exit(1)
|
|
131
|
+
})
|
package/definitions/globals.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
type Instance<T> = T extends new (...a: any) => infer I ? I : never
|
|
2
|
+
|
|
3
|
+
type EventNames<T> = {
|
|
4
|
+
[K in keyof T]:
|
|
5
|
+
K extends `add_${infer E}`
|
|
6
|
+
? `remove_${E}` extends keyof T ? E : never
|
|
7
|
+
: never
|
|
8
|
+
}[keyof T]
|
|
9
|
+
|
|
10
|
+
type Handler<T, E extends string> =
|
|
11
|
+
`add_${E}` extends keyof T
|
|
12
|
+
? T[`add_${E}`] extends (arg: infer A) => any
|
|
13
|
+
? A
|
|
14
|
+
: never
|
|
15
|
+
: never
|
|
11
16
|
|
|
12
17
|
declare const onejs: {
|
|
13
18
|
add_onReload(handler: () => void): void
|
|
@@ -20,10 +25,20 @@ declare const onejs: {
|
|
|
20
25
|
// objects: Record<string, any>
|
|
21
26
|
// }
|
|
22
27
|
|
|
23
|
-
subscribe<T, K extends
|
|
28
|
+
subscribe<T, K extends EventNames<T>>(
|
|
24
29
|
eventSource: T,
|
|
25
30
|
eventName: K,
|
|
26
|
-
handler:
|
|
31
|
+
handler: Function
|
|
32
|
+
): CS.System.Action
|
|
33
|
+
|
|
34
|
+
subscribe<
|
|
35
|
+
C extends new (...a: any) => any,
|
|
36
|
+
I extends Instance<C>,
|
|
37
|
+
E extends EventNames<I>
|
|
38
|
+
>(
|
|
39
|
+
eventSource: C,
|
|
40
|
+
eventName: E,
|
|
41
|
+
handler: Function
|
|
27
42
|
): CS.System.Action
|
|
28
43
|
|
|
29
44
|
subscribe(eventName: string, handler: () => void): CS.System.Action
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type Breakpoint = "base" | "xs" | "sm" | "md" | "lg" | "xl";
|
|
2
|
+
export type Responsive = number | Partial<Record<Breakpoint, number>>;
|
|
3
|
+
/** Pick the right value for the current width. */
|
|
4
|
+
export declare function resolveResponsive<T>(val: Responsive, w?: number): number;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const BP = {
|
|
2
|
+
xs: 480,
|
|
3
|
+
sm: 640,
|
|
4
|
+
md: 768,
|
|
5
|
+
lg: 1024,
|
|
6
|
+
xl: 1280,
|
|
7
|
+
};
|
|
8
|
+
/** Current viewport width (UI Toolkit) */
|
|
9
|
+
const getWidth = () => document.body.ve.resolvedStyle.width;
|
|
10
|
+
/** Pick the right value for the current width. */
|
|
11
|
+
export function resolveResponsive(val, w = getWidth()) {
|
|
12
|
+
if (typeof val !== "object")
|
|
13
|
+
return val;
|
|
14
|
+
let picked = val.base;
|
|
15
|
+
for (const [bp, px] of Object.entries(BP)) {
|
|
16
|
+
if (w >= px && val[bp] !== undefined)
|
|
17
|
+
picked = val[bp];
|
|
18
|
+
}
|
|
19
|
+
setTimeout(() => {
|
|
20
|
+
console.log(CS.UnityEngine.Screen.dpi);
|
|
21
|
+
}, 1);
|
|
22
|
+
return picked;
|
|
23
|
+
}
|
package/package.json
CHANGED