feat(cli): create npm dir while creating new project

This commit is contained in:
LongYinan 2020-12-25 18:10:53 +08:00
parent 9fb5a61a0b
commit 1537f44038
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
3 changed files with 35 additions and 14 deletions

View file

@ -2,8 +2,11 @@ import { join } from 'path'
import { DefaultPlatforms, PlatformDetail, parseTriple } from './parse-triple'
export function getNapiConfig(packageJson = 'package.json') {
const packageJsonPath = join(process.cwd(), packageJson)
export function getNapiConfig(
packageJson = 'package.json',
cwd = process.cwd(),
) {
const packageJsonPath = join(cwd, packageJson)
const pkgJson = require(packageJsonPath)
const { version: packageVersion, napi, name } = pkgJson

View file

@ -17,15 +17,12 @@ export class CreateNpmDirCommand extends Command {
description: 'Create npm packages dir for platforms',
})
@Command.String('-t,--target')
targetDir!: string
@Command.String('-c,--config')
config = 'package.json'
@Command.Path('create-npm-dir')
async execute() {
const pkgJsonDir = this.config
static create = async (
config: string,
targetDirPath: string,
cwd: string,
) => {
const pkgJsonDir = config
debug(`Read content from [${chalk.yellowBright(pkgJsonDir)}]`)
const {
platforms,
@ -33,12 +30,11 @@ export class CreateNpmDirCommand extends Command {
version,
binaryName,
content,
} = getNapiConfig(pkgJsonDir)
} = getNapiConfig(pkgJsonDir, cwd)
for (const platformDetail of platforms) {
const targetDir = join(
process.cwd(),
this.targetDir,
targetDirPath,
'npm',
`${platformDetail.platformArchABI}`,
)
@ -78,6 +74,21 @@ export class CreateNpmDirCommand extends Command {
await writeFileAsync(targetReadme, readme(packageName, platformDetail))
}
}
@Command.String('-t,--target')
targetDir!: string
@Command.String('-c,--config')
config = 'package.json'
@Command.Path('create-npm-dir')
async execute() {
await CreateNpmDirCommand.create(
this.config,
join(process.cwd(), this.targetDir),
process.cwd(),
)
}
}
function readme(packageName: string, platformDetail: PlatformDetail) {

View file

@ -5,6 +5,7 @@ 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 { spawn } from '../spawn'
@ -146,6 +147,12 @@ export class NewProjectCommand extends Command {
createGithubActionsCIYml(binaryName, this.targets!),
)
}
await CreateNpmDirCommand.create(
'package.json',
join(process.cwd(), this.dirname!),
join(process.cwd(), this.dirname!),
)
}
private writeFile(path: string, content: string) {