-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat: pages for npm cached packages #164
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"presets": [["@verdaccio", {"typescript": true}]] | ||
"presets": [["@verdaccio", {"typescript": true}]], | ||
"plugins": [ | ||
["@babel/plugin-proposal-decorators", { "legacy":true }] | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import fs from 'fs'; | |
import _ from 'lodash'; | ||
import path from 'path'; | ||
|
||
export function getFileStats(packagePath: string): any { | ||
async function getFileStats(packagePath: string): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
fs.stat(packagePath, (err, stats) => { | ||
if (_.isNil(err) === false) { | ||
|
@@ -13,7 +13,7 @@ export function getFileStats(packagePath: string): any { | |
}); | ||
} | ||
|
||
export function readDirectory(packagePath: string): Promise<any> { | ||
async function readDirectory(packagePath: string): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
fs.readdir(packagePath, (err, scopedPackages) => { | ||
if (_.isNil(err) === false) { | ||
|
@@ -29,44 +29,84 @@ function hasScope(file: string) { | |
return file.match(/^@/); | ||
} | ||
|
||
export async function findPackages(storagePath: string, validationHandler: Function) { | ||
const listPackages: Array<any> = []; | ||
async function getVersions(scopePath: string, packageName: string): Promise<string[]> { | ||
const versions: string[] = []; | ||
const arr: string[] = await readDirectory(scopePath); | ||
arr.forEach( (filePath: string) => { | ||
const fileName = path.basename( filePath); | ||
// fileName should start with package name and should ends with '.tgz' | ||
if (fileName.indexOf(packageName)===0 && fileName.indexOf('.tgz')!==-1) { | ||
const v: string = (fileName.split(packageName+'-')[1] || '').replace('.tgz', ''); | ||
versions.push(v); | ||
} | ||
}); | ||
return versions; | ||
} | ||
|
||
|
||
export async function findPackages(storagePath: string) { | ||
//stats | ||
const startTS = Date.now(); | ||
let packages_count = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
let versions_count = 0; | ||
|
||
const listPackages: any = {}; | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const scopePath = path.resolve(storagePath); | ||
const storageDirs = await readDirectory(scopePath); | ||
for (const directory of storageDirs) { | ||
// we check whether has 2nd level | ||
if (hasScope(directory)) { | ||
// we read directory multiple | ||
const scopeDirectory = path.resolve(storagePath, directory); | ||
const scopedPackages = await readDirectory(scopeDirectory); | ||
for (const scopedDirName of scopedPackages) { | ||
if (validationHandler(scopedDirName)) { | ||
const stats = await getFileStats(path.resolve(storagePath, directory)); | ||
if (stats.isDirectory()) { | ||
// we check whether has 2nd level | ||
if (hasScope(directory)) { | ||
// we read directory multiple | ||
const scopeDirectory = path.resolve(storagePath, directory); | ||
const scopedPackages = await readDirectory(scopeDirectory); | ||
for (const scopedDirName of scopedPackages) { | ||
// we build the complete scope path | ||
const scopePath = path.resolve(storagePath, directory, scopedDirName); | ||
// list content of such directory | ||
listPackages.push({ | ||
name: `${directory}/${scopedDirName}`, | ||
path: scopePath | ||
}); | ||
const stats = await getFileStats(scopePath); | ||
if (stats.isDirectory()) { | ||
const versions = await getVersions(scopePath, scopedDirName); | ||
// list content of such directory | ||
listPackages[`${directory}/${scopedDirName}`] = //{ | ||
// path: scopePath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented code can be removed 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
versions | ||
// }; | ||
packages_count++; | ||
versions_count += versions.length; | ||
} | ||
} | ||
} else { | ||
// otherwise we read as single level | ||
const scopePath = path.resolve(storagePath, directory); | ||
const stats = await getFileStats(scopePath); | ||
if (stats.isDirectory()) { | ||
|
||
const versions = await getVersions(scopePath, directory); | ||
listPackages[directory] = //{ | ||
// path: scopePath, | ||
versions | ||
// }; | ||
packages_count++; | ||
versions_count += versions.length; | ||
} | ||
} | ||
} else { | ||
// otherwise we read as single level | ||
if (validationHandler(directory)) { | ||
const scopePath = path.resolve(storagePath, directory); | ||
listPackages.push({ | ||
name: directory, | ||
path: scopePath | ||
}); | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
reject(error); | ||
} | ||
|
||
resolve(listPackages); | ||
resolve({ | ||
packages: listPackages, | ||
stats: { | ||
ts: Date.now(), | ||
duration: Date.now() - startTS, | ||
packages_count, | ||
versions_count, | ||
} | ||
}); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ | |
"typeRoots": [ | ||
"./node_modules/@verdaccio/types/lib/verdaccio", | ||
"./node_modules/@types" | ||
] | ||
], | ||
"experimentalDecorators": true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, we should not use experimental stuff. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this line to allow MSVC to use decorators (as https://ihatetomatoes.net/how-to-remove-experimentaldecorators-warning-in-vscode/ says). Do not know why they call it "experimental" as decorators was added many years ago. Looks like this thing is not very experimental and we can use it. |
||
}, | ||
"include": [ | ||
"src/*.ts", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to use
reduce
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed: cannot imagine how to use
reduce
here, but I changed it to usefilter
andmap
.