Visualize Your Ideas With Code
fix(vite-plugin): create empty output directory if not exist (#787)
SleeklyCoding20cceef8525e
diff --git a/packages/vite-plugin/src/main.ts b/packages/vite-plugin/src/main.tsindex 83b64439..d8bbdbf7 100644--- a/packages/vite-plugin/src/main.ts+++ b/packages/vite-plugin/src/main.ts@@ -395,6 +395,9 @@ export default ({}if (name === '__open-output-path') {+ if (!fs.existsSync(outputPath)) {+ fs.mkdirSync(outputPath, {recursive: true});+ }openInExplorer(outputPath);res.end();return;diff --git a/packages/vite-plugin/src/openInExplorer.ts b/packages/vite-plugin/src/openInExplorer.tsindex b0bf9896..f0aad56b 100644--- a/packages/vite-plugin/src/openInExplorer.ts+++ b/packages/vite-plugin/src/openInExplorer.ts@@ -1,24 +1,41 @@-import {spawn} from 'child_process';+import {execSync, spawn} from 'child_process';import {platform} from 'os';export function openInExplorer(file: string) {- let explorer: string | null = null;+ let command: string | null = null;+ let args = [file];const os = platform();+switch (os) {case 'win32':- explorer = 'explorer';+ command = 'explorer';break;case 'linux':- explorer = 'xdg-open';+ if (isRunningOnWSL()) {+ command = 'bash';+ args = ['-c', `cd ${file} && explorer.exe .`];+ } else {+ command = 'xdg-open';+ }break;case 'darwin':- explorer = 'open';+ command = 'open';break;}- if (explorer) {- spawn(explorer, [file], {detached: true}).unref();+ if (command) {+ spawn(command, args, {detached: true}).unref();} else {console.warn(`Unsupported OS: ${os}`);}}++function isRunningOnWSL(): boolean {+ try {+ const uname = execSync('uname -a').toString().toLowerCase();+ return uname.includes('microsoft');+ } catch (error) {+ console.error(`exec error: ${error}`);+ return false;+ }+}