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.
feat: add publish action to cli-api (#15)
- Loading branch information
1 parent
709c402
commit 74419c5
Showing
20 changed files
with
715 additions
and
342 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,71 @@ | ||
import { IDeprecatePackageParams, INpmRegClientWrapper, RegClient, TBatchResult } from '@qiwi/npm-batch-client' | ||
|
||
import { TDeprecationConfig } from '../interfaces' | ||
import { npmRegClientWrapperFactory, printResults, printResultsJson } from '../utils' | ||
|
||
export const performDeprecation = async ( | ||
config: TDeprecationConfig, | ||
customBatchClient?: INpmRegClientWrapper | ||
): Promise<void> => { | ||
const batchClient = customBatchClient || npmRegClientWrapperFactory(config, ['deprecate'], new RegClient()) | ||
|
||
return batchClient | ||
.deprecateBatch(config.data) | ||
.then(data => processResults(handleSettledResults(data), config)) | ||
} | ||
|
||
type THandledValue = null | PromiseRejectedResult['reason'] | ||
|
||
export const processResults = (results: THandledValue[], config: TDeprecationConfig): void => { | ||
const enrichedResults = enrichResults(results, config.data) | ||
const successfulResults = getSuccessfulResults(enrichedResults) | ||
const failedResults = getFailedResults(enrichedResults) | ||
|
||
if (config.batch?.jsonOutput) { | ||
printResultsJson( | ||
successfulResults, | ||
failedResults | ||
) | ||
return | ||
} | ||
|
||
printResults( | ||
successfulResults, | ||
failedResults, | ||
['packageName', 'version', 'message'], | ||
['packageName', 'version', 'message', 'error'], | ||
'Following packages are deprecated successfully:', | ||
'Following packages are not deprecated due to errors:' | ||
) | ||
} | ||
|
||
type TEnrichedBatchResult = { | ||
result: THandledValue | ||
packageInfo: IDeprecatePackageParams | ||
} | ||
|
||
export const enrichResults = ( | ||
results: THandledValue[], | ||
data: IDeprecatePackageParams[] | ||
): TEnrichedBatchResult[] => | ||
results.map((result, i) => ({ result, packageInfo: data[i] })) | ||
|
||
export const handleSettledResults = (results: TBatchResult<null>[]): THandledValue[] => | ||
results.map(result => result.status === 'rejected' | ||
? result.reason | ||
: result.value | ||
) | ||
|
||
export const getSuccessfulResults = ( | ||
results: TEnrichedBatchResult[] | ||
): IDeprecatePackageParams[] => | ||
results | ||
.filter(item => item.result === null) | ||
.map(item => item.packageInfo) | ||
|
||
export const getFailedResults = ( | ||
results: TEnrichedBatchResult[] | ||
): Array<IDeprecatePackageParams & { error: any }> => | ||
results | ||
.filter(item => item.result !== null) | ||
.map(item => ({ ...item.packageInfo, error: item.result.message ?? item.result })) |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export * from './deprecation' | ||
export * from './deprecate' |
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,56 @@ | ||
import { | ||
INpmRegClientWrapper, | ||
RegClient, | ||
TBatchResult, | ||
TPublishResult, | ||
TTarballOpts | ||
} from '@qiwi/npm-batch-client' | ||
|
||
import { TPublishConfig } from '../interfaces' | ||
import { npmRegClientWrapperFactory, printResults, printResultsJson } from '../utils' | ||
|
||
export const performPublish = ( | ||
config: TPublishConfig, | ||
customBatchClient?: INpmRegClientWrapper | ||
): Promise<void> => { | ||
const batchClient = customBatchClient || npmRegClientWrapperFactory(config, ['publish'], new RegClient()) | ||
|
||
return batchClient.publishBatch(config.data) | ||
.then(data => processPublishResults(data, config)) | ||
.catch(console.error) | ||
} | ||
|
||
type TEnrichedResult = { | ||
result: TBatchResult<TPublishResult> | ||
params: TTarballOpts | ||
} | ||
|
||
export const processPublishResults = (results: TBatchResult<TPublishResult>[], config: TPublishConfig): void => { | ||
const enrichedResults: TEnrichedResult[] = results | ||
.map((result, i) => ({ result, params: config.data[i] })) | ||
|
||
const successfulPackages = enrichedResults | ||
.filter((item) => item.result.status === 'fulfilled' && item.result.value.success) | ||
.map(item => item.params) | ||
|
||
const failedPackages = enrichedResults | ||
.filter((item) => item.result.status === 'rejected' || !item.result.value.success) | ||
.map((item: TEnrichedResult) => ({ | ||
...item.params, | ||
error: item.result.status === 'rejected' ? item.result.reason : 'no data' | ||
})) | ||
|
||
if (config.batch?.jsonOutput) { | ||
printResultsJson(successfulPackages, failedPackages) | ||
return | ||
} | ||
|
||
printResults( | ||
successfulPackages, | ||
failedPackages, | ||
['name', 'version', 'filePath', 'access'], | ||
['name', 'version', 'filePath', 'access', 'error'], | ||
'Following packages are published successfully:', | ||
'Following packages are not published due to 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
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 |
---|---|---|
@@ -1,3 +1,62 @@ | ||
import { NpmRegClientWrapper, RegClient } from '@qiwi/npm-batch-client' | ||
import assert from 'assert' | ||
import { readFileSync } from 'fs' | ||
|
||
import { defaultRateLimit } from '../default' | ||
import { IBaseConfig } from '../interfaces' | ||
import { withRateLimit } from './withRateLimit' | ||
|
||
export const readFileToString = (path: string): string => readFileSync(path).toString() | ||
|
||
// TODO migrate to blork - needs ts libdefs to be added at first | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const assertString = (value: any, name: string): void => { | ||
assert.ok(typeof value === 'string', `${name} should be a string`) | ||
} | ||
|
||
export const printResultsJson = ( | ||
successfulPackages: Array<any>, | ||
failedPackages: Array<any>, | ||
logger = console | ||
): void => { | ||
logger.log( | ||
JSON.stringify( | ||
{ | ||
successfulPackages, | ||
failedPackages | ||
}, | ||
null, // eslint-disable-line unicorn/no-null | ||
'\t' | ||
) | ||
) | ||
} | ||
|
||
export const printResults = ( | ||
successfulPackages: Array<any>, | ||
failedPackages: Array<any>, | ||
successfulPackagesFields: string[], | ||
failedPackagesFields: string[], | ||
successfulCaption: string, | ||
failedCaption: string, | ||
logger = console | ||
): void => { | ||
if (successfulPackages.length > 0) { | ||
logger.log(successfulCaption) | ||
logger.table(successfulPackages, successfulPackagesFields) | ||
} | ||
|
||
if (failedPackages.length > 0) { | ||
logger.error(failedCaption) | ||
logger.table(failedPackages, failedPackagesFields) | ||
} | ||
} | ||
|
||
export const npmRegClientWrapperFactory = ( | ||
config: IBaseConfig, | ||
limitedMethods: string[], | ||
regClient: RegClient | ||
): NpmRegClientWrapper => new NpmRegClientWrapper( | ||
config.registryUrl, | ||
config.auth, | ||
withRateLimit<RegClient>(regClient, config.batch?.ratelimit || defaultRateLimit, limitedMethods) | ||
) |
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
Oops, something went wrong.