Skip to content

Commit

Permalink
refactor: Provide a better function name and move condition to functi…
Browse files Browse the repository at this point in the history
…on call
  • Loading branch information
RSeidelsohn committed Apr 10, 2023
1 parent 1b60109 commit ef5a5c4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,27 @@ const recursivelyCollectAllDependencies = (options) => {
* order to making `--direct` work with newer versions of npm, we need to filter out all non-dependencies from
* the json result.
*/
const removeUnwantedDependencies = (json, args) => {
if (args.direct === 0) {
const allDependencies = Object.keys(json.dependencies);
let wantedDependencies = [];

if (args.production && !args.development) {
const devDependencies = Object.keys(json.devDependencies);
wantedDependencies = Object.keys(json._dependencies).filter(
(directDependency) => !devDependencies.includes(directDependency),
);
} else if (!args.production && args.development) {
wantedDependencies = Object.keys(json.devDependencies);
} else {
wantedDependencies = Object.keys(json._dependencies);
const deleteNonDirectDependencies = (json, options) => {
const allDependencies = Object.keys(json.dependencies);
let wantedDependencies = [];

if (options.production && !options.development) {
const devDependencies = Object.keys(json.devDependencies);
wantedDependencies = Object.keys(json._dependencies).filter(
(directDependency) => !devDependencies.includes(directDependency),
);
} else if (!options.production && options.development) {
wantedDependencies = Object.keys(json.devDependencies);
} else {
wantedDependencies = Object.keys(json._dependencies);
}

allDependencies.forEach((currentDependency) => {
if (!wantedDependencies.includes(currentDependency)) {
delete json.dependencies[currentDependency];
}
});
};

allDependencies.forEach((currentDependency) => {
if (!wantedDependencies.includes(currentDependency)) {
Expand Down Expand Up @@ -458,7 +464,9 @@ exports.init = function init(initArgs, callback) {
}

read(args.start, opts, (err, json) => {
removeUnwantedDependencies(json, args);
if (optionsForReadingInstalledPackages.depth === 0) {
deleteNonDirectDependencies(json, args);
}

const data = recursivelyCollectAllDependencies({
_args: args,
Expand Down

0 comments on commit ef5a5c4

Please sign in to comment.