Skip to content

Commit

Permalink
[NPM] Fixes in handling update.
Browse files Browse the repository at this point in the history
  • Loading branch information
torbacz committed Jan 7, 2025
1 parent 1bb6803 commit bfabf4d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/DependencyUpdated.Projects.Npm/Models/PackageRoot.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace DependencyUpdated.Projects.Npm.Models;

public record PackageRoot(Dictionary<string, string> Dependencies, Dictionary<string, string> DevDependencies);
public record PackageRoot(Dictionary<string, string>? Dependencies, Dictionary<string, string>? DevDependencies);
33 changes: 27 additions & 6 deletions src/DependencyUpdated.Projects.Npm/NpmUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ internal sealed class NpmUpdater : IProjectUpdater

public IReadOnlyCollection<string> GetAllProjectFiles(string searchPath)
{
return ValidNpmPatterns.SelectMany(dotnetPattern =>
var allPaths = ValidNpmPatterns.SelectMany(dotnetPattern =>
Directory.GetFiles(searchPath, dotnetPattern, SearchOption.AllDirectories)).ToList();
allPaths.RemoveAll(x => x.Contains("node_modules"));
allPaths.RemoveAll(x => x.Contains("dist"));
return allPaths;
}

public IReadOnlyCollection<UpdateResult> HandleProjectUpdate(IReadOnlyCollection<string> fullPath,
Expand All @@ -45,6 +48,11 @@ public IReadOnlyCollection<UpdateResult> HandleProjectUpdate(IReadOnlyCollection
var projectDeps = ParseProject(path);
foreach (var dependency in dependenciesToUpdate)
{
if (projectDeps.All(x => x.Name != dependency.Name))
{
continue;
}

var process = GetProcess(directory, dependency);
process.WaitForExit();
var oldDep = projectDeps.First(x => x.Name == dependency.Name);
Expand Down Expand Up @@ -182,13 +190,26 @@ private static HashSet<DependencyDetails> ParseProject(string path)
return new HashSet<DependencyDetails>();
}

var dependencies = package.Dependencies
.Where(x => IsValidVersion(x.Value))
.Select(d => new DependencyDetails(d.Key, ParseVersion(d.Value))).ToList();
var devDependencies = package.DevDependencies
var dependencies = ConvertToDependencies(package.Dependencies);
var devDependencies = ConvertToDependencies(package.DevDependencies);
return dependencies.Concat(devDependencies).ToHashSet();
}

private static ICollection<DependencyDetails> ConvertToDependencies(Dictionary<string, string>? dependencies)
{
if (dependencies is null)
{
return Array.Empty<DependencyDetails>();
}

if (dependencies.Count == 0)
{
return Array.Empty<DependencyDetails>();
}

return dependencies
.Where(x => IsValidVersion(x.Value))
.Select(d => new DependencyDetails(d.Key, ParseVersion(d.Value))).ToList();
return dependencies.Concat(devDependencies).ToHashSet();
}

private static Version ParseVersion(string data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<None Update="Projects\package.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Projects\node_modules\package.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit bfabf4d

Please sign in to comment.