Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: files meta only return currrent files and sub directory #687

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 } :
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
{ 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 => {
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading