Skip to content

Commit e0e5038

Browse files
committed
fix: streamline code and enhance cross-platform compatibility in CodeInjector
1 parent e449f95 commit e0e5038

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

adminforth/modules/codeInjector.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ let TMP_DIR;
1616
try {
1717
TMP_DIR = os.tmpdir();
1818
} catch (e) {
19-
// Cross-platform fallback for temp directory
2019
if (process.platform === 'win32') {
2120
TMP_DIR = process.env.TEMP || process.env.TMP || 'C:\\Windows\\Temp';
2221
} else {
2322
TMP_DIR = '/tmp';
24-
}
23+
}//maybe we can consider to use node_modules/.cache/adminforth here instead of tmp
2524
}
2625

2726
function stripAnsiCodes(str) {
@@ -67,11 +66,7 @@ function hashify(obj) {
6766
function notifyWatcherIssue(limit) {
6867
console.log('Ran out of file handles after watching %s files.', limit);
6968
console.log('Falling back to polling which uses more CPU.');
70-
if (process.platform === 'win32') {
71-
console.log('On Windows, this is usually handled automatically by the system.');
72-
} else {
73-
console.log('Run ulimit -n 10000 to increase the limit for open files.');
74-
}
69+
console.log('Run ulimit -n 10000 to increase the limit for open files.');
7570
}
7671

7772
class CodeInjector implements ICodeInjector {
@@ -83,14 +78,11 @@ class CodeInjector implements ICodeInjector {
8378
devServerPort: number = null;
8479

8580
spaTmpPath(): string {
86-
const brandSlug = this.adminforth.config.customization?.brandNameSlug;
87-
process.env.HEAVY_DEBUG && console.log(`🔧 spaTmpPath() - brandSlug: "${brandSlug}", TMP_DIR: "${TMP_DIR}"`);
81+
const brandSlug = this.adminforth.config.customization.brandNameSlug
8882
if (!brandSlug) {
8983
throw new Error('brandSlug is empty, but it should be populated at least by config Validator ');
9084
}
91-
const fullPath = path.join(TMP_DIR, 'adminforth', brandSlug, 'spa_tmp');
92-
process.env.HEAVY_DEBUG && console.log(`🔧 spaTmpPath() result: "${fullPath}"`);
93-
return fullPath;
85+
return path.join(TMP_DIR, 'adminforth', brandSlug, 'spa_tmp');
9486
}
9587

9688
cleanup() {
@@ -124,7 +116,7 @@ class CodeInjector implements ICodeInjector {
124116
envOverrides?: { [key: string]: string }
125117
}) {
126118
const nodeBinary = process.execPath; // Path to the Node.js binary running this script
127-
const npmPath = path.join(path.dirname(nodeBinary), process.platform === 'win32' ? 'npm.cmd' : 'npm'); // Cross-platform npm executable
119+
const npmPath = path.join(path.dirname(nodeBinary), 'npm'); // Path to the npm executable
128120
const env = {
129121
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
130122
FORCE_COLOR: '1',
@@ -135,13 +127,7 @@ class CodeInjector implements ICodeInjector {
135127
console.log(`⚙️ exec: npm ${command}`);
136128
process.env.HEAVY_DEBUG && console.log(`🪲 npm ${command} cwd:`, cwd);
137129
process.env.HEAVY_DEBUG && console.time(`npm ${command} done in`);
138-
139-
// Cross-platform command execution
140-
const commandToExecute = process.platform === 'win32'
141-
? `"${nodeBinary}" "${npmPath}" ${command}`
142-
: `${nodeBinary} ${npmPath} ${command}`;
143-
144-
const { stdout: out, stderr: err } = await execAsync(commandToExecute, {
130+
const { stdout: out, stderr: err } = await execAsync(`${nodeBinary} ${npmPath} ${command}`, {
145131
cwd,
146132
env,
147133
});
@@ -237,18 +223,12 @@ class CodeInjector implements ICodeInjector {
237223

238224
async prepareSources() {
239225
// collects all files and folders into SPA_TMP_DIR
240-
process.env.HEAVY_DEBUG && console.log(`🔧 prepareSources() started`);
241226

242227
// check spa tmp folder exists and create if not
243228
try {
244-
process.env.HEAVY_DEBUG && console.log(`🔧 Checking if spaTmpPath exists: ${this.spaTmpPath()}`);
245229
await fs.promises.access(this.spaTmpPath(), fs.constants.F_OK);
246-
process.env.HEAVY_DEBUG && console.log(`🔧 spaTmpPath already exists`);
247230
} catch (e) {
248-
process.env.HEAVY_DEBUG && console.log(`🔧 spaTmpPath doesn't exist, creating directory: ${this.spaTmpPath()}`);
249-
await fs.promises.mkdir(this.spaTmpPath(), { recursive: true });
250-
process.env.HEAVY_DEBUG && console.log(`🔧 Successfully created spaTmpPath directory`);
251-
231+
await fs.promises.mkdir(this.spaTmpPath(), { recursive: true });
252232
}
253233

254234
const icons = [];
@@ -341,14 +321,9 @@ class CodeInjector implements ICodeInjector {
341321

342322
await fsExtra.copy(spaDir, this.spaTmpPath(), {
343323
filter: (src) => {
344-
// Cross-platform path filtering for adminforth/* used for local development and /dist/* used for production
345-
const adminforthSpaNodeModules = path.join('adminforth', 'spa', 'node_modules');
346-
const adminforthSpaDist = path.join('adminforth', 'spa', 'dist');
347-
const distSpaNodeModules = path.join('dist', 'spa', 'node_modules');
348-
const distSpaDist = path.join('dist', 'spa', 'dist');
349-
350-
const filterPasses = !src.includes(adminforthSpaNodeModules) && !src.includes(adminforthSpaDist)
351-
&& !src.includes(distSpaNodeModules) && !src.includes(distSpaDist);
324+
// /adminforth/* used for local development and /dist/* used for production
325+
const filterPasses = !src.includes('/adminforth/spa/node_modules') && !src.includes('/adminforth/spa/dist')
326+
&& !src.includes('/dist/spa/node_modules') && !src.includes('/dist/spa/dist');
352327
if (process.env.HEAVY_DEBUG && !filterPasses) {
353328
console.log('🪲⚙️ fsExtra.copy filtered out', src);
354329
}
@@ -543,9 +518,9 @@ class CodeInjector implements ICodeInjector {
543518
// we dont't need to add baseUrl in front of assets here, because it is already added by Vite/Vue
544519
indexHtmlContent = indexHtmlContent.replace(
545520
'/* IMPORTANT:ADMINFORTH FAVICON */',
546-
this.adminforth.config.customization.favicon?.replace('@@/', '/assets/')
521+
this.adminforth.config.customization.favicon?.replace('@@/', `/assets/`)
547522
||
548-
'/assets/favicon.png'
523+
`/assets/favicon.png`
549524
);
550525
await fs.promises.writeFile(indexHtmlPath, indexHtmlContent);
551526

@@ -818,12 +793,7 @@ class CodeInjector implements ICodeInjector {
818793
console.log(`${this.adminforth.formatAdminForth()} Bundling ${hotReload ? 'and listening for changes (🔥 Hotreload)' : ' (no hot reload)'}`);
819794
this.adminforth.runningHotReload = hotReload;
820795

821-
process.env.HEAVY_DEBUG && console.log(`🔧 Starting prepareSources() - Platform: ${process.platform}, TMP_DIR: ${TMP_DIR}`);
822-
process.env.HEAVY_DEBUG && console.log(`🔧 spaTmpPath: ${this.spaTmpPath()}`);
823-
824796
await this.prepareSources();
825-
process.env.HEAVY_DEBUG && console.log(`🔧 prepareSources() completed successfully`);
826-
827797

828798
if (hotReload) {
829799
await Promise.all([
@@ -902,14 +872,14 @@ class CodeInjector implements ICodeInjector {
902872
const command = 'run dev';
903873
console.log(`⚙️ spawn: npm ${command}...`);
904874
const nodeBinary = process.execPath;
905-
const npmPath = path.join(path.dirname(nodeBinary), process.platform === 'win32' ? 'npm.cmd' : 'npm');
875+
const npmPath = path.join(path.dirname(nodeBinary), 'npm');
906876
const env = {
907877
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
908878
FORCE_COLOR: '1',
909879
...process.env,
910880
};
911881

912-
const devServer = spawn(nodeBinary, [npmPath, ...command.split(' ')], {
882+
const devServer = spawn(`${nodeBinary}`, [`${npmPath}`, ...command.split(' ')], {
913883
cwd,
914884
env,
915885
});

0 commit comments

Comments
 (0)