@@ -74,8 +74,9 @@ export async function stopDetachedInstance(
7474 process . kill ( instance . pid , "SIGTERM" ) ;
7575 } catch ( err ) {
7676 // process.kill throws if the process was not found (or was a group process in Windows)
77- removeDetachedInstanceFile ( instance . pid ) ;
7877 return false ;
78+ } finally {
79+ removeDetachedInstanceFile ( instance . pid ) ;
7980 }
8081 return true ;
8182 }
@@ -166,17 +167,17 @@ export async function startDetachedInstance(
166167/**
167168 * Fetch all instance of Ganache running in detached mode. Cleans up any
168169 * instance files for processes that are no longer running.
169- * @returns Promise<DetachedInstance[]> resolves with an array of instances
170+ * @returns { Promise<DetachedInstance[]> } resolves with an array of instances
170171 */
171172export async function getDetachedInstances ( ) : Promise < DetachedInstance [ ] > {
172173 const files = readdirSync ( dataPath ) ;
173174 const instances : DetachedInstance [ ] = [ ] ;
175+ const processes = await psList ( ) ;
174176
175177 for ( let i = 0 ; i < files . length ; i ++ ) {
176178 const filename = files [ i ] ;
177179 const pid = parseInt ( filename ) ;
178180
179- const processes = await psList ( ) ;
180181 const foundProcess = processes . find ( p => p . pid === pid ) ;
181182
182183 let shouldRemoveFile = false ;
@@ -206,6 +207,8 @@ export async function getDetachedInstances(): Promise<DetachedInstance[]> {
206207 if ( shouldRemoveFile ) removeDetachedInstanceFile ( pid ) ;
207208 }
208209
210+ instances . sort ( ( a , b ) => b . startTime - a . startTime ) ;
211+
209212 return instances ;
210213}
211214
@@ -222,7 +225,7 @@ async function findDetachedInstanceByName(
222225}
223226
224227/**
225- * Flattens parsed, and namespaced args into an array of arguments to be passed
228+ * Flattens parsed and namespaced args into an array of arguments to be passed
226229 * to a child process. This handles "special" arguments, such as "action",
227230 * "flavor" and "--detach".
228231 * @param {object } args to be flattened
0 commit comments