smartnpm/ts/smartnpm.classes.npmregistry.ts

165 lines
4.7 KiB
TypeScript
Raw Normal View History

2018-09-01 14:40:42 +00:00
import * as plugins from './smartnpm.plugins';
2020-03-17 00:38:58 +00:00
import * as paths from './smartnpm.paths';
2017-08-14 15:50:48 +00:00
2017-08-15 15:03:21 +00:00
// interfaces
2018-09-01 14:40:42 +00:00
import { ISearchObject } from './smartnpm.interfaces';
2017-08-14 15:50:48 +00:00
2017-08-15 15:03:21 +00:00
// classes
2018-09-01 14:40:42 +00:00
import { NpmPackage } from './smartnpm.classes.npmpackage';
2017-08-14 15:50:48 +00:00
2019-09-06 09:12:23 +00:00
export interface INpmRegistryConstructorOptions {
npmRegistryUrl?: string;
}
2017-08-14 15:50:48 +00:00
export class NpmRegistry {
2019-09-06 09:12:23 +00:00
public options: INpmRegistryConstructorOptions;
public registry: string;
2018-09-01 14:40:42 +00:00
private searchDomain = 'https://api.npms.io/v2/search?q=';
2019-09-06 09:12:23 +00:00
constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
const defaultOptions: INpmRegistryConstructorOptions = {
2020-06-25 20:03:52 +00:00
npmRegistryUrl: 'https://registry.npmjs.org',
2019-09-06 09:12:23 +00:00
};
this.options = {
...defaultOptions,
2020-06-25 20:03:52 +00:00
...optionsArg,
2019-09-06 09:12:23 +00:00
};
}
2020-03-17 00:38:58 +00:00
/**
* gets info about a package
* @param packageName
*/
2019-09-06 11:22:54 +00:00
public async getPackageInfo(packageName: string): Promise<NpmPackage> {
const fullMetadata = await plugins.packageJson(packageName, {
2019-09-06 09:12:23 +00:00
registryUrl: this.options.npmRegistryUrl,
2020-06-25 20:03:52 +00:00
fullMetadata: true,
2019-09-06 09:12:23 +00:00
});
2020-03-08 19:34:36 +00:00
const npmPackage = await NpmPackage.createFromFullMetadata(this, fullMetadata);
2019-09-06 11:22:54 +00:00
return npmPackage;
2019-09-06 09:12:23 +00:00
}
2020-03-17 00:38:58 +00:00
/**
* saves a package to disk
* @param packageName
* @param targetDir
*/
public async savePackageToDisk(packageName: string, targetDir: string): Promise<void> {
const npmPackage = await this.getPackageInfo(packageName);
await npmPackage.saveToDisk(targetDir);
}
/**
* gets a file from a package as Smartfile
*/
public async getFileFromPackage(packageName: string, filePath: string) {
const baseDir = plugins.path.join(paths.nogitDir, packageName.replace('/', '__'));
await plugins.smartfile.fs.ensureDir(baseDir);
await this.savePackageToDisk(packageName, baseDir);
const smartfile = await plugins.smartfile.Smartfile.fromFilePath(
plugins.path.join(baseDir, 'package', filePath)
);
await plugins.smartfile.fs.remove(baseDir);
return smartfile;
}
/**
* searches for a package on npm
* @param searchObjectArg
*/
public async searchOnNpm(searchObjectArg: ISearchObject) {
2019-09-06 09:12:23 +00:00
if (this.options.npmRegistryUrl !== 'https://registry.npmjs.org') {
throw Error(`cannot search registries other than registry.gitlab.com`);
}
2018-09-01 14:40:42 +00:00
let searchString = '';
2019-09-06 09:12:23 +00:00
const addToSearchString = (addStringArg: string) => {
2018-09-01 14:40:42 +00:00
searchString = `${searchString}+${addStringArg}`;
};
2017-08-15 12:20:33 +00:00
2017-08-15 15:03:21 +00:00
// name
2018-09-01 14:40:42 +00:00
if (searchObjectArg.name) {
searchString = `${searchObjectArg.name}`;
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// metadata
2018-09-01 14:40:42 +00:00
if (searchObjectArg.author) {
addToSearchString(`author:${searchObjectArg.author}`);
}
if (searchObjectArg.maintainer) {
addToSearchString(`maintainer:${searchObjectArg.maintainer}`);
}
if (searchObjectArg.scope) {
addToSearchString(`scope:${searchObjectArg.scope}`);
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// status
if (searchObjectArg.deprecated) {
if (searchObjectArg.deprecated === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:deprecated`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:deprecated`);
2017-08-15 12:20:33 +00:00
}
}
if (searchObjectArg.unstable) {
if (searchObjectArg.unstable === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:unstable`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:unstable`);
2017-08-15 12:20:33 +00:00
}
}
if (searchObjectArg.insecure) {
if (searchObjectArg.insecure === true) {
2018-09-01 14:40:42 +00:00
addToSearchString(`is:insecure`);
2017-08-15 12:20:33 +00:00
} else {
2018-09-01 14:40:42 +00:00
addToSearchString(`not:insecure`);
2017-08-15 12:20:33 +00:00
}
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// search behaviour
2018-09-01 14:40:42 +00:00
if (searchObjectArg.boostExact) {
addToSearchString(`boost-exact:${searchObjectArg.boostExact}`);
}
if (searchObjectArg.scoreEffect) {
addToSearchString(`score-effect:${searchObjectArg.scoreEffect}`);
}
2017-08-15 15:03:21 +00:00
2017-08-15 12:20:33 +00:00
// analytics
2018-09-01 14:40:42 +00:00
if (searchObjectArg.qualityWeight) {
addToSearchString(`author:${searchObjectArg.qualityWeight}`);
}
if (searchObjectArg.popularityWeight) {
addToSearchString(`author:${searchObjectArg.popularityWeight}`);
}
if (searchObjectArg.maintenanceWeight) {
addToSearchString(`author:${searchObjectArg.maintenanceWeight}`);
}
2017-08-15 12:20:33 +00:00
2020-06-25 20:03:52 +00:00
console.log(
`info: Search on npm for ${plugins.consolecolor.coloredString(searchString, 'pink')}`
2018-09-01 14:40:42 +00:00
);
2017-08-15 15:03:21 +00:00
2018-02-14 22:51:08 +00:00
let body: any;
try {
2019-09-06 09:12:23 +00:00
const response = await plugins.smartrequest.getJson(this.searchDomain + searchString, {});
2018-09-01 14:40:42 +00:00
body = response.body;
2018-02-14 22:51:08 +00:00
} catch {
// we do nothing
}
// lets create the packageArray
2019-09-06 09:12:23 +00:00
const packageArray: NpmPackage[] = [];
2018-02-14 22:51:08 +00:00
// if request failed just return it empty
if (!body || typeof body === 'string') {
2018-09-01 14:40:42 +00:00
return packageArray;
2017-09-13 15:33:07 +00:00
}
2018-02-14 22:51:08 +00:00
2019-09-06 11:22:54 +00:00
for (const packageSearchInfoArg of body.results) {
const npmPackage = await this.getPackageInfo(packageSearchInfoArg.package.name);
packageArray.push(npmPackage);
2017-08-15 15:03:21 +00:00
}
2018-09-01 14:40:42 +00:00
return packageArray;
2017-08-14 15:50:48 +00:00
}
}