import axios from 'axios' ; import semver from 'semver' ; import { getComponentWebPath } from "../helpers" ; const componentPath = getComponentWebPath ( 'script' ) ; export interface PluginUpdateCheck { updateAvailable ? : string ; versions : any ; } export async function checkUpdate ( npmPackage , npmPackageVersion ) { const response = await axios . get ( `https://registry.npmjs.cf/${ npmPackage }` ) ; const { data } = response ; const versions = Object . values ( data . versions ) . sort ( ( a , b ) => semver . compare ( a . version , b . version ) ) . reverse ( ) ; let updateAvailable ; let latest ; if ( data [ "dist-tags" ] ) { latest = data [ "dist-tags" ] . latest ; if ( npmPackageVersion && semver . gt ( latest , npmPackageVersion ) ) { updateAvailable = latest ; } } for ( const [ k , v ] of Object . entries ( data [ 'dist-tags' ] ) ) { const found = versions . find ( ( version ) => version . version === v ) ; if ( found ) { found . tag = k ; } } if ( latest ) { const index = versions . findIndex ( ( v ) => v . version === latest ) ; const [ spliced ] = versions . splice ( index , 1 ) ; versions . unshift ( spliced ) ; } return { updateAvailable , versions , } ; } export async function installNpm ( npmPackage , version ? ) { let suffix = version ? `/${ version }` : '' ; return axios . post ( `${ componentPath }/install/${ npmPackage }${ suffix }` ) . then ( response => response . data . id ) ; } export function getNpmPath ( npmPackage ) { return `https://www.npmjs.com/package/${ npmPackage }` ; }