|
| 1 | +import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { inspect } from 'node:util' |
| 4 | + |
| 5 | +import { request } from '@octokit/request' |
| 6 | +import { compile } from 'nexe' |
| 7 | + |
| 8 | + |
| 9 | +const skipUpload = process.argv.length > 2 && process.argv[2] === '--skip-upload' |
| 10 | + |
| 11 | +// valid platform values: 'windows' | 'mac' | 'alpine' | 'linux' // NodePlatform in nexe |
| 12 | +// valid arch values 'x86' | 'x64' | 'arm' | 'arm64' // NodeArch in nexe |
| 13 | + |
| 14 | +const osByPlatform = { |
| 15 | + darwin: 'mac', |
| 16 | + win32: 'windows', |
| 17 | +} |
| 18 | + |
| 19 | +const os = osByPlatform[process.platform] ?? process.platform |
| 20 | +const arch = process.arch |
| 21 | +const version = process.version.substring(1) |
| 22 | + |
| 23 | +const target = `${os}-${arch}-${version}` |
| 24 | + |
| 25 | +console.log(`building ${version}`) |
| 26 | +console.log(`process.arch = [${process.arch}]`) |
| 27 | +console.log(`process.platform = [${process.platform}]`) |
| 28 | +console.log(`target = ${target}`) |
| 29 | + |
| 30 | +mkdir('dist').catch((error) => { |
| 31 | + if (error.code !== 'EEXIST') throw error |
| 32 | +}) |
| 33 | + |
| 34 | +const owner = 'SmartThingsCommunity' |
| 35 | +const repo = 'cli-nexe-builds' |
| 36 | + |
| 37 | +const ghToken = process.env.GH_TOKEN |
| 38 | + |
| 39 | +if (!ghToken) { |
| 40 | + console.error('Did not get github token. Missing secret?') |
| 41 | + process.exit(1) |
| 42 | +} |
| 43 | + |
| 44 | +const __dirname = import.meta.dirname |
| 45 | +const packageData = JSON.parse(await readFile(path.join(__dirname, '../package.json'))) |
| 46 | +const releaseVersion = packageData.version |
| 47 | + |
| 48 | +const gitAPIHeaders = { |
| 49 | + authorization: `token ${ghToken}`, |
| 50 | +} |
| 51 | +const releases = (await request("GET /repos/:owner/:repo/releases", { |
| 52 | + headers: gitAPIHeaders, |
| 53 | + owner, |
| 54 | + repo, |
| 55 | +})).data |
| 56 | +const release = releases.find(release => release.tag_name === releaseVersion) |
| 57 | + |
| 58 | +const asset = release.assets?.find(asset => asset.name === target) |
| 59 | + |
| 60 | +const outputFilename = path.join(__dirname, `../dist/${target}`) |
| 61 | +if (asset) { |
| 62 | + console.log('Found asset already exists; skipping.') |
| 63 | +} else { |
| 64 | + console.log(`Building ${outputFilename}.`) |
| 65 | + compile({ |
| 66 | + input: 'bin/dummy.mjs', |
| 67 | + build: true, |
| 68 | + verbose: true, |
| 69 | + mangle: false, |
| 70 | + output: outputFilename, |
| 71 | + python: 'python3', |
| 72 | + targets: [target], |
| 73 | + }).then(async () => { |
| 74 | + if (skipUpload) { |
| 75 | + console.log('Build finished; skipping upload.') |
| 76 | + } else { |
| 77 | + console.log('Build finished; uploading asset.') |
| 78 | + |
| 79 | + const currentDir = path.join(__dirname, '..') |
| 80 | + const distFiles = await readdir(path.join(currentDir, 'dist')) |
| 81 | + console.log(`files in dist dir = ${JSON.stringify(distFiles)}`) |
| 82 | + |
| 83 | + const filename = os === 'windows' ? `${outputFilename}.exe` : outputFilename |
| 84 | + const buildFileContents = await readFile(filename) |
| 85 | + console.log(`read file containing ${buildFileContents.length} bytes`) |
| 86 | + await request( |
| 87 | + `POST /repos/:owner/:repo/releases/:release_id/assets?name=:name`, |
| 88 | + { |
| 89 | + baseUrl: "https://uploads.github.com", |
| 90 | + headers: { |
| 91 | + 'Content-Type': 'application/x-binary', |
| 92 | + 'Content-Length': buildFileContents.length, |
| 93 | + ...gitAPIHeaders, |
| 94 | + }, |
| 95 | + name: target, |
| 96 | + owner, |
| 97 | + repo, |
| 98 | + release_id: release.id, |
| 99 | + data: buildFileContents, |
| 100 | + }, |
| 101 | + ) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
0 commit comments