diff --git a/scripts/afterAllArtifactBuild.js b/scripts/afterAllArtifactBuild.js index 9348414..aeb6912 100644 --- a/scripts/afterAllArtifactBuild.js +++ b/scripts/afterAllArtifactBuild.js @@ -2,7 +2,11 @@ const crypto = require("crypto") const fs = require("fs/promises") const path = require("path") -const ALGORITHMS = ["sha1", "sha256"] +const PACKAGE_JSON_PATH = "package.json" + +const DEFAULT_ALGORITHM = "sha256" +const ALGORITHMS = ["sha1", DEFAULT_ALGORITHM] + const CHUNK_SIZE = 1000000 async function calcChecksum(filePath, algorithm) { @@ -24,15 +28,23 @@ async function calcChecksum(filePath, algorithm) { return hash.digest("hex") } -exports.default = async context => { - const artifactPaths = context.artifactPaths +async function parseJson(path) { + return JSON.parse(await fs.readFile(path, { encoding: "utf-8" })) +} + +async function createChecksumFiles(artifactPaths) { const maxArtifactPathLength = Math.max( ...artifactPaths.map(artifactPath => artifactPath.length), ) const maxAlgorithmNameLength = Math.max(...ALGORITHMS.map(algorithm => algorithm.length)) + const artifacts = [] for (const artifactPath of artifactPaths) { + const artifact = { + path: artifactPath, + } for (const algorithm of ALGORITHMS) { const checksum = await calcChecksum(artifactPath, algorithm) + artifact[algorithm] = checksum await fs.writeFile( `${artifactPath}.${algorithm}`, `${checksum} ${path.basename(artifactPath)}\n`, @@ -43,5 +55,34 @@ exports.default = async context => { )} ${checksum}`, ) } + artifacts.push(artifact) + } + return artifacts +} + +async function createScoopManifest(outDir, artifacts) { + const MANIFEST_TEMPLAE_PATH = "scripts/scoop-manifest.template.json" + const MANIFEST_NAME = "mdview.json" + const INDENTATION = 4 + + const zipArtifact = artifacts.find(artifact => artifact.path.toLowerCase().endsWith(".zip")) + if (!zipArtifact) { + return } + + const version = (await parseJson(PACKAGE_JSON_PATH)).version + const manifest = await parseJson(MANIFEST_TEMPLAE_PATH) + + manifest.version = version + manifest.url = manifest.url.replaceAll("{{ VERSION }}", version) + manifest.hash = zipArtifact[DEFAULT_ALGORITHM] + + const manifestPath = path.join(outDir, MANIFEST_NAME) + await fs.writeFile(manifestPath, JSON.stringify(manifest, null, INDENTATION)) + console.log(`Genrated Scoop manifest: ${manifestPath}`) +} + +exports.default = async context => { + const artifacts = await createChecksumFiles(context.artifactPaths) + await createScoopManifest(context.outDir, artifacts) } diff --git a/scripts/scoop-manifest.template.json b/scripts/scoop-manifest.template.json new file mode 100644 index 0000000..6c9df8f --- /dev/null +++ b/scripts/scoop-manifest.template.json @@ -0,0 +1,18 @@ +{ + "version": "", + "description": "Graphical application to view Markdown files nicely rendered", + "homepage": "https://github.com/c3er/mdview", + "license": "MIT", + "url": "https://github.com/c3er/mdview/releases/download/v{{ VERSION }}/mdview-{{ VERSION }}-x64.zip", + "hash": "", + "bin": "mdview.exe", + "shortcuts": [["mdview.exe", "Markdown Viewer"]], + "persist": ".data", + "checkver": "github", + "autoupdate": { + "url": "https://github.com/c3er/mdview/releases/download/v$version/mdview-$version-x64.zip", + "hash": { + "url": "https://github.com/c3er/mdview/releases/download/v$version/mdview-$version-x64.zip.sha256" + } + } +}