Skip to content

Commit

Permalink
feat: package type config for multi-package dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Sep 8, 2023
1 parent de6e128 commit 4e2828a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/cli/cmds/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ exports.builder = (yargs) =>
describe: 'The path to the application to audit',
type: 'string',
})
.option('pt', {
alias: 'package-type',
demandOption: false,
describe: 'The type of package to search for at the given path',
type: 'string',
})
.option('md', {
alias: 'max-depth',
demandOption: false,
Expand Down Expand Up @@ -194,6 +200,7 @@ exports.handler = async (argv) => {

const configuration = {
appPath,
packageType: argv.packageType,
includeDev:
typeof fileConfig.includeDev !== 'undefined' ? fileConfig.includeDev : argv.includeDev,
skipLicenseIssues:
Expand Down
8 changes: 7 additions & 1 deletion src/files/lockfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,15 @@ const loadLockfiles = async (appPath) => {
return lockfiles;
};

const loadLockfile = async (appPath) => {
const loadLockfile = async (appPath, packageType) => {
const lockfiles = await loadLockfiles(appPath);

if (packageType === 'npm') {
return lockfiles.npm || lockfiles.yarn || lockfiles.pnpm;
}
if (packageType === 'composer') {
return lockfiles.composer;
}
return lockfiles.npm || lockfiles.yarn || lockfiles.pnpm || lockfiles.composer;
};

Expand Down
36 changes: 21 additions & 15 deletions src/graph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ const generateComposerGraph = require('./generateComposerGraph');

const generateGraphPromise = async (
appPath,
{packageData, loadDataFrom = false, rootIsShell = false, includeDev = false, onProgress} = {},
{
packageData,
loadDataFrom = false,
rootIsShell = false,
includeDev = false,
packageType,
onProgress,
} = {},
) => {
const workspace = await loadWorkspace(appPath);
let lockfile = await loadLockfile(appPath);
let lockfile = await loadLockfile(appPath, packageType);

if (!lockfile && workspace) {
lockfile = await loadLockfile(workspace.path);
lockfile = await loadLockfile(workspace.path, packageType);
}

if (!lockfile) {
Expand Down Expand Up @@ -89,18 +96,9 @@ const generateGraphPromise = async (
}

if (loadDataFrom === 'disk') {
const managersToPackageType = {
npm: 'npm',
yarn: 'npm',
'yarn-classic': 'npm',
pnpm: 'npm',
composer: 'composer',
};
const installedPackages = await loadInstalledPackages(workspace?.path || appPath);
additionalPackageData = additionalPackageData.concat(
installedPackages.filter(
({packageType}) => packageType === managersToPackageType[lockfile.manager],
),
installedPackages.filter(({packageType: pt}) => packageType === pt),
);
}

Expand Down Expand Up @@ -147,13 +145,20 @@ const generateGraphAsync = (appPath, options, done = () => {}) => {

const generateGraph = (
appPath,
{packageData, loadDataFrom = false, rootIsShell = false, includeDev = false, onProgress} = {},
{
packageData,
loadDataFrom = false,
rootIsShell = false,
includeDev = false,
packageType,
onProgress,
} = {},
done = undefined,
) => {
if (typeof done === 'function') {
return generateGraphAsync(
appPath,
{packageData, loadDataFrom, rootIsShell, includeDev, onProgress},
{packageData, loadDataFrom, rootIsShell, includeDev, packageType, onProgress},
done,
);
}
Expand All @@ -163,6 +168,7 @@ const generateGraph = (
loadDataFrom,
rootIsShell,
includeDev,
packageType,
onProgress,
});
};
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {getRegistryAudit, setupRegistries} = require('./registry');

const getReport = async ({
appPath,
packageType,
dependencyGraph,
includeDev = false,
licensePolicy,
Expand Down Expand Up @@ -51,6 +52,7 @@ const getReport = async ({
loadDataFrom,
rootIsShell,
includeDev,
packageType,
onProgress: (progress) => onProgress({type: 'progress', stage: 'graph', progress}),
}));
const packageGraph = dGraph.root;
Expand Down

0 comments on commit 4e2828a

Please sign in to comment.