forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Desktop: Seamless-Updates: generated and uploaded latest-mac-arm64.ym…
…l to GitHub Releases (laurent22#11042)
- Loading branch information
1 parent
4fa61e4
commit 5763de3
Showing
10 changed files
with
278 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as crypto from 'crypto'; | ||
|
||
export interface GenerateInfo { | ||
version: string; | ||
dmgPath: string; | ||
zipPath: string; | ||
releaseDate: string; | ||
} | ||
|
||
const calculateHash = (filePath: string): string => { | ||
const fileBuffer = fs.readFileSync(filePath); | ||
const hashSum = crypto.createHash('sha512'); | ||
hashSum.update(fileBuffer); | ||
return hashSum.digest('base64'); | ||
}; | ||
|
||
const getFileSize = (filePath: string): number => { | ||
return fs.statSync(filePath).size; | ||
}; | ||
|
||
export const generateLatestArm64Yml = (info: GenerateInfo, destinationPath: string): string | undefined => { | ||
if (!fs.existsSync(info.dmgPath) || !fs.existsSync(info.zipPath)) { | ||
throw new Error(`One or both executable files do not exist: ${info.dmgPath}, ${info.zipPath}`); | ||
} | ||
if (!info.version) { | ||
throw new Error('Version is empty'); | ||
} | ||
if (!destinationPath) { | ||
throw new Error('Destination path is empty'); | ||
} | ||
|
||
console.info('Calculating hash of files...'); | ||
const dmgHash: string = calculateHash(info.dmgPath); | ||
const zipHash: string = calculateHash(info.zipPath); | ||
|
||
console.info('Calculating size of files...'); | ||
const dmgSize: number = getFileSize(info.dmgPath); | ||
const zipSize: number = getFileSize(info.zipPath); | ||
|
||
console.info('Generating content of latest-mac-arm64.yml file...'); | ||
|
||
if (!fs.existsSync(destinationPath)) { | ||
fs.mkdirSync(destinationPath); | ||
} | ||
|
||
const yamlFilePath: string = path.join(destinationPath, 'latest-mac-arm64.yml'); | ||
const yamlContent = `version: ${info.version} | ||
files: | ||
- url: ${path.basename(info.zipPath)} | ||
sha512: ${zipHash} | ||
size: ${zipSize} | ||
- url: ${path.basename(info.dmgPath)} | ||
sha512: ${dmgHash} | ||
size: ${dmgSize} | ||
path: ${path.basename(info.zipPath)} | ||
sha512: ${zipHash} | ||
releaseDate: '${info.releaseDate}' | ||
`; | ||
|
||
fs.writeFileSync(yamlFilePath, yamlContent); | ||
console.log(`YML file for version ${info.version} was generated successfully at ${destinationPath} for arm64.`); | ||
|
||
const fileContent: string = fs.readFileSync(yamlFilePath, 'utf8'); | ||
console.log('Generated YML Content:\n', fileContent); | ||
|
||
return yamlFilePath; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as fs from 'fs'; | ||
import { createWriteStream } from 'fs'; | ||
import * as path from 'path'; | ||
import { promisify } from 'util'; | ||
import { GitHubRelease, GitHubReleaseAsset } from '../utils/checkForUpdatesUtils'; | ||
|
||
const pipeline = promisify(require('stream').pipeline); | ||
|
||
export interface Context { | ||
repo: string; // {owner}/{repo} | ||
githubToken: string; | ||
targetTag: string; | ||
} | ||
|
||
const apiBaseUrl = 'https://api.github.com/repos/'; | ||
const defaultApiHeaders = (context: Context) => ({ | ||
'Authorization': `token ${context.githubToken}`, | ||
'X-GitHub-Api-Version': '2022-11-28', | ||
'Accept': 'application/vnd.github+json', | ||
}); | ||
|
||
export const getTargetRelease = async (context: Context, targetTag: string): Promise<GitHubRelease> => { | ||
console.log('Fetching releases...'); | ||
|
||
// Note: We need to fetch all releases, not just /releases/tag/tag-name-here. | ||
// The latter doesn't include draft releases. | ||
|
||
const result = await fetch(`${apiBaseUrl}${context.repo}/releases`, { | ||
method: 'GET', | ||
headers: defaultApiHeaders(context), | ||
}); | ||
|
||
const releases = await result.json(); | ||
if (!result.ok) { | ||
throw new Error(`Error fetching release: ${JSON.stringify(releases)}`); | ||
} | ||
|
||
for (const release of releases) { | ||
if (release.tag_name === targetTag) { | ||
return release; | ||
} | ||
} | ||
|
||
throw new Error(`No release with tag ${targetTag} found!`); | ||
}; | ||
|
||
// Download a file from Joplin Desktop releases | ||
export const downloadFile = async (asset: GitHubReleaseAsset, destinationDir: string): Promise<string> => { | ||
const downloadPath = path.join(destinationDir, asset.name); | ||
if (!fs.existsSync(destinationDir)) { | ||
fs.mkdirSync(destinationDir); | ||
} | ||
|
||
/* eslint-disable no-console */ | ||
console.log(`Downloading ${asset.name} to ${downloadPath}`); | ||
const response = await fetch(asset.browser_download_url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to download file: Status Code ${response.status}`); | ||
} | ||
const fileStream = createWriteStream(downloadPath); | ||
await pipeline(response.body, fileStream); | ||
console.log('Download successful!'); | ||
/* eslint-enable no-console */ | ||
return downloadPath; | ||
}; | ||
|
||
export const updateReleaseAsset = async (context: Context, assetUrl: string, newName: string) => { | ||
console.log('Updating asset with URL', assetUrl, 'to have name, ', newName); | ||
|
||
// See https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#update-a-release-asset | ||
const result = await fetch(assetUrl, { | ||
method: 'PATCH', | ||
headers: defaultApiHeaders(context), | ||
body: JSON.stringify({ | ||
name: newName, | ||
}), | ||
}); | ||
|
||
if (!result.ok) { | ||
throw new Error(`Unable to update release asset: ${await result.text()}`); | ||
} | ||
}; | ||
|
||
export const uploadReleaseAsset = async (context: Context, release: GitHubRelease, filePath: string): Promise<void> => { | ||
console.log(`Uploading file from ${filePath} to release ${release.tag_name}`); | ||
|
||
const fileContent = fs.readFileSync(filePath); | ||
const fileName = path.basename(filePath); | ||
const uploadUrl = `https://uploads.github.com/repos/${context.repo}/releases/${release.id}/assets?name=${encodeURIComponent(fileName)}`; | ||
|
||
const response = await fetch(uploadUrl, { | ||
method: 'POST', | ||
headers: { | ||
...defaultApiHeaders(context), | ||
'Content-Type': 'application/octet-stream', | ||
}, | ||
body: fileContent, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Failed to upload asset: ${await response.text()}`); | ||
} else { | ||
console.log(`${fileName} uploaded successfully.`); | ||
} | ||
}; |
Oops, something went wrong.