From 6fb2ec0e6a79412af93f8364ef344ecba05b7d94 Mon Sep 17 00:00:00 2001 From: DavHau Date: Sun, 27 Feb 2022 18:46:07 +0700 Subject: [PATCH] imrpove nodejs discovery - use dream-lock transaltor for packages without any dependencies - scan sub-directories of non-project dirs --- src/discoverers/nodejs/default.nix | 30 ++++++++++++------ .../nodejs/pure/package-lock/default.nix | 31 +++++++++++++------ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/discoverers/nodejs/default.nix b/src/discoverers/nodejs/default.nix index 9ff8c558..07ddbf3b 100644 --- a/src/discoverers/nodejs/default.nix +++ b/src/discoverers/nodejs/default.nix @@ -20,9 +20,16 @@ let getTranslatorNames = path: let nodes = l.readDir path; + packageJson = l.fromJSON (l.readFile "${path}/package.json"); in l.optionals (nodes ? "package-lock.json") [ "package-lock" ] ++ l.optionals (nodes ? "yarn.lock") [ "yarn-lock" ] + # if the package has no dependencies we use the + # package-lock translator with `packageLock = null` + ++ l.optionals + (! packageJson ? dependencies + && ! packageJson ? devDependencies) + [ "package-lock" ] ++ [ "package-json" ]; # returns the parsed package.json of a given directory @@ -78,12 +85,21 @@ let # twice. alreadyDiscovered ? {}, }: + let + foundSubProjects = alreadyDiscovered: + l.flatten + ((l.mapAttrsToList + (dname: dir: discoverInternal { + inherit alreadyDiscovered; + tree = dir; + }) + (tree.directories or {}))); + in # skip if not a nodajs project if alreadyDiscovered ? "${tree.relPath}" - || ! tree ? files."package.json" - then + || ! tree ? files."package.json" then # this will be cleaned by `flatten` for sub-directories - [] + foundSubProjects alreadyDiscovered else let @@ -130,13 +146,7 @@ let # Thanks to `alreadyDiscovered`, workspace projects won't be discovered # a second time. ++ - l.flatten - ((l.mapAttrsToList - (dname: dir: discoverInternal { - alreadyDiscovered = alreadyDiscovered'; - tree = dir; - }) - (tree.directories or {}))); + (foundSubProjects alreadyDiscovered'); in { diff --git a/src/translators/nodejs/pure/package-lock/default.nix b/src/translators/nodejs/pure/package-lock/default.nix index c7cd9942..433c7126 100644 --- a/src/translators/nodejs/pure/package-lock/default.nix +++ b/src/translators/nodejs/pure/package-lock/default.nix @@ -53,13 +53,23 @@ let getPackageLock = project: let # returns the parsed package-lock.json for a given project - fileRelPath = + dirRelPath = if project ? subsystemInfo.workspaceParent then - "${project.subsystemInfo.workspaceParent}/package-lock.json" + "${project.subsystemInfo.workspaceParent}" else - "${project.relPath}/package-lock.json"; + "${project.relPath}"; + + packageJson = + (tree.getNodeFromPath "${dirRelPath}/package.json").jsonContent; + + hasNoDependencies = + ! packageJson ? dependencies && ! packageJson ? devDependenices; + in - (tree.getNodeFromPath fileRelPath).jsonContent; + if hasNoDependencies then + null + else + (tree.getNodeFromPath "${dirRelPath}/package-lock.json").jsonContent; getPackageJson = project: let @@ -110,7 +120,11 @@ let dev = ! noDev; - parsed = packageLock; + packageLockDeps = + if packageLock == null then + {} + else + packageLock.dependencies or {}; workspacePackageJson = l.genAttrs @@ -126,8 +140,7 @@ let json.version) workspacePackageJson; - rootDependencies = - packageLock.dependencies or {}; + rootDependencies = packageLockDeps; packageJsonDeps = packageJson.dependencies or {} // packageJson.devDependencies or {}; @@ -135,7 +148,7 @@ let parsedDependencies = l.filterAttrs (name: dep: packageJsonDeps ? "${name}") - parsed.dependencies or {}; + packageLockDeps; identifyGitSource = dependencyObject: # TODO: when integrity is there, and git url is github then use tarball instead @@ -223,7 +236,7 @@ let )); packages = - { "${defaultPackage}" = parsed.version or "unknown"; } + { "${defaultPackage}" = packageJson.version or "unknown"; } // workspacePackages; mainPackageDependencies =