import fs from "fs/promises" ; import path from "path" ; import type { Options } from "@miniflare/shared" ; import { red } from "kleur/colors" ; import type { MiniflareOptions } from "miniflare" ; import open from "open" ; import { updateCheck } from "./updater" ; function suppressWarnings ( ) { const originalEmitWarning = process . emitWarning ; process . emitWarning = ( warning , ctorTypeOptions , ctorCode , ctor ) => { if ( ctorTypeOptions === "ExperimentalWarning" ) { const warningString = warning . toString ( ) ; if ( warningString . startsWith ( "VM Modules" ) || warningString . startsWith ( "stream/web" ) || warningString . startsWith ( "buffer.Blob" ) ) { return ; } } originalEmitWarning ( warning , ctorTypeOptions , ctorCode , ctor ) ; } ; } async function main ( ) { const { ParseError , buildHelp , parseArgv , } = require ( "@miniflare/cli-parser" ) ; const { Log , LogLevel , } = require ( "@miniflare/shared" ) ; const { Miniflare , PLUGINS , } = require ( "miniflare" ) ; let options ; try { options = parseArgv ( PLUGINS , process . argv . slice ( 2 ) ) ; } catch ( e ) { if ( ! ( e instanceof ParseError ) ) throw e ; if ( e . code === "ERR_VERSION" ) { console . error ( e . message ) ; return ; } const execName = process . env . MINIFLARE_EXEC_NAME ?? "miniflare" ; console . error ( buildHelp ( PLUGINS , execName ) ) ; if ( e . code === "ERR_HELP" ) return ; console . error ( `\n${ red ( e . message ) }` ) ; process . exitCode = 1 ; return ; } options . wranglerConfigPath ??= true ; options . packagePath ??= true ; options . envPathDefaultFallback = true ; if ( options . buildWatchPaths ?. length || options . liveReload ) { options . watch = true ; } if ( options . durableObjects && Object . keys ( options . durableObjects ) . length ) { options . modules = true ; } const logLevel = options ?. verbose ? LogLevel . VERBOSE : options ?. debug ? LogLevel . DEBUG : LogLevel . INFO ; const mfOptions = options as MiniflareOptions ; mfOptions . log = new Log ( logLevel ) ; mfOptions . sourceMap = true ; mfOptions . logUnhandledRejections = true ; const mf = new Miniflare ( mfOptions ) ; try { await mf . startServer ( ) ; await mf . startScheduler ( ) ; } catch ( e ) { mf . log . error ( e ) ; process . exitCode = 1 ; await mf . dispose ( ) ; return ; } const openURL = await mf . getOpenURL ( ) ; try { if ( openURL ) await open ( openURL ) ; } catch ( e ) { mf . log . warn ( "Unable to open browser: " + e . stack ) ; } const plugins = await mf . getPlugins ( ) ; if ( plugins . CorePlugin . updateCheck === false ) return ; try { const pkgFile = path . join ( __dirname , ".." , ".." , "package.json" ) ; const pkg = JSON . parse ( await fs . readFile ( pkgFile , "utf8" ) ) ; const cacheDir = path . resolve ( "node_modules" , ".mf" ) ; await fs . mkdir ( cacheDir , { recursive : true } ) ; const lastCheckFile = path . join ( cacheDir , "update-check" ) ; await updateCheck ( { pkg , lastCheckFile , log : mf . log } ) ; } catch ( e ) { mf . log . debug ( "Unable to check for updates: " + e . stack ) ; } } suppressWarnings ( ) ; void main ( ) ;