2019-10-08 03:19:18 +07:00
|
|
|
import { isObject } from 'util';
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import { UpLinks } from '@verdaccio/types';
|
2019-02-03 17:23:33 +07:00
|
|
|
import isString from 'lodash/isString';
|
|
|
|
import format from 'date-fns/format';
|
|
|
|
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
|
|
|
|
|
|
|
|
export const TIMEFORMAT = 'DD.MM.YYYY, HH:mm:ss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats license field for webui.
|
|
|
|
* @see https://docs.npmjs.com/files/package.json#license
|
|
|
|
*/
|
2019-06-20 19:37:28 +07:00
|
|
|
// License should use type License defined above, but conflicts with the unit test that provide array or empty object
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2019-07-07 17:17:22 +07:00
|
|
|
export function formatLicense(license: any): string | undefined {
|
2019-02-03 17:23:33 +07:00
|
|
|
if (isString(license)) {
|
|
|
|
return license;
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
if (license && isObject(license) && license.type) {
|
2019-02-03 17:23:33 +07:00
|
|
|
return license.type;
|
|
|
|
}
|
|
|
|
|
2019-07-07 17:17:22 +07:00
|
|
|
return;
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
export interface Repository {
|
|
|
|
type: string;
|
|
|
|
url: string;
|
|
|
|
}
|
|
|
|
|
2019-02-03 17:23:33 +07:00
|
|
|
/**
|
|
|
|
* Formats repository field for webui.
|
|
|
|
* @see https://docs.npmjs.com/files/package.json#repository
|
|
|
|
*/
|
2019-06-20 19:37:28 +07:00
|
|
|
|
|
|
|
// Repository should use type Repository defined above, but conflicts with the unit test that provide array or empty object
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
export function formatRepository(repository: any): string | null {
|
2019-02-03 17:23:33 +07:00
|
|
|
if (isString(repository)) {
|
|
|
|
return repository;
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
if (repository && isObject(repository) && repository.url) {
|
2019-02-03 17:23:33 +07:00
|
|
|
return repository.url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:20:05 +07:00
|
|
|
export function formatDate(lastUpdate: string | number): string {
|
2019-06-20 19:37:28 +07:00
|
|
|
return format(new Date(lastUpdate), TIMEFORMAT);
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:20:05 +07:00
|
|
|
export function formatDateDistance(lastUpdate: Date | string | number): string {
|
2019-06-20 19:37:28 +07:00
|
|
|
return distanceInWordsToNow(new Date(lastUpdate));
|
|
|
|
}
|
|
|
|
|
2019-10-03 02:52:27 +07:00
|
|
|
export function buildScopePackage(scope: string, packageName: string): string {
|
2019-08-25 19:34:27 +07:00
|
|
|
return `@${scope}/${packageName}`;
|
2019-06-20 19:37:28 +07:00
|
|
|
}
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For <LastSync /> component
|
|
|
|
* @param {array} uplinks
|
|
|
|
*/
|
2019-06-20 19:37:28 +07:00
|
|
|
export function getLastUpdatedPackageTime(uplinks: UpLinks = {}): string {
|
2019-02-03 17:23:33 +07:00
|
|
|
let lastUpdate = 0;
|
2019-06-20 19:37:28 +07:00
|
|
|
Object.keys(uplinks).forEach(function computeUplink(upLinkName): void {
|
2019-02-03 17:23:33 +07:00
|
|
|
const status = uplinks[upLinkName];
|
|
|
|
if (status.fetched > lastUpdate) {
|
|
|
|
lastUpdate = status.fetched;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return lastUpdate ? formatDate(lastUpdate) : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For <LastSync /> component
|
|
|
|
* @param {Object} time
|
|
|
|
* @returns {Array} last 3 releases
|
|
|
|
*/
|
2019-06-20 19:37:28 +07:00
|
|
|
export function getRecentReleases(time = {}): unknown {
|
|
|
|
const recent = Object.keys(time).map((version): unknown => ({
|
2019-02-03 17:23:33 +07:00
|
|
|
version,
|
|
|
|
time: formatDate(time[version]),
|
|
|
|
}));
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
return recent.slice(recent.length - 3, recent.length).reverse();
|
2019-02-03 17:23:33 +07:00
|
|
|
}
|