Skip to content

Commit

Permalink
feat(nuxi): switch to unjs/giget for nuxi init (#7361)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 8, 2022
1 parent 3a3d913 commit 6e16f76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/nuxi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"destr": "^1.1.1",
"execa": "^6.1.0",
"flat": "^5.0.2",
"giget": "^0.0.4",
"jiti": "^1.15.0",
"listhen": "^0.2.15",
"mlly": "^0.5.14",
Expand All @@ -44,7 +45,6 @@
"scule": "^0.3.2",
"semver": "^7.3.7",
"superb": "^4.0.0",
"tiged": "^2.12.0",
"unbuild": "latest"
},
"optionalDependencies": {
Expand Down
33 changes: 13 additions & 20 deletions packages/nuxi/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { existsSync, readdirSync } from 'node:fs'
// @ts-expect-error missing types
import createTiged from 'tiged'
import { downloadRepo, startShell } from 'giget'
import { relative, resolve } from 'pathe'
import superb from 'superb'
import consola from 'consola'
Expand Down Expand Up @@ -28,30 +26,21 @@ const resolveTemplate = (template: string | boolean) => {
export default defineNuxtCommand({
meta: {
name: 'init',
usage: 'npx nuxi init|create [--verbose|-v] [--template,-t] [dir]',
usage: 'npx nuxi init|create [--template,-t] [--force] [--offline] [--prefer-offline] [--shell] [dir]',
description: 'Initialize a fresh project'
},
async invoke (args) {
// Clone template
const src = resolveTemplate(args.template || args.t)
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
const tiged = createTiged(src, { cache: false /* TODO: buggy */, verbose: (args.verbose || args.v) })
if (existsSync(dstDir) && readdirSync(dstDir).length) {
consola.error(`Directory ${dstDir} is not empty. Please pick another name or remove it first. Aborting.`)
process.exit(1)
}
const formatArgs = (msg: string) => msg.replace('options.', '--')
tiged.on('warn', (event: any) => consola.warn(formatArgs(event.message)))
tiged.on('info', (event: any) => consola.info(formatArgs(event.message)))
try {
await tiged.clone(dstDir)
} catch (e: any) {
if (e.toString().includes('could not find commit hash')) {
consola.error(`Failed to clone template from \`${src}\`. Please check the repo is valid and that you have installed \`git\` correctly.`)
process.exit(1)
}
throw e
if (args.verbose || args.v) {
process.env.DEBUG = process.env.DEBUG || 'true'
}
await downloadRepo(src, dstDir, {
force: args.force,
offline: args.offline,
preferOffline: args['prefer-offline']
})

// Show next steps
const relativeDist = rpath(dstDir)
Expand All @@ -65,5 +54,9 @@ export default defineNuxtCommand({
for (const step of nextSteps) {
consola.log(` ${step}\n`)
}

if (args.shell) {
startShell(dstDir)
}
}
})

0 comments on commit 6e16f76

Please sign in to comment.