From d581c9f15922f0b6a9bb8e729302e39ca5a2bd1d Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Wed, 1 Jan 2020 20:47:49 +1100 Subject: [PATCH] Prebuild Node.js modules; set up auto CLI and Node.js building --- .github/workflows/cli.yml | 49 +++++++++++++++++++++++++++++++++ .github/workflows/nodejs.yml | 52 ++++++++++++++++++++++++++++++++++++ README.md | 8 ++++++ nodejs/install.js | 48 +++++++++++++++++++++++++++++++++ nodejs/native/Cargo.toml | 2 +- nodejs/package.json | 8 ++++-- version | 4 +++ 7 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/cli.yml create mode 100644 .github/workflows/nodejs.yml create mode 100644 nodejs/install.js diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml new file mode 100644 index 0000000..7c9cdd5 --- /dev/null +++ b/.github/workflows/cli.yml @@ -0,0 +1,49 @@ +name: Build and upload CLI + +on: + create: + tags: + - 'v*' + +jobs: + cli: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + include: + - os: ubuntu-latest + ARCH: linux-x86_64 + MIME: application/octet-stream + EXT: '' + - os: windows-latest + ARCH: windows-x86_64 + MIME: application/vnd.microsoft.portable-executable + EXT: '.exe' + - os: macos-latest + ARCH: macos-x86_64 + MIME: application/octet-stream + EXT: '' + steps: + - uses: actions/checkout@v1 + - name: Get version + id: version + run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} + - name: Get file name + id: file + run: echo ::set-output name=FILE::${{ steps.version.outputs.VERSION }}-${{ matrix.ARCH }}${{ matrix.EXT }} + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + default: true + - name: Build CLI + run: cargo build --release + - uses: chrislennon/action-aws-cli@v1.1 + - name: Upload to S3 + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: us-west-2 + run: aws s3 cp ./nodejs/native/index.node s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/${{ steps.file.outputs.FILE }} --content-type ${{ matrix.MIME }} diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..837f468 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,52 @@ +name: Build and upload Node.js native module + +on: + create: + tags: + - 'v*' + +jobs: + nodejs: + runs-on: ${{ matrix.os }} + strategy: + matrix: + # Use windows-2016 as windows-2019 has LNK1181 error when building Neon module. + os: [ubuntu-latest, windows-2016, macos-latest] + node: [8, 9, 10, 11, 12, 13] + include: + - os: ubuntu-latest + ARCH: linux-x86_64 + - os: windows-latest + ARCH: windows-x86_64 + - os: macos-latest + ARCH: macos-x86_64 + steps: + - uses: actions/checkout@v1 + - name: Get version + id: version + run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/v} + - name: Get file name + id: file + run: echo ::set-output name=FILE::${{ steps.version.outputs.VERSION }}-${{ matrix.ARCH }}-node${{ matrix.node }}.node + - name: Set up Node.js + uses: actions/setup-node@master + with: + node-version: ${{ matrix.node }}.x + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + profile: minimal + default: true + - name: Build native module + working-directory: ./nodejs + run: | + npm install -g neon-cli + neon build --release + - uses: chrislennon/action-aws-cli@v1.1 + - name: Upload to S3 + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + AWS_REGION: us-west-2 + run: aws s3 cp ./nodejs/native/index.node s3://${{ secrets.AWS_S3_BUCKET }}/hyperbuild/bin/${{ steps.file.outputs.FILE }} diff --git a/README.md b/README.md index bf40b41..7f3325b 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,14 @@ Available as: ### CLI +##### Get + +[Windows](https://wilsonl.in/hyperbuild/bin/0.0.4-windows-x86_64.exe) | +[macOS](https://wilsonl.in/hyperbuild/bin/0.0.4-macos-x86_64) | +[Linux](https://wilsonl.in/hyperbuild/bin/0.0.4-linux-x86_64) + +##### Use + ```bash hyperbuild --src /path/to/src.html --out /path/to/output.min.html ``` diff --git a/nodejs/install.js b/nodejs/install.js new file mode 100644 index 0000000..3e895d0 --- /dev/null +++ b/nodejs/install.js @@ -0,0 +1,48 @@ +const fs = require('fs'); +const path = require('path'); +const request = require('request'); + +const binaryPath = path.join(__dirname, "native", "index.node"); +const binaryName = [ + require('./package').version, + '-', + [process.platform, { + darwin: 'macos', + linux: 'linux', + win32: 'windows', + }], + '-', + [process.arch, { + x64: 'x86_64', + }], + '-node', + [process.versions.modules, { + // Sourced from https://nodejs.org/en/download/releases/. + 57: 8, + 59: 9, + 64: 10, + 67: 11, + 72: 12, + 79: 13, + }], + '.node', +].map(p => typeof p == 'string' + ? p + : p[1][p[0]] +).join(''); +const binaryUrl = `https://wilsonl.in/hyperbuild/bin/${binaryName}`; + +console.log(`Fetching ${binaryUrl}...`); +request(binaryUrl) + .on('response', res => { + if (res.statusCode !== 200) { + console.error(`Failed to download prebuilt native module with status ${res.statusCode}, will build from source`); + process.exit(1); + } + }) + .on('error', err => { + console.error(err); + console.error(`Could not download prebuilt native module, building from source...`); + process.exit(1); + }) + .pipe(fs.createWriteStream(binaryPath)); diff --git a/nodejs/native/Cargo.toml b/nodejs/native/Cargo.toml index 28d79cb..67b67a2 100644 --- a/nodejs/native/Cargo.toml +++ b/nodejs/native/Cargo.toml @@ -9,7 +9,7 @@ exclude = ["artifacts.json", "index.node"] edition = "2018" [lib] -name = "hyperbuild" +name = "hyperbuild_nodejs_lib" crate-type = ["cdylib"] [build-dependencies] diff --git a/nodejs/package.json b/nodejs/package.json index 60e840c..da0221b 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -7,6 +7,9 @@ "type": "git", "url": "git+https://github.com/wilsonzlin/hyperbuild.git" }, + "engines": { + "node": ">=8 <=13" + }, "author": { "email": "npm@wilsonl.in", "name": "Wilson Lin", @@ -18,10 +21,11 @@ }, "homepage": "https://github.com/wilsonzlin/hyperbuild#readme", "dependencies": { - "neon-cli": "^0.3.3" + "neon-cli": "^0.3.3", + "request": "^2.88.0" }, "scripts": { - "install": "neon build --release", + "install": "node install.js || neon build --release", "prepack": "cp ../README.md .", "postpack": "rm README.md" }, diff --git a/version b/version index cd562ac..f227263 100755 --- a/version +++ b/version @@ -18,3 +18,7 @@ for (const f of ["README.md", "nodejs/native/Cargo.toml"]) { for (const f of ["nodejs/package.json"]) { replaceInFile(f, /^(\s*)"version": "\d+\.\d+\.\d+",\s*$/m, `$1"version": "${NEW_VERSION}",`); } + +for (const f of ["README.md"]) { + replaceInFile(f, /(wilsonl\.in\/hyperbuild\/bin\/)\d+\.\d+\.\d+/g, `$1${NEW_VERSION}`); +}