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

This commit is contained in:
pooya parsa 2022-09-10 15:37:25 +02:00 committed by GitHub
parent 9abc7a2122
commit 270055b86d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 71 deletions

View File

@ -4,13 +4,19 @@
npx nuxi init|create [--verbose|-v] [--template,-t] [dir] 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 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. `--force` | `false` | Force clone to any existing directory.
`--offline` | `false` | Do not attempt to download from github and only use local cache. `--offline` | `false` | Do not attempt to download from github and only use local cache.
`--prefer-offline` | `false` | Try local cache first. (might be outdated) `--prefer-offline` | `false` | Try local cache first to download templates.
`--shell` | `false` | (experimental) Open shell in cloned directory. `--shell` | `false` | Open shell in cloned directory (experimental).
`dir` | `nuxt-app` | Name of the install directory.
## 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)

View File

@ -34,7 +34,7 @@
"destr": "^1.1.1", "destr": "^1.1.1",
"execa": "^6.1.0", "execa": "^6.1.0",
"flat": "^5.0.2", "flat": "^5.0.2",
"giget": "^0.0.4", "giget": "^0.1.5",
"jiti": "^1.15.0", "jiti": "^1.15.0",
"listhen": "^0.2.15", "listhen": "^0.2.15",
"mlly": "^0.5.14", "mlly": "^0.5.14",
@ -44,7 +44,6 @@
"pkg-types": "^0.3.5", "pkg-types": "^0.3.5",
"scule": "^0.3.2", "scule": "^0.3.2",
"semver": "^7.3.7", "semver": "^7.3.7",
"superb": "^4.0.0",
"unbuild": "latest" "unbuild": "latest"
}, },
"optionalDependencies": { "optionalDependencies": {

View File

@ -1,27 +1,11 @@
import { downloadRepo, startShell } from 'giget' import { downloadTemplate, startShell } from 'giget'
import { relative, resolve } from 'pathe' import { relative } from 'pathe'
import superb from 'superb'
import consola from 'consola' import consola from 'consola'
import { defineNuxtCommand } from './index' import { defineNuxtCommand } from './index'
const rpath = (p: string) => relative(process.cwd(), p) const rpath = (p: string) => relative(process.cwd(), p)
const resolveTemplate = (template: string | boolean) => { const DEFAULT_REGISTRY = 'https://raw.githubusercontent.com/nuxt/starter/templates/templates'
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}`
}
export default defineNuxtCommand({ export default defineNuxtCommand({
meta: { meta: {
@ -31,32 +15,31 @@ export default defineNuxtCommand({
}, },
async invoke (args) { async invoke (args) {
// Clone template // Clone template
const src = resolveTemplate(args.template || args.t) const template = args.template || args.t || 'v3'
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
if (args.verbose || args.v) { const t = await downloadTemplate(template, {
process.env.DEBUG = process.env.DEBUG || 'true' dir: args._[0] as string,
}
await downloadRepo(src, dstDir, {
force: args.force, force: args.force,
offline: args.offline, offline: args.offline,
preferOffline: args['prefer-offline'] preferOffline: args['prefer-offline'],
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY
}) })
// Show next steps // Show next steps
const relativeDist = rpath(dstDir) const relativeDist = rpath(t.dir)
const nextSteps = [ const nextSteps = [
relativeDist.length > 1 && `📁 \`cd ${relativeDist}\``, !args.shell && relativeDist.length > 1 && `\`cd ${relativeDist}\``,
'💿 Install dependencies with `npm install` or `yarn install` or `pnpm install --shamefully-hoist`', '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`' 'Start development server with `npm run dev` or `yarn dev` or `pnpm run dev`'
].filter(Boolean) ].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) { for (const step of nextSteps) {
consola.log(` ${step}\n`) consola.log(` ${step}`)
} }
if (args.shell) { if (args.shell) {
startShell(dstDir) startShell(t.dir)
} }
} }
}) })

View File

@ -7442,9 +7442,9 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"giget@npm:^0.0.4": "giget@npm:^0.1.5":
version: 0.0.4 version: 0.1.5
resolution: "giget@npm:0.0.4" resolution: "giget@npm:0.1.5"
dependencies: dependencies:
colorette: ^2.0.19 colorette: ^2.0.19
mri: ^1.2.0 mri: ^1.2.0
@ -7453,7 +7453,7 @@ __metadata:
tar: ^6.1.11 tar: ^6.1.11
bin: bin:
giget: dist/cli.mjs giget: dist/cli.mjs
checksum: 6e6f86c125c477d47133ddc8768bcb5e4d91d9ada6f372174e2b56d1d854cefdc1e7b5e64884e618126dcc147cfc1448f661698cd6ddc6fce29703fa1a50dea9 checksum: 3f9e50f3bd4f9109c7cb511427449daa66c54f466a7e3169bd2475984b60b83e5e840dc35b71373ee137be22458ad1a31564fa2f99e9fc50ec760849d4d30e62
languageName: node languageName: node
linkType: hard linkType: hard
@ -10151,7 +10151,7 @@ __metadata:
execa: ^6.1.0 execa: ^6.1.0
flat: ^5.0.2 flat: ^5.0.2
fsevents: ~2.3.2 fsevents: ~2.3.2
giget: ^0.0.4 giget: ^0.1.5
jiti: ^1.15.0 jiti: ^1.15.0
listhen: ^0.2.15 listhen: ^0.2.15
mlly: ^0.5.14 mlly: ^0.5.14
@ -10161,7 +10161,6 @@ __metadata:
pkg-types: ^0.3.5 pkg-types: ^0.3.5
scule: ^0.3.2 scule: ^0.3.2
semver: ^7.3.7 semver: ^7.3.7
superb: ^4.0.0
unbuild: latest unbuild: latest
dependenciesMeta: dependenciesMeta:
fsevents: fsevents:
@ -12621,15 +12620,6 @@ __metadata:
languageName: node languageName: node
linkType: hard 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": "supports-color@npm:^5.3.0":
version: 5.5.0 version: 5.5.0
resolution: "supports-color@npm:5.5.0" resolution: "supports-color@npm:5.5.0"
@ -13267,22 +13257,6 @@ __metadata:
languageName: node languageName: node
linkType: hard 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": "unique-slug@npm:^3.0.0":
version: 3.0.0 version: 3.0.0
resolution: "unique-slug@npm:3.0.0" resolution: "unique-slug@npm:3.0.0"