Skip to content

Commit

Permalink
fix: files meta only return currrent files and sub directory (#687)
Browse files Browse the repository at this point in the history
closes #680

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **Bug Fixes**
- Improved file and directory handling to ensure accurate listing and
filtering of package version files.

- **Tests**
- Updated test cases to reflect the new logic for file and directory
handling, ensuring more reliable and accurate test results.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored May 19, 2024
1 parent 8a927fc commit e442580
Show file tree
Hide file tree
Showing 4 changed files with 352 additions and 43 deletions.
42 changes: 17 additions & 25 deletions app/port/controller/PackageVersionFileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,32 +210,24 @@ export class PackageVersionFileController extends AbstractController {
}

async #listFilesByDirectory(packageVersion: PackageVersion, directory: string) {
const files = await this.packageVersionFileService.listPackageVersionFiles(packageVersion, directory);
if (!files || files.length === 0) return null;
// convert files to directory and file
const directories = new Map<string, DirectoryItem>();
const { files, directories } = await this.packageVersionFileService.listPackageVersionFiles(packageVersion, directory);
if (files.length === 0 && directories.length === 0) return null;

const info: DirectoryItem = {
path: directory,
type: 'directory',
files: [],
};
for (const file of files) {
// make sure parent directories exists
const splits = file.directory.split('/');
for (const [ index, name ] of splits.entries()) {
const parentPath = index === 0 ? '' : `/${splits.slice(1, index).join('/')}`;
const directoryPath = parentPath !== '/' ? `${parentPath}/${name}` : `/${name}`;
let directoryItem = directories.get(directoryPath);
if (!directoryItem) {
directoryItem = {
path: directoryPath,
type: 'directory',
files: [],
};
directories.set(directoryPath, directoryItem);
if (parentPath) {
// only set the first time
directories.get(parentPath!)!.files.push(directoryItem);
}
}
}
directories.get(file.directory)!.files.push(formatFileItem(file));
info.files.push(formatFileItem(file));
}
for (const name of directories) {
info.files.push({
path: name,
type: 'directory',
files: [],
} as DirectoryItem);
}
return directories.get(directory);
return info;
}
}
25 changes: 22 additions & 3 deletions app/repository/PackageVersionFileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,39 @@ export class PackageVersionFileRepository extends AbstractRepository {
}

async listPackageVersionFiles(packageVersionId: string, directory: string) {
const where = directory === '/' ? { packageVersionId } :
const isRoot = directory === '/';
const where = isRoot ? { packageVersionId } :
{ packageVersionId, directory: { $or: [{ $eq: directory }, { $like: `${directory}/%` }] } };
// only return current directory's files and directories
// https://github.com/cnpm/cnpmcore/issues/680
const models = await this.PackageVersionFile.find(where);
const distIds = models.map(model => model.distId);
const distIds: string[] = [];
const prefix = isRoot ? directory : `${directory}/`;
const subDirectories = new Set<string>();
const needModels: PackageVersionFileModel[] = [];
for (const item of models) {
if (item.directory === directory) {
// sub file
distIds.push(item.distId);
needModels.push(item);
} else {
// only keep directory = '/' or sub directory like `/dist` but not `/dist/foo`
// sub directory
const subDirectoryName = item.directory.substring(prefix.length).split('/')[0];
subDirectories.add(`${prefix}${subDirectoryName}`);
}
}
const distModels = await this.Dist.find({ distId: distIds });
const distEntitiesMap = new Map<string, DistEntity>();
for (const distModel of distModels) {
const dist = ModelConvertor.convertModelToEntity(distModel, DistEntity);
distEntitiesMap.set(distModel.distId, dist);
}
return models.map(model => {
const files = needModels.map(model => {
const dist = distEntitiesMap.get(model.distId);
return ModelConvertor.convertModelToEntity(model, PackageVersionFileEntity, { dist });
});
return { files, directories: Array.from(subDirectories) };
}

async hasPackageVersionFiles(packageVersionId: string) {
Expand Down
Loading

0 comments on commit e442580

Please sign in to comment.