Skip to content

Commit 2e2bb32

Browse files
@jotadevelopersergiohgz
authored andcommitted
feat: implement search
1 parent c057afb commit 2e2bb32

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

plugins/local-storage/src/local-database.js

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import fs from 'fs';
44
import _ from 'lodash';
55
import Path from 'path';
66
import LocalFS from './local-fs';
7+
// $FlowFixMe
8+
import async from 'async';
79
import mkdirp from 'mkdirp';
810
import type { StorageList, LocalStorage, Logger, Config, Callback } from '@verdaccio/types';
9-
import type { IPackageStorage, ILocalData } from '@verdaccio/local-storage';
11+
import type { IPackageStorage, IPluginStorage } from '@verdaccio/local-storage';
1012

1113
/**
1214
* Handle local database.
1315
*/
14-
class LocalDatabase implements ILocalData {
16+
class LocalDatabase implements IPluginStorage {
1517
path: string;
1618
logger: Logger;
1719
data: LocalStorage;
@@ -56,6 +58,87 @@ class LocalDatabase implements ILocalData {
5658
}
5759
}
5860

61+
search(onPackage: Callback, onEnd: Callback, validateName: any): void {
62+
const storages = this._getCustomPackageLocalStorages();
63+
const base = Path.dirname(this.config.self_path);
64+
async.eachSeries(
65+
Object.keys(storages),
66+
function(storage, cb) {
67+
fs.readdir(Path.resolve(base, storage), function(err, files) {
68+
if (err) {
69+
return cb(err);
70+
}
71+
72+
async.eachSeries(
73+
files,
74+
function(file, cb) {
75+
if (file.match(/^@/)) {
76+
// scoped
77+
fs.readdir(Path.resolve(base, storage, file), function(err, files) {
78+
if (err) {
79+
return cb(err);
80+
}
81+
82+
async.eachSeries(
83+
files,
84+
(file2, cb) => {
85+
if (validateName(file2)) {
86+
const item = {
87+
name: `${file}/${file2}`,
88+
path: Path.resolve(base, storage, file, file2)
89+
};
90+
91+
onPackage(item, cb);
92+
} else {
93+
cb();
94+
}
95+
},
96+
cb
97+
);
98+
});
99+
} else if (validateName(file)) {
100+
onPackage(
101+
{
102+
name: file,
103+
path: Path.resolve(base, storage, file)
104+
},
105+
cb
106+
);
107+
} else {
108+
cb();
109+
}
110+
},
111+
cb
112+
);
113+
});
114+
},
115+
onEnd
116+
);
117+
}
118+
119+
_getCustomPackageLocalStorages() {
120+
const storages = {};
121+
122+
// add custom storage if exist
123+
if (this.config.storage) {
124+
storages[this.config.storage] = true;
125+
}
126+
127+
const { packages } = this.config;
128+
129+
if (packages) {
130+
const listPackagesConf = Object.keys(packages || {});
131+
132+
listPackagesConf.map(pkg => {
133+
if (packages[pkg].storage) {
134+
storages[packages[pkg].storage] = true;
135+
}
136+
});
137+
}
138+
139+
return storages;
140+
}
141+
59142
/**
60143
* Remove an element from the database.
61144
* @param {*} name

plugins/local-storage/src/local-fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ class LocalFS implements ILocalPackageManager {
281281
return JSON.stringify(value, null, '\t');
282282
}
283283

284-
_getStorage(name: string = '') {
285-
const storagePath: string = path.join(this.path, name);
284+
_getStorage(fileName: string = '') {
285+
const storagePath: string = path.join(this.path, fileName);
286286

287287
return storagePath;
288288
}

0 commit comments

Comments
 (0)