UNPKG

@stryker-mutator/core

Version:

The extendable JavaScript mutation testing framework

65 lines 2.53 kB
import { commonTokens, tokens } from '@stryker-mutator/api/plugin'; import { errorToString } from '@stryker-mutator/util'; import { initializerTokens } from './index.js'; const getName = (packageName) => { return packageName .replace('@stryker-mutator/', '') .replace('stryker-', '') .split('-')[0]; }; const mapSearchResultToPromptOption = (searchResults) => searchResults.objects.map((result) => ({ name: getName(result.package.name), pkg: result.package, })); const handleResult = (from) => (response) => { if (response.statusCode === 200 && response.result) { return response.result; } else { throw new Error(`Path ${from} resulted in http status code: ${response.statusCode}.`); } }; export class NpmClient { log; innerNpmClient; npmRegistry; static inject = tokens(commonTokens.logger, initializerTokens.restClientNpm, initializerTokens.npmRegistry); constructor(log, innerNpmClient, npmRegistry) { this.log = log; this.innerNpmClient = innerNpmClient; this.npmRegistry = npmRegistry; } getTestRunnerOptions() { return this.search(`/-/v1/search?text=keywords:${encodeURIComponent('@stryker-mutator/test-runner-plugin')}`).then(mapSearchResultToPromptOption); } getTestReporterOptions() { return this.search(`/-/v1/search?text=keywords:${encodeURIComponent('@stryker-mutator/reporter-plugin')}`).then(mapSearchResultToPromptOption); } async getAdditionalConfig(pkgInfo) { const path = `/${encodeURIComponent(pkgInfo.name)}/${pkgInfo.version}`; try { const response = await this.innerNpmClient.get(path); return handleResult(path)(response); } catch (err) { this.log.warn(`Could not fetch additional initialization config for dependency ${pkgInfo.name}. You might need to configure it manually`, err); return pkgInfo; } } async search(path) { this.log.debug(`Searching: ${path}`); try { const response = await this.innerNpmClient.get(path); return handleResult(path)(response); } catch (err) { this.log.error(`Unable to reach '${this.npmRegistry}' (for query ${path}). Please check your internet connection.`, errorToString(err)); const result = { objects: [], total: 0, }; return result; } } } //# sourceMappingURL=npm-client.js.map