11import { spawn } from 'child_process' ;
22import find from 'find-process' ;
3+ import path from 'path' ;
4+ import fs from 'fs' ;
5+ import which from 'which' ;
36import tcpPortUsed from 'tcp-port-used' ;
47import type { ChildProcess } from 'child_process' ;
58import type { Process } from '../utils/processes' ;
@@ -26,12 +29,10 @@ const CARDANO_WALLET_START_TIMEOUT = 60 * 1000; // 60 seconds | unit: millisecon
2629
2730const CARDANO_WALLET_START_CHECK_INTERVAL = 500 ; // 500 ms | unit: milliseconds
2831
29- const SHELLEY_TEST_DATA = '../../utils/cardano/selfnode' ;
30- const TOKEN_METADATA_REGISTRY = './utils/cardano/selfnode/token-metadata.json' ;
3132const TOKEN_METADATA_SERVER_PORT = 65432 ;
3233const TOKEN_METADATA_SERVER = `http://localhost:${ TOKEN_METADATA_SERVER_PORT } /` ;
3334const TOKEN_METADATA_SERVER_PROCESS_NAME =
34- platform === 'win32'
35+ environment . isWindows
3536 ? 'mock-token-metadata-server.exe'
3637 : 'mock-token-metadata-server' ;
3738export async function CardanoSelfnodeLauncher (
@@ -47,6 +48,17 @@ export async function CardanoSelfnodeLauncher(
4748 processName,
4849 onStop,
4950 } = selfnodeOptions ;
51+
52+ const SHELLEY_TEST_DATA = ( ( ) => {
53+ const binDir = path . dirname ( which . sync ( selfnodeBin ) ) ;
54+ return firstExisting ( 'SHELLEY_TEST_DATA' , [
55+ path . resolve ( path . join ( binDir , 'data' , 'cardano-node-shelley' ) ) , // Windows installer
56+ path . resolve ( path . join ( binDir , '..' , 'Resources' , 'data' , 'cardano-node-shelley' ) ) , // Darwin installer
57+ // Linux installer substitutes SHELLEY_TEST_DATA in the local-cluster Bash wrapper
58+ '../../utils/cardano/selfnode' // nix-shell
59+ ] )
60+ } ) ( ) ;
61+
5062 setupMockTokenMetadataServer ( mockTokenMetadataServerBin ) ;
5163 // @ts -ignore ts-migrate(2322) FIXME: Type '{ pid: number; ppid?: number; uid?: number; ... Remove this comment to see the full error message
5264 const processList : Array < Process > = await find ( 'port' , CARDANO_WALLET_PORT ) ;
@@ -86,11 +98,17 @@ export async function CardanoSelfnodeLauncher(
8698 SHELLEY_TEST_DATA ,
8799 TOKEN_METADATA_SERVER ,
88100 } ,
89- detached : true ,
101+ detached : environment . isDev , // XXX: detaching breaks Windows launching the local-cluster.exe + cardano-launcher
102+ // will not allow you to start another Daedalus when previous local-cluster.exe is running, especially awkward
103+ // on macOS without stdout/stderr logging; I’m not sure if this is ever needed in `nix-shell`, but leaving as-is
104+ // for now – @michalrus
105+
90106 // allows Daedalus to exit independently of selfnode (1/3)
91107 stdio : 'ignore' , // allows Daedalus to exit independently of selfnode (2/3)
92108 } ) ;
93- nodeProcess . unref ( ) ; // allows Daedalus to exit independently of selfnode (3/3)
109+ if ( environment . isDev ) {
110+ nodeProcess . unref ( ) ; // allows Daedalus to exit independently of selfnode (3/3)
111+ }
94112
95113 const node : Selfnode = setupSelfnode ( {
96114 processName,
@@ -118,6 +136,17 @@ export async function CardanoSelfnodeLauncher(
118136 } ) ;
119137}
120138
139+ /**
140+ * Return the first existing file path among candidates. If none exist, return the last one, and log a warning an identifier.
141+ */
142+ const firstExisting = ( identifier : string , candidates : Array < string > ) : string => {
143+ const existing = candidates . filter ( fs . existsSync ) ;
144+ if ( existing . length > 0 ) return existing [ 0 ] ;
145+ const fallback = candidates [ candidates . length - 1 ] ;
146+ logger . warn ( `${ identifier } candidates don’t exist, will use fallback` , { identifier, candidates, fallback } ) ;
147+ return fallback ;
148+ } ;
149+
121150const setupSelfnode = ( {
122151 processName,
123152 nodeProcess,
@@ -148,6 +177,15 @@ const setupSelfnode = ({
148177const setupMockTokenMetadataServer = async (
149178 mockTokenMetadataServerBin : string
150179) => {
180+ const TOKEN_METADATA_REGISTRY = ( ( ) => {
181+ const binDir = path . dirname ( which . sync ( mockTokenMetadataServerBin ) ) ;
182+ return firstExisting ( 'TOKEN_METADATA_REGISTRY' , [
183+ path . resolve ( path . join ( binDir , 'token-metadata.json' ) ) , // Windows and Linux installers
184+ path . resolve ( path . join ( binDir , '..' , 'Resources' , 'token-metadata.json' ) ) , // Darwin installer
185+ './utils/cardano/selfnode/token-metadata.json' // nix-shell
186+ ] ) ;
187+ } ) ( ) ;
188+
151189 // @ts -ignore ts-migrate(2322) FIXME: Type '{ pid: number; ppid?: number; uid?: number; ... Remove this comment to see the full error message
152190 const processList : Array < Process > = await find (
153191 'port' ,
@@ -181,11 +219,13 @@ const setupMockTokenMetadataServer = async (
181219 ] ,
182220 {
183221 env : { ...process . env } ,
184- detached : true ,
222+ detached : environment . isDev ,
185223 stdio : 'ignore' ,
186224 }
187225 ) ;
188- mockTokenMetadataServerProcess . unref ( ) ;
226+ if ( environment . isDev ) {
227+ mockTokenMetadataServerProcess . unref ( ) ;
228+ }
189229 mockTokenMetadataServer = mockTokenMetadataServerProcess ;
190230 }
191231} ;
0 commit comments