import fs from 'fs' ; import os from 'os' ; import path from 'path' ; import * as cache from '@actions/cache' ; import * as core from '@actions/core' ; import * as io from '@actions/io' ; import * as exec from '@actions/exec' ; import jsSHA from 'jssha/dist/sha256' ; param { string } file returns { string } export function hashFile ( file ) { const sha = new jsSHA ( 'SHA-256' , 'TEXT' , { encoding : 'UTF8' } ) ; sha . update ( fs . readFileSync ( file ) . toString ( ) ) ; return sha . getHash ( 'HEX' ) . slice ( 0 , 8 ) ; } param { string } workspace export async function restoreWorkspaceCache ( workspace ) { try { core . startGroup ( 'Trying to restore workspace contents from cache...' ) ; const hash = hashFile ( path . join ( workspace , 'repos.md' ) ) ; const paths = [ path . join ( workspace , 'workspace' ) ] ; const cacheHit = await cache . restoreCache ( paths , `scala-steward-${ hash }-${ Date . now ( ) . toString ( ) }` , [ `scala-steward-${ hash }` , 'scala-steward-' ] ) ; if ( cacheHit ) { core . info ( 'Scala Steward workspace contents restored from cache' ) ; } else { core . info ( 'Scala Steward workspace contents weren\'t found on cache' ) ; } core . endGroup ( ) ; } catch ( error ) { core . debug ( ( error as Error ) . message ) ; core . warning ( 'Unable to restore workspace from cache' ) ; core . endGroup ( ) ; } } param { string } workspace export async function saveWorkspaceCache ( workspace ) { try { core . startGroup ( 'Saving workspace to cache...' ) ; await io . rmRF ( path . join ( workspace , 'workspace' , 'store' , 'refresh_error' ) ) ; await io . rmRF ( path . join ( workspace , 'workspace' , 'repos' ) ) ; const hash = hashFile ( path . join ( workspace , 'repos.md' ) ) ; await cache . saveCache ( [ path . join ( workspace , 'workspace' ) ] , `scala-steward-${ hash }-${ Date . now ( ) . toString ( ) }` ) ; core . info ( 'Scala Steward workspace contents saved to cache' ) ; core . endGroup ( ) ; } catch ( error ) { core . debug ( ( error as Error ) . message ) ; core . warning ( 'Unable to save workspace to cache' ) ; core . endGroup ( ) ; } } param { Buffer } reposList param { string } token returns { string } export async function prepare ( reposList , token ) { try { const stewarddir = `${ os . homedir ( ) }/scala-steward` ; await io . mkdirP ( stewarddir ) ; fs . writeFileSync ( `${ stewarddir }/repos.md` , reposList ) ; fs . writeFileSync ( `${ stewarddir }/askpass.sh` , `#!/bin/sh\n\necho '${ token }'` ) ; await exec . exec ( 'chmod' , [ '+x' , `${ stewarddir }/askpass.sh` ] , { silent : true } ) ; core . info ( '✓ Scala Steward workspace created' ) ; return stewarddir ; } catch ( error ) { core . debug ( ( error as Error ) . message ) ; throw new Error ( 'Unable to create Scala Steward workspace' ) ; } } /**
 * Removes the Scala Steward's workspace.
 */ export async function remove ( ) { await io . rmRF ( `${ os . homedir ( ) }/scala-steward` ) ; }