napi-rs/src/create-npm-dir.ts

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-09-04 17:22:15 +09:00
import { join } from 'path'
import chalk from 'chalk'
import { Command } from 'clipanion'
import { pick } from 'lodash'
import { getNapiConfig } from './consts'
import { debugFactory } from './debug'
import { spawn } from './spawn'
import { writeFileAsync } from './utils'
const debug = debugFactory('create-npm-dir')
export class CreateNpmDirCommand extends Command {
@Command.String('-t,--target')
targetDir!: string
@Command.Path('create-npm-dir')
async execute() {
const pkgJsonDir = join(this.targetDir, 'package.json')
debug(`Read content from [${chalk.yellowBright(pkgJsonDir)}]`)
const {
platforms,
muslPlatforms,
packageName,
version,
binaryName,
content,
} = getNapiConfig(pkgJsonDir)
for (const platform of [...platforms, ...muslPlatforms]) {
const targetDir = join(process.cwd(), this.targetDir, 'npm', platform)
await spawn(`mkdir -p ${targetDir}`)
const binaryFileName = `${binaryName}.${platform}.node`
const targetPackageJson = join(targetDir, 'package.json')
debug(`Write file [${chalk.yellowBright(targetPackageJson)}]`)
await writeFileAsync(
targetPackageJson,
JSON.stringify(
{
name: `${packageName}-${platform}`,
version,
os: [platform.replace('-musl', '')],
2020-09-04 17:22:15 +09:00
main: binaryFileName,
files: [binaryFileName],
...pick(
content,
'description',
'keywords',
'author',
'homepage',
'license',
'cpu',
'engines',
'publishConfig',
'repository',
'bugs',
),
},
null,
2,
),
)
const targetReadme = join(targetDir, 'README.md')
debug(`Write target README.md [${chalk.yellowBright(targetReadme)}]`)
await writeFileAsync(targetReadme, readme(packageName, platform))
}
}
}
function readme(packageName: string, platform: string) {
2020-09-08 16:41:44 +09:00
return `# \`${packageName}-${platform}\`
2020-09-04 17:22:15 +09:00
2020-09-08 16:41:44 +09:00
This is the **${platform}** 64-bit binary for \`${packageName}\`
2020-09-04 17:22:15 +09:00
`
}