fix node scripts and add jest

This commit is contained in:
Caelan Sayler
2024-08-25 21:43:59 +01:00
committed by Caelan
parent 42cd1dcb99
commit 6cd0fb63ea
13 changed files with 6148 additions and 452 deletions

View File

@@ -1,5 +0,0 @@
NODE_VERSION=20.x
NPM_REGISTRY=https://registry.npmjs.org
RUST_VERSION=stable
ACTIONS_USER=github-actions
ACTIONS_EMAIL=github-actions@github.com

View File

@@ -1,76 +0,0 @@
name: 'Setup Neon'
description: 'Setup the Neon toolchain.'
inputs:
platform:
description: 'Platform being built for.'
required: false
default: ''
use-rust:
description: 'Install Rust?'
required: false
default: 'true'
use-cross:
description: 'Install cross-rs?'
required: false
default: 'false'
workspace:
description: 'Path to workspace being setup.'
required: false
default: '.'
outputs:
rust:
description: 'Rust version installed.'
value: ${{ steps.rust.outputs.version }}
node:
description: 'Node version installed.'
value: ${{ steps.node.outputs.version }}
target:
description: 'Rust target architecture installed.'
value: ${{ steps.target.outputs.target }}
runs:
using: "composite"
steps:
- name: Set Environment Variables
uses: falti/dotenv-action@d1cd55661714e830a6e26f608f81d36e23424fed # v1.1.2
with:
path: ./.github/.env
export-variables: true
keys-case: bypass
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: ${{ env.NPM_REGISTRY }}
cache: npm
- name: Install Dependencies
shell: bash
run: npm ci
- name: Compute Rust Target
if: ${{ inputs['use-rust'] == 'true' }}
id: target
shell: bash
run: echo target=$(npx neon list-platforms | jq -r '.["${{ inputs.platform }}"]') | tee -a $GITHUB_OUTPUT
working-directory: ${{ inputs.workspace }}
- name: Install Rust
if: ${{ inputs['use-rust'] == 'true' }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.RUST_VERSION }}
target: ${{ steps.target.outputs.target }}
override: true
- name: Install cross-rs
if: ${{ inputs['use-cross'] == 'true' }}
uses: baptiste0928/cargo-install@v2
with:
crate: cross
- name: Node Version
id: node
shell: bash
run: |
echo version=$(node -e 'console.log(process.versions.node)') | tee -a $GITHUB_OUTPUT
- name: Rust Version
if: ${{ inputs['use-rust'] == 'true' }}
id: rust
shell: bash
run: |
echo version=$(cargo -Vv | fgrep release: | cut -d' ' -f2) | tee -a $GITHUB_OUTPUT

View File

@@ -1,137 +0,0 @@
name: Build
on:
workflow_call:
inputs:
ref:
description: 'The branch, tag, or SHA to check out'
required: true
type: string
update-version:
description: 'Update version before building?'
required: false
type: boolean
default: false
version:
description: 'Version update (ignored if update-version is false)'
required: false
type: string
default: 'patch'
github-release:
description: 'Publish GitHub release?'
required: false
type: boolean
default: false
tag:
description: 'The release tag (ignored if github-release is false)'
required: false
type: string
default: ''
jobs:
matrix:
name: Matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.result }}
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
ref: ${{ inputs.ref }}
- name: Setup Neon Environment
uses: ./.github/actions/setup
with:
use-rust: false
- name: Look Up Matrix Data
id: matrixData
shell: bash
run: echo "json=$(npx neon show ci github | jq -rc)" | tee -a $GITHUB_OUTPUT
- name: Compute Matrix
id: matrix
uses: actions/github-script@v7
with:
script: |
const platforms = ${{ steps.matrixData.outputs.json }};
const macOS = platforms.macOS.map(platform => {
return { os: "macos-latest", platform, script: "build" };
});
const windows = platforms.Windows.map(platform => {
return { os: "windows-latest", platform, script: "build" };
});
const linux = platforms.Linux.map(platform => {
return { os: "ubuntu-latest", platform, script: "cross" };
});
return [...macOS, ...windows, ...linux];
binaries:
name: Binaries
needs: [matrix]
strategy:
matrix:
cfg: ${{ fromJSON(needs.matrix.outputs.matrix) }}
runs-on: ${{ matrix.cfg.os }}
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
ref: ${{ inputs.ref }}
- name: Setup Neon Environment
id: neon
uses: ./.github/actions/setup
with:
use-cross: ${{ matrix.cfg.script == 'cross' }}
platform: ${{ matrix.cfg.platform }}
- name: Update Version
if: ${{ inputs.update-version }}
shell: bash
run: |
git config --global user.name $ACTIONS_USER
git config --global user.email $ACTIONS_EMAIL
npm version ${{ inputs.version }} -m "v%s"
- name: Build
shell: bash
env:
CARGO_BUILD_TARGET: ${{ steps.neon.outputs.target }}
NEON_BUILD_PLATFORM: ${{ matrix.cfg.platform }}
run: npm run ${{ matrix.cfg.script }}
- name: Pack
id: pack
shell: bash
run: |
mkdir -p dist
echo filename=$(basename $(npm pack ./platforms/${{ matrix.cfg.platform }} --silent --pack-destination=./dist --json | jq -r '.[0].filename')) | tee -a $GITHUB_OUTPUT
- name: Release
if: ${{ inputs.github-release }}
uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 # v2.0.4
with:
files: ./dist/${{ steps.pack.outputs.filename }}
tag_name: ${{ inputs.tag }}
main:
name: Main
needs: [matrix]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
ref: ${{ inputs.ref }}
- name: Setup Neon Environment
uses: ./.github/actions/setup
with:
use-rust: false
- name: Pack
id: pack
shell: bash
run: |
mkdir -p dist
echo "filename=$(npm pack --silent --pack-destination=./dist)" | tee -a $GITHUB_OUTPUT
- name: Release
if: ${{ inputs.github-release }}
uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 # v2.0.4
with:
files: ./dist/${{ steps.pack.outputs.filename }}
tag_name: ${{ inputs.tag }}

View File

@@ -1,135 +0,0 @@
name: Release
run-name: |
${{ (inputs.dryrun && 'Dry run')
|| format('Release: {0}', (inputs.version == 'custom' && inputs.custom) || inputs.version) }}
on:
workflow_dispatch:
inputs:
dryrun:
description: 'Dry run (no npm publish)'
required: false
type: boolean
default: true
version:
description: 'Version component to update (or "custom" to provide exact version)'
required: true
type: choice
options:
- patch
- minor
- major
- prepatch
- preminor
- premajor
- prerelease
- custom
custom:
description: 'Custom version'
required: false
default: ''
jobs:
setup:
name: Setup
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
dryrun: ${{ steps.dryrun.outputs.dryrun }}
publish: ${{ steps.publish.outputs.publish }}
ref: ${{ steps.tag.outputs.tag || github.event.repository.default_branch }}
tag: ${{ steps.tag.outputs.tag || '' }}
steps:
- name: Validate Workflow Inputs
if: ${{ inputs.version == 'custom' && inputs.custom == '' }}
shell: bash
run: |
echo '::error::No custom version number provided'
exit 1
- id: dryrun
name: Validate Dry Run Event
if: ${{ inputs.dryrun }}
shell: bash
run: echo dryrun=true | tee -a $GITHUB_OUTPUT
- id: publish
name: Validate Publish Event
if: ${{ !inputs.dryrun }}
shell: bash
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
if [[ -z $NPM_TOKEN ]]; then
echo "::error::Secret NPM_TOKEN is not defined for this GitHub repo."
echo "::error::To publish to npm, this action requires:"
echo "::error:: • an npm access token;"
echo "::error:: • with Read-Write access to this project's npm packages;"
echo "::error:: • stored as a repo secret named NPM_TOKEN."
echo "::error::See https://docs.npmjs.com/about-access-tokens for info about creating npm tokens."
echo "::error:: 💡 The simplest method is to create a Classic npm token of type Automation."
echo "::error:: 💡 For greater security, consider using a Granual access token."
echo "::error::See https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions for info about how to store GitHub repo secrets."
exit 1
fi
echo publish=true | tee -a $GITHUB_OUTPUT
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Neon Environment
uses: ./.github/actions/setup
with:
use-rust: false
- name: Tag Release
if: ${{ !inputs.dryrun }}
id: tag
shell: bash
run: |
git config --global user.name $ACTIONS_USER
git config --global user.email $ACTIONS_EMAIL
npm version -m 'v%s' '${{ (inputs.version == 'custom' && inputs.custom) || inputs.version }}'
git push --follow-tags
echo tag=$(git describe --abbrev=0) | tee -a $GITHUB_OUTPUT
build:
name: Build
needs: [setup]
permissions:
contents: write
uses: ./.github/workflows/build.yml
with:
ref: ${{ needs.setup.outputs.ref }}
tag: ${{ needs.setup.outputs.tag }}
update-version: ${{ !!needs.setup.outputs.dryrun }}
version: ${{ (inputs.version == 'custom' && inputs.custom) || inputs.version }}
github-release: ${{ !!needs.setup.outputs.publish }}
publish:
name: Publish
if: ${{ needs.setup.outputs.publish }}
needs: [setup, build]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@v3
with:
ref: ${{ needs.setup.outputs.ref }}
- name: Setup Neon Environment
uses: ./.github/actions/setup
with:
use-rust: false
- name: Fetch
uses: robinraju/release-downloader@c39a3b234af58f0cf85888573d361fb6fa281534 # v1.10
with:
tag: ${{ needs.setup.outputs.tag }}
fileName: "*.tgz"
out-file-path: ./dist
- name: Publish
shell: bash
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
for p in ./dist/*.tgz ; do
npm publish --access public $p
done

View File

@@ -1,71 +0,0 @@
name: Test
run-name: |
${{ (github.event_name == 'pull_request' && format('Test (PR #{0}): {1}', github.event.number, github.event.pull_request.title))
|| format('Test: {0}', github.event.head_commit.message) }}
on:
# Event: A maintainer has pushed commits or merged a PR to main.
push:
# Limiting push events to 'main' prevents duplicate runs of this workflow
# when maintainers push to internal PRs.
branches:
- main
# Event: A contributor has created or updated a PR.
pull_request:
types: [opened, synchronize, reopened, labeled]
branches:
- main
jobs:
pr:
name: Pull Request Details
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
outputs:
branch: ${{ steps.pr-ref.outputs.branch || github.event.repository.default_branch }}
steps:
- name: PR Branch
id: pr-ref
shell: bash
run: echo "branch=$(gh pr view $PR_NO --repo $REPO --json headRefName --jq '.headRefName')" | tee -a "$GITHUB_OUTPUT"
env:
REPO: ${{ github.repository }}
PR_NO: ${{ github.event.number }}
GH_TOKEN: ${{ github.token }}
# Labeling a PR with a `ci:full-matrix` label does a full matrix build on
# every run of this workflow for that PR, in addition to the other tests.
full-matrix:
name: Build
if: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'ci:full-matrix') }}
needs: [pr]
permissions:
contents: write
uses: ./.github/workflows/build.yml
with:
ref: ${{ needs.pr.outputs.branch }}
update-version: true
github-release: false
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Neon Environment
id: neon
uses: ./.github/actions/setup
with:
platform: linux-x64-gnu
- name: Build
shell: bash
env:
CARGO_BUILD_TARGET: ${{ steps.neon.outputs.target }}
NEON_BUILD_PLATFORM: linux-x64-gnu
run: npm run debug
- name: Test
shell: bash
run: npm test

View File

@@ -0,0 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
transform: {
"^.+.ts$": ["ts-jest",{}],
},
};

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,15 @@
{
"name": "veloz",
"name": "velopack",
"version": "0.1.0",
"description": "",
"main": "./lib/index.cjs",
"scripts": {
"test": "tsc &&cargo test",
"cargo-build": "tsc &&cargo build --message-format=json > cargo.log",
"cross-build": "tsc &&cross build --message-format=json > cross.log",
"postcargo-build": "neon dist < cargo.log",
"postcross-build": "neon dist -m /target < cross.log",
"debug": "npm run cargo-build --",
"build": "npm run cargo-build -- --release",
"cross": "npm run cross-build -- --release",
"prepack": "tsc &&neon update",
"version": "neon bump --binaries platforms && git add .",
"release": "gh workflow run release.yml -f dryrun=false -f version=patch",
"dryrun": "gh workflow run publish.yml -f dryrun=true"
"test": "npm run debug && jest --silent=false",
"debug": "tsc && cargo build && neon dist -f ../../target/debug/velopack_nodeffi.dll",
"clean": "cargo clean && del-cli platforms/**/index.node index.node lib"
},
"author": "",
"license": "ISC",
"author": "Velopack Ltd, Caelan Sayler",
"license": "MIT",
"exports": {
".": {
"import": {
@@ -44,10 +35,20 @@
"devDependencies": {
"@neon-rs/cli": "^0.1.73",
"@tsconfig/node20": "^20.1.4",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"del-cli": "^5.1.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"typescript": "^5.3.3"
},
"dependencies": {
"@neon-rs/load": "^0.1.73"
},
"optionalDependencies": {
"@velopack/darwin-arm64": "0.1.0",
"@velopack/darwin-x64": "0.1.0",
"@velopack/linux-x64-gnu": "0.1.0",
"@velopack/win32-x64-msvc": "0.1.0"
}
}

View File

@@ -1,3 +0,0 @@
// This module is the ESM entry point for the library.
export * from './index.cjs';

View File

@@ -1,11 +1,11 @@
import * as addon from './load.cjs';
import { UpdateInfo } from './bindings/UpdateInfo';
import { UpdateOptions } from './bindings/UpdateOptions';
import * as addon from './load';
import type { UpdateInfo } from './bindings/UpdateInfo';
import type { UpdateOptions } from './bindings/UpdateOptions';
export { UpdateInfo, UpdateOptions };
type UpdateManagerOpaque = {};
declare module "./load.cjs" {
declare module "./load" {
function js_new_update_manager(urlOrPath: string, options?: string): UpdateManagerOpaque;
function js_get_current_version(um: UpdateManagerOpaque): string;
// function js_get_app_id(um: UpdateManagerOpaque): string;

View File

@@ -5,12 +5,14 @@
//
// https://www.npmjs.com/package/@neon-rs/cli
module.exports = require('@neon-rs/load').proxy({
import { proxy } from "@neon-rs/load";
module.exports = proxy({
platforms: {
'win32-x64-msvc': () => require('@velopack/win32-x64-msvc'),
'darwin-x64': () => require('@velopack/darwin-x64'),
'darwin-arm64': () => require('@velopack/darwin-arm64'),
'linux-x64-gnu': () => require('@velopack/linux-x64-gnu')
},
debug: () => require('../index.node')
debug: () => require('../index.node'),
});

View File

@@ -0,0 +1,26 @@
import { VelopackApp } from "../src/index";
test("VelopackApp should handle restarted event", () => {
let afterInstall = false;
let beforeUninstall = false;
let beforeUpdate = false;
let afterUpdate = false;
let restarted = false;
let firstRun = false;
VelopackApp.build()
.onAfterInstallFastCallback(() => afterInstall = true)
.onBeforeUninstallFastCallback(() => beforeUninstall = true)
.onBeforeUpdateFastCallback(() => beforeUpdate = true)
.onAfterUpdateFastCallback(() => afterUpdate = true)
.onRestarted(() => restarted = true)
.onFirstRun(() => firstRun = true)
.run();
expect(afterInstall).toBe(false);
expect(beforeUninstall).toBe(false);
expect(beforeUpdate).toBe(false);
expect(afterUpdate).toBe(false);
expect(restarted).toBe(true);
expect(firstRun).toBe(true);
});

View File

@@ -5,5 +5,5 @@
"declaration": true,
"outDir": "lib",
},
"exclude": ["lib"]
"exclude": ["lib", "test"]
}