-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.env.mjs
135 lines (115 loc) · 4.14 KB
/
.env.mjs
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
#!/usr/bin/env node
import dotenv from 'dotenv';
import minimist from 'minimist';
import os from 'os';
import path from 'path';
import process from 'process';
import puppeteer from 'puppeteer';
import upath from 'upath';
import { fileURLToPath } from 'url';
import productionEnv from './.env.build.json' with { type: 'json' };
// Setup `__dirname` and `__filename` equivalents for ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Load environment variables
const { parsed: dotenvConfig } = dotenv.config({ override: true, path: path.join(__dirname, '.env') });
export { dotenvConfig };
export const PROJECT_DIR = productionEnv.PROJECT_DIR;
// Argument parsing
const argv = minimist(process.argv.slice(2));
const is_debug = argv.debug || false;
// System info
const cpuCount = os.cpus().length;
const isGitHubCI = process.env.CI === 'true';
export { argv, cpuCount, is_debug, isGitHubCI };
// Electron and packaging checks
const isElectron = typeof process.versions.electron !== 'undefined';
const isPkg = typeof process.pkg !== 'undefined';
const assets = [
'public/static/**/*',
'src/database.*',
'assets/**/*',
'django_backend/certificates/**/*',
'./assets/chrome-extensions/**/*',
'./assets/database/**/*'
];
export { assets, isElectron };
// Chromium executable path handling
let chromiumExecutablePath = isPkg
? puppeteer
.executablePath()
.replace(/^.*?\/node_modules\/puppeteer\/\.local-chromium/, path.join(path.dirname(process.execPath), 'chromium'))
: puppeteer.executablePath();
if (process.platform === 'win32') {
chromiumExecutablePath = isPkg
? puppeteer
.executablePath()
.replace(
/^.*?\\node_modules\\puppeteer\\\.local-chromium/,
path.join(path.dirname(process.execPath), 'chromium')
)
: puppeteer.executablePath();
}
export { chromiumExecutablePath };
// GitHub token read-only
const githubTokenRO = process.env.GITHUB_TOKEN_READ_ONLY;
export { githubTokenRO };
/**
* Determines whether debug mode is enabled based on environment variables and system hostname.
* @returns {boolean} - True if in debug mode, false otherwise.
*/
const isDebug = (() => {
const isGitHubCI = process.env.CI !== undefined && process.env.GITHUB_ACTIONS === 'true';
const isGitHubCodespaces = process.env.CODESPACES === 'true';
if (isGitHubCI || isGitHubCodespaces) return true;
const debugPC = ['DESKTOP-JVTSJ6I'];
const hostname = os.hostname();
return hostname.startsWith('codespaces-') || debugPC.includes(hostname);
})();
export { isDebug };
/**
* Resolves a file or folder path relative to the project directory.
* @param {...string} paths - Path segments to resolve.
* @returns {string} - Resolved absolute path.
*/
export function getFromProject(...paths) {
const actual = path.join(__dirname, ...paths);
if (os.platform() === 'win32') {
const { root } = path.parse(actual);
const dirWithoutRoot = actual.replace(root, '');
return root.toUpperCase() + dirWithoutRoot;
}
return actual;
}
/**
* Resolves a file or folder path relative to the project directory in UNIX format.
* @param {...string} paths - Path segments to resolve.
* @returns {string} - Resolved path in UNIX format.
*/
export function getFromProjectUnix(...paths) {
return upath.join(__dirname, ...paths);
}
// Directories
const tmpDir = getFromProject('tmp');
const dataDir = getFromProject('data');
const srcDir = path.join('src');
export { dataDir, srcDir, tmpDir };
/**
* Resolves a file or folder path near the executable location.
* If running within Electron, considers whether the app is packaged.
* @param {...string} paths - Path segments to resolve.
* @returns {Promise<string>} - Resolved path.
*/
export async function getFromNearExe(...paths) {
const electron = await import('electron').catch(() => ({}));
if (electron.app) {
const isPackaged = electron.app.isPackaged;
if ((process.env.NODE_ENV === 'development' && !isPackaged) || !isPackaged) {
return path.join(__dirname, ...paths);
} else {
return path.join(process.env.PORTABLE_EXECUTABLE_DIR, ...paths);
}
} else {
return path.join(__dirname, ...paths);
}
}