import { PortablePath , ppath , xfs } from "@yarnpkg/fslib" ; import { stringifySyml } from "@yarnpkg/parsers" ; import path from "path" ; import { URL } from "url" ; import { execFile } from "./exec" ; import { Registry } from "./Registry" ; const YARN_RC = stringifySyml ( { checksumBehavior : "ignore" , plugins : [ path . join ( __dirname , "../../bundles/@yarnpkg/plugin-outdated.js" ) ] , } ) ; interface RunOptions { cwd ? : PortablePath ; } const registry = new Registry ( ) ; export async function makeTemporaryEnv ( ) { const [ tempDir , homeDir , registryUrl ] = await Promise . all ( [ xfs . mktempPromise ( ) , xfs . mktempPromise ( ) , registry . start ( ) , ] ) ; const destroy = async ( ) => { await xfs . removePromise ( tempDir , { recursive : true } ) ; await xfs . removePromise ( homeDir , { recursive : true } ) ; } ; const readFile = ( source ) => { const path = ppath . join ( tempDir , source as PortablePath ) ; return xfs . readFilePromise ( path , "utf8" ) ; } ; const writeFile = async ( target , body ) => { const path = target as PortablePath ; await xfs . mkdirpPromise ( ppath . join ( tempDir , ppath . dirname ( path ) ) ) ; await xfs . writeFilePromise ( ppath . join ( tempDir , path ) , body ) ; } ; const writeJSON = async ( target , body ) => { await writeFile ( target , JSON . stringify ( body ) ) ; } ; const run = ( command , { cwd } = { } ) => { const yarnPath = `${ __dirname }/../../.yarn/releases/yarn-3.1.1.cjs` ; const yarnBinary = require . resolve ( yarnPath ) ; return execFile ( process . execPath , [ yarnBinary , ... command . split ( " " ) ] , { cwd : cwd || tempDir , env : { HOME : homeDir , USERPROFILE : homeDir , YARN_CACHE_KEY_OVERRIDE : "0" , YARN_ENABLE_GLOBAL_CACHE : "false" , YARN_ENABLE_INLINE_BUILDS : "false" , YARN_ENABLE_PROGRESS_BARS : "false" , YARN_ENABLE_TELEMETRY : "0" , YARN_ENABLE_TIMERS : "false" , YARN_NPM_REGISTRY_SERVER : registryUrl , YARN_UNSAFE_HTTP_WHITELIST : new URL ( registryUrl ) . hostname , } , } ) ; } ; await writeFile ( ".yarnrc.yml" , YARN_RC ) ; return { destroy , readFile , registry , run , writeFile , writeJSON , } ; }