-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathNodeUtils.js
More file actions
362 lines (333 loc) · 12.9 KB
/
NodeUtils.js
File metadata and controls
362 lines (333 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
// @INCLUDE_IN_API_DOCS
/*global logger*/
/**
* Generic node util APIs connector. see `src-node/utils.js` for node peer
*/
define(function (require, exports, module) {
const Strings = require("strings"),
NodeConnector = require('NodeConnector');
const UTILS_NODE_CONNECTOR = "ph_utils";
let utilsConnector;
if(Phoenix.isNativeApp) {
// node not available in browser builds!
utilsConnector = NodeConnector.createNodeConnector(UTILS_NODE_CONNECTOR, exports);
}
/**
* Fetches text content from a URL
* This is only available in the native app
*
* @param {string} url
* @param {string} encoding
* @return {Promise<string>}
*/
async function fetchURLText(url, encoding) {
if(!Phoenix.isNativeApp) {
throw new Error("node not available in browser");
}
const {buffer} = await utilsConnector.execPeer("getURLContent", {url});
return iconv.decode(Buffer.from(buffer), encoding);
}
/**
* updates the localized strings in brackets `Strings` to node.
*
* @private
* @return {Promise<boolean>} Promise resolves to true if strings was updated in node, else false(in browser.)
*/
async function _updateNodeLocaleStrings() {
if(!Phoenix.isNativeApp) {
// this does nothing in browser builds.
return false;
}
await utilsConnector.execPeer("setLocaleStrings", Strings);
return true;
}
/**
* Gets the version of the Phoenix binary
* This is only available in the native app
*
* @return {Promise<string>}
*/
async function getPhoenixBinaryVersion() {
if(!Phoenix.isNativeApp) {
throw new Error("getPhoenixBinaryVersion not available in browser");
}
const cliArgs = await window.__TAURI__.invoke('_get_commandline_args');
const phoenixBinPath = cliArgs[0];
return utilsConnector.execPeer("getPhoenixBinaryVersion", phoenixBinPath);
}
/**
* Retrieves the Linux OS flavor name
* This is only available in the native app on Linux
*
* @return {Promise<string|null>}
*/
async function getLinuxOSFlavorName() {
if(Phoenix.platform !== "linux" || !Phoenix.isNativeApp) {
return null;
}
return utilsConnector.execPeer("getLinuxOSFlavorName");
}
/**
* Opens a URL in the default browser.
* This is only available in the native app.
*
* @param {string} url
* @param {string} browserName
*/
async function openUrlInBrowser(url, browserName) {
if(!Phoenix.isNativeApp) {
throw new Error("openUrlInBrowser not available in browser");
}
if(Phoenix.platform === "win") {
return Phoenix.app._openUrlInBrowserWin(url, browserName);
}
return utilsConnector.execPeer("openUrlInBrowser", {url, browserName});
}
async function _loadNodeExtensionModule(moduleNativeDir) {
if(!Phoenix.isNativeApp) {
throw new Error("_loadNodeExtensionModule not available in browser");
}
return utilsConnector.execPeer("_loadNodeExtensionModule", {moduleNativeDir});
}
async function _npmInstallInFolder(moduleNativeDir) {
if(!Phoenix.isNativeApp) {
throw new Error("_npmInstallInFolder not available in browser");
}
return utilsConnector.execPeer("_npmInstallInFolder", {moduleNativeDir});
}
/**
* Gets an environment variable's value
* This is only available in the native app
*
* @param {string} varName
* @return {Promise<string>}
*/
async function getEnvironmentVariable(varName) {
if(!Phoenix.isNativeApp) {
throw new Error("getEnvironmentVariable not available in browser");
}
return utilsConnector.execPeer("getEnvironmentVariable", varName);
}
/**
* Runs ESLint on a file
* This is only available in the native app
*
* @param {string} text
* @param {string} fullFilePath
* @param {string} projectFullPath
*/
async function ESLintFile(text, fullFilePath, projectFullPath) {
if(!Phoenix.isNativeApp) {
throw new Error("ESLintFile not available in browser");
}
return utilsConnector.execPeer("ESLintFile", {
text,
fullFilePath: window.fs.getTauriPlatformPath(fullFilePath),
projectFullPath: window.fs.getTauriPlatformPath(projectFullPath)
});
}
/**
* Runs ESLint on a file
* This is only available in the native app
*
* @param {string} cwd the working directory of terminal
* @param {boolean} [usePowerShell]
*/
async function openNativeTerminal(cwd, usePowerShell = false) {
if(!Phoenix.isNativeApp) {
throw new Error("openNativeTerminal not available in browser");
}
return utilsConnector.execPeer("openNativeTerminal", {
cwd: window.fs.getTauriPlatformPath(cwd),
usePowerShell
});
}
/**
* Opens a file in the default application for its type on Windows, macOS, and Linux.
*
* @param {string} fullPath - The path to the file/folder to open.
* @returns {Promise<void>} - Resolves if the file/folder is opened successfully, rejects otherwise.
*/
async function openInDefaultApp(fullPath) {
if(!Phoenix.isNativeApp) {
throw new Error("openInDefaultApp not available in browser");
}
return utilsConnector.execPeer("openInDefaultApp", window.fs.getTauriPlatformPath(fullPath));
}
let cachedDeviceID = undefined;
/**
* gets the os device id. this usually won't change till os reinstall.
*
* @returns {Promise<string|null>} - Resolves with the os identifier or null
* @throws {Error} - If called from the browser
*/
async function getDeviceID() {
if (!Phoenix.isNativeApp) {
throw new Error("getDeviceID not available in browser");
}
if(cachedDeviceID !== undefined) {
return cachedDeviceID;
}
try {
cachedDeviceID = await utilsConnector.execPeer("getDeviceID");
return cachedDeviceID;
} catch (err) {
cachedDeviceID = null;
logger.reportError(err, "getDeviceID failed in NodeUtils");
}
return cachedDeviceID;
}
/**
* Enables device license by creating a system-wide license file.
* On Windows, macOS, and Linux this will request elevation if needed.
*
* @returns {Promise<boolean>} - Resolves true if system wide defile file added, else false.
* @throws {Error} - If called from the browser
*/
async function addDeviceLicenseSystemWide() {
if (!Phoenix.isNativeApp) {
throw new Error("addDeviceLicense not available in browser");
}
try {
await utilsConnector.execPeer("addDeviceLicense");
return true;
} catch (err) {
logger.reportError(err, "system wide device license activation failed");
}
return false;
}
/**
* Removes the system-wide device license file.
* On Windows, macOS, and Linux this will request elevation if needed.
*
* @returns {Promise<boolean>} - Resolves true if system wide defile file removed, else false.
* @throws {Error} - If called from the browser
*/
async function removeDeviceLicenseSystemWide() {
if (!Phoenix.isNativeApp) {
throw new Error("removeDeviceLicense not available in browser");
}
try {
await utilsConnector.execPeer("removeDeviceLicense");
return true;
} catch (err) {
logger.reportError(err, "system wide device license remove failed");
}
return false;
}
/**
* Checks if the current machine is configured to check for system-wide device license for all users at app start.
* This validates that the system-wide license file exists, contains valid JSON, and has `licensedDevice: true`.
*
* @returns {Promise<boolean>} - Resolves with `true` if the device is licensed, `false` otherwise.
*/
async function isLicensedDeviceSystemWide() {
if (!Phoenix.isNativeApp) {
console.error("isLicensedDevice not available in browser");
return false;
}
try {
return utilsConnector.execPeer("isLicensedDevice");
} catch (err) {
logger.reportError(err, "system wide device check failed");
}
return false;
}
/**
* Retrieves the operating system username of the current user.
* This method is only available in native apps.
*
* @throws {Error} Throws an error if called in a browser environment.
* @return {Promise<string>} A promise that resolves to the OS username of the current user.
*/
async function getOSUserName() {
if (!Phoenix.isNativeApp) {
throw new Error("getOSUserName not available in browser");
}
return utilsConnector.execPeer("getOSUserName");
}
let _systemSettingsDir;
/**
* Retrieves the directory path for system settings. This method is applicable to native apps only.
*
* @return {Promise<string>} A promise that resolves to the path of the system settings directory.
* @throws {Error} If the method is called in browser app.
*/
async function getSystemSettingsDir() {
if (!Phoenix.isNativeApp) {
throw new Error("getSystemSettingsDir is sudo folder is win/linux/mac. not available in browser.");
}
if(!_systemSettingsDir){
_systemSettingsDir = await utilsConnector.execPeer("getSystemSettingsDir");
}
return _systemSettingsDir;
}
if(NodeConnector.isNodeAvailable()) {
// todo we need to update the strings if a user extension adds its translations. Since we dont support
// node extensions for now, should consider when we support node extensions.
_updateNodeLocaleStrings();
}
try {
if(Phoenix.isTestWindow) {
if(Phoenix.isNativeApp) {
async function _setIsTestWindowGitHubActions() {
const actionsEnv = await utilsConnector.execPeer("getEnvironmentVariable", "GITHUB_ACTIONS");
Phoenix.isTestWindowGitHubActions = !!actionsEnv;
}
_setIsTestWindowGitHubActions().catch(e=>{
console.error("Error setting Phoenix.isTestWindowGitHubActions", e);
});
} else {
const urlSearchParams = new window.URLSearchParams(window.location.search || "");
Phoenix.isTestWindowGitHubActions = urlSearchParams.get("isTestWindowGitHubActions") === "yes";
}
}
} catch (e) {
console.error("Error setting Phoenix.isTestWindowGitHubActions", e);
}
// private apis
exports._loadNodeExtensionModule = _loadNodeExtensionModule;
exports._npmInstallInFolder = _npmInstallInFolder;
// public apis
exports.fetchURLText = fetchURLText;
exports.getPhoenixBinaryVersion = getPhoenixBinaryVersion;
exports.getLinuxOSFlavorName = getLinuxOSFlavorName;
exports.openUrlInBrowser = openUrlInBrowser;
exports.ESLintFile = ESLintFile;
exports.getEnvironmentVariable = getEnvironmentVariable;
exports.openNativeTerminal = openNativeTerminal;
exports.openInDefaultApp = openInDefaultApp;
exports.addDeviceLicenseSystemWide = addDeviceLicenseSystemWide;
exports.removeDeviceLicenseSystemWide = removeDeviceLicenseSystemWide;
exports.isLicensedDeviceSystemWide = isLicensedDeviceSystemWide;
exports.getDeviceID = getDeviceID;
exports.getOSUserName = getOSUserName;
exports.getSystemSettingsDir = getSystemSettingsDir;
/**
* checks if Node connector is ready
*
* @return {boolean} returns true if it's ready, otherwise false
*/
exports.isNodeReady = NodeConnector.isNodeReady;
window.NodeUtils = exports;
});