Merge pull request #685 from napi-rs/clipanion-3

feat(cli): upgrade clipanion v3
This commit is contained in:
LongYinan 2021-08-07 00:32:40 +08:00 committed by GitHub
commit 452dda3f43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 620 additions and 1850 deletions

View file

@ -6,7 +6,6 @@ parserOptions:
ecmaVersion: 2020
sourceType: module
extraFileExtensions: ['.cjs', '.mjs']
project: ./tsconfig.json
env:
browser: true
@ -51,24 +50,6 @@ rules:
{
'newlines-between': 'always',
'alphabetize': { 'order': 'asc', 'caseInsensitive': true },
'pathGroups':
[
{
'pattern': '@slardar/**',
'group': 'internal',
'position': 'before',
},
{
'pattern': '@perfkit/**',
'group': 'internal',
'position': 'before',
},
{
'pattern': '@maiev/**',
'group': 'internal',
'position': 'before',
},
],
},
]
@ -208,6 +189,12 @@ rules:
]
overrides:
- files:
- ./cli/**/*.ts
plugins:
- '@typescript-eslint'
parserOptions:
project: ./tsconfig.json
- files:
- ./test_module/**/*.{ts,js}
plugins:

View file

@ -1,5 +1,8 @@
# `@napi-rs/cli`
[![Download](https://img.shields.io/npm/dm/@napi-rs/cli)](https://www.npmjs.com/package/@napi-rs/cli)
[![Install size](https://packagephobia.com/badge?p=@napi-rs/cli)](https://packagephobia.com/result?p=@napi-rs/cli)
> Cli tools for napi-rs
## Commands

View file

@ -31,14 +31,12 @@
"bugs": {
"url": "https://github.com/napi-rs/napi-rs/issues"
},
"dependencies": {
"@octokit/rest": "^18.8.0",
"devDependencies": {
"@octokit/rest": "^18.9.0",
"chalk": "^4.1.2",
"clipanion": "^2.6.2",
"clipanion": "^3.0.1",
"debug": "^4.3.2",
"fdir": "^5.1.0",
"inquirer": "^8.1.2",
"lodash": "^4.17.21",
"putasset": "^5.0.3",
"toml": "^3.0.0",
"tslib": "^2.3.0"

View file

@ -1,7 +1,7 @@
import { join, parse } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { Command, Option } from 'clipanion'
import { fdir } from 'fdir'
import { getNapiConfig } from './consts'
@ -15,16 +15,14 @@ export class ArtifactsCommand extends Command {
description: 'Copy artifacts from Github Actions into specified dir',
})
@Command.String('-d,--dir')
sourceDir = 'artifacts'
static paths = [['artifacts']]
@Command.String('--dist')
distDir = 'npm'
sourceDir = Option.String('-d,--dir', 'artifacts')
@Command.String('-c,--config')
configFileName?: string
distDir = Option.String('--dist', 'npm')
configFileName?: string = Option.String('-c,--config')
@Command.Path('artifacts')
async execute() {
const { platforms, binaryName, packageJsonPath } = getNapiConfig(
this.configFileName,

View file

@ -2,7 +2,7 @@ import { execSync } from 'child_process'
import { join, parse, sep } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { Command, Option } from 'clipanion'
import toml from 'toml'
import { getNapiConfig } from './consts'
@ -17,36 +17,28 @@ export class BuildCommand extends Command {
description: 'Build and copy native module into specified dir',
})
@Command.Boolean(`--platform`)
appendPlatformToFilename = false
static paths = [['build']]
@Command.Boolean(`--release`)
isRelease = false
appendPlatformToFilename = Option.Boolean(`--platform`, false)
@Command.String('--config,-c')
configFileName?: string
isRelease = Option.Boolean(`--release`, false)
@Command.String('--cargo-name')
cargoName?: string
configFileName?: string = Option.String('--config,-c')
@Command.String('--target')
targetTripleDir = process.env.RUST_TARGET ?? ''
cargoName?: string = Option.String('--cargo-name')
@Command.String('--features')
features?: string
targetTripleDir = Option.String('--target', process.env.RUST_TARGET ?? '')
@Command.String('--cargo-flags')
cargoFlags = ''
features?: string = Option.String('--features')
@Command.String('--cargo-cwd')
cargoCwd!: string
cargoFlags = Option.String('--cargo-flags', '')
@Command.String({
cargoCwd?: string = Option.String('--cargo-cwd')
destDir = Option.String({
required: false,
})
target = '.'
@Command.Path('build')
async execute() {
const cwd = this.cargoCwd
? join(process.cwd(), this.cargoCwd)
@ -146,6 +138,12 @@ export class BuildCommand extends Command {
)
}
const targetRootDir = await findUp(cwd)
if (!targetRootDir) {
throw new TypeError('No target dir found')
}
const targetDir = join(
this.targetTripleDir,
this.isRelease ? 'release' : 'debug',
@ -156,26 +154,25 @@ export class BuildCommand extends Command {
: ''
debug(`Platform name: ${platformName || chalk.green('[Empty]')}`)
const distFileName = `${binaryName}${platformName}.node`
let distModulePath = join(this.destDir ?? '.', distFileName)
let distModulePath = this.target
? join(this.target, `${binaryName}${platformName}.node`)
: join('target', targetDir, `${binaryName}${platformName}.node`)
const parsedDist = parse(distModulePath)
if (!parsedDist.ext) {
distModulePath = `${distModulePath}${platformName}.node`
}
const dir = await findUp(cwd)
if (!dir) {
throw new TypeError('No target dir found')
}
const sourcePath = join(dir, 'target', targetDir, `${dylibName}${libExt}`)
const sourcePath = join(
targetRootDir,
'target',
targetDir,
`${dylibName}${libExt}`,
)
if (await existsAsync(distModulePath)) {
debug(`remove old binary [${chalk.yellowBright(sourcePath)}]`)
debug(`remove old binary [${chalk.yellowBright(distModulePath)}]`)
await unlinkAsync(distModulePath)
}

View file

@ -2,13 +2,12 @@ import { mkdirSync } from 'fs'
import { join } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { pick } from 'lodash'
import { Command, Option } from 'clipanion'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
import { PlatformDetail } from './parse-triple'
import { writeFileAsync } from './utils'
import { writeFileAsync, pick } from './utils'
const debug = debugFactory('create-npm-dir')
@ -17,6 +16,8 @@ export class CreateNpmDirCommand extends Command {
description: 'Create npm packages dir for platforms',
})
static paths = [['create-npm-dir']]
static create = async (
config: string,
targetDirPath: string,
@ -72,13 +73,10 @@ export class CreateNpmDirCommand extends Command {
}
}
@Command.String('-t,--target')
targetDir!: string
targetDir: string = Option.String('-t,--target')!
@Command.String('-c,--config')
config = 'package.json'
config = Option.String('-c,--config', 'package.json')
@Command.Path('create-npm-dir')
async execute() {
await CreateNpmDirCommand.create(
this.config,

View file

@ -5,7 +5,6 @@ import { Cli } from 'clipanion'
import { ArtifactsCommand } from './artifacts'
import { BuildCommand } from './build'
import { CreateNpmDirCommand } from './create-npm-dir'
import { NewProjectCommand } from './new'
import { PrePublishCommand } from './pre-publish'
import { VersionCommand } from './version'
@ -17,7 +16,6 @@ const cli = new Cli({
cli.register(ArtifactsCommand)
cli.register(BuildCommand)
cli.register(CreateNpmDirCommand)
cli.register(NewProjectCommand)
cli.register(PrePublishCommand)
cli.register(VersionCommand)

View file

@ -1,18 +0,0 @@
export const createCargoConfig = (
enableLinuxArm7: boolean,
enableLinuxArm8: boolean,
) => {
let result = ''
if (enableLinuxArm7) {
result = `[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"`
}
if (enableLinuxArm8) {
result = `${result}
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
`
}
return result
}

View file

@ -1,18 +0,0 @@
export const createCargoContent = (name: string) => `[package]
edition = "2018"
name = "${name.replace('@', '').replace('/', '_').toLowerCase()}"
version = "0.0.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
napi = "1"
napi-derive = "1"
[build-dependencies]
napi-build = "1"
[profile.release]
lto = true
`

View file

@ -1,668 +0,0 @@
const OLDEST_LTS_NODE = 10
const LATEST_LTS_NODE = 14
const SUPPORTED_NODE_VERSIONS = [10, 12, 14, 15]
const OS_LINUX = 'ubuntu-18.04'
const OS_OSX = 'macos-latest'
const OS_WINDOWS = 'windows-latest'
const STEP_BUILD = 'build'
const STEP_BUILD_LINUX_MUSL = 'build-linux-musl'
const STEP_BUILD_LINUX_ARM7 = 'build-linux-arm7'
const STEP_BUILD_LINUX_ARM8 = 'build-linux-aarch64'
const STEP_BUILD_APPLE_SILICON = 'build-apple-silicon'
const STEP_BUILD_ANDROID = 'build-android-aarch64'
const STEP_TEST = 'test'
const STEP_TEST_LINUX_MUSL = 'test-linux-musl'
const STEP_TEST_LINUX_ARM8 = 'test-linux-aarch64'
export const createGithubActionsCIYml = (
binaryName: string,
targets: string[],
) => {
const enableWindowsX86 = targets.includes('x86_64-pc-windows-msvc')
const enableMacOSX86 = targets.includes('x86_64-apple-darwin')
const enableLinuxX86 = targets.includes('x86_64-unknown-linux-gnu')
const enableLinuxMuslX86 = targets.includes('x86_64-unknown-linux-musl')
const enableLinuxArm7 = targets.includes('armv7-unknown-linux-gnueabihf')
const enableLinuxArm8 = targets.includes('aarch64-unknown-linux-gnu')
const enableAppleSilicon = targets.includes('aarch64-apple-darwin')
const enableAndroid = targets.includes('aarch64-linux-android')
const os: string[] = []
const requiredSteps: string[] = []
if (enableLinuxX86) {
os.push(OS_LINUX)
}
if (enableMacOSX86) {
os.push(OS_OSX)
}
if (enableWindowsX86) {
os.push(OS_WINDOWS)
}
if (os.length) {
requiredSteps.push(STEP_TEST)
}
if (enableLinuxMuslX86) {
requiredSteps.push(STEP_TEST_LINUX_MUSL)
}
if (enableLinuxArm7) {
requiredSteps.push(STEP_BUILD_LINUX_ARM7)
}
if (enableLinuxArm8) {
requiredSteps.push(STEP_TEST_LINUX_ARM8)
}
if (enableAppleSilicon) {
requiredSteps.push(STEP_BUILD_APPLE_SILICON)
}
if (enableAndroid) {
requiredSteps.push(STEP_BUILD_ANDROID)
}
const BUILD_SCRIPT = !os.length
? ''
: `${STEP_BUILD}:
if: "!contains(github.event.head_commit.message, 'skip ci')"
strategy:
fail-fast: false
matrix:
os: [${os.join(', ')}]
name: stable - \${{ matrix.os }} - node@${LATEST_LTS_NODE}
runs-on: \${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${LATEST_LTS_NODE}
check-latest: true
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: stable-\${{ matrix.os }}-node@${LATEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: stable-\${{ matrix.os }}-node@${LATEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-\${{ matrix.os }}-node@${LATEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: 'Build'
run: yarn build
env:
MACOSX_DEPLOYMENT_TARGET: '10.13'
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-\${{ matrix.os }}
path: \${{ env.APP_NAME }}.*.node
- name: Clear the cargo caches
run: |
cargo install cargo-cache --no-default-features --features ci-autoclean
cargo-cache`
const BUILD_MUSL_SCRIPT = !enableLinuxMuslX86
? ''
: `${STEP_BUILD_LINUX_MUSL}:
if: "!contains(github.event.head_commit.message, 'skip ci')"
name: stable - linux-musl - node@${OLDEST_LTS_NODE}
runs-on: ${OS_LINUX}
steps:
- uses: actions/checkout@v2
- name: Login to registry
run: |
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD $DOCKER_REGISTRY_URL
env:
DOCKER_REGISTRY_URL: docker.pkg.github.com
DOCKER_USERNAME: \${{ github.actor }}
DOCKER_PASSWORD: \${{ secrets.GITHUB_TOKEN }}
- name: Pull docker image
run: |
docker pull docker.pkg.github.com/napi-rs/napi-rs/rust-nodejs-alpine:lts
docker tag docker.pkg.github.com/napi-rs/napi-rs/rust-nodejs-alpine:lts builder
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: stable-node-alpine-@${OLDEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: stable-node-alpine-@${OLDEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-alpine-node@${OLDEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: 'Build'
run: |
docker run --rm -v ~/.cargo/git:/root/.cargo/git -v ~/.cargo/registry:/root/.cargo/registry -v $(pwd):/\${{ env.APP_NAME }} -e DEBUG="napi:*" -w /\${{ env.APP_NAME }} builder sh -c "yarn build"
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-linux-musl
path: \${{ env.APP_NAME }}.*.node`
const BUILD_LINUX_ARM7_SCRIPT = !enableLinuxArm7
? ''
: `${STEP_BUILD_LINUX_ARM7}:
name: stable - arm7-unknown-linux-gnu - node@${LATEST_LTS_NODE}
runs-on: ${OS_LINUX}
steps:
- run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: ${LATEST_LTS_NODE}
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: stable-linux-arm7-gnu-node@${LATEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: stable-linux-arm7-gnu-node@${LATEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-linux-arm7-gnu-node@${LATEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: Install arm7 toolchain
run: rustup target add armv7-unknown-linux-gnueabihf
- name: Install cross compile toolchain
run: |
sudo apt-get update
sudo apt-get install gcc-arm-linux-gnueabihf -y
- name: Install dependencies
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Cross build arm7
run: yarn build --target armv7-unknown-linux-gnueabihf
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-linux-arm7
path: \${{ env.APP_NAME }}.*.node`
const BUILD_LINUX_ARM8_SCRIPT = !enableLinuxArm8
? ''
: `${STEP_BUILD_LINUX_ARM8}:
name: stable - aarch64-unknown-linux-gnu - node@${LATEST_LTS_NODE}
runs-on: ${OS_LINUX}
steps:
- run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${LATEST_LTS_NODE}
check-latest: true
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- name: Install aarch64 toolchain
run: rustup target add aarch64-unknown-linux-gnu
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: stable-linux-aarch64-gnu-node@${LATEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: stable-linux-aarch64-gnu-node@${LATEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-linux-aarch64-gnu-node@${LATEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: Install cross compile toolchain
run: |
sudo apt-get update
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu -y
- name: Install dependencies
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Cross build aarch64
run: yarn build --target aarch64-unknown-linux-gnu
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-linux-aarch64
path: \${{ env.APP_NAME }}.*.node`
const BUILD_APPLE_SILICON_SCRIPT = !enableAppleSilicon
? ''
: `${STEP_BUILD_APPLE_SILICON}:
name: nightly - aarch64-apple-darwin - node@${LATEST_LTS_NODE}
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${LATEST_LTS_NODE}
check-latest: true
- name: Install
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
profile: minimal
override: true
- name: Install aarch64 toolchain
run: rustup target add aarch64-apple-darwin
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: nightly-apple-aarch64-node@${LATEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: nightly-apple-aarch64-node@${LATEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-apple-aarch64-node@${LATEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: Install dependencies
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Cross build aarch64
run: yarn build --target aarch64-apple-darwin
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-apple-aarch64
path: \${{ env.APP_NAME }}.*.node`
const BUILD_ANDROID_SCRIPT = !enableAndroid
? ''
: `${STEP_BUILD_ANDROID}:
name: Build - Android - aarch64
runs-on: ${OS_OSX}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: ${LATEST_LTS_NODE}
- name: Install aarch64 toolchain
run: rustup target add aarch64-linux-android
- name: Generate Cargo.lock
uses: actions-rs/cargo@v1
with:
command: generate-lockfile
- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: nightly-apple-aarch64-node@${LATEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: nightly-apple-aarch64-node@${LATEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-apple-aarch64-node@${LATEST_LTS_NODE}-\${{ hashFiles('yarn.lock') }}
- name: Install dependencies
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Build
shell: bash
run: |
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="\${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/darwin-x86_64/bin/aarch64-linux-android24-clang"
yarn build --target aarch64-linux-android
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: bindings-android-aarch64
path: \${{ env.APP_NAME }}.*.node`
const TEST_SCRIPT = !os.length
? ''
: `${STEP_TEST}:
name: Test bindings on \${{ matrix.os }} - node@\${{ matrix.node }}
needs:
- ${STEP_BUILD}
strategy:
fail-fast: false
matrix:
os: [${os.join(', ')}]
node: [${SUPPORTED_NODE_VERSIONS.join(', ')}]
runs-on: \${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: \${{ matrix.node }}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-test-\${{ matrix.os }}-node@\${{ matrix.node }}-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: bindings-\${{ matrix.os }}
path: .
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: yarn test`
const TEST_MUSL_SCRIPT = !enableLinuxMuslX86
? ''
: `${STEP_TEST_LINUX_MUSL}:
name: Test bindings on alpine - node@\${{ matrix.node }}
needs:
- ${STEP_BUILD_LINUX_MUSL}
strategy:
fail-fast: false
matrix:
node: [${SUPPORTED_NODE_VERSIONS.join(', ')}]
runs-on: ${OS_LINUX}
steps:
- uses: actions/checkout@v2
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-alpine-node@\${{ matrix.node }}-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --ignore-scripts --registry https://registry.npmjs.org
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: bindings-linux-musl
path: .
- name: List files
run: ls -R .
shell: bash
- name: Run tests
run: docker run --rm -v $(pwd):/\${{ env.APP_NAME }} -w /\${{ env.APP_NAME }} node:\${{ matrix.node }}-alpine sh -c "yarn test" `
const TEST_LINUX_ARM8_SCRIPT = !enableLinuxArm8
? ''
: `${STEP_TEST_LINUX_ARM8}:
name: stable - aarch64-unknown-linux-gnu - node@\${{ matrix.node }}
runs-on: ${OS_LINUX}
needs:
- ${STEP_BUILD_LINUX_ARM8}
strategy:
fail-fast: false
matrix:
node: [${SUPPORTED_NODE_VERSIONS.join(', ')}]
steps:
- run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: \${{ matrix.node }}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-test-linux-aarch64-gnu-node@\${{ matrix.node }}-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: bindings-linux-aarch64
path: .
- name: List
run: ls -a
- name: Run tests
uses: docker://multiarch/ubuntu-core:arm64-focal
with:
args: >
sh -c "
apt-get update && \\
apt-get install -y ca-certificates gnupg2 curl && \\
curl -sL https://deb.nodesource.com/setup_\${{ matrix.node }}.x | bash - && \\
apt-get install -y nodejs && \\
node ./simple-test.js
"`
return `name: CI
env:
DEBUG: 'napi:*'
APP_NAME: '${binaryName}'
on:
push:
branches:
- main
tags-ignore:
- '**'
pull_request:
jobs:
${[
BUILD_SCRIPT,
BUILD_MUSL_SCRIPT,
BUILD_LINUX_ARM7_SCRIPT,
BUILD_LINUX_ARM8_SCRIPT,
BUILD_APPLE_SILICON_SCRIPT,
BUILD_ANDROID_SCRIPT,
TEST_SCRIPT,
TEST_MUSL_SCRIPT,
TEST_LINUX_ARM8_SCRIPT,
]
.filter((s) => s.length)
.map((script) => ` ${script}`)
.join('\n\n')}
dependabot:
needs:
${requiredSteps.map((s) => ` - ${s}`).join('\n')}
runs-on: ${OS_LINUX}
steps:
- name: auto-merge
uses: ridedott/dependabot-auto-merge-action@master
with:
GITHUB_LOGIN: dependabot[bot]
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
publish:
name: Publish
runs-on: ${OS_LINUX}
needs:
${requiredSteps.map((s) => ` - ${s}`).join('\n')}
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: ${LATEST_LTS_NODE}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: npm-cache-publish-ubuntu-latest-\${{ hashFiles('yarn.lock') }}
- name: 'Install dependencies'
run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000
- name: Download all artifacts
uses: actions/download-artifact@v2
with:
path: artifacts
- name: Move artifacts
run: yarn artifacts
- name: List packages
run: ls -R npm
shell: bash
- name: Publish
run: |
${'if git log -1 --pretty=%B | grep "^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+$";'}
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --access public
${'elif git log -1 --pretty=%B | grep "^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+";'}
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --tag next --access public
else
echo "Not a release, skipping publish"
fi
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: \${{ secrets.NPM_TOKEN }}
`
}

View file

@ -1,196 +0,0 @@
import { writeFileSync, mkdirSync } from 'fs'
import { join } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import inquirer, { prompt } from 'inquirer'
import { CreateNpmDirCommand } from '../create-npm-dir'
import { debugFactory } from '../debug'
import { DefaultPlatforms } from '../parse-triple'
import { createCargoContent } from './cargo'
import { createCargoConfig } from './cargo-config'
import { createGithubActionsCIYml } from './ci-yml'
import { createIndexJs } from './indexjs'
import { LibRs } from './lib-rs'
import { NPMIgnoreFiles } from './npmignore'
import { createPackageJson } from './package'
const NAME_PROMOTE_NAME = 'Package name'
const DIR_PROMOTE_NAME = 'Dir name'
const ENABLE_GITHUB_ACTIONS_PROMOTE_NAME = 'Enable github actions'
const debug = debugFactory('create')
const BUILD_RS = `extern crate napi_build;
fn main() {
napi_build::setup();
}
`
const SupportedPlatforms: string[] = [
'aarch64-apple-darwin',
'aarch64-linux-android',
'aarch64-unknown-linux-gnu',
'armv7-unknown-linux-gnueabihf',
'x86_64-apple-darwin',
'x86_64-pc-windows-msvc',
'x86_64-unknown-linux-gnu',
'x86_64-unknown-linux-musl',
]
export class NewProjectCommand extends Command {
static usage = Command.Usage({
description: 'Create a new project from scratch',
})
@Command.String({
name: '-n,--name',
required: false,
})
name?: string
@Command.String({
name: '-d,--dirname',
required: false,
})
dirname?: string
@Command.Array('--targets,-t')
targets?: string[]
@Command.Boolean(`--dry-run`)
dryRun = false
@Command.Boolean(`--enable-github-actions`)
enableGithubActions!: boolean
@Command.Path('new')
async execute() {
await this.getName()
if (!this.dirname) {
const [scope, name] = this.name?.split('/') ?? []
const defaultProjectDir = name ?? scope
const dirAnswer = await prompt({
type: 'input',
name: DIR_PROMOTE_NAME,
default: defaultProjectDir,
})
this.dirname = dirAnswer[DIR_PROMOTE_NAME]
}
if (!this.targets) {
const { targets } = await inquirer.prompt([
{
type: 'checkbox',
name: 'targets',
message: 'Choose targets you want to support',
default: DefaultPlatforms.map((p) => p.raw),
choices: SupportedPlatforms,
},
])
if (!targets.length) {
throw new TypeError('At least choose one target')
}
this.targets = targets
}
if (this.enableGithubActions === undefined) {
const answer = await inquirer.prompt([
{
type: 'confirm',
name: ENABLE_GITHUB_ACTIONS_PROMOTE_NAME,
message: 'Enable github actions?',
default: true,
choices: SupportedPlatforms,
},
])
this.enableGithubActions = answer[ENABLE_GITHUB_ACTIONS_PROMOTE_NAME]
}
debug(`Running command: ${chalk.green('[${command}]')}`)
if (!this.dryRun) {
mkdirSync(join(process.cwd(), this.dirname!))
mkdirSync(join(process.cwd(), this.dirname!, 'src'))
}
const [s, pkgName] = this.name!.split('/')
const binaryName = pkgName ?? s
this.writeFile('Cargo.toml', createCargoContent(this.name!))
this.writeFile('.npmignore', NPMIgnoreFiles)
this.writeFile('build.rs', BUILD_RS)
this.writeFile('index.js', createIndexJs(this.name!, binaryName))
this.writeFile(
'package.json',
JSON.stringify(
createPackageJson(this.name!, binaryName, this.targets!),
null,
2,
),
)
this.writeFile('src/lib.rs', LibRs)
if (this.enableGithubActions) {
const githubDir = join(process.cwd(), this.dirname!, '.github')
const workflowsDir = join(githubDir, 'workflows')
if (!this.dryRun) {
mkdirSync(githubDir)
mkdirSync(workflowsDir)
}
this.writeFile(
join('.github', 'workflows', 'CI.yml'),
createGithubActionsCIYml(binaryName, this.targets!),
)
}
await CreateNpmDirCommand.create(
'package.json',
join(process.cwd(), this.dirname!),
join(process.cwd(), this.dirname!),
)
const enableLinuxArm8 = this.targets!.includes('aarch64-unknown-linux-gnu')
const enableLinuxArm7 = this.targets!.includes(
'armv7-unknown-linux-gnueabihf',
)
const cargoConfig = createCargoConfig(enableLinuxArm7, enableLinuxArm8)
if (cargoConfig.length) {
const configDir = join(process.cwd(), this.dirname!, '.config')
if (!this.dryRun) {
mkdirSync(configDir)
this.writeFile(join('.config', 'config.toml'), cargoConfig)
}
}
}
private writeFile(path: string, content: string) {
const distDir = join(process.cwd(), this.dirname!)
this.context.stdout.write(chalk.green(`Writing ${chalk.blue(path)}\n`))
if (!this.dryRun) {
writeFileSync(join(distDir, path), content)
}
}
private async getName() {
if (!this.name) {
const nameAnswer = await prompt({
type: 'input',
name: NAME_PROMOTE_NAME,
suffix: ' (The name filed in your package.json)',
})
const name = nameAnswer[NAME_PROMOTE_NAME]
if (!name) {
await this.getName()
} else {
this.name = name
}
}
}
}

View file

@ -1,15 +0,0 @@
export const createIndexJs = (
pkgName: string,
name: string,
) => `const { loadBinding } = require('@node-rs/helper')
/**
* __dirname means load native addon from current dir
* '${name}' is the name of native addon
* the second arguments was decided by \`napi.name\` field in \`package.json\`
* the third arguments was decided by \`name\` field in \`package.json\`
* \`loadBinding\` helper will load \`${name}.[PLATFORM].node\` from \`__dirname\` first
* If failed to load addon, it will fallback to load from \`${pkgName}-[PLATFORM]\`
*/
module.exports = loadBinding(__dirname, '${name}', '${pkgName}')
`

View file

@ -1,50 +0,0 @@
export const LibRs = `#![deny(clippy::all)]
#[macro_use]
extern crate napi_derive;
use std::convert::TryInto;
use napi::{CallContext, Env, JsNumber, JsObject, Result, Task};
struct AsyncTask(u32);
impl Task for AsyncTask {
type Output = u32;
type JsValue = JsNumber;
fn compute(&mut self) -> Result<Self::Output> {
use std::thread::sleep;
use std::time::Duration;
sleep(Duration::from_millis(self.0 as u64));
Ok(self.0 * 2)
}
fn resolve(self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
env.create_uint32(output)
}
}
#[module_exports]
fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("sync", sync_fn)?;
exports.create_named_method("sleep", sleep)?;
Ok(())
}
#[js_function(1)]
fn sync_fn(ctx: CallContext) -> Result<JsNumber> {
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?;
ctx.env.create_uint32(argument + 100)
}
#[js_function(1)]
fn sleep(ctx: CallContext) -> Result<JsObject> {
let argument: u32 = ctx.get::<JsNumber>(0)?.try_into()?;
let task = AsyncTask(argument);
let async_task = ctx.env.spawn(task)?;
Ok(async_task.promise_object())
}
`

View file

@ -1,11 +0,0 @@
export const NPMIgnoreFiles = `target
Cargo.lock
.cargo
.github
npm
.eslintrc
.prettierignore
rustfmt.toml
yarn.lock
*.node
`

View file

@ -1,58 +0,0 @@
import { DefaultPlatforms } from '../parse-triple'
export const createPackageJson = (
name: string,
binaryName: string,
targets: string[],
) => {
const pkgContent = {
name,
version: '0.0.0',
napi: {
name: binaryName,
},
license: 'MIT',
dependencies: {
'@node-rs/helper': '^1.1.0',
},
devDependencies: {
'@napi-rs/cli': '^1.1.0',
},
engines: {
node: '>= 10',
},
scripts: {
artifacts: 'napi artifacts',
build: 'napi build --platform --release',
'build:debug': 'napi build --platform',
prepublishOnly: 'napi prepublish -t npm',
version: 'napi version',
},
}
const triples: any = {}
const defaultTargetsSupported = DefaultPlatforms.every((p) =>
targets!.includes(p.raw),
)
const isOnlyDefaultTargets =
targets.length === 3 &&
DefaultPlatforms.every((p) => targets.includes(p.raw))
if (!isOnlyDefaultTargets) {
if (!defaultTargetsSupported) {
triples.defaults = false
triples.additional = targets
} else {
triples.additional = targets.filter(
(t) => !DefaultPlatforms.map((p) => p.raw).includes(t),
)
}
}
// @ts-expect-error
pkgContent.napi.triples = triples
return pkgContent
}

View file

@ -2,7 +2,7 @@ import { join } from 'path'
import { Octokit } from '@octokit/rest'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { Command, Option } from 'clipanion'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
@ -25,22 +25,18 @@ export class PrePublishCommand extends Command {
'Update package.json and copy addons into per platform packages',
})
@Command.String(`-p,--prefix`)
prefix = 'npm'
static paths = [['prepublish']]
@Command.String('--tagstyle,-t')
tagStyle: 'npm' | 'lerna' = 'lerna'
prefix = Option.String(`-p,--prefix`, 'npm')
@Command.String('-c,--config')
configFileName?: string
tagStyle: 'npm' | 'lerna' = Option.String('--tagstyle,-t', 'lerna')
@Command.Boolean('--dry-run')
isDryRun = false
configFileName?: string = Option.String('-c,--config')
@Command.Boolean('--skip-gh-release')
skipGHRelease = false
isDryRun = Option.Boolean('--dry-run', false)
skipGHRelease = Option.Boolean('--skip-gh-release', false)
@Command.Path('prepublish')
async execute() {
const { packageJsonPath, platforms, version, packageName, binaryName } =
getNapiConfig(this.configFileName)

View file

@ -6,3 +6,10 @@ export const writeFileAsync = promisify(writeFile)
export const existsAsync = promisify(exists)
export const unlinkAsync = promisify(unlink)
export const copyFileAsync = promisify(copyFile)
export function pick<O, K extends keyof O>(o: O, ...keys: K[]): Pick<O, K> {
return keys.reduce((acc, key) => {
acc[key] = o[key]
return acc
}, {} as O)
}

View file

@ -1,7 +1,7 @@
import { join } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { Command, Option } from 'clipanion'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
@ -15,6 +15,8 @@ export class VersionCommand extends Command {
description: 'Update versions in created npm dir',
})
static paths = [['version']]
static async updatePackageJson(prefix: string, configFileName?: string) {
const { version, platforms } = getNapiConfig(configFileName)
for (const platformDetail of platforms) {
@ -30,13 +32,10 @@ export class VersionCommand extends Command {
}
}
@Command.String(`-p,--prefix`)
prefix = 'npm'
prefix = Option.String(`-p,--prefix`, 'npm')
@Command.String('-c,--config')
configFileName?: string
configFileName?: string = Option.String('-c,--config')
@Command.Path('version')
async execute() {
await VersionCommand.updatePackageJson(this.prefix, this.configFileName)
await spawn('git add .')

View file

@ -1,10 +1,10 @@
const { readFileSync, writeFileSync } = require('fs')
const { join } = require('path')
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
const esbuild = require('esbuild')
const { groupBy, mapValues } = require('lodash')
import * as esbuild from 'esbuild'
import { groupBy, mapValues } from 'lodash'
const { parseTriple } = require('./cli/scripts/parse-triple')
import { parseTriple } from './cli/src/parse-triple'
const RAW_LIST = readFileSync(join(__dirname, 'triples', 'target-list'), 'utf8')
@ -28,7 +28,7 @@ const tripleLists = RAW_LIST.trim()
const platformArchTriples = mapValues(
groupBy(
Object.values(tripleLists).filter((k) =>
Object.values(tripleLists).filter((k: { platform?: string }) =>
SUPPORTED_PLATFORM.has(k.platform),
),
'platform',

View file

@ -11,13 +11,14 @@
"license": "MIT",
"scripts": {
"bench": "cross-env TS_NODE_PROJECT='./bench/tsconfig.json' node -r ts-node/register/transpile-only bench/bench.ts",
"build": "tsc -p tsconfig.json && shx chmod 777 cli/scripts/index.js && node generate-triplelist.js",
"build": "tsc -p tsconfig.json --noEmit && yarn bundle && shx chmod 777 cli/scripts/index.js && node -r ts-node/register/transpile-only ./generate-triple-list.ts",
"build:bench": "yarn --cwd ./bench build",
"build:memory": "yarn --cwd ./memory-testing build",
"build:test": "yarn --cwd ./test_module build",
"build:test:aarch64": "yarn --cwd ./test_module build-aarch64",
"build:test:android": "yarn --cwd ./test_module build --target aarch64-linux-android",
"build:test:armv7": "yarn --cwd ./test_module build-armv7",
"bundle": "esbuild ./cli/src/index.ts --bundle --platform=node --outdir=cli/scripts --main-fields=\"module,main,browser\" --external:iconv-lite",
"format": "run-p format:md format:json format:yaml format:source format:rs",
"format:md": "prettier --parser markdown --write ./**/*.md",
"format:json": "prettier --parser json --write ./**/*.json",
@ -55,9 +56,8 @@
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/inquirer": "^7.3.3",
"@types/lodash": "^4.14.171",
"@types/node": "^16.4.10",
"@types/lodash-es": "^4.14.172",
"@types/node": "^16.4.13",
"@types/sinon": "^10.0.2",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
@ -65,7 +65,7 @@
"benny": "^3.6.15",
"c8": "^7.8.0",
"cross-env": "^7.0.3",
"esbuild": "^0.12.17",
"esbuild": "^0.12.18",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",

View file

@ -3,8 +3,6 @@
"allowSyntheticDefaultImports": true,
"declaration": true,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"allowJs": true,
"module": "CommonJS",

1249
yarn.lock

File diff suppressed because it is too large Load diff