From 2c699b532669da2f93e14040f21fa461ff415675 Mon Sep 17 00:00:00 2001 From: Eckhardt-D Date: Thu, 30 May 2024 07:42:25 +0200 Subject: [PATCH] feat(init): Use user provided dir for package.json name field --- src/commands/init.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 4c90c4e8..1a001ca9 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,10 +1,11 @@ import { downloadTemplate, startShell } from 'giget' import type { DownloadTemplateResult } from 'giget' -import { relative, resolve } from 'pathe' +import { relative, resolve, join } from 'pathe' import { consola } from 'consola' import { installDependencies } from 'nypm' import type { PackageManagerName } from 'nypm' import { defineCommand } from 'citty' +import { readPackageJSON, writePackageJSON } from 'pkg-types' import { sharedArgs } from './_shared' @@ -83,6 +84,13 @@ export default defineCommand({ preferOffline: Boolean(ctx.args.preferOffline), registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY, }) + + if (ctx.args.dir.length > 0) { + const pkg = await readPackageJSON(template.dir) + // Handles paths like ../some/dir with sane fallback + pkg.name = ctx.args.dir.split('/').at(-1) || pkg.name + await writePackageJSON(join(template.dir, 'package.json'), pkg) + } } catch (err) { if (process.env.DEBUG) {