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' import { DefaultPlatforms, PlatformDetail, parseTriple } from './parse-triple'
export function getNapiConfig(packageJson = 'package.json') { export function getNapiConfig(
const packageJsonPath = join(process.cwd(), packageJson) packageJson = 'package.json',
cwd = process.cwd(),
) {
const packageJsonPath = join(cwd, packageJson)
const pkgJson = require(packageJsonPath) const pkgJson = require(packageJsonPath)
const { version: packageVersion, napi, name } = pkgJson const { version: packageVersion, napi, name } = pkgJson

View file

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

View file

@ -5,6 +5,7 @@ import chalk from 'chalk'
import { Command } from 'clipanion' import { Command } from 'clipanion'
import inquirer, { prompt } from 'inquirer' import inquirer, { prompt } from 'inquirer'
import { CreateNpmDirCommand } from '../create-npm-dir'
import { debugFactory } from '../debug' import { debugFactory } from '../debug'
import { DefaultPlatforms } from '../parse-triple' import { DefaultPlatforms } from '../parse-triple'
import { spawn } from '../spawn' import { spawn } from '../spawn'
@ -146,6 +147,12 @@ export class NewProjectCommand extends Command {
createGithubActionsCIYml(binaryName, this.targets!), 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) { private writeFile(path: string, content: string) {