@@ -72,8 +72,6 @@ public class NewTSPModuleCmdlet : DevCmdletBase
7272 /// Consider updating the logic to use a more unique name if this becomes an issue.
7373 /// </summary>
7474 private const string tempDirName = "TempTypeSpecFiles" ;
75-
76- private string _npmPath = "" ;
7775
7876 [ Parameter ( HelpMessage = "The location of the TSP config file (can be a URL or local path). Will look for `tsp-location.yaml` in current directory if not provided." ) ]
7977 public string TSPLocation { get ; set ; }
@@ -268,19 +266,24 @@ persist tsp-location.yaml to Emitter output directory
268266 File . WriteAllText ( Path . Combine ( emitterOutputDir , "tsp-location.yaml" ) , YamlHelper . Serialize ( tspLocationData ) ) ;
269267
270268 string emitterPackageJsonPath = Path . Combine ( RepoRoot , "eng" , "emitter-package.json" ) ;
271- File . Copy ( emitterPackageJsonPath , Path . Combine ( Path . GetDirectoryName ( tempTSPLocation ) , "package.json" ) , true ) ;
269+ tempTSPLocation = Path . GetDirectoryName ( tempTSPLocation ) ;
270+ File . Copy ( emitterPackageJsonPath , Path . Combine ( tempTSPLocation , "package.json" ) , true ) ;
272271
273272 /*
274273 emit from tempTSPLocation
275274 */
276275 try
277276 {
278- InstallDependencies ( Path . GetDirectoryName ( tempTSPLocation ) ) . Wait ( ) ;
279- RunCommand ( FindNPMCommandFromPath ( "tsp" ) , $ "compile ./ --emit { EmitterPath ?? emitterName } --output-dir { emitterOutputDir } ", Path . GetDirectoryName ( tempTSPLocation ) ) . Wait ( ) ;
277+ if ( ! File . Exists ( Path . Combine ( tempTSPLocation , "package.json" ) ) )
278+ {
279+ throw new FileNotFoundException ( $ "package.json not found in { tempTSPLocation } ") ;
280+ }
281+ RunCommand ( FindCommandFromPath ( "npm" ) , File . Exists ( Path . Combine ( tempTSPLocation , "package-lock.json" ) ) ? "ci" : "install" , tempTSPLocation ) . Wait ( ) ;
282+ RunCommand ( "node" , $ "{ Path . Combine ( "node_modules" , "@typespec" , "compiler" , "cmd" , "tsp" ) } compile ./ --emit { EmitterPath ?? emitterName } --output-dir { emitterOutputDir } ", tempTSPLocation ) . Wait ( ) ;
280283 }
281284 catch ( Exception ex )
282285 {
283- throw new InvalidOperationException ( $ "Failed to emit from TSP config [{ tempTSPLocation } ]: { ex . Message } ", ex ) ;
286+ throw new InvalidOperationException ( $ "Failed to emit from TSP config [{ Path . Combine ( tempTSPLocation , "tspconfig.yaml" ) } ]: { ex . Message } ", ex ) ;
284287 }
285288 finally
286289 {
@@ -299,22 +302,34 @@ emit from tempTSPLocation
299302 }
300303 }
301304
302- private string FindNPMCommandFromPath ( string command )
305+ private string FindCommandFromPath ( string command )
303306 {
304- string commandSuffix = Environment . OSVersion . Platform == PlatformID . Win32NT ? ".cmd" : "" ;
305- if ( string . IsNullOrEmpty ( _npmPath ) || ! File . Exists ( _npmPath ) )
306- {
307- string pathEnv = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
308- string npmPath = pathEnv . Split ( Path . PathSeparator ) . FirstOrDefault ( path => path . EndsWith ( "npm" ) ) ;
309- _npmPath = npmPath ;
310- }
311- string commandPath = Path . Combine ( _npmPath , command + commandSuffix ) ;
312- if ( ! File . Exists ( commandPath ) )
307+ string pathEnv = Environment . GetEnvironmentVariable ( "PATH" ) ?? string . Empty ;
308+ string [ ] paths = pathEnv . Split ( Path . PathSeparator ) ;
309+ string [ ] extensions = Environment . OSVersion . Platform == PlatformID . Win32NT
310+ ? new [ ] { ".cmd" , ".exe" , ".bat" , "" }
311+ : new [ ] { "" } ;
312+
313+ foreach ( string path in paths )
313314 {
314-
315- throw new FileNotFoundException ( $ "Command '{ command } ' not found in system PATH.") ;
315+ foreach ( string extension in extensions )
316+ {
317+ try
318+ {
319+ string fullPath = Path . Combine ( path , command + extension ) ;
320+ if ( File . Exists ( fullPath ) )
321+ {
322+ return fullPath ;
323+ }
324+ }
325+ catch
326+ {
327+ // Ignore invalid paths in PATH
328+ }
329+ }
316330 }
317- return commandPath ;
331+
332+ throw new FileNotFoundException ( $ "Command '{ command } ' not found in system PATH.") ;
318333 }
319334
320335 private string NormalizePath ( string path ) => path . Replace ( '/' , Path . DirectorySeparatorChar ) . Replace ( '\\ ' , Path . DirectorySeparatorChar ) ;
@@ -333,16 +348,6 @@ private void ForceDeleteDir(string path)
333348 Directory . Delete ( path , true ) ;
334349 }
335350
336- private async Task InstallDependencies ( string workingDirectory )
337- {
338- if ( ! File . Exists ( Path . Combine ( workingDirectory , "package.json" ) ) )
339- {
340- throw new FileNotFoundException ( $ "package.json not found in { workingDirectory } ") ;
341- }
342- string args = File . Exists ( Path . Combine ( workingDirectory , "package-lock.json" ) ) ? "ci" : "install" ;
343- await RunCommand ( FindNPMCommandFromPath ( "npm" ) , args , workingDirectory ) ;
344- }
345-
346351 private ( string , string , string , string ) ResolveTSPConfigUri ( string uri )
347352 {
348353 Match match = Regex . Match ( uri , UriRegex , RegexOptions . IgnoreCase ) ;
@@ -541,15 +546,19 @@ private string ConstructTSPConfigUriFromTSPLocation(string tspLocationPath, (str
541546 {
542547 Dictionary < string , object > tspLocationPWDContent = YamlHelper . Deserialize < Dictionary < string , object > > ( File . ReadAllText ( tspLocationPath ) ) ;
543548 //if tspconfig emitted previously was from local, only record the absolute directory name
549+ ( string RemoteDirectory , string RemoteCommit , string RemoteRepositoryName , string RemoteForkName ) = remoteInfo ;
550+ //if tspconfig emitted previously was from local, only record the absolute directory name
544551 if ( File . Exists ( ( string ) tspLocationPWDContent [ "directory" ] ) && string . IsNullOrEmpty ( ( string ) tspLocationPWDContent [ "repo" ] ) && string . IsNullOrEmpty ( ( string ) tspLocationPWDContent [ "commit" ] ) )
545552 {
546- if ( remoteInfo != ( null , null , null , null ) )
553+ if ( ! string . IsNullOrEmpty ( RemoteDirectory ) ||
554+ ! string . IsNullOrEmpty ( RemoteCommit ) ||
555+ ! string . IsNullOrEmpty ( RemoteRepositoryName ) ||
556+ ! string . IsNullOrEmpty ( RemoteForkName ) )
547557 {
548558 throw new ArgumentException ( "Emitted by local TSP last time, cannot update by remote info. Please provide remote `-TSPLocation`." ) ;
549559 }
550560 return ( string ) tspLocationPWDContent [ "directory" ] ;
551561 }
552- ( string RemoteDirectory , string RemoteCommit , string RemoteRepositoryName , string RemoteForkName ) = remoteInfo ;
553562 //otherwise it was from remote, construct its url
554563 string repo = ! string . IsNullOrEmpty ( RemoteForkName ) ? $ "{ RemoteForkName } /azure-rest-api-specs" : ( ! string . IsNullOrEmpty ( RemoteRepositoryName ) ? RemoteRepositoryName : ( string ) tspLocationPWDContent [ "repo" ] ) ;
555564 string commit = ! string . IsNullOrEmpty ( RemoteCommit ) ? RemoteCommit : ( string ) tspLocationPWDContent [ "commit" ] ;
0 commit comments