feat(cli): new project command

This commit is contained in:
LongYinan 2020-12-16 00:04:11 +08:00
parent 5869e04283
commit 6fe60f69c1
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
10 changed files with 1001 additions and 1 deletions

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
exclude = ["./test_module", "./bench"] exclude = ["./test_module", "./bench", "./testing"]
members = [ members = [
"./build", "./build",
"./napi", "./napi",

5
cli/.npmignore Normal file
View file

@ -0,0 +1,5 @@
.git
yarn.lock
target
package-template/npm
package-template/README.md

View file

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

18
cli/src/new/cargo.ts Normal file
View file

@ -0,0 +1,18 @@
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
`

668
cli/src/new/ci-yml.ts Normal file
View file

@ -0,0 +1,668 @@
const OLDEST_LTS_NODE = 10
const LATEST_LTS_NODE = 14
const SUPPORTED_NODE_VERSIONS = [10, 12, 14, 15]
const OS_LINUX = 'ubuntu-latest'
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-beta
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@v1
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@v1
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@v1
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@v1
with:
path: ~/.cargo/registry
key: stable-node-alpine-@${OLDEST_LTS_NODE}-cargo-registry-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: stable-node-alpine-@${OLDEST_LTS_NODE}-cargo-index-trimmed-\${{ hashFiles('**/Cargo.lock') }}
- name: Cache NPM dependencies
uses: actions/cache@v1
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@v1
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@v1
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@v1
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-beta
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@v1
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@v1
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@v1
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-beta
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@v1
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@v1
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@v1
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@v1
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@v1
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@v1
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-beta
with:
node-version: \${{ matrix.node }}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v1
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@v1
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-beta
with:
node-version: \${{ matrix.node }}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v1
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-beta
with:
node-version: ${LATEST_LTS_NODE}
check-latest: true
- name: Cache NPM dependencies
uses: actions/cache@v1
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 }}
`
}

175
cli/src/new/index.ts Normal file
View file

@ -0,0 +1,175 @@
import { writeFileSync, mkdirSync } from 'fs'
import { join } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import inquirer, { prompt } from 'inquirer'
import { debugFactory } from '../debug'
import { DefaultPlatforms } from '../parse-triple'
import { spawn } from '../spawn'
import { createCargoContent } from './cargo'
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 [, defaultProjectDir] = this.name?.split('/') ?? []
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]
}
const command = `mkdir -p ${this.dirname}`
debug(`Running command: ${chalk.green('[${command}]')}`)
if (!this.dryRun) {
await spawn(command)
mkdirSync(join(process.cwd(), this.dirname!, 'src'))
}
const [, binaryName] = this.name!.split('/')
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!),
)
}
}
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
}
}
}
}

15
cli/src/new/indexjs.ts Normal file
View file

@ -0,0 +1,15 @@
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}')
`

50
cli/src/new/lib-rs.ts Normal file
View file

@ -0,0 +1,50 @@
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())
}
`

11
cli/src/new/npmignore.ts Normal file
View file

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

56
cli/src/new/package.ts Normal file
View file

@ -0,0 +1,56 @@
import { DefaultPlatforms } from '../parse-triple'
export const createPackageJson = (
name: string,
binaryName: string,
targets: string[],
) => {
const pkgContent = {
name,
napi: {
name: binaryName,
},
dependencies: {
'@node-rs/helper': '^1.0.0',
},
devDependencies: {
'@napi-rs/cli': '^1.0.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
}