import * as path from "path" ; import * as fse from "fs-extra" ; export enum SetupPlatform { CloudflarePages = "cloudflare-pages" , CloudflareWorkers = "cloudflare-workers" , Node = "node" } export function isSetupPlatform ( platform ) { return [ SetupPlatform . CloudflarePages , SetupPlatform . CloudflareWorkers , SetupPlatform . Node ] . includes ( platform ) ; } export async function setupRemix ( platform ) { let remixPkgJsonFile ; try { remixPkgJsonFile = resolvePackageJsonFile ( "remix" ) ; } catch ( error ) { if ( error . code === "MODULE_NOT_FOUND" ) { console . error ( `Missing the "remix" package. Please run \`npm install remix\` before \`remix setup\`.` ) ; return ; } else { throw error ; } } let platformPkgJsonFile = resolvePackageJsonFile ( `@remix-run/${ platform }` ) ; let serverPkgJsonFile = resolvePackageJsonFile ( `@remix-run/server-runtime` ) ; let clientPkgJsonFile = resolvePackageJsonFile ( `@remix-run/react` ) ; let remixDeps = { } ; await assignDependency ( remixDeps , platformPkgJsonFile ) ; await assignDependency ( remixDeps , serverPkgJsonFile ) ; await assignDependency ( remixDeps , clientPkgJsonFile ) ; let remixPkgJson = await fse . readJSON ( remixPkgJsonFile ) ; remixPkgJson . dependencies = remixDeps ; await fse . writeJSON ( remixPkgJsonFile , remixPkgJson , { spaces : 2 } ) ; let remixPkgDir = path . dirname ( remixPkgJsonFile ) ; let platformExportsDir = path . resolve ( platformPkgJsonFile , ".." , "magicExports" ) ; let serverExportsDir = path . resolve ( serverPkgJsonFile , ".." , "magicExports" ) ; let clientExportsDir = path . resolve ( clientPkgJsonFile , ".." , "magicExports" ) ; await Promise . all ( [ fse . copy ( platformExportsDir , remixPkgDir ) , fse . copy ( serverExportsDir , remixPkgDir ) , fse . copy ( clientExportsDir , remixPkgDir ) ] ) ; } function resolvePackageJsonFile ( packageName ) { return require . resolve ( path . join ( packageName , "package.json" ) ) ; } async function assignDependency ( deps , pkgJsonFile ) { let pkgJson = await fse . readJSON ( pkgJsonFile ) ; deps [ pkgJson . name ] = pkgJson . version ; }