onejs-core 3.0.2 → 3.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/package.json +1 -1
  2. package/scripts/switch.cjs +50 -50
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": "3.0.2",
4
+ "version": "3.0.4",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/Singtaa/onejs-core"
@@ -3,7 +3,6 @@ const tar = require('tar')
3
3
  const url = require('url')
4
4
  const path = require('path')
5
5
  const fse = require('fs-extra')
6
- const xml2js = require('xml2js')
7
6
  const { rimraf } = require('rimraf')
8
7
  const ProgressBar = require('progress')
9
8
 
@@ -66,6 +65,9 @@ async function Process(backend, outputDir) {
66
65
 
67
66
  // Safe keep asmdef files, et al.
68
67
  const onejsDir = await getOneJSUnityDir()
68
+ if (!onejsDir) {
69
+ throw new Error('Could not locate the OneJS Unity package directory. See messages above for details.')
70
+ }
69
71
  const a = path.join(onejsDir, 'Puerts/Editor/com.tencent.puerts.core.Editor.asmdef')
70
72
  const b = path.join(upmDir, 'Editor/com.tencent.puerts.core.Editor.asmdef')
71
73
  const c = path.join(onejsDir, 'Puerts/Runtime/com.tencent.puerts.core.asmdef')
@@ -98,9 +100,9 @@ async function Process(backend, outputDir) {
98
100
  // }
99
101
 
100
102
  async function getOneJSUnityDir() {
101
- let oneJSPath = null
103
+ const oneJSKeys = ["com.dragonground.onejs", "com.singtaa.onejs"]
102
104
 
103
- // Step 1: Check manifest.json
105
+ // Step 1: Check manifest.json for local (file:) paths
104
106
  if (fs.existsSync(manifestPath)) {
105
107
  const manifestContent = fs.readFileSync(manifestPath, "utf8")
106
108
  let manifestJson
@@ -110,69 +112,67 @@ async function getOneJSUnityDir() {
110
112
  }
111
113
 
112
114
  const dependencies = manifestJson && manifestJson.dependencies
113
- const oneJSKeys = ["com.dragonground.onejs", "com.singtaa.onejs"]
114
115
 
115
116
  if (dependencies) {
116
117
  for (const key of oneJSKeys) {
117
118
  const packagePath = dependencies[key]
118
119
  if (typeof packagePath === "string") {
119
- const v = packagePath.trim() // e.g., "file:PATH/TO/OneJS"
120
+ const v = packagePath.trim()
120
121
  if (v.startsWith("file:")) {
121
- oneJSPath = path.resolve(projectDir, v.substring(5)) // strip "file:"
122
- return oneJSPath
122
+ // Local path dependency, e.g., "file:PATH/TO/OneJS"
123
+ return path.resolve(projectDir, v.substring(5))
123
124
  }
124
125
  }
125
126
  }
126
127
  }
127
- }
128
128
 
129
- // Step 2: If not found, parse OneJS.Runtime.csproj
130
- if (!oneJSPath) {
131
- const csprojPath = path.join(projectDir, 'OneJS.Runtime.csproj')
132
-
133
- if (fs.existsSync(csprojPath)) {
134
- const csprojContent = fs.readFileSync(csprojPath, 'utf8')
135
- const parser = new xml2js.Parser()
136
-
137
- try {
138
- const result = await parser.parseStringPromise(csprojContent)
139
-
140
- const project = result.Project
141
- const itemGroups = project.ItemGroup
142
-
143
- if (itemGroups && itemGroups.length > 0) {
144
- for (const itemGroup of itemGroups) {
145
- if (itemGroup.Compile) {
146
- for (const compileItem of itemGroup.Compile) {
147
- const includePath = compileItem.$.Include
148
-
149
- // Normalize path separators for cross-platform compatibility
150
- const normalizedIncludePath = includePath.replace(/\\/g, '/')
151
- const searchIndex = normalizedIncludePath.indexOf('OneJS/Runtime/Engine/ScriptEngine.cs')
152
-
153
- if (searchIndex !== -1) {
154
- oneJSPath = normalizedIncludePath.substring(0, searchIndex + 'OneJS'.length)
155
- oneJSPath = path.resolve(projectDir, oneJSPath)
156
- return oneJSPath
157
- }
158
- }
159
- }
160
- }
129
+ // Step 2: Check Library/PackageCache for Git URL or registry dependencies
130
+ const packageCacheDir = path.join(projectDir, 'Library', 'PackageCache')
131
+ if (dependencies && fs.existsSync(packageCacheDir)) {
132
+ for (const key of oneJSKeys) {
133
+ if (typeof dependencies[key] === "string") {
134
+ const found = findPackageInCache(packageCacheDir, key)
135
+ if (found) return found
161
136
  }
162
-
163
- console.error('Could not find OneJS path in csproj file.')
164
- return null
165
- } catch (err) {
166
- console.error('Error parsing csproj file:', err)
167
- return null
168
137
  }
169
- } else {
170
- console.error('OneJS.Runtime.csproj file does not exist.')
171
- return null
172
138
  }
173
139
  }
174
140
 
175
- return oneJSPath
141
+ // Step 3: Last resort - scan PackageCache even without manifest entry
142
+ const packageCacheDir = path.join(projectDir, 'Library', 'PackageCache')
143
+ if (fs.existsSync(packageCacheDir)) {
144
+ for (const key of oneJSKeys) {
145
+ const found = findPackageInCache(packageCacheDir, key)
146
+ if (found) return found
147
+ }
148
+ }
149
+
150
+ console.error(
151
+ 'Could not find OneJS package directory.\n' +
152
+ 'Please make sure OneJS is installed in your Unity project via one of:\n' +
153
+ ' - A local path (file:) dependency in Packages/manifest.json\n' +
154
+ ' - A Git URL dependency in Packages/manifest.json\n' +
155
+ ' - The Unity Package Manager registry\n' +
156
+ 'And that this script is run from the OneJS npm project inside the Unity project.'
157
+ )
158
+ return null
159
+ }
160
+
161
+ function findPackageInCache(packageCacheDir, packageName) {
162
+ try {
163
+ const entries = fs.readdirSync(packageCacheDir)
164
+ for (const entry of entries) {
165
+ if (entry.startsWith(packageName + '@') || entry === packageName) {
166
+ const candidatePath = path.join(packageCacheDir, entry)
167
+ if (fs.statSync(candidatePath).isDirectory()) {
168
+ return candidatePath
169
+ }
170
+ }
171
+ }
172
+ } catch (err) {
173
+ // PackageCache not readable, skip
174
+ }
175
+ return null
176
176
  }
177
177
 
178
178
  function ensureDirectoryExistence(filePath) {