(moduleName: string, nodePath: string | undefined, cwd: string | undefined, tracer: (message: string, verbose?: string) => void)
| 102 | |
| 103 | |
| 104 | export function resolve(moduleName: string, nodePath: string | undefined, cwd: string | undefined, tracer: (message: string, verbose?: string) => void): Thenable<string> { |
| 105 | interface Message { |
| 106 | c: string; |
| 107 | s?: boolean; |
| 108 | a?: any; |
| 109 | r?: any |
| 110 | } |
| 111 | |
| 112 | const nodePathKey: string = 'NODE_PATH'; |
| 113 | |
| 114 | const app: string = [ |
| 115 | "var p = process;", |
| 116 | "p.on('message',function(m){", |
| 117 | "if(m.c==='e'){", |
| 118 | "p.exit(0);", |
| 119 | "}", |
| 120 | "else if(m.c==='rs'){", |
| 121 | "try{", |
| 122 | "var r=require.resolve(m.a);", |
| 123 | "p.send({c:'r',s:true,r:r});", |
| 124 | "}", |
| 125 | "catch(err){", |
| 126 | "p.send({c:'r',s:false});", |
| 127 | "}", |
| 128 | "}", |
| 129 | "});" |
| 130 | ].join(''); |
| 131 | |
| 132 | return new Promise<any>((resolve, reject) => { |
| 133 | let env = process.env; |
| 134 | let newEnv = Object.create(null); |
| 135 | Object.keys(env).forEach(key => newEnv[key] = env[key]); |
| 136 | |
| 137 | if (nodePath) { |
| 138 | if (newEnv[nodePathKey]) { |
| 139 | newEnv[nodePathKey] = nodePath + path.delimiter + newEnv[nodePathKey]; |
| 140 | } else { |
| 141 | newEnv[nodePathKey] = nodePath; |
| 142 | } |
| 143 | if (tracer) { |
| 144 | tracer(`NODE_PATH value is: ${newEnv[nodePathKey]}`); |
| 145 | } |
| 146 | } |
| 147 | newEnv['ATOM_SHELL_INTERNAL_RUN_AS_NODE'] = '1'; |
| 148 | try { |
| 149 | let cp: ChildProcess = fork('', [], <any>{ |
| 150 | cwd: cwd, |
| 151 | env: newEnv, |
| 152 | execArgv: ['-e', app] |
| 153 | }); |
| 154 | cp.on('error', (error: any) => { |
| 155 | reject(error); |
| 156 | }); |
| 157 | cp.on('message', (message: Message) => { |
| 158 | if (message.c === 'r') { |
| 159 | cp.send({ c: 'e' }); |
| 160 | if (message.s) { |
| 161 | resolve(message.r); |
no test coverage detected