Skip to content
This repository was archived by the owner on Feb 12, 2021. It is now read-only.

Commit 82e4e9e

Browse files
committed
feat: resolve special paths
1 parent 98d274d commit 82e4e9e

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

preload.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const {
99
showOpenDialog
1010
} = require('./utils/renderer/electron')
1111

12+
const { resolveRuntimeDependency } = require('./utils/main/util')
13+
1214
// Enabling spectron integration https://github.com/electron/spectron#node-integration
1315
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
1416
window.electronRequire = require
@@ -98,7 +100,11 @@ const Grid = {
98100
}
99101
},
100102
platform: {
101-
name: process.platform
103+
name: process.platform,
104+
hasRuntime: dependency => {
105+
const result = resolveRuntimeDependency(dependency) !== undefined
106+
return result
107+
}
102108
},
103109
notify,
104110
showOpenDialog,

utils/main/util.js

+42-7
Original file line numberDiff line numberDiff line change
@@ -124,42 +124,77 @@ const checkConnection = async (host, port, timeout = 2000) => {
124124
})
125125
}
126126

127-
const resolveRuntimeDependency = runtimeDependency => {
127+
const resolveRuntimeDependency = (runtimeDependency = {}) => {
128128
const { name, version, type } = runtimeDependency
129129
if (name === 'Java') {
130130
if ('JAVA_HOME' in process.env) {
131-
console.log('found java', process.env['JAVA_HOME'])
132131
const JAVA_HOME = process.env['JAVA_HOME']
133-
// windows: const JAVA_EXE = `${JAVA_HOME}/bin/java.exe`
132+
const JAVA_BIN = path.join(
133+
JAVA_HOME,
134+
'bin',
135+
process.platform === 'win32' ? 'java.exe' : 'java'
136+
)
137+
return fs.existsSync(JAVA_BIN) ? JAVA_BIN : undefined
134138
} else {
135139
// MAC:
136140
if (process.platform === 'darwin') {
137141
if (fs.existsSync('/Library/Java/JavaVirtualMachines/')) {
138142
const vms = fs.readdirSync('/Library/Java/JavaVirtualMachines/')
139143
// /Contents/Home/bin/java
140-
console.log('found vms', vms)
144+
// console.log('found vms', vms)
141145
}
142146
// alternative tests
143147
// /usr/bin/java
144148
// /usr/libexec/java_home -V
145149
// execute 'which java'
146150
const javaPath = '/usr/bin/java'
147-
return javaPath
151+
return fs.existsSync(javaPath) ? javaPath : undefined
148152
}
149-
console.log('JAVA_HOME not set')
150153
// console.log(process.env.PATH.includes('java'))
151154
}
152155
return undefined
153156
}
154157
return undefined
155158
}
156159

160+
const resolvePackagePath = (flag, release) => {
161+
// NOTE order important
162+
// more specific matcher
163+
const PACKAGE_PATH = release.extractedPackagePath
164+
if (flag.includes('%PACKAGE_PATH/*%')) {
165+
if (!PACKAGE_PATH) throw new Error('cannot resolve %PACKAGE_PATH%')
166+
// console.log('rewrite path in flag', flag)
167+
const files = fs.readdirSync(PACKAGE_PATH)
168+
const subdirs = files.filter(
169+
f => fs.lstatSync(path.join(PACKAGE_PATH, f)).isDirectory
170+
)
171+
if (subdirs.length === 0) {
172+
throw new Error('cannot resolve path for flag', flag)
173+
}
174+
if (subdirs.length > 1) {
175+
console.warn('WARNING: ambiguous paths found for', flag, subdirs)
176+
}
177+
const PACKAGE_SUBDIR = subdirs[0]
178+
flag = flag.replace(
179+
'%PACKAGE_PATH/*%',
180+
path.join(PACKAGE_PATH, PACKAGE_SUBDIR)
181+
)
182+
}
183+
// less specific matcher
184+
if (flag.includes('%PACKAGE_PATH%')) {
185+
if (!PACKAGE_PATH) throw new Error('cannot resolve %PACKAGE_PATH%')
186+
flag = flag.replace('%PACKAGE_PATH%', PACKAGE_PATH)
187+
}
188+
return flag
189+
}
190+
157191
module.exports = {
158192
checkConnection,
159193
getShippedGridUiPath,
160194
getCachePath,
161195
getUserDataPath,
162196
getPluginCachePath,
163197
getBinaryUpdater,
164-
resolveRuntimeDependency
198+
resolveRuntimeDependency,
199+
resolvePackagePath
165200
}

0 commit comments

Comments
 (0)