Skip to content

Commit cfcf80a

Browse files
committed
fix: enhance cross-platform compatibility for temp directory and command execution
1 parent 0c5c808 commit cfcf80a

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

adminforth/modules/codeInjector.ts

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

2227
function stripAnsiCodes(str) {
@@ -62,7 +67,11 @@ function hashify(obj) {
6267
function notifyWatcherIssue(limit) {
6368
console.log('Ran out of file handles after watching %s files.', limit);
6469
console.log('Falling back to polling which uses more CPU.');
65-
console.log('Run ulimit -n 10000 to increase the limit for open files.');
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+
}
6675
}
6776

6877
class CodeInjector implements ICodeInjector {
@@ -112,7 +121,7 @@ class CodeInjector implements ICodeInjector {
112121
envOverrides?: { [key: string]: string }
113122
}) {
114123
const nodeBinary = process.execPath; // Path to the Node.js binary running this script
115-
const npmPath = path.join(path.dirname(nodeBinary), 'npm'); // Path to the npm executable
124+
const npmPath = path.join(path.dirname(nodeBinary), process.platform === 'win32' ? 'npm.cmd' : 'npm'); // Cross-platform npm executable
116125
const env = {
117126
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
118127
FORCE_COLOR: '1',
@@ -123,7 +132,13 @@ class CodeInjector implements ICodeInjector {
123132
console.log(`⚙️ exec: npm ${command}`);
124133
process.env.HEAVY_DEBUG && console.log(`🪲 npm ${command} cwd:`, cwd);
125134
process.env.HEAVY_DEBUG && console.time(`npm ${command} done in`);
126-
const { stdout: out, stderr: err } = await execAsync(`${nodeBinary} ${npmPath} ${command}`, {
135+
136+
// Cross-platform command execution
137+
const commandToExecute = process.platform === 'win32'
138+
? `"${nodeBinary}" "${npmPath}" ${command}`
139+
: `${nodeBinary} ${npmPath} ${command}`;
140+
141+
const { stdout: out, stderr: err } = await execAsync(commandToExecute, {
127142
cwd,
128143
env,
129144
});
@@ -317,9 +332,14 @@ class CodeInjector implements ICodeInjector {
317332

318333
await fsExtra.copy(spaDir, this.spaTmpPath(), {
319334
filter: (src) => {
320-
// /adminforth/* used for local development and /dist/* used for production
321-
const filterPasses = !src.includes('/adminforth/spa/node_modules') && !src.includes('/adminforth/spa/dist')
322-
&& !src.includes('/dist/spa/node_modules') && !src.includes('/dist/spa/dist');
335+
// Cross-platform path filtering for adminforth/* used for local development and /dist/* used for production
336+
const adminforthSpaNodeModules = path.join('adminforth', 'spa', 'node_modules');
337+
const adminforthSpaDist = path.join('adminforth', 'spa', 'dist');
338+
const distSpaNodeModules = path.join('dist', 'spa', 'node_modules');
339+
const distSpaDist = path.join('dist', 'spa', 'dist');
340+
341+
const filterPasses = !src.includes(adminforthSpaNodeModules) && !src.includes(adminforthSpaDist)
342+
&& !src.includes(distSpaNodeModules) && !src.includes(distSpaDist);
323343
if (process.env.HEAVY_DEBUG && !filterPasses) {
324344
console.log('🪲⚙️ fsExtra.copy filtered out', src);
325345
}
@@ -357,8 +377,8 @@ class CodeInjector implements ICodeInjector {
357377
await fsExtra.copy(src, to, {
358378
recursive: true,
359379
dereference: true,
360-
// exclue if node_modules comes after /custom/ in path
361-
filter: (src) => !src.includes('/custom/node_modules'),
380+
// exclude if node_modules comes after /custom/ in path
381+
filter: (src) => !src.includes(path.join('custom', 'node_modules')),
362382
});
363383
}
364384

@@ -514,9 +534,9 @@ class CodeInjector implements ICodeInjector {
514534
// we dont't need to add baseUrl in front of assets here, because it is already added by Vite/Vue
515535
indexHtmlContent = indexHtmlContent.replace(
516536
'/* IMPORTANT:ADMINFORTH FAVICON */',
517-
this.adminforth.config.customization.favicon?.replace('@@/', `/assets/`)
537+
this.adminforth.config.customization.favicon?.replace('@@/', '/assets/')
518538
||
519-
`/assets/favicon.png`
539+
'/assets/favicon.png'
520540
);
521541
await fs.promises.writeFile(indexHtmlPath, indexHtmlContent);
522542

@@ -668,7 +688,7 @@ class CodeInjector implements ICodeInjector {
668688
'change',
669689
async (file) => {
670690
process.env.HEAVY_DEBUG && console.log(`🐛 File ${file} changed (SPA), preparing sources...`);
671-
await this.updatePartials({ filesUpdated: [file.replace(spaPath + '/', '')] });
691+
await this.updatePartials({ filesUpdated: [file.replace(spaPath + path.sep, '')] });
672692
}
673693
)
674694
watcher.on('fallback', notifyWatcherIssue);
@@ -725,7 +745,7 @@ class CodeInjector implements ICodeInjector {
725745
'change',
726746
async (fileOrDir) => {
727747
// copy one file
728-
const relativeFilename = fileOrDir.replace(customComponentsDir + '/', '');
748+
const relativeFilename = fileOrDir.replace(customComponentsDir + path.sep, '');
729749
if (process.env.HEAVY_DEBUG) {
730750
console.log(`🔎 fileOrDir ${fileOrDir} changed`);
731751
console.log(`🔎 relativeFilename ${relativeFilename}`);
@@ -868,14 +888,14 @@ class CodeInjector implements ICodeInjector {
868888
const command = 'run dev';
869889
console.log(`⚙️ spawn: npm ${command}...`);
870890
const nodeBinary = process.execPath;
871-
const npmPath = path.join(path.dirname(nodeBinary), 'npm');
891+
const npmPath = path.join(path.dirname(nodeBinary), process.platform === 'win32' ? 'npm.cmd' : 'npm');
872892
const env = {
873893
VITE_ADMINFORTH_PUBLIC_PATH: this.adminforth.config.baseUrl,
874894
FORCE_COLOR: '1',
875895
...process.env,
876896
};
877897

878-
const devServer = spawn(`${nodeBinary}`, [`${npmPath}`, ...command.split(' ')], {
898+
const devServer = spawn(nodeBinary, [npmPath, ...command.split(' ')], {
879899
cwd,
880900
env,
881901
});

0 commit comments

Comments
 (0)