Skip to content

Commit

Permalink
refactor: Extract function into helpers module file
Browse files Browse the repository at this point in the history
  • Loading branch information
RSeidelsohn committed Apr 12, 2023
1 parent 1be32a5 commit 25fcb16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
38 changes: 37 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,45 @@ const getFormattedOutput = (json, args) => {
return licenseChecker.asTree(jsonCopy);
};

/**
* ! This function has a wanted sideeffect, as it modifies the json object that is passed by reference.
*
* The depth attribute set in the options parameter here - which is defined by setting the `--direct` flag - is of
* no use with npm < 3, as the older npm versions flattened all dependencies into one single directory. So in
* order to making `--direct` work with older versions of npm, we need to filter out all non-dependencies from
* the json result.
*/
// TODO: Add tests for this function
const deleteNonDirectDependenciesFromAllDependencies = (
{ _dependencies: directDependencies, dependencies: allDependencies, devDependencies },
options,
) => {
const allDependenciesArray = Object.keys(allDependencies);
const directDependenciesArray = Object.keys(directDependencies);
const devDependenciesArray = Object.keys(devDependencies);
let wantedDependenciesArray = [];

if (options.production && !options.development) {
wantedDependenciesArray = directDependenciesArray.filter(
(directDependency) => !devDependenciesArray.includes(directDependency),
);
} else if (!options.production && options.development) {
wantedDependenciesArray = devDependenciesArray;
} else {
wantedDependenciesArray = directDependenciesArray;
}

allDependenciesArray.forEach((currentDependency) => {
if (!wantedDependenciesArray.includes(currentDependency)) {
delete allDependencies[currentDependency];
}
});
};

module.exports = {
shouldColorizeOutput,
colorizeOutput,
deleteNonDirectDependenciesFromAllDependencies,
filterJson,
getFormattedOutput,
shouldColorizeOutput,
};
38 changes: 2 additions & 36 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const createHash = require('crypto').createHash;

const getLicenseTitle = require('./getLicenseTitle');
const licenseFiles = require('./license-files');
const helpers = require('../lib/helpers');

// Set up debug logging
// https://www.npmjs.com/package/debug#stderr-vs-stdout
Expand Down Expand Up @@ -361,41 +362,6 @@ const recursivelyCollectAllDependencies = (options) => {
return data;
};

/**
* ! This function has a wanted sideeffect, as it modifies the json object that is passed by reference.
*
* The depth attribute set in the options parameter here - which is defined by setting the `--direct` flag - is of
* no use with npm < 3, as the older npm versions flattened all dependencies into one single directory. So in
* order to making `--direct` work with older versions of npm, we need to filter out all non-dependencies from
* the json result.
*/
// TODO: Add tests for this function
const deleteNonDirectDependenciesFromAllDependencies = (
{ _dependencies: directDependencies, dependencies: allDependencies, devDependencies },
options,
) => {
const allDependenciesArray = Object.keys(allDependencies);
const directDependenciesArray = Object.keys(directDependencies);
const devDependenciesArray = Object.keys(devDependencies);
let wantedDependenciesArray = [];

if (options.production && !options.development) {
wantedDependenciesArray = directDependenciesArray.filter(
(directDependency) => !devDependenciesArray.includes(directDependency),
);
} else if (!options.production && options.development) {
wantedDependenciesArray = devDependenciesArray;
} else {
wantedDependenciesArray = directDependenciesArray;
}

allDependenciesArray.forEach((currentDependency) => {
if (!wantedDependenciesArray.includes(currentDependency)) {
delete allDependencies[currentDependency];
}
});
};

exports.init = function init(args, callback) {
// Fix path if on Windows:
const workingDir = args.start.replace(/\\\\/g, '\\');
Expand Down Expand Up @@ -455,7 +421,7 @@ exports.init = function init(args, callback) {
// the package.json file in the property '_dependencies'. The property 'dependencies' contains all dependencies,
// including the ones that are only required by other dependencies.
if (optionsForReadingInstalledPackages.depth === 0) {
deleteNonDirectDependenciesFromAllDependencies(installedPackagesJson, args);
helpers.deleteNonDirectDependenciesFromAllDependencies(installedPackagesJson, args);
}

const data = recursivelyCollectAllDependencies({
Expand Down

0 comments on commit 25fcb16

Please sign in to comment.