Created
September 21, 2024 08:59
-
-
Save thomaspoignant/4402f870806388e070003b5e6ed12eaf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
import * as fs from "fs"; | |
import {writeFile} from "fs/promises"; | |
import {configuration} from "../config/config"; | |
const tarToZip = require('tar-to-zip'); | |
const downloadGoffFromGitHubRelease = async (tag: string, destinationPath: string, repoSlug: string, templatedFileName: string): Promise<string> => { | |
const version = tag.replace('v', ''); | |
const fileName = templatedFileName.replace('{version}', version); | |
const url = `https://github.com/${repoSlug}/releases/download/${tag}/${fileName}` | |
const response = await fetch(url); | |
if (!response.ok) { | |
throw new Error(`Failed to download file: ${response.statusText}`); | |
} | |
createArtifactDirectory(destinationPath); | |
const buffer = await response.arrayBuffer(); | |
await writeFile(`${destinationPath}/${fileName}`, Buffer.from(buffer)); | |
return `${destinationPath}/${fileName}`; | |
} | |
const prepareLambda = async (input: string, output: string) => { | |
const zip = fs.createWriteStream(output); | |
const map = ({name}: { name: string }) => { | |
return { | |
name: name.replace(/^go-feature-flag$/, 'bootstrap') | |
}; | |
}; | |
tarToZip(input, {map}) | |
.getStream() | |
.pipe(zip); | |
}; | |
const createArtifactDirectory = (location: string) => { | |
if (!fs.existsSync(location)) { | |
fs.mkdirSync(location, {recursive: true}); | |
} | |
} | |
(async () => { | |
try { | |
const downloadResults = await Promise.all( | |
[ | |
"go-feature-flag_{version}_Linux_arm64.tar.gz", | |
"go-feature-flag-editor_{version}_Linux_arm64.tar.gz" | |
].map(templatedFileName => | |
downloadGoffFromGitHubRelease(configuration.version, 'artifacts.out', 'thomaspoignant/go-feature-flag', templatedFileName) | |
) | |
); | |
for (const result of downloadResults) { | |
const output = result.replace('artifacts.out/','').split('_')[0]; | |
await prepareLambda(result, `artifacts.out/${output}.zip`); | |
} | |
console.log("SUCCESS") | |
} catch (error) { | |
console.error(error); | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment