1+ import crypto from 'node:crypto' ;
12import fs from 'node:fs' ;
23import os from 'node:os' ;
34import path from 'node:path' ;
@@ -37,24 +38,35 @@ export function getToolsDir() {
3738 return path . join ( getProtoHome ( ) , 'tools' ) ;
3839}
3940
41+ export function getUidFile ( ) {
42+ return path . join ( getProtoHome ( ) , 'id' ) ;
43+ }
44+
4045export function getWorkingDir ( ) {
4146 return process . env . GITHUB_WORKSPACE ?? process . cwd ( ) ;
4247}
4348
44- export function isCacheEnabled ( ) : boolean {
49+ export function isCacheEnabled ( ) {
4550 return core . getBooleanInput ( 'cache' ) && cache . isFeatureAvailable ( ) ;
4651}
4752
4853export function isUsingMoon ( ) {
4954 return fs . existsSync ( path . join ( getWorkingDir ( ) , core . getInput ( 'workspace-root' ) , '.moon' ) ) ;
5055}
5156
57+ export function extractMajorMinor ( version : string ) {
58+ const [ major , minor ] = version . split ( '.' ) ;
59+
60+ return `${ major } .${ minor } ` ;
61+ }
62+
5263export function getCacheKeyPrefix ( ) {
5364 // v1 - Before proto v0.24 changes
5465 return 'moonrepo-toolchain-v2' ;
5566}
5667
5768export async function getToolchainCacheKey ( ) {
69+ const hasher = crypto . createHash ( 'sha1' ) ;
5870 const files = [ '.prototools' ] ;
5971
6072 if ( isUsingMoon ( ) ) {
@@ -67,9 +79,27 @@ export async function getToolchainCacheKey() {
6779 }
6880 }
6981
70- const toolchainHash = await glob . hashFiles ( files . join ( '\n' ) ) ;
82+ core . debug ( `Hashing files: ${ files . join ( ', ' ) } ` ) ;
7183
72- return `${ getCacheKeyPrefix ( ) } -${ process . platform } -${ toolchainHash } ` ;
84+ hasher . update ( await glob . hashFiles ( files . join ( '\n' ) ) ) ;
85+
86+ const protoVersion = process . env . PROTO_CLI_VERSION ;
87+
88+ if ( protoVersion ) {
89+ core . debug ( `Hashing proto version: ${ protoVersion } ` ) ;
90+
91+ hasher . update ( extractMajorMinor ( protoVersion ) ) ;
92+ }
93+
94+ const moonVersion = process . env . MOON_CLI_VERSION ;
95+
96+ if ( moonVersion ) {
97+ core . debug ( `Hashing moon version: ${ moonVersion } ` ) ;
98+
99+ hasher . update ( extractMajorMinor ( moonVersion ) ) ;
100+ }
101+
102+ return `${ getCacheKeyPrefix ( ) } -${ process . platform } -${ hasher . digest ( 'hex' ) } ` ;
73103}
74104
75105export async function installBin ( bin : string ) {
@@ -98,20 +128,28 @@ export async function installBin(bin: string) {
98128
99129 const binDir = getBinDir ( ) ;
100130 const binPath = path . join ( binDir , WINDOWS ? `${ bin } .exe` : bin ) ;
131+ const envPrefix = bin . toUpperCase ( ) ;
101132
102133 await execa ( script , version === 'latest' ? [ ] : [ version ] , {
103134 env : {
104- [ `${ bin . toUpperCase ( ) } _INSTALL_DIR` ] : binDir ,
135+ [ `${ envPrefix } _INSTALL_DIR` ] : binDir ,
105136 } ,
106- stdio : core . isDebug ( ) || ! ! process . env [ `${ bin . toUpperCase ( ) } _DEBUG` ] ? 'inherit' : 'pipe' ,
137+ stdio : core . isDebug ( ) || ! ! process . env [ `${ envPrefix } _DEBUG` ] ? 'inherit' : 'pipe' ,
107138 } ) ;
108139
109140 core . info ( `Installed binary to ${ binPath } ` ) ;
110141
111142 core . info ( 'Checking version' ) ;
112143
113144 try {
114- await execa ( binPath , [ '--version' ] , { stdio : 'inherit' } ) ;
145+ const result = await execa ( binPath , [ '--version' ] , { stdio : 'pipe' } ) ;
146+
147+ if ( result . stdout ) {
148+ // eslint-disable-next-line require-atomic-updates
149+ process . env [ `${ envPrefix } _CLI_VERSION` ] = result . stdout . replace ( bin , '' ) . trim ( ) ;
150+
151+ core . info ( result . stdout ) ;
152+ }
115153 } catch ( error ) {
116154 core . error ( String ( error ) ) ;
117155 }
0 commit comments