Skip to content

Commit

Permalink
fix: load composer root packages with no version
Browse files Browse the repository at this point in the history
  • Loading branch information
gabidobo committed Sep 8, 2023
1 parent 7b60ac9 commit 65dad8e
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions src/files/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const loadInstalledPackages = async (rootPath, subPath = '') => {
composer: 'composer.json',
};

let packagesAtRoot = (
const packagesAtRoot = (
await Promise.all(
Object.entries(manifestFilenames).map(async ([manager, manifestFilename]) => {
try {
Expand All @@ -59,53 +59,73 @@ const loadInstalledPackages = async (rootPath, subPath = '') => {
encoding: 'utf-8',
},
);
const packageAtRootData = JSON.parse(manifestContent);
let packageAtRootData = JSON.parse(manifestContent);

if (manager === 'composer') {
packageAtRootData = normalizeComposerManifest(packageAtRootData);
}

packageAtRootData.relativePath = subPath;
packageAtRootData.packageType = manager;
packageAtRootData.isDependency = subPath.includes('node_modules');
// Composer is handled separately below
packageAtRootData.size = await getPackageSize(currentPath);

return packageAtRootData;
// eslint-disable-next-line no-empty
} catch (error) {
return null;
}
}),
)
).filter((m) => m && m.name && m.version);
).filter((p) => p);

if (currentDirname === 'composer' && fs.existsSync(path.join(currentPath, 'installed.json'))) {
if (
currentDirname === 'vendor' &&
fs.existsSync(path.join(currentPath, 'composer', 'installed.json'))
) {
try {
const composerInstalledData = await fs.promises.readFile(
path.join(currentPath, 'installed.json'),
path.join(currentPath, 'composer', 'installed.json'),
{
encoding: 'utf-8',
},
);
const composerInstalled = JSON.parse(composerInstalledData);
packagesAtRoot = await Promise.all(
(composerInstalled.packages || []).map(async (p) => ({
const composerVendorPackages = await Promise.all(
(Array.isArray(composerInstalled)
? composerInstalled
: composerInstalled.packages || []
).map(async (p) => ({
...normalizeComposerManifest(p),
relativePath: path.join(subPath, p['install-path']),
relativePath: p['install-path']
? path.join(subPath, 'composer', p['install-path'])
: undefined,
packageType: 'composer',
size: await getPackageSize(path.join(currentPath, p['install-path'])),
isDependency: true,
size: p['install-path']
? await getPackageSize(path.join(currentPath, 'composer', p['install-path']))
: undefined,
})),
);
// eslint-disable-next-line no-empty
} catch (error) {}
}

const subdirectories = (await fs.promises.readdir(currentPath, {withFileTypes: true}))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
return [...packagesAtRoot, ...composerVendorPackages];
} catch (error) {
return packagesAtRoot;
}
} else {
const subdirectories = (await fs.promises.readdir(currentPath, {withFileTypes: true}))
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

const allChildren = await subdirectories.reduce(async (previous, subdir) => {
const children = await previous;
const subDirChildren = await loadInstalledPackages(rootPath, path.join(subPath, subdir));
const allChildren = await subdirectories.reduce(async (previous, subdir) => {
const children = await previous;
const subDirChildren = await loadInstalledPackages(rootPath, path.join(subPath, subdir));

return [...children, ...subDirChildren];
}, Promise.resolve([]));
return [...children, ...subDirChildren];
}, Promise.resolve([]));

return [...packagesAtRoot, ...allChildren];
return [...packagesAtRoot, ...allChildren];
}
};

module.exports = {loadInstalledPackages, getPackageSize};

0 comments on commit 65dad8e

Please sign in to comment.