Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(cli)!: use giget 0.1x with template registry for nuxi init #7404

Merged
merged 5 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions docs/content/3.api/5.commands/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
npx nuxi init|create [--verbose|-v] [--template,-t] [dir]
```

The `init` command initializes a fresh Nuxt project.
The `init` command initializes a fresh Nuxt project using [unjs/giget](https://github.com/unjs/giget).

## Options

Option | Default | Description
-------------------------|-----------------|------------------
`--template, -t` | `nuxt/starter#v3` | Specify a Git repository to use as a template.
`--template, -t` | `v3` | Specify template name or git repository to use as a template. Format is `gh:org/name` to use a custom github template.
`--force` | `false` | Force clone to any existing directory.
`--offline` | `false` | Do not attempt to download from github and only use local cache.
`--prefer-offline` | `false` | Try local cache first. (might be outdated)
`--shell` | `false` | (experimental) Open shell in cloned directory.
`dir` | `nuxt-app` | Name of the install directory.
`--prefer-offline` | `false` | Try local cache first to download templates.
`--shell` | `false` | Open shell in cloned directory (experimental).

## Environment variables

- `NUXI_INIT_REGISTRY`: Set to a custom template registry. ([learn more](https://github.com/unjs/giget#custom-registry)).
- Default registry is loaded from [nuxt/starter/templates](https://github.com/nuxt/starter/tree/templates/templates)
3 changes: 1 addition & 2 deletions packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"destr": "^1.1.1",
"execa": "^6.1.0",
"flat": "^5.0.2",
"giget": "^0.0.4",
"giget": "^0.1.5",
"jiti": "^1.15.0",
"listhen": "^0.2.15",
"mlly": "^0.5.14",
Expand All @@ -44,7 +44,6 @@
"pkg-types": "^0.3.5",
"scule": "^0.3.2",
"semver": "^7.3.7",
"superb": "^4.0.0",
"unbuild": "latest"
},
"optionalDependencies": {
Expand Down
49 changes: 16 additions & 33 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { downloadRepo, startShell } from 'giget'
import { relative, resolve } from 'pathe'
import superb from 'superb'
import { downloadTemplate, startShell } from 'giget'
import { relative } from 'pathe'
import consola from 'consola'
import { defineNuxtCommand } from './index'

const rpath = (p: string) => relative(process.cwd(), p)

const resolveTemplate = (template: string | boolean) => {
if (typeof template === 'boolean') {
consola.error('Please specify a template!')
process.exit(1)
}

if (!template) {
template = 'v3'
}

if (template.includes('/')) {
return template
}

return `nuxt/starter#${template}`
}
const DEFAULT_REGISTRY = 'https://github.com/raw/nuxt/starter/templates/templates'

export default defineNuxtCommand({
meta: {
Expand All @@ -31,32 +15,31 @@ export default defineNuxtCommand({
},
async invoke (args) {
// Clone template
const src = resolveTemplate(args.template || args.t)
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
if (args.verbose || args.v) {
process.env.DEBUG = process.env.DEBUG || 'true'
}
await downloadRepo(src, dstDir, {
const template = args.template || args.t || 'v3'

const t = await downloadTemplate(template, {
dir: args._[0] as string,
force: args.force,
offline: args.offline,
preferOffline: args['prefer-offline']
preferOffline: args['prefer-offline'],
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY
})

// Show next steps
const relativeDist = rpath(dstDir)
const relativeDist = rpath(t.dir)
const nextSteps = [
relativeDist.length > 1 && `πŸ“ \`cd ${relativeDist}\``,
'πŸ’Ώ Install dependencies with `npm install` or `yarn install` or `pnpm install --shamefully-hoist`',
'πŸš€ Start development server with `npm run dev` or `yarn dev` or `pnpm run dev`'
!args.shell && relativeDist.length > 1 && `\`cd ${relativeDist}\``,
'Install dependencies with `npm install` or `yarn install` or `pnpm install --shamefully-hoist`',
'Start development server with `npm run dev` or `yarn dev` or `pnpm run dev`'
].filter(Boolean)

consola.log(`\n ✨ Your ${superb.random()} Nuxt project is just created! Next steps:\n`)
consola.log(`✨ Nuxt project is created with \`${t.name}\` template. Next steps:`)
for (const step of nextSteps) {
consola.log(` ${step}\n`)
consola.log(` β€Ί ${step}`)
}

if (args.shell) {
startShell(dstDir)
startShell(t.dir)
}
}
})
36 changes: 5 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7442,9 +7442,9 @@ __metadata:
languageName: node
linkType: hard

"giget@npm:^0.0.4":
version: 0.0.4
resolution: "giget@npm:0.0.4"
"giget@npm:^0.1.5":
version: 0.1.5
resolution: "giget@npm:0.1.5"
dependencies:
colorette: ^2.0.19
mri: ^1.2.0
Expand All @@ -7453,7 +7453,7 @@ __metadata:
tar: ^6.1.11
bin:
giget: dist/cli.mjs
checksum: 6e6f86c125c477d47133ddc8768bcb5e4d91d9ada6f372174e2b56d1d854cefdc1e7b5e64884e618126dcc147cfc1448f661698cd6ddc6fce29703fa1a50dea9
checksum: 3f9e50f3bd4f9109c7cb511427449daa66c54f466a7e3169bd2475984b60b83e5e840dc35b71373ee137be22458ad1a31564fa2f99e9fc50ec760849d4d30e62
languageName: node
linkType: hard

Expand Down Expand Up @@ -10151,7 +10151,7 @@ __metadata:
execa: ^6.1.0
flat: ^5.0.2
fsevents: ~2.3.2
giget: ^0.0.4
giget: ^0.1.5
jiti: ^1.15.0
listhen: ^0.2.15
mlly: ^0.5.14
Expand All @@ -10161,7 +10161,6 @@ __metadata:
pkg-types: ^0.3.5
scule: ^0.3.2
semver: ^7.3.7
superb: ^4.0.0
unbuild: latest
dependenciesMeta:
fsevents:
Expand Down Expand Up @@ -12621,15 +12620,6 @@ __metadata:
languageName: node
linkType: hard

"superb@npm:^4.0.0":
version: 4.0.0
resolution: "superb@npm:4.0.0"
dependencies:
unique-random-array: ^2.0.0
checksum: b80b4d347954ac994863386f6f6ebe176b93ebe8a14258191c118d92ab78296095b8195ae23f4b21e83519ac7eec36cc553fc2672914dd5d3ee98418e93a6bdc
languageName: node
linkType: hard

"supports-color@npm:^5.3.0":
version: 5.5.0
resolution: "supports-color@npm:5.5.0"
Expand Down Expand Up @@ -13267,22 +13257,6 @@ __metadata:
languageName: node
linkType: hard

"unique-random-array@npm:^2.0.0":
version: 2.0.0
resolution: "unique-random-array@npm:2.0.0"
dependencies:
unique-random: ^2.1.0
checksum: c983e511fe6f45b9ee9b78d26a78e94ba798a4afaa2ca289948e694bc6ad75efca0dd1944e94a010ab56dd574daf4ebcf72727c6d581693610d3b0a8192ef2e5
languageName: node
linkType: hard

"unique-random@npm:^2.1.0":
version: 2.1.0
resolution: "unique-random@npm:2.1.0"
checksum: 7bc33f364f288172a948c82c0bbb7393fd175f3a78932aa3a77f070d786d5e86db869dc72f7cb9e0a26e5d3142bfd5c3c9d134a68473a3b1f4e2300d84dbf39d
languageName: node
linkType: hard

"unique-slug@npm:^3.0.0":
version: 3.0.0
resolution: "unique-slug@npm:3.0.0"
Expand Down