import * as fs from "fs-extra" ; import * as path from "path" ; import { cargoInstall } from "./cargoBuild" ; import { fetchDistributable , getDownloadUrl } from "./fetchDist" ; import { getExecutableFileName , moveFromPath , verifyOutputFile } from "./fsUtil" ; import { ConsoleLogger , ILogger } from "./logger" ; /**
 * Should correspond to both a git tag on the blockstack-core repo and a
 * set of clarity-binary distributables uploaded to the cloud storage endpoint.
 */ export const CORE_SDK_TAG = "2.0.11.0.0" ; export const BLOCKSTACK_CORE_SOURCE_TAG_ENV_VAR = "BLOCKSTACK_CORE_SOURCE_TAG" ; export const BLOCKSTACK_CORE_SOURCE_BRANCH_ENV_VAR = "BLOCKSTACK_CORE_SOURCE_BRANCH" ; export const BLOCKSTACK_CORE_SOURCE_PATH_ENV_VAR = "BLOCKSTACK_CORE_SOURCE_PATH" ; returns function getOverriddenCoreSource ( ) { for ( const [ key , val ] of Object . entries ( process . env ) ) { if ( val === undefined ) { continue ; } const keyStr = key . toLocaleUpperCase ( ) ; if ( keyStr === BLOCKSTACK_CORE_SOURCE_TAG_ENV_VAR ) { return { specifier : "tag" , value : val } ; } else if ( keyStr === BLOCKSTACK_CORE_SOURCE_BRANCH_ENV_VAR ) { return { specifier : "branch" , value : val } ; } else if ( keyStr === BLOCKSTACK_CORE_SOURCE_PATH_ENV_VAR ) { return { specifier : "path" , value : val } ; } } return false ; } see https function getThisPackageDir ( ) { const packagePath = path . dirname ( require . resolve ( "../package.json" ) ) ; return packagePath ; } param checkExists param versionTag export function getDefaultBinaryFilePath ( { checkExists = true , versionTag , } = { } ) { if ( ! versionTag ) { versionTag = CORE_SDK_TAG ; } const thisPkgDir = path . resolve ( getThisPackageDir ( ) ) ; const binFileName = getExecutableFileName ( "clarity-cli" ) ; const binFilePath = path . join ( thisPkgDir , ".native-bin" , versionTag , binFileName ) ; if ( checkExists && ! fs . existsSync ( binFilePath ) ) { throw new Error ( `Native binary does not appear to be installed at ${ binFilePath }` ) ; } return binFilePath ; } export async function installDefaultPath ( ) { const installPath = getDefaultBinaryFilePath ( { checkExists : false } ) ; const logger = ConsoleLogger ; let versionTag = CORE_SDK_TAG ; let versionBranch ; let fromSource = false ; const sourceOverride = getOverriddenCoreSource ( ) ; if ( sourceOverride && sourceOverride . specifier === "path" ) { logger . log ( `Found path source env var ${ sourceOverride . specifier }=${ sourceOverride . value }` ) ; return moveFromPath ( { logger , outputFilePath : installPath , inputFilePAth : sourceOverride . value , } ) ; } if ( sourceOverride !== false && sourceOverride . specifier !== "path" ) { logger . log ( `Found git source env var ${ sourceOverride . specifier }=${ sourceOverride . value }` ) ; fromSource = true ; if ( sourceOverride . specifier === "branch" ) { versionTag = undefined ; versionBranch = sourceOverride . value ; } else { versionTag = sourceOverride . value ; } } if ( ! fromSource ) { const distFileAvailable = getDownloadUrl ( logger , CORE_SDK_TAG ) ; if ( ! distFileAvailable ) { fromSource = true ; } } const outputIsValid = verifyOutputFile ( logger , true , installPath ) ; if ( ! outputIsValid ) { return false ; } let success ; if ( fromSource ) { success = await cargoInstall ( { logger : logger , overwriteExisting : true , outputFilePath : installPath , buildPackage : "blockstack-core" , gitBranch : versionBranch , gitTag : versionTag , } ) ; } else { success = await fetchDistributable ( { logger : logger , overwriteExisting : true , outputFilePath : installPath , versionTag : versionTag ! , } ) ; } return success ; } export async function install ( opts ) { const outputIsValid = verifyOutputFile ( opts . logger , opts . overwriteExisting , opts . outputFilePath ) ; if ( ! outputIsValid ) { return false ; } if ( opts . fromSource ) { return cargoInstall ( { ... opts , buildPackage : "blockstack-core" , gitTag : opts . versionTag } ) ; } else { return fetchDistributable ( opts ) ; } }