@@ -2,6 +2,9 @@ import { existsSync } from 'node:fs'
22import path from 'path'
33import { defu } from 'defu'
44import { addPrerenderRoutes , installModule , defineNuxtModule , addPlugin , extendViteConfig , createResolver , logger , addComponentsDir , addServerHandler , resolveAlias , addVitePlugin } from '@nuxt/kit'
5+ import { findNearestFile } from 'pkg-types'
6+ // @ts -ignore
7+ import gitUrlParse from 'git-url-parse'
58
69const log = logger . withTag ( '@nuxt/studio' )
710
@@ -88,9 +91,11 @@ export default defineNuxtModule<ModuleOptions>({
8891
8992 const apiURL = process . env . NUXT_PUBLIC_STUDIO_API_URL || process . env . STUDIO_API || 'https://api.nuxt.studio'
9093 const publicToken = process . env . NUXT_PUBLIC_STUDIO_TOKENS
94+ const gitInfo = await _getLocalGitInfo ( nuxt . options . rootDir ) || _getGitEnv ( ) || { }
9195 nuxt . options . runtimeConfig . studio = defu ( nuxt . options . runtimeConfig . studio as any , {
9296 publicToken,
93- project : options . project
97+ project : options . project ,
98+ gitInfo
9499 } )
95100 nuxt . options . runtimeConfig . public . studio = defu ( nuxt . options . runtimeConfig . public . studio as any , { apiURL } )
96101
@@ -138,3 +143,90 @@ export default defineNuxtModule<ModuleOptions>({
138143 } )
139144 }
140145} )
146+
147+ // --- Utilities to get git info ---
148+
149+ interface GitInfo {
150+ // Repository name
151+ name : string ,
152+ // Repository owner/organization
153+ owner : string ,
154+ // Repository URL
155+ url : string ,
156+ }
157+
158+ async function _getLocalGitInfo ( rootDir : string ) : Promise < GitInfo | void > {
159+ const remote = await _getLocalGitRemote ( rootDir )
160+ if ( ! remote ) {
161+ return
162+ }
163+
164+ // https://www.npmjs.com/package/git-url-parse#clipboard-example
165+ const { name, owner, source } = gitUrlParse ( remote ) as Record < string , string >
166+ const url = `https://${ source } /${ owner } /${ name } `
167+
168+ return {
169+ name,
170+ owner,
171+ url
172+ }
173+ }
174+
175+ async function _getLocalGitRemote ( dir : string ) {
176+ try {
177+ // https://www.npmjs.com/package/parse-git-config#options
178+ const parseGitConfig = await import ( 'parse-git-config' as string ) . then (
179+ m => m . promise
180+ ) as ( opts : { path : string } ) => Promise < Record < string , Record < string , string > > >
181+ const gitDir = await findNearestFile ( '.git/config' , { startingFrom : dir } )
182+ const parsed = await parseGitConfig ( { path : gitDir } )
183+ if ( ! parsed ) {
184+ return
185+ }
186+ const gitRemote = parsed [ 'remote "origin"' ] . url
187+ return gitRemote
188+ } catch ( err ) {
189+
190+ }
191+ }
192+
193+ function _getGitEnv ( ) : GitInfo {
194+ // https://github.com/unjs/std-env/issues/59
195+ const envInfo = {
196+ // Provider
197+ provider : process . env . VERCEL_GIT_PROVIDER || // vercel
198+ ( process . env . GITHUB_SERVER_URL ? 'github' : undefined ) || // github
199+ '' ,
200+ // Owner
201+ owner : process . env . VERCEL_GIT_REPO_OWNER || // vercel
202+ process . env . GITHUB_REPOSITORY_OWNER || // github
203+ process . env . CI_PROJECT_PATH ?. split ( '/' ) . shift ( ) || // gitlab
204+ '' ,
205+ // Name
206+ name : process . env . VERCEL_GIT_REPO_SLUG ||
207+ process . env . GITHUB_REPOSITORY ?. split ( '/' ) . pop ( ) || // github
208+ process . env . CI_PROJECT_PATH ?. split ( '/' ) . splice ( 1 ) . join ( '/' ) || // gitlab
209+ '' ,
210+ // Url
211+ url : process . env . REPOSITORY_URL || '' // netlify
212+ }
213+
214+ if ( ! envInfo . url && envInfo . provider && envInfo . owner && envInfo . name ) {
215+ envInfo . url = `https://${ envInfo . provider } .com/${ envInfo . owner } /${ envInfo . name } `
216+ }
217+
218+ // If only url available (ex: Netlify)
219+ if ( ! envInfo . name && ! envInfo . owner && envInfo . url ) {
220+ try {
221+ const { name, owner } = gitUrlParse ( envInfo . url ) as Record < string , string >
222+ envInfo . name = name
223+ envInfo . owner = owner
224+ } catch { }
225+ }
226+
227+ return {
228+ name : envInfo . name ,
229+ owner : envInfo . owner ,
230+ url : envInfo . url
231+ }
232+ }
0 commit comments