Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
aa71105b2d | |||
e0ccb6c076 | |||
af0d95f327 | |||
d0ac8f1418 | |||
62f8106fc6 | |||
e87b8c994b | |||
694ba7be25 | |||
952399c26e | |||
ab1e83b8bf | |||
67980f9f41 | |||
2d34397b9b | |||
2744e1a92b |
5226
package-lock.json
generated
5226
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartnpm",
|
||||
"version": "1.0.24",
|
||||
"version": "1.0.30",
|
||||
"private": false,
|
||||
"description": "interface with npm to retrieve package information",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -14,9 +14,9 @@
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsrun": "^1.2.12",
|
||||
"@gitzone/tstest": "^1.0.52",
|
||||
"@gitzone/tstest": "^1.0.54",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^14.14.41",
|
||||
"@types/node": "^15.0.1",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
|
14
test/test.ts
14
test/test.ts
@ -37,11 +37,21 @@ tap.test('should get package from verdaccio', async () => {
|
||||
});
|
||||
|
||||
tap.test('should get a specific file from a package', async () => {
|
||||
const bundleFile = await verdaccioRegistry.getFileFromPackage(
|
||||
const wantedFile = await verdaccioRegistry.getFileFromPackage(
|
||||
'@pushrocks/websetup',
|
||||
'ts/index.ts'
|
||||
);
|
||||
console.log(bundleFile.contentBuffer.toString());
|
||||
console.log(wantedFile.contentBuffer.toString());
|
||||
});
|
||||
|
||||
tap.test('should get a specific file from a package', async () => {
|
||||
const wantedFiles = await verdaccioRegistry.getFilesFromPackage(
|
||||
'@pushrocks/websetup',
|
||||
'ts/'
|
||||
);
|
||||
for(const file of wantedFiles) {
|
||||
console.log(file.path);
|
||||
}
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -91,21 +91,22 @@ export class NpmPackage {
|
||||
public async saveToCache() {}
|
||||
|
||||
/**
|
||||
* get file from package
|
||||
* get files from package
|
||||
*/
|
||||
public async getFileFromPackage(
|
||||
public async getFilesFromPackage(
|
||||
filePath: string,
|
||||
optionsArg?: {
|
||||
optionsArg: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.Smartfile> {
|
||||
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile>();
|
||||
},
|
||||
returnOnFirstArg = false
|
||||
): Promise<plugins.smartfile.Smartfile[]> {
|
||||
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
|
||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||
let tarballUrl = this.dist.tarball;
|
||||
if (optionsArg && (optionsArg.version || optionsArg.distTag)) {
|
||||
let tarballUrl = this.dist?.tarball;
|
||||
if (optionsArg?.version || optionsArg?.distTag) {
|
||||
if (optionsArg.distTag && optionsArg.version) {
|
||||
throw new Error('Please either sepcify version OR disttag, not both.');
|
||||
throw new Error('Please either specify version OR disttag, not both.');
|
||||
}
|
||||
let targetVersion: plugins.smartversion.SmartVersion;
|
||||
if (optionsArg.distTag) {
|
||||
@ -116,38 +117,58 @@ export class NpmPackage {
|
||||
targetVersion = new plugins.smartversion.SmartVersion(targetDistTag.targetVersion);
|
||||
}
|
||||
} else {
|
||||
const smartversion = plugins.smartversion.SmartVersion.fromFuzzyString(optionsArg.version);
|
||||
targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(optionsArg.version);
|
||||
}
|
||||
|
||||
// lets find the best matching release
|
||||
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
|
||||
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
|
||||
if (!bestMatchingVersion) {
|
||||
return null;
|
||||
}
|
||||
tarballUrl = this.allVersions.find(
|
||||
(packageVersion) => packageVersion.version === bestMatchingVersion
|
||||
).dist.tarball;
|
||||
}
|
||||
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
|
||||
const wantedFilePath = plugins.path.join('package', filePath);
|
||||
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
|
||||
const subscription = fileObservable.subscribe(
|
||||
(fileArg) => {
|
||||
if (fileArg.path === wantedFilePath) {
|
||||
// lets care about caching
|
||||
|
||||
// returnOnFirstArg requires exact match
|
||||
if (returnOnFirstArg && fileArg.path === wantedFilePath) {
|
||||
// lets resolve with the wanted file
|
||||
done.resolve(fileArg);
|
||||
done.resolve([fileArg]);
|
||||
subscription.unsubscribe();
|
||||
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
|
||||
allMatchingFiles.push(fileArg);
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
console.log(err);
|
||||
},
|
||||
() => {
|
||||
done.resolve(allMatchingFiles);
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
);
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* get files from package
|
||||
*/
|
||||
public async getFileFromPackage(
|
||||
filePath: string,
|
||||
optionsArg?: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}
|
||||
): Promise<plugins.smartfile.Smartfile> {
|
||||
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* updates the package with information from the registry
|
||||
*/
|
||||
|
@ -36,6 +36,9 @@ export class NpmRegistry {
|
||||
const fullMetadata = await plugins.packageJson(packageName, {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
fullMetadata: true,
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
return null;
|
||||
});
|
||||
const versionData = await plugins.packageJson(packageName, {
|
||||
registryUrl: this.options.npmRegistryUrl,
|
||||
@ -75,6 +78,14 @@ export class NpmRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
|
||||
distTag?: string;
|
||||
version?: string;
|
||||
}): Promise<plugins.smartfile.Smartfile[]> {
|
||||
const npmPackage = await this.getPackageInfo(packageNameArg);
|
||||
return npmPackage.getFilesFromPackage(filePath, optionsArg);
|
||||
}
|
||||
|
||||
public async getPackageAsSmartfileVirtualDir(packageNameArg: string): Promise<plugins.smartfile.VirtualDirectory> {
|
||||
/**
|
||||
* TODO: rewrite as memory only
|
||||
|
@ -22,9 +22,11 @@ export class RegistryCache {
|
||||
}
|
||||
|
||||
public async cacheSmartFile (fileIdArg: string, smartfileArg: plugins.smartfile.Smartfile) {
|
||||
await this.levelCache.storeCacheEntryByKey(fileIdArg, new plugins.levelcache.CacheEntry({
|
||||
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||
ttl: 60000
|
||||
}));
|
||||
if (smartfileArg) {
|
||||
await this.levelCache.storeCacheEntryByKey(fileIdArg, new plugins.levelcache.CacheEntry({
|
||||
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||
ttl: 60000
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user