Skip to content

Commit

Permalink
fix(cli): copy binding files into wasi packages (#1881)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 31, 2023
1 parent 65273a4 commit f298016
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
6 changes: 6 additions & 0 deletions cli/codegen/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ const ARTIFACTS_OPTIONS: CommandSchema = {
description: 'Path to the folder where the npm packages put',
default: "'npm'",
},
{
name: 'buildOutputDir',
type: 'string',
description:
'Path to the build output dir, only needed when targets contains `wasm32-wasi-*`',
},
],
}

Expand Down
8 changes: 4 additions & 4 deletions cli/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from 'child_process'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { kebabCase, startCase } from 'lodash-es'

Expand Down
1 change: 1 addition & 0 deletions cli/docs/artifacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ new NapiCli().artifacts({
| packageJsonPath | --package-json-path | string | false | 'package.json' | Path to `package.json` |
| outputDir | --output-dir,-o,-d | string | false | './artifacts' | Path to the folder where all built `.node` files put, same as `--output-dir` of build command |
| npmDir | --npm-dir | string | false | 'npm' | Path to the folder where the npm packages put |
| buildOutputDir | --build-output-dir | string | false | | Path to the build output dir, only needed when targets contains `wasm32-wasi-*` |
43 changes: 39 additions & 4 deletions cli/src/api/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, parse, resolve } from 'node:path'
import { join, parse } from 'node:path'

import * as colors from 'colorette'

Expand All @@ -20,11 +20,11 @@ const debug = debugFactory('artifacts')
export async function collectArtifacts(userOptions: ArtifactsOptions) {
const options = applyDefaultArtifactsOptions(userOptions)

const packageJsonPath = resolve(options.cwd, options.packageJsonPath)
const packageJsonPath = join(options.cwd, options.packageJsonPath)
const { targets, binaryName } = await readNapiConfig(packageJsonPath)

const distDirs = targets.map((platform) =>
resolve(options.cwd, options.npmDir, platform.platformArchABI),
join(options.cwd, options.npmDir, platform.platformArchABI),
)

const universalSourceBins = new Set(
Expand All @@ -36,7 +36,7 @@ export async function collectArtifacts(userOptions: ArtifactsOptions) {
.filter(Boolean) as string[],
)

await collectNodeBinaries(resolve(options.cwd, options.outputDir)).then(
await collectNodeBinaries(join(options.cwd, options.outputDir)).then(
(output) =>
Promise.all(
output.map(async (filePath) => {
Expand Down Expand Up @@ -80,6 +80,41 @@ export async function collectArtifacts(userOptions: ArtifactsOptions) {
}),
),
)

const wasiTarget = targets.find((t) => t.platform === 'wasi')
if (wasiTarget) {
const wasiDir = join(
options.cwd,
options.npmDir,
wasiTarget.platformArchABI,
)
const cjsFile = join(
options.buildOutputDir ?? options.cwd,
`${binaryName}.wasi.cjs`,
)
const workerFile = join(
options.buildOutputDir ?? options.cwd,
`wasi-worker.mjs`,
)
debug.info(
`Move wasi cjs file [${colors.yellowBright(
cjsFile,
)}] to [${colors.yellowBright(wasiDir)}]`,
)
await writeFileAsync(
join(wasiDir, `${binaryName}.wasi.cjs`),
await readFileAsync(cjsFile),
)
debug.info(
`Move wasi worker file [${colors.yellowBright(
workerFile,
)}] to [${colors.yellowBright(wasiDir)}]`,
)
await writeFileAsync(
join(wasiDir, `wasi-worker.mjs`),
await readFileAsync(workerFile),
)
}
}

async function collectNodeBinaries(root: string) {
Expand Down
17 changes: 17 additions & 0 deletions cli/src/api/create-npm-dirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
Target,
} from '../utils/index.js'

import type { PackageMeta } from './templates/package.json.js'

const debug = debugFactory('create-npm-dirs')

export async function createNpmDirs(userOptions: CreateNpmDirsOptions) {
Expand Down Expand Up @@ -82,6 +84,21 @@ export async function createNpmDirs(userOptions: CreateNpmDirsOptions) {
if (target.arch !== 'wasm32') {
// @ts-expect-error
scopedPackageJson.os = [target.platform]
} else {
const entry = `${binaryName}.wasi.cjs`
scopedPackageJson.files.push(entry, `wasi-worker.mjs`)
scopedPackageJson.main = entry
const emnapiCore = await fetch(
`https://registry.npmjs.org/@emnapi/core`,
).then((res) => res.json() as Promise<PackageMeta>)
const emnapiRuntime = await fetch(
`https://registry.npmjs.org/@emnapi/runtime`,
).then((res) => res.json() as Promise<PackageMeta>)
// @ts-expect-error
scopedPackageJson.dependencies = {
'@emnapi/core': `^${emnapiCore['dist-tags'].latest}`,
'@emnapi/runtime': `^${emnapiRuntime['dist-tags'].latest}`,
}
}

if (target.abi === 'gnu') {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/api/templates/package.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
} from '../../utils/config.js'
import { UNIVERSAL_TARGETS } from '../../utils/target.js'

interface PackageMeta {
export interface PackageMeta {
'dist-tags': { [index: string]: string }
}

Expand Down
10 changes: 10 additions & 0 deletions cli/src/def/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ export abstract class BaseArtifactsCommand extends Command {
description: 'Path to the folder where the npm packages put',
})

buildOutputDir?: string = Option.String('--build-output-dir', {
description:
'Path to the build output dir, only needed when targets contains `wasm32-wasi-*`',
})

getOptions() {
return {
cwd: this.cwd,
configPath: this.configPath,
packageJsonPath: this.packageJsonPath,
outputDir: this.outputDir,
npmDir: this.npmDir,
buildOutputDir: this.buildOutputDir,
}
}
}
Expand Down Expand Up @@ -75,6 +81,10 @@ export interface ArtifactsOptions {
* @default 'npm'
*/
npmDir?: string
/**
* Path to the build output dir, only needed when targets contains `wasm32-wasi-*`
*/
buildOutputDir?: string
}

export function applyDefaultArtifactsOptions(options: ArtifactsOptions) {
Expand Down

0 comments on commit f298016

Please sign in to comment.