Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Nodejs/Product/Npm/RootPackageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ namespace Microsoft.NodejsTools.Npm {
public static class RootPackageFactory {
public static IRootPackage Create(
string fullPathToRootDirectory,
bool showMissingDevOptionalSubPackages = false) {
bool showMissingDevOptionalSubPackages = false,
int maxDepth = 1) {
return new RootPackage(
fullPathToRootDirectory,
showMissingDevOptionalSubPackages);
showMissingDevOptionalSubPackages,
null,
0,
maxDepth);
}
}
}
22 changes: 13 additions & 9 deletions Nodejs/Product/Npm/SPI/NodeModules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@

namespace Microsoft.NodejsTools.Npm.SPI {
internal class NodeModules : AbstractNodeModules {
private Dictionary<string, ModuleInfo> _allModules;
private readonly Dictionary<string, ModuleInfo> _allModules;
private static readonly string[] _ignoredDirectories = { @"\.bin", @"\.staging" };

public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages, Dictionary<string, ModuleInfo> allModulesToDepth = null, int depth = 0) {
public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages, Dictionary<string, ModuleInfo> allModulesToDepth = null, int depth = 0, int maxDepth = 1) {
if (depth >= maxDepth) {
return;
}

var modulesBase = Path.Combine(parent.Path, NodejsConstants.NodeModulesFolder);

_allModules = allModulesToDepth ?? new Dictionary<string, ModuleInfo>();
Expand All @@ -44,13 +48,13 @@ public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages,
// _requiredBy dependencies that begin with hash characters represent top-level dependencies
foreach (var requiredBy in packageJson.RequiredBy) {
if (requiredBy.StartsWith("#") || requiredBy == "/") {
AddTopLevelModule(parent, showMissingDevOptionalSubPackages, moduleDir, depth);
AddTopLevelModule(parent, showMissingDevOptionalSubPackages, moduleDir, depth, maxDepth);
break;
}
}
} else {
// This dependency is a top-level dependency not added by npm v3
AddTopLevelModule(parent, showMissingDevOptionalSubPackages, moduleDir, depth);
AddTopLevelModule(parent, showMissingDevOptionalSubPackages, moduleDir, depth, maxDepth);
}
}
}
Expand All @@ -67,7 +71,7 @@ public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages,
// try to find folder by recursing up tree
do {
moduleDir = Path.Combine(moduleDir, dependency.Name);
if (AddModuleIfNotExists(parent, moduleDir, showMissingDevOptionalSubPackages, depth, dependency)) {
if (AddModuleIfNotExists(parent, moduleDir, showMissingDevOptionalSubPackages, depth, maxDepth, dependency)) {
break;
}

Expand All @@ -80,12 +84,12 @@ public NodeModules(IRootPackage parent, bool showMissingDevOptionalSubPackages,
_packagesSorted.Sort(new PackageComparer());
}

private void AddTopLevelModule(IRootPackage parent, bool showMissingDevOptionalSubPackages, string moduleDir, int depth) {
private void AddTopLevelModule(IRootPackage parent, bool showMissingDevOptionalSubPackages, string moduleDir, int depth, int maxDepth) {
Debug.Assert(depth == 0, "Depth should be 0 when adding a top level dependency");
AddModuleIfNotExists(parent, moduleDir, showMissingDevOptionalSubPackages, depth);
AddModuleIfNotExists(parent, moduleDir, showMissingDevOptionalSubPackages, depth, maxDepth);
}

private bool AddModuleIfNotExists(IRootPackage parent, string moduleDir, bool showMissingDevOptionalSubPackages, int depth, IDependency dependency = null) {
private bool AddModuleIfNotExists(IRootPackage parent, string moduleDir, bool showMissingDevOptionalSubPackages, int depth, int maxDepth, IDependency dependency = null) {
depth++;

ModuleInfo moduleInfo;
Expand Down Expand Up @@ -120,7 +124,7 @@ private bool AddModuleIfNotExists(IRootPackage parent, string moduleDir, bool sh

moduleInfo.RequiredBy.Add(parent.Path);

var pkg = new Package(parent, moduleDir, showMissingDevOptionalSubPackages, _allModules, depth);
var pkg = new Package(parent, moduleDir, showMissingDevOptionalSubPackages, _allModules, depth, maxDepth);
if (dependency != null) {
pkg.RequestedVersionRange = dependency.VersionRangeText;
}
Expand Down
5 changes: 3 additions & 2 deletions Nodejs/Product/Npm/SPI/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public Package(
string fullPathToRootDirectory,
bool showMissingDevOptionalSubPackages,
Dictionary<string, ModuleInfo> allModules = null,
int depth = 0)
: base(fullPathToRootDirectory, showMissingDevOptionalSubPackages, allModules, depth) {
int depth = 0,
int maxDepth = 1)
: base(fullPathToRootDirectory, showMissingDevOptionalSubPackages, allModules, depth, maxDepth) {
_parent = parent;
}

Expand Down
5 changes: 3 additions & 2 deletions Nodejs/Product/Npm/SPI/RootPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public RootPackage(
string fullPathToRootDirectory,
bool showMissingDevOptionalSubPackages,
Dictionary<string, ModuleInfo> allModules = null,
int depth = 0) {
int depth = 0,
int maxDepth = 1) {
Path = fullPathToRootDirectory;
var packageJsonFile = System.IO.Path.Combine(fullPathToRootDirectory, "package.json");
try {
Expand All @@ -44,7 +45,7 @@ public RootPackage(
}

try {
Modules = new NodeModules(this, showMissingDevOptionalSubPackages, allModules, depth);
Modules = new NodeModules(this, showMissingDevOptionalSubPackages, allModules, depth, maxDepth);
} catch (PathTooLongException) {
// otherwise we fail to create it completely...
}
Expand Down
2 changes: 1 addition & 1 deletion Nodejs/Tests/NpmTests/ModuleHierarchyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void ReadRootDependencyRecursive() {
var rootDir = FilesystemPackageJsonTestHelpers.CreateRootPackage(manager, PkgSingleRecursiveDependency);
RunNpmInstall(rootDir);

var pkg = RootPackageFactory.Create(rootDir);
var pkg = RootPackageFactory.Create(rootDir, false, int.MaxValue);

var json = pkg.PackageJson;
var dependencies = json.AllDependencies;
Expand Down