-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath.js
More file actions
119 lines (105 loc) · 3.7 KB
/
Copy pathpath.js
File metadata and controls
119 lines (105 loc) · 3.7 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
// Node imports
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { setTimeout } from "node:timers/promises";
// Third party imports
import { v4 as uuidv4 } from "uuid";
// Local imports
import { appMode } from "./app_mode.js";
function executablePath(execPath, execName) {
const resourcesPath = process.env.RESOURCES_PATH;
const mode = process.env.MODE;
const nodeEnv = process.env.NODE_ENV;
console.log("[executablePath]", { execPath, execName, mode, nodeEnv, resourcesPath });
if (mode === appMode.DESKTOP && nodeEnv === "production") {
const execPathInResources = path.join(resourcesPath, executableName(execName));
if (fs.existsSync(execPathInResources)) {
console.log(`[executablePath] Found executable in resources path: ${execPathInResources}`);
return execPathInResources;
}
}
const localExecPath = path.join(execPath, executableName(execName));
if (fs.existsSync(localExecPath)) {
console.log(`[executablePath] Found executable in local path: ${localExecPath}`);
return localExecPath;
}
throw new Error(`Executable not found: ${execName}`);
}
function executableName(execName) {
if (process.platform === "win32") {
return `${execName}.exe`;
}
return execName;
}
function createPath(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
console.log(`${dirPath} directory created successfully!`);
}
return dirPath;
}
function generateProjectFolderPath(projectName) {
return path.join(os.tmpdir(), projectName.replaceAll("/", "_"), uuidv4());
}
function extensionFolderPath(projectFolderPath, extensionId) {
return path.join(projectFolderPath, "extensions", extensionId);
}
async function lookForLocalExtensionDistPath(rootPath, extentionRepoName, frontendFile) {
const localExtensionPath = path.join(rootPath, "..", extentionRepoName);
const localExtensionDistPath = path.join(localExtensionPath, "dist");
if (!fs.existsSync(localExtensionDistPath)) {
return;
}
console.log(
`[extensionFrontendPath] Found existing folder: ${localExtensionDistPath}, deleting it...`,
);
fs.rmSync(localExtensionDistPath, { recursive: true, force: true });
const now = new Date();
fs.utimesSync(path.join(localExtensionPath, "package.json"), now, now);
const rebuiltFilePath = path.join(localExtensionDistPath, frontendFile);
const MAX_DELETE_FOLDER_RETRIES = 10;
const MILLISECONDS_PER_RETRY = 1000;
for (let i = 0; i <= MAX_DELETE_FOLDER_RETRIES; i += 1) {
if (fs.existsSync(rebuiltFilePath)) {
console.log(`Found rebuilt file: ${rebuiltFilePath}`);
return rebuiltFilePath;
}
console.log(`Waiting for rebuild... attempt ${i}/${MAX_DELETE_FOLDER_RETRIES}`);
// oxlint-disable-next-line no-await-in-loop
await setTimeout(MILLISECONDS_PER_RETRY);
}
}
async function extensionFrontendPath(unzippedExtensionPath, frontendFile, rootPath, extensionId) {
console.log("[extensionFrontendPath]", {
unzippedExtensionPath,
frontendFile,
rootPath,
extensionId,
});
const extentionRepoName = extensionId
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join("-");
const localFilePath = await lookForLocalExtensionDistPath(
rootPath,
extentionRepoName,
frontendFile,
);
if (localFilePath) {
return localFilePath;
}
const unzippedfrontendFilePath = path.join(unzippedExtensionPath, frontendFile);
if (fs.existsSync(unzippedfrontendFilePath)) {
return unzippedfrontendFilePath;
}
throw new Error(`Failed to find ${unzippedfrontendFilePath}`);
}
export {
createPath,
extensionFrontendPath,
extensionFolderPath,
executablePath,
executableName,
generateProjectFolderPath,
};