Skip to content

Commit 2c699b5

Browse files
committed
feat(init): Use user provided dir for package.json name field
1 parent bca75ab commit 2c699b5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/commands/init.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { downloadTemplate, startShell } from 'giget'
22
import type { DownloadTemplateResult } from 'giget'
3-
import { relative, resolve } from 'pathe'
3+
import { relative, resolve, join } from 'pathe'
44
import { consola } from 'consola'
55
import { installDependencies } from 'nypm'
66
import type { PackageManagerName } from 'nypm'
77
import { defineCommand } from 'citty'
8+
import { readPackageJSON, writePackageJSON } from 'pkg-types'
89

910
import { sharedArgs } from './_shared'
1011

@@ -83,6 +84,13 @@ export default defineCommand({
8384
preferOffline: Boolean(ctx.args.preferOffline),
8485
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY,
8586
})
87+
88+
if (ctx.args.dir.length > 0) {
89+
const pkg = await readPackageJSON(template.dir)
90+
// Handles paths like ../some/dir with sane fallback
91+
pkg.name = ctx.args.dir.split('/').at(-1) || pkg.name
92+
await writePackageJSON(join(template.dir, 'package.json'), pkg)
93+
}
8694
}
8795
catch (err) {
8896
if (process.env.DEBUG) {

0 commit comments

Comments
 (0)