Skip to content

Commit

Permalink
fix: support cross compilation (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiBread committed Mar 6, 2024
1 parent 3ef7223 commit ce4a484
Show file tree
Hide file tree
Showing 15 changed files with 2,333 additions and 999 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/built-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Build and Test

on:
push:
branches:
- main
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
x86_64:
name: x86_64
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
os: [macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019, windows-2022]
node: [20, 21]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js v${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
registry-url: https://registry.yarnpkg.org/

- name: Install dependencies
run: yarn --mode=skip-build

- name: Build from source
run: yarn build:from-source

- name: Package
run: yarn build

- name: Test
run: yarn vitest run

aarch64:
name: aarch64
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, ubuntu-22.04]
node: [20, 21]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js v${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
registry-url: https://registry.yarnpkg.org/

- name: Install dependencies
run: yarn --mode=skip-build

- name: Build from source
run: yarn build:from-source

- name: Package build
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ yarn node-pre-gyp --target_arch=arm64 configure build package
- name: Build TypeScript
run: yarn tsc --build

- name: Test
run: yarn vitest run
91 changes: 91 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Release

on:
release:
types:
- published

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
x86_64:
name: Prebuild x86_64
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019, windows-2022]
node: [20, 21]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Use Node.js v${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
registry-url: https://registry.yarnpkg.org/

- name: Install dependencies
run: yarn --mode=skip-build

- name: Build from source
run: yarn build:from-source

- name: Package
run: yarn build

- name: Upload
uses: ./scripts/prebuild
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: "build/stage/**/*.tar.gz"

aarch64:
name: Prebuild aarch64
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04, ubuntu-22.04]
node: [20, 21]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v3
with:
version: 8.15.0

- name: Use Node.js v${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: pnpm

- name: Install dependencies
run: yarn --mode=skip-build

- name: Build from source
run: yarn build:from-source

- name: Package prebuild
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ yarn node-pre-gyp --target_arch=arm64 configure build package
- name: Build TypeScript
run: yarn tsc --build

- name: Upload
uses: ./scripts/prebuild
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
path: "build/stage/**/*.tar.gz"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.DS_Store
dist
build
build
prebuild
!scripts/prebuild
5 changes: 3 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"targets": [
{
"target_name": "inspectPromise",
"target_name": "<(module_name)",
"sources": [
"src/binding.cc"
]
],
"product_dir": "<(module_path)"
}
]
}
22 changes: 22 additions & 0 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from "vitest";
import { inspectPromise } from "./";

describe("inspectPromise", () => {
test("pending", () => {
const p = new Promise(() => {});
const details = inspectPromise(p);

expect(details.state).toBe("pending");
expect(details.value).toBe(undefined);
});

test("fulfilled", () => {
const p = Promise.resolve(42);
const details = inspectPromise(p);

expect(details.state).toBe("fulfilled");
expect(details.value).toBe(42);
});

// rejected case not covered for ci
});
9 changes: 7 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import path from "node:path";
import { find } from "@mapbox/node-pre-gyp";

interface Binding {
inspectPromise(promise: Promise<unknown>): [number, unknown];
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const binding = require(require.resolve("../build/Release/inspectPromise.node")) as Binding;
const bindingPath = find(path.resolve(path.join(__dirname, "../package.json")));
const binding = require(bindingPath) as Binding;

export type PromiseState = (typeof states)[number];

Expand Down
3 changes: 3 additions & 0 deletions node-pre-gyp.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "@mapbox/node-pre-gyp" {
function find(path: string): string;
}
44 changes: 37 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
"./extend": "./dist/extend.js"
},
"files": [
"build",
"dist",
"src"
"src",
"binding.gyp"
],
"scripts": {
"build": "rm -rf dist && tsc --build && node-gyp rebuild",
"style": "npm run lint:fix && prettier",
"install": "node-pre-gyp install --fallback-to-build",
"clean": "node scripts/clean.js",
"build": "node-pre-gyp configure build package && tsc --build",
"build:from-source": "node-pre-gyp install --fallback-to-build --build-from-source",
"test": "node-pre-gyp configure build && vitest run ./lib/index.test.ts",
"style": "yarn run lint:fix && prettier",
"lint": "eslint src --ext .ts",
"lint:fix": "npm run lint -- --fix",
"lint:fix": "yarn run lint -- --fix",
"prettier": "prettier -w src"
},
"repository": {
Expand All @@ -39,15 +43,41 @@
"prettier": {
"printWidth": 100,
"tabWidth": 4,
"useTabs": true
"useTabs": true,
"overrides": [
{
"files": [
"*.yml"
],
"options": {
"tabWidth": 2,
"useTabs": false
}
}
]
},
"binary": {
"module_name": "inspectPromise",
"module_path": "./prebuild/{module_name}-{node_abi}-{platform}-{arch}-{libc}-{libc_version}/",
"host": "https://github.com/xiBread/inspect-promise/releases/download/",
"remote_path": "v{version}",
"package_name": "{module_name}-v{version}-{node_abi}-{platform}-{arch}-{libc}-{libc_version}.tar.gz",
"pkg_path": "."
},
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.11"
},
"devDependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@actions/glob": "^0.4.0",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.5",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^1.3.1"
}
}

0 comments on commit ce4a484

Please sign in to comment.