@@ -2,7 +2,6 @@ import { isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
22import { getDataModels , getLiteral , hasAttribute } from '@zenstackhq/sdk' ;
33import colors from 'colors' ;
44import fs from 'fs' ;
5- import getLatestVersion from 'get-latest-version' ;
65import { getDocument , LangiumDocument , LangiumDocuments , linkContentToContainer } from 'langium' ;
76import { NodeFileSystem } from 'langium/node' ;
87import path from 'path' ;
@@ -20,6 +19,8 @@ import { CliError } from './cli-error';
2019// required minimal version of Prisma
2120export const requiredPrismaVersion = '4.8.0' ;
2221
22+ const CHECK_VERSION_TIMEOUT = 1000 ;
23+
2324/**
2425 * Loads a zmodel document from a file.
2526 * @param fileName File name
@@ -267,12 +268,37 @@ export function checkRequiredPackage(packageName: string, minVersion?: string) {
267268
268269export async function checkNewVersion ( ) {
269270 const currVersion = getVersion ( ) ;
270- const latestVersion = await getLatestVersion ( 'zenstack' ) ;
271+ let latestVersion : string ;
272+ try {
273+ latestVersion = await getLatestVersion ( ) ;
274+ } catch {
275+ // noop
276+ return ;
277+ }
278+
271279 if ( latestVersion && semver . gt ( latestVersion , currVersion ) ) {
272280 console . log ( `A newer version ${ colors . cyan ( latestVersion ) } is available.` ) ;
273281 }
274282}
275283
284+ export async function getLatestVersion ( ) {
285+ const fetchResult = await fetch ( 'https://registry.npmjs.org/zenstack' , {
286+ headers : { accept : 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' } ,
287+ signal : AbortSignal . timeout ( CHECK_VERSION_TIMEOUT ) ,
288+ } ) ;
289+
290+ if ( fetchResult . ok ) {
291+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
292+ const data : any = await fetchResult . json ( ) ;
293+ const latestVersion = data ?. [ 'dist-tags' ] ?. latest ;
294+ if ( typeof latestVersion === 'string' && semver . valid ( latestVersion ) ) {
295+ return latestVersion ;
296+ }
297+ }
298+
299+ throw new Error ( 'invalid npm registry response' ) ;
300+ }
301+
276302export async function formatDocument ( fileName : string , isPrismaStyle = true ) {
277303 const services = createZModelServices ( NodeFileSystem ) . ZModel ;
278304 const extensions = services . LanguageMetaData . fileExtensions ;
0 commit comments