import https from 'https' ; import path from 'path' ; import fs from 'fs' ; import { promisify } from 'util' ; import { exec } from 'child_process' ; import { Optional } from '../interface/option' ; const jsUrl = 'https://registry.npm.taobao.org/ssr-with-js' ; const tsUrl = 'https://registry.npm.taobao.org/ssr-with-ts' ; const processError = ( err ) => { if ( err ) { console . log ( 'err' , err ) ; process . exit ( ) ; } } ; const execWithPromise = promisify ( exec ) ; const resolveApp = ( source ) => { return path . resolve ( __dirname , `../../${ source }` ) ; } ; const getWithPromise = ( url , timeout ? ) : Promise < any > => { return new Promise ( ( resolve , reject ) => { const timer = setTimeout ( ( ) => { reject ( 'url request timeout:' + url ) ; } , timeout || 5000 ) ; let data = '' ; https . get ( url , res => { clearTimeout ( timer ) ; res . on ( 'data' , ( chunk ) => { data += chunk . toString ( ) ; } ) ; res . on ( 'end' , ( ) => { resolve ( JSON . parse ( data ) ) ; } ) ; } ) . on ( 'error' , ( err ) => { reject ( err ) ; } ) ; } ) ; } ; export param { Optional } option returns { Promise < boolean > } async function getVersionEffective ( option ) { if ( fs . existsSync ( resolveApp ( './cache' ) ) ) { const url = option . language === 'typescript' ? tsUrl : jsUrl ; const language = option . language === 'javascript' ? 'js' : 'ts' ; try { const { 'dist-tags' : { latest } } = await getWithPromise ( url ) ; const localVersion = require ( resolveApp ( `./cache/example/ssr-with-${ language }/package.json` ) ) . version . trim ( ) ; return latest === localVersion ; } catch ( error ) { return true ; } } return false ; } export { getVersionEffective , processError , execWithPromise , getWithPromise , resolveApp } ;