@@ -3,6 +3,7 @@ const { exec, execFile } = require('child_process');
33const fs = require ( 'fs' ) ;
44const fsPromise = require ( 'fs' ) . promises ;
55const path = require ( 'path' ) ;
6+ const os = require ( 'os' ) ;
67const { lintFile} = require ( "./ESLint/service" ) ;
78let openModule , open ; // dynamic import when needed
89
@@ -146,6 +147,86 @@ async function _npmInstallInFolder({moduleNativeDir}) {
146147 } ) ;
147148}
148149
150+ /**
151+ * If it's a dir that exists, returns that
152+ * If it's a file, it returns the parent directory if it exists
153+ * If no parent exists, it returns the original path.
154+ *
155+ * @param {string } cwd - The path to validate.
156+ * @returns {string } - An existing directory or the original path.
157+ */
158+ function _getValidDirectory ( cwd ) {
159+ let currentPath = path . resolve ( cwd ) ;
160+ const exists = fs . existsSync ( currentPath ) ;
161+
162+ if ( exists ) {
163+ const isPathDir = fs . statSync ( currentPath ) . isDirectory ( ) ;
164+ if ( isPathDir ) {
165+ return currentPath ;
166+ }
167+ return path . dirname ( currentPath ) ;
168+ }
169+
170+ currentPath = path . dirname ( currentPath ) ;
171+ if ( fs . existsSync ( currentPath ) ) {
172+ return currentPath ;
173+ }
174+
175+ // If no valid directory is found, fallback to the original cwd
176+ return cwd ;
177+ }
178+
179+ /**
180+ * Opens a native terminal window with the specified current working directory.
181+ * Returns a Promise that resolves if the terminal starts successfully, or rejects if it fails.
182+ *
183+ * @param {string } cwd - The directory to open the terminal in.
184+ * @param {boolean } usePowerShell - Whether to use PowerShell instead of cmd on Windows.
185+ * @returns {Promise<void> } - Resolves if the terminal starts, rejects otherwise.
186+ */
187+ function openNativeTerminal ( { cwd, usePowerShell = false } ) {
188+ return new Promise ( ( resolve , reject ) => {
189+ const platform = os . platform ( ) ;
190+ cwd = _getValidDirectory ( cwd ) ;
191+ let command ;
192+
193+ if ( platform === 'win32' ) {
194+ if ( usePowerShell ) {
195+ command = `start powershell -NoExit -Command "Set-Location -Path '${ cwd } '"` ;
196+ } else {
197+ command = `start cmd /K "cd /D ${ cwd } "` ;
198+ }
199+ } else if ( platform === 'darwin' ) {
200+ command = `open -a Terminal "${ cwd } "` ;
201+ } else {
202+ command = `
203+ if command -v gnome-terminal > /dev/null 2>&1; then
204+ gnome-terminal --working-directory="${ cwd } ";
205+ elif command -v konsole > /dev/null 2>&1; then
206+ konsole --workdir "${ cwd } ";
207+ elif command -v xfce4-terminal > /dev/null 2>&1; then
208+ xfce4-terminal --working-directory="${ cwd } ";
209+ elif command -v xterm > /dev/null 2>&1; then
210+ xterm -e "cd '${ cwd } ' && bash";
211+ else
212+ echo "No supported terminal emulator found.";
213+ exit 1;
214+ fi
215+ ` ;
216+ }
217+
218+ // Execute the terminal command
219+ exec ( command , ( error ) => {
220+ if ( error ) {
221+ reject ( new Error ( `Failed to start terminal: ${ error . message } ` ) ) ;
222+ } else {
223+ resolve ( ) ;
224+ }
225+ } ) ;
226+ } ) ;
227+ }
228+
229+
149230async function ESLintFile ( { text, fullFilePath, projectFullPath} ) {
150231 return lintFile ( text , fullFilePath , projectFullPath ) ;
151232}
@@ -161,5 +242,6 @@ exports.getLinuxOSFlavorName = getLinuxOSFlavorName;
161242exports . openUrlInBrowser = openUrlInBrowser ;
162243exports . getEnvironmentVariable = getEnvironmentVariable ;
163244exports . ESLintFile = ESLintFile ;
245+ exports . openNativeTerminal = openNativeTerminal ;
164246exports . _loadNodeExtensionModule = _loadNodeExtensionModule ;
165247exports . _npmInstallInFolder = _npmInstallInFolder ;
0 commit comments