Skip to content

Commit

Permalink
🐛 Fix: Build Multi archs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashu11-A committed May 4, 2024
1 parent 4516173 commit c878043
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 51 deletions.
58 changes: 42 additions & 16 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,66 @@ jobs:
name: Upload artifact
with:
name: pre-build
path: ./
path: ./build

build_multi_arch:
name: Build Multi Archs
build_multi_linux:
name: Build Linux ${{ matrix.arch }}
needs: prepare
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest] #[windows-latest]
arch: [aarch64, x64]
node-version: [20.x]
steps:
- name: Set up QEMU
if: matrix.os == 'ubuntu-latest'
uses: docker/setup-qemu-action@v3
- name: Checkout repository
uses: actions/checkout@v4

- name: Display structure
run: ls

- uses: actions/download-artifact@v4
with:
github-token: ${{ github.token }}
name: pre-build
path: ./
path: ./build

- uses: uraimo/run-on-arch-action@v2
name: Build artifact ${{ matrix.arch }}
if: matrix.arch == 'aarch64'
id: build
with:
arch: ${{ matrix.arch }}
distro: ubuntu-latest
githubToken: ${{ github.token }}
install: |
apt update && apt upgrade -y && apt install -y curl
curl -fsSL https://fnm.vercel.app/install | bash
fnm install ${{ matrix.node-version }}
fnm use ${{ matrix.node-version }}
rm -rf node_modules && npm i
run: |
npm run build:release -- --only-build
- name: Rebuild Modules Linux
if: matrix.os == 'ubuntu-latest'
run: rm -rf node_modules && npm ci
- name: Node.js ${{ matrix.node-version }}
if: matrix.arch == 'x64'
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
check-latest: true

- name: Rebuild Modules Windows
if: matrix.os == 'windows-latest'
run: rm -r node_modules && npm ci
- name: Rebuild Modules
if: matrix.arch == 'x64'
run: rm -rf node_modules && npm ci

- name: Build Release
if: matrix.arch == 'x64'
run: npm run build:release -- --only-build

- uses: actions/upload-artifact@v4
name: Upload artifact
with:
name: build-${{ matrix.os }}
name: build-ubuntu-latest-${{ matrix.arch }}
path: ./build

- name: Upload binaries to release
Expand Down
74 changes: 39 additions & 35 deletions release.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { exec } from '@yao-pkg/pkg'
import { exec as processChild } from 'child_process'
import { type ExecException, exec as processChild } from 'child_process'
import { Presets, SingleBar } from 'cli-progress'
import { createHash } from 'crypto'
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs'
import { readFile, rm, rmdir, stat, writeFile } from 'fs/promises'
import { readFile, rm, stat, writeFile } from 'fs/promises'
import { glob } from 'glob'
import { obfuscate } from 'javascript-obfuscator'
import os from 'os'
Expand All @@ -24,43 +24,43 @@ interface BuildInfo {
type BuildManifest = Record<string, BuildInfo>

interface Compress {
directory?: string,
directory?: string
outdir?: string
}

interface BuildConstructor {
/**
* Directory of Typescript Resources
*/
directory: string;
directory: string
/**
* Outdir of build
*/
outdir: string;
outdir: string
/**
* Plataforms of builds
*/
platforms: Array<'linux' | 'alpine' | 'linuxstatic' | 'macos'>;
platforms: Array<'linux' | 'alpine' | 'linuxstatic' | 'macos'>
/**
* Archs of build
*/
archs: Array<'x64' | 'arm64'>;
archs: Array<'x64' | 'arm64'>
/**
* Node version of build
*/
nodeVersion: '18' | '20';
nodeVersion: '18' | '20'
/**
* Compress build? (default:true)
*/
compress?: boolean;
compress?: boolean
/**
* Obfuscate build? (default:true)
*/
obfuscate?: boolean;
obfuscate?: boolean
/**
* Make application build? (default:true)
*/
pkgbuild?: boolean;
pkgbuild?: boolean
}

class Build {
Expand All @@ -81,13 +81,13 @@ class Build {

formatBytes (bytes: number, decimals = 2): string {
if (bytes === 0) return '0 Bytes'

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

const i = Math.floor(Math.log(bytes) / Math.log(k))

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}

Expand All @@ -98,8 +98,8 @@ class Build {
console.debug('\n\nIniciando Compressão...')
const paths = await glob([`${directory}/**/*.js`])
const builded: Record<string, { size: number }> = {}
const cacheBuilded = JSON.parse(await readFile('release/build.json', { encoding: 'utf-8'}).catch(() => '{}')) as Record<string, { size: number }>
const cacheBuilded = JSON.parse(await readFile('release/build.json', { encoding: 'utf-8' }).catch(() => '{}')) as Record<string, { size: number }>

this.progressBar.start(paths.length, 0)

for (const filePath of paths) {
Expand All @@ -112,13 +112,12 @@ class Build {
const fileContent = await readFile(filePath)
const fileName = path.basename(filePath)

if (cacheBuilded[`${newPath}/${fileName}`]?.size === (await readFile(`${newPath}/${fileName}`).catch(() => new Buffer(''))).byteLength) {
if (cacheBuilded[`${newPath}/${fileName}`]?.size === (await readFile(`${newPath}/${fileName}`).catch(() => Buffer.from(''))).byteLength) {
console.log(`Arquivo: ${filePath} já foi comprimido!`)
this.progressBar.increment()
continue
}


if (!existsSync(newPath)) mkdirSync(newPath, { recursive: true })

const result = await minify({ [filePath]: fileContent.toString('utf-8') }, {
Expand Down Expand Up @@ -184,7 +183,7 @@ class Build {

writeFileSync(filePath, response.getObfuscatedCode(), 'utf8')
this.progressBar.increment()
}
}
this.progressBar.stop()
}

Expand All @@ -194,44 +193,44 @@ class Build {
await writeFile(path.join(process.cwd(), 'build/src/settings/settings.json'), json)

console.debug('Configurando package.json')
const packageJson = JSON.parse(await readFile(path.join(process.cwd(), 'package.json'), { encoding: 'utf-8' })) as Record<string, string | Object | null>
packageJson['main'] = 'src/app.js'
packageJson['bin'] = 'src/app.js'
const packageJson = JSON.parse(await readFile(path.join(process.cwd(), 'package.json'), { encoding: 'utf-8' })) as Record<string, string | object | null>
packageJson.main = 'src/app.js'
packageJson.bin = 'src/app.js'
const remove = ['devDependencies', 'scripts', 'keywords', 'pkg']

for (const name of remove) {
delete packageJson[name]
delete packageJson?.[name]
}

await writeFile(path.join(process.cwd(), 'build/package.json'), JSON.stringify(packageJson, null, 2))
}

async installModules() {
async installModules (): Promise<void> {
console.debug('Baixando node_modules, sem as devDependencies\n\n')
if (existsSync(`${this.options.outdir}/node_modules`)) {
await new Promise<void>(async (resolve, reject) => {
processChild(`cd ${this.options.outdir} && rm -r node_modules`, (error, stdout, stderr) => {
await new Promise<ExecException | null>((resolve, reject) => {
processChild(`cd ${this.options.outdir} && rm -r node_modules`, (error, _stdout, stderr) => {
if (error !== null || stderr !== '') {
reject(error ?? stderr)
}
resolve()
resolve(null)
})
})
}
await new Promise<void>(async (resolve, reject) => {
processChild(`cd ${this.options.outdir} && npm i && npm rebuild better_sqlite3 && npm rebuild`, (error, stdout, stderr) => {
await new Promise<ExecException | null>((resolve, reject) => {
processChild(`cd ${this.options.outdir} && npm i && npm rebuild better_sqlite3 && npm rebuild`, (error, _stdout, stderr) => {
if (error !== null || stderr !== '') {
reject(error ?? stderr)
}
resolve()
resolve(null)
})
})
}

async clearModules (): Promise<void> {
const removeModules = await glob([`${this.options.outdir}/node_modules/**/*`], {
ignore: ['/**/*.js', '/**/*.json', '/**/*.cjs', '/**/*.node', '/**/*.yml'],
});
ignore: ['/**/*.js', '/**/*.json', '/**/*.cjs', '/**/*.node', '/**/*.yml']
})

console.debug('\n\nRemovendo arquivos inúteis...')
this.progressBar.start(removeModules.length, 0)
Expand All @@ -241,7 +240,7 @@ class Build {
this.progressBar.increment()
}

this.progressBar.stop
this.progressBar.stop()
}

async pkgbuild (): Promise<void> {
Expand Down Expand Up @@ -316,15 +315,20 @@ class Build {

const args = process.argv.slice(2)
const version = process.version.split('.')[0].replace('v', '')
const arch = process.arch

if (!(version === '18' || version === '20')) {
throw new Error(`Versão do nodejs invalida!\nVersão atual: ${version}, Versões suportadas: [18, 20]`)
}

if (!(arch === 'arm64' || arch === 'x64')) {
throw new Error(`Versão do nodejs invalida!\nVersão atual: ${version}, Versões suportadas: [18, 20]`)
}

const build = new Build({
directory: 'dist',
outdir: 'build',
archs: ['x64', 'arm64'],
archs: [arch],
platforms: ['linux'],
nodeVersion: version
})
Expand All @@ -344,4 +348,4 @@ async function start (): Promise<void> {
}
}

void start()
void start()

0 comments on commit c878043

Please sign in to comment.