onejs-core 2.0.2 → 2.0.4

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.
Files changed (2) hide show
  1. package/bin/oj.js +130 -2
  2. package/package.json +1 -1
package/bin/oj.js CHANGED
@@ -1,3 +1,131 @@
1
1
  #!/usr/bin/env node
2
-
3
- console.log("ojojoj")
2
+ "use strict"
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "onejs-core",
3
3
  "description": "The JS part of OneJS, a UI framework and Scripting Engine for Unity.",
4
- "version": "2.0.2",
4
+ "version": "2.0.4",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./typings.d.ts",
7
7
  "bin": {