2024-09-14 23:16:42 +02:00
import path = require ( 'path' ) ;
import { parseArgs } from 'util' ;
2024-09-16 23:13:24 +02:00
import { Context , downloadFileFromGitHub , getTargetRelease , updateReleaseAsset , uploadReleaseAsset } from './githubReleasesUtils' ;
2024-09-14 23:16:42 +02:00
import { GitHubRelease } from '../utils/checkForUpdatesUtils' ;
import { GenerateInfo , generateLatestArm64Yml } from './generateLatestArm64Yml' ;
const basePath = path . join ( __dirname , '..' ) ;
const downloadDir = path . join ( basePath , 'downloads' ) ;
// Renames release assets in Joplin Desktop releases
const renameReleaseAssets = async ( context : Context , release : GitHubRelease ) = > {
// Patterns used to rename releases
const renamePatterns : [ RegExp , string ] [ ] = [
[ /-arm64\.dmg$/ , '-arm64.DMG' ] ,
] ;
for ( const asset of release . assets ) {
for ( const [ pattern , replacement ] of renamePatterns ) {
if ( asset . name . match ( pattern ) ) {
const newName = asset . name . replace ( pattern , replacement ) ;
await updateReleaseAsset ( context , asset . url , newName ) ;
2024-09-15 16:31:41 +02:00
asset . name = newName ;
2024-09-14 23:16:42 +02:00
// Only rename a release once.
break ;
}
}
}
} ;
// Creates release assets in Joplin Desktop releases
const createReleaseAssets = async ( context : Context , release : GitHubRelease ) = > {
// Create latest-mac-arm64.yml file and publish
2024-09-17 10:35:39 +02:00
let dmgPath : string ;
let zipPath : string ;
2024-09-14 23:16:42 +02:00
for ( const asset of release . assets ) {
2024-09-17 10:35:39 +02:00
console . log ( ` Checking asset: ${ asset . name } ` ) ;
2024-09-14 23:16:42 +02:00
if ( asset . name . endsWith ( 'arm64.zip' ) ) {
2024-09-16 23:13:24 +02:00
zipPath = await downloadFileFromGitHub ( context , asset , downloadDir ) ;
2024-09-14 23:16:42 +02:00
} else if ( asset . name . endsWith ( 'arm64.DMG' ) ) {
2024-09-16 23:13:24 +02:00
dmgPath = await downloadFileFromGitHub ( context , asset , downloadDir ) ;
2024-09-14 23:16:42 +02:00
}
}
2024-09-17 10:35:39 +02:00
if ( ! zipPath || ! dmgPath ) {
2024-09-15 21:17:44 +02:00
const formattedAssets = release . assets . map ( asset = > ( {
name : asset.name ,
url : asset.url ,
} ) ) ;
throw new Error ( ` Zip path: ${ zipPath } and/or dmg path: ${ dmgPath } are not defined. Logging assets of release: ${ JSON . stringify ( formattedAssets , null , 2 ) } ` ) ;
}
2024-09-14 23:16:42 +02:00
const info : GenerateInfo = {
version : release.tag_name.slice ( 1 ) ,
dmgPath : dmgPath ,
zipPath : zipPath ,
releaseDate : new Date ( ) . toISOString ( ) ,
} ;
const latestArm64FilePath = generateLatestArm64Yml ( info , downloadDir ) ;
2024-09-17 10:35:39 +02:00
await uploadReleaseAsset ( context , release , latestArm64FilePath ) ;
2024-09-14 23:16:42 +02:00
} ;
const modifyReleaseAssets = async ( ) = > {
const args = parseArgs ( {
options : {
tag : { type : 'string' } ,
token : { type : 'string' } ,
repo : { type : 'string' } ,
} ,
} ) ;
if ( ! args . values . tag || ! args . values . token || ! args . values . repo ) {
throw new Error ( [
'Required arguments: --tag, --token, --repo' ,
' --tag should be a git tag with an associated release (e.g. v12.12.12)' ,
' --token should be a GitHub API token' ,
' --repo should be a string in the form user/reponame (e.g. laurent22/joplin)' ,
] . join ( '\n' ) ) ;
}
const context : Context = {
repo : args.values.repo ,
githubToken : args.values.token ,
targetTag : args.values.tag ,
} ;
const release = await getTargetRelease ( context , context . targetTag ) ;
if ( ! release . assets ) {
console . log ( release ) ;
throw new Error ( ` Release ${ release . tag_name } missing assets! ` ) ;
}
console . log ( 'Renaming release assets for tag' , context . targetTag , context . repo ) ;
2024-09-17 10:35:39 +02:00
await renameReleaseAssets ( context , release ) ;
2024-09-14 23:16:42 +02:00
console . log ( 'Creating latest-mac-arm64.yml asset for tag' , context . targetTag , context . repo ) ;
2024-09-17 10:35:39 +02:00
await createReleaseAssets ( context , release ) ;
2024-09-14 23:16:42 +02:00
} ;
void modifyReleaseAssets ( ) ;