11import { existsSync } from 'node:fs'
2- import path from 'path'
2+ import path from 'node: path'
33import { defu } from 'defu'
44import { addPrerenderRoutes , installModule , defineNuxtModule , addPlugin , extendViteConfig , createResolver , logger , addComponentsDir , addServerHandler , resolveAlias , addVitePlugin } from '@nuxt/kit'
55import { findNearestFile } from 'pkg-types'
6- // @ts -ignore
6+ // @ts -expect-error import does exist
77import gitUrlParse from 'git-url-parse'
88import { version } from '../package.json'
99
@@ -13,7 +13,7 @@ export interface ModuleOptions {
1313 /**
1414 * Enable Studio mode
1515 * @default : 'production'
16- ** /
16+ */
1717 enabled : 'production' | true
1818}
1919
@@ -22,17 +22,17 @@ export interface ModuleHooks {}
2222export default defineNuxtModule < ModuleOptions > ( {
2323 meta : {
2424 name : 'studio' ,
25- configKey : 'studio'
25+ configKey : 'studio' ,
2626 } ,
2727 defaults : {
28- enabled : 'production'
28+ enabled : 'production' ,
2929 } ,
30- async setup ( options , nuxt ) {
31- // @ts -ignore
30+ async setup ( options , nuxt ) {
31+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3232 nuxt . hook ( 'schema:resolved' , ( schema : any ) => {
3333 nuxt . options . runtimeConfig . appConfigSchema = {
3434 properties : schema . properties ?. appConfig ,
35- default : schema . default ?. appConfig
35+ default : schema . default ?. appConfig ,
3636 }
3737 nuxt . options . runtimeConfig . contentSchema = schema . properties ?. content || { }
3838 } )
@@ -53,25 +53,24 @@ export default defineNuxtModule<ModuleOptions>({
5353 const contentModule = '@nuxt/content'
5454 // Check Content module is installed
5555 if (
56- ! nuxt . options . runtimeConfig . content &&
57- ! nuxt . options . modules . includes ( contentModule )
56+ ! nuxt . options . runtimeConfig . content
57+ && ! nuxt . options . modules . includes ( contentModule )
5858 ) {
5959 log . warn ( 'Could not find `@nuxt/content` module. Please install it to enable preview mode.' )
6060 return
6161 }
6262 // Check Content module version
6363 const contentModuleVersion = await import ( contentModule )
6464 . then ( m => m . default || m )
65- . then ( ( m : any ) => m . getMeta ( ) )
65+ . then ( m => m . getMeta ( ) )
6666 . then ( m => m . version )
6767 . catch ( ( ) => '0' )
6868 if ( contentModuleVersion < '2.1.1' ) {
6969 log . warn ( 'Please update `@nuxt/content` to version 2.1.1 or higher to enable preview mode.' )
7070 return
7171 }
7272
73- // Check Pinceau module activated
74- // @ts -ignore
73+ // @ts -expect-error Check Pinceau module activated
7574 nuxt . hook ( 'pinceau:options' , ( options ) => {
7675 options . studio = true
7776 } )
@@ -82,34 +81,34 @@ export default defineNuxtModule<ModuleOptions>({
8281 const publicToken = process . env . NUXT_PUBLIC_STUDIO_TOKENS
8382 const iframeMessagingAllowedOrigins = process . env . IFRAME_MESSAGING_ALLOWED_ORIGINS
8483 const gitInfo = await _getLocalGitInfo ( nuxt . options . rootDir ) || _getGitEnv ( ) || { }
85- nuxt . options . runtimeConfig . studio = defu ( nuxt . options . runtimeConfig . studio as any , {
84+ nuxt . options . runtimeConfig . studio = defu ( nuxt . options . runtimeConfig . studio , {
8685 version,
8786 publicToken,
88- gitInfo
87+ gitInfo,
8988 } )
90- nuxt . options . runtimeConfig . public . studio = defu ( nuxt . options . runtimeConfig . public . studio as any , { apiURL, iframeMessagingAllowedOrigins } )
89+ nuxt . options . runtimeConfig . public . studio = defu ( nuxt . options . runtimeConfig . public . studio , { apiURL, iframeMessagingAllowedOrigins } )
9190
9291 extendViteConfig ( ( config ) => {
9392 config . optimizeDeps = config . optimizeDeps || { }
9493 config . optimizeDeps . include = config . optimizeDeps . include || [ ]
9594 config . optimizeDeps . include . push (
96- 'socket.io-client' , 'slugify'
95+ 'socket.io-client' , 'slugify' ,
9796 )
9897 } )
9998
10099 if ( contentModuleVersion === '2.10.0' ) {
101100 addVitePlugin ( {
102101 name : 'content-resolver' ,
103102 enforce : 'pre' ,
104- resolveId ( id , importer ) {
103+ resolveId ( id , importer ) {
105104 if ( id . endsWith ( '.mjs' ) && ( ( importer || '' ) . includes ( '@nuxt/content/dist' ) || id . includes ( '@nuxt/content/dist' ) ) ) {
106105 id = id
107106 . replace ( '.mjs' , '.js' )
108107 . replace ( / ^ \/ n o d e _ m o d u l e s / , './node_modules/' )
109108
110109 return path . resolve ( path . dirname ( importer || __dirname ) , id . replace ( '.mjs' , '.js' ) )
111110 }
112- }
111+ } ,
113112 } )
114113 }
115114
@@ -123,29 +122,29 @@ export default defineNuxtModule<ModuleOptions>({
123122 addServerHandler ( {
124123 method : 'get' ,
125124 route : '/__studio.json' ,
126- handler : resolve ( './runtime/server/routes/studio' )
125+ handler : resolve ( './runtime/server/routes/studio' ) ,
127126 } )
128127 addPrerenderRoutes ( '/__studio.json' )
129128
130129 // Install dependencies
131130 await installModule ( 'nuxt-component-meta' , {
132- globalsOnly : true
131+ globalsOnly : true ,
133132 } )
134- }
133+ } ,
135134} )
136135
137136// --- Utilities to get git info ---
138137
139138interface GitInfo {
140139 // Repository name
141- name : string ,
140+ name : string
142141 // Repository owner/organization
143- owner : string ,
142+ owner : string
144143 // Repository URL
145- url : string ,
144+ url : string
146145}
147146
148- async function _getLocalGitInfo ( rootDir : string ) : Promise < GitInfo | void > {
147+ async function _getLocalGitInfo ( rootDir : string ) : Promise < GitInfo | undefined > {
149148 const remote = await _getLocalGitRemote ( rootDir )
150149 if ( ! remote ) {
151150 return
@@ -158,15 +157,15 @@ async function _getLocalGitInfo (rootDir: string): Promise<GitInfo | void> {
158157 return {
159158 name,
160159 owner,
161- url
160+ url,
162161 }
163162}
164163
165- async function _getLocalGitRemote ( dir : string ) {
164+ async function _getLocalGitRemote ( dir : string ) {
166165 try {
167166 // https://www.npmjs.com/package/parse-git-config#options
168167 const parseGitConfig = await import ( 'parse-git-config' as string ) . then (
169- m => m . promise || m . default || m
168+ m => m . promise || m . default || m ,
170169 ) as ( opts : { path : string } ) => Promise < Record < string , Record < string , string > > >
171170 const gitDir = await findNearestFile ( '.git/config' , { startingFrom : dir } )
172171 const parsed = await parseGitConfig ( { path : gitDir } )
@@ -175,30 +174,31 @@ async function _getLocalGitRemote (dir: string) {
175174 }
176175 const gitRemote = parsed [ 'remote "origin"' ] . url
177176 return gitRemote
178- } catch ( err ) {
179-
177+ }
178+ catch {
179+ // Ignore error
180180 }
181181}
182182
183- function _getGitEnv ( ) : GitInfo {
183+ function _getGitEnv ( ) : GitInfo {
184184 // https://github.com/unjs/std-env/issues/59
185185 const envInfo = {
186186 // Provider
187- provider : process . env . VERCEL_GIT_PROVIDER || // vercel
188- ( process . env . GITHUB_SERVER_URL ? 'github' : undefined ) || // github
189- '' ,
187+ provider : process . env . VERCEL_GIT_PROVIDER // vercel
188+ || ( process . env . GITHUB_SERVER_URL ? 'github' : undefined ) // github
189+ || '' ,
190190 // Owner
191- owner : process . env . VERCEL_GIT_REPO_OWNER || // vercel
192- process . env . GITHUB_REPOSITORY_OWNER || // github
193- process . env . CI_PROJECT_PATH ?. split ( '/' ) . shift ( ) || // gitlab
194- '' ,
191+ owner : process . env . VERCEL_GIT_REPO_OWNER // vercel
192+ || process . env . GITHUB_REPOSITORY_OWNER // github
193+ || process . env . CI_PROJECT_PATH ?. split ( '/' ) . shift ( ) // gitlab
194+ || '' ,
195195 // Name
196- name : process . env . VERCEL_GIT_REPO_SLUG ||
197- process . env . GITHUB_REPOSITORY ?. split ( '/' ) . pop ( ) || // github
198- process . env . CI_PROJECT_PATH ?. split ( '/' ) . splice ( 1 ) . join ( '/' ) || // gitlab
199- '' ,
196+ name : process . env . VERCEL_GIT_REPO_SLUG
197+ || process . env . GITHUB_REPOSITORY ?. split ( '/' ) . pop ( ) // github
198+ || process . env . CI_PROJECT_PATH ?. split ( '/' ) . splice ( 1 ) . join ( '/' ) // gitlab
199+ || '' ,
200200 // Url
201- url : process . env . REPOSITORY_URL || '' // netlify
201+ url : process . env . REPOSITORY_URL || '' , // netlify
202202 }
203203
204204 if ( ! envInfo . url && envInfo . provider && envInfo . owner && envInfo . name ) {
@@ -211,12 +211,15 @@ function _getGitEnv (): GitInfo {
211211 const { name, owner } = gitUrlParse ( envInfo . url ) as Record < string , string >
212212 envInfo . name = name
213213 envInfo . owner = owner
214- } catch { }
214+ }
215+ catch {
216+ // Ignore error
217+ }
215218 }
216219
217220 return {
218221 name : envInfo . name ,
219222 owner : envInfo . owner ,
220- url : envInfo . url
223+ url : envInfo . url ,
221224 }
222225}
0 commit comments