generated from qiwi/blank-ts-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edf6647
commit dc28262
Showing
18 changed files
with
518 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { INpmRegClientWrapper, Packument, RegClient, TBatchResult } from '@qiwi/npm-batch-client' | ||
|
||
import { TGetConfig } from '../interfaces' | ||
import { npmRegClientWrapperFactory, printResults, printResultsJson, writeToFile } from '../utils' | ||
|
||
export const performGet = ( | ||
config: TGetConfig, | ||
customBatchClient?: INpmRegClientWrapper | ||
): Promise<void> => { | ||
const batchClient = customBatchClient || npmRegClientWrapperFactory(config, ['get'], new RegClient()) | ||
|
||
return batchClient.getBatch(config.data, config.batch?.skipErrors) | ||
.then(results => processGetResults(results, config)) | ||
} | ||
|
||
export const isResultSuccessful = ( | ||
item: PromiseSettledResult<Packument> | ||
): item is PromiseFulfilledResult<Packument> & { name: string } => | ||
item.status === 'fulfilled' && | ||
typeof item.value === 'object' && | ||
item.value !== null && | ||
!(item.value as any).error | ||
|
||
export const isResultFailed = (item: PromiseSettledResult<Packument>): boolean => | ||
item.status === 'rejected' || | ||
typeof item.value !== 'object' || | ||
!item.value || | ||
(item.value as any).error | ||
|
||
export const getErrorMessage = (item: PromiseSettledResult<Packument>): string => | ||
item.status === 'rejected' | ||
? item.reason?.message ?? item.reason | ||
: `got ${typeof item.value === 'object' ? JSON.stringify(item.value) : item.value} instead of Packument` | ||
|
||
|
||
export const processGetResults = ( | ||
results: TBatchResult<Packument>[], | ||
config: TGetConfig | ||
): void => { | ||
const enrichedResults = results.map((item, i) => ({ ...item, name: config.data[i] })) | ||
const packuments = enrichedResults | ||
.filter(isResultSuccessful) | ||
.map(({ name, value }) => ({ name, value })) | ||
const successfulPackages = packuments.map(({ name }) => ({ name })) | ||
const failedPackages = enrichedResults | ||
.filter(isResultFailed) | ||
.map(item => ({ name: item.name, error: getErrorMessage(item) })) | ||
|
||
if (config.batch?.jsonOutput) { | ||
printResultsJson({ | ||
successfulPackages, | ||
failedPackages, | ||
packuments | ||
}) | ||
return | ||
} | ||
|
||
writeToFile(config.batch?.path as string, packuments) | ||
|
||
printResults( | ||
successfulPackages, | ||
failedPackages, | ||
['name'], | ||
['name', 'error'], | ||
`Packuments of following packages have been written to ${config.batch?.path}:`, | ||
'Packuments of following packages have not been downloaded because of errors:' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './deprecate' | ||
export * from './publish' | ||
export * from './get' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { Packument, TBatchResult } from '@qiwi/npm-batch-client' | ||
|
||
import { TGetConfig } from '../../../main/ts' | ||
import { performGet, processGetResults } from '../../../main/ts/executors/get' | ||
import * as misc from '../../../main/ts/utils/misc' | ||
|
||
const registryUrl = 'http://localhost' | ||
|
||
const config: TGetConfig = { | ||
registryUrl, | ||
auth: { | ||
username: 'username', | ||
password: 'password', | ||
email: '[email protected]' | ||
}, | ||
batch: { | ||
jsonOutput: true, | ||
skipErrors: true | ||
}, | ||
action: 'get', | ||
data: [] | ||
} | ||
|
||
beforeEach(jest.restoreAllMocks) | ||
|
||
describe('performGet', () => { | ||
it('calls getBatch', async () => { | ||
const npmClientMock = { | ||
getBatch: jest.fn(() => Promise.resolve([])) | ||
} | ||
const printResultsJsonSpy = jest.spyOn(misc, 'printResultsJson') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
|
||
await performGet(config, npmClientMock as any) | ||
|
||
expect(npmClientMock.getBatch).toHaveBeenCalledWith(config.data, true) | ||
expect(printResultsJsonSpy).toHaveBeenCalledWith({ | ||
failedPackages: [], | ||
successfulPackages: [], | ||
packuments: [] | ||
}) | ||
}) | ||
}) | ||
|
||
describe('processGetResults', () => { | ||
const results: TBatchResult<Packument>[] = [ | ||
{ | ||
status: 'fulfilled', | ||
value: {} as Packument | ||
}, | ||
{ | ||
status: 'fulfilled', | ||
value: undefined as any | ||
}, | ||
{ | ||
status: 'rejected', | ||
reason: 'error' | ||
}, | ||
{ | ||
status: 'rejected', | ||
reason: new Error('error') | ||
}, | ||
{ | ||
status: 'rejected', | ||
reason: undefined | ||
} | ||
] | ||
|
||
it('handles results and writes them to a file', () => { | ||
const customConfig = { | ||
...config, | ||
batch: { path: 'foo' }, | ||
data: ['foo', 'bar', 'baz', 'bat', 'qux'] | ||
} | ||
const printResults = jest.spyOn(misc, 'printResults') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
const writeFileSyncSpy = jest.spyOn(misc, 'writeToFile') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
const printResultsJsonSpy = jest.spyOn(misc, 'printResultsJson') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
|
||
processGetResults(results, customConfig) | ||
|
||
expect(printResultsJsonSpy).not.toHaveBeenCalled() | ||
expect(writeFileSyncSpy).toHaveBeenCalledWith( | ||
'foo', | ||
[{ name: 'foo', value: {} }], | ||
) | ||
expect(printResults).toHaveBeenCalled() | ||
}) | ||
|
||
it('prints results in json', () => { | ||
const customConfig = { | ||
...config, | ||
batch: { jsonOutput: true }, | ||
data: ['foo', 'bar', 'baz', 'bat', 'qux'] | ||
} | ||
|
||
const printResults = jest.spyOn(misc, 'printResults') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
const writeFileSyncSpy = jest.spyOn(misc, 'writeToFile') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
const printResultsJsonSpy = jest.spyOn(misc, 'printResultsJson') | ||
.mockImplementation(() => { /* noop */ | ||
}) | ||
|
||
processGetResults(results, customConfig) | ||
|
||
expect(printResults).not.toHaveBeenCalled() | ||
expect(writeFileSyncSpy).not.toHaveBeenCalled() | ||
expect(printResultsJsonSpy).toHaveBeenCalledWith({ | ||
successfulPackages: [ | ||
{ name: 'foo' }, | ||
], | ||
failedPackages: [ | ||
{ name: 'bar', error: 'got undefined instead of Packument' }, | ||
{ name: 'baz', error: 'error' }, | ||
{ name: 'bat', error: 'error' }, | ||
{ name: 'qux', error: undefined }, | ||
], | ||
packuments: [{ name: 'foo', value: {} }] | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.