From 1537f44038199523df435d71618326816e53c627 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 25 Dec 2020 18:10:53 +0800 Subject: [PATCH] feat(cli): create npm dir while creating new project --- cli/src/consts.ts | 7 +++++-- cli/src/create-npm-dir.ts | 35 +++++++++++++++++++++++------------ cli/src/new/index.ts | 7 +++++++ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/cli/src/consts.ts b/cli/src/consts.ts index e3e33d64..973a6512 100644 --- a/cli/src/consts.ts +++ b/cli/src/consts.ts @@ -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 diff --git a/cli/src/create-npm-dir.ts b/cli/src/create-npm-dir.ts index 67e189e1..3d352aeb 100644 --- a/cli/src/create-npm-dir.ts +++ b/cli/src/create-npm-dir.ts @@ -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) { diff --git a/cli/src/new/index.ts b/cli/src/new/index.ts index 56bcf672..8f6e61bc 100644 --- a/cli/src/new/index.ts +++ b/cli/src/new/index.ts @@ -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) {