1
1
import { existsSync } from 'node:fs'
2
- import path from 'path'
2
+ import path from 'node: path'
3
3
import { defu } from 'defu'
4
4
import { addPrerenderRoutes , installModule , defineNuxtModule , addPlugin , extendViteConfig , createResolver , logger , addComponentsDir , addServerHandler , resolveAlias , addVitePlugin } from '@nuxt/kit'
5
5
import { findNearestFile } from 'pkg-types'
6
- // @ts -ignore
6
+ // @ts -expect-error import does exist
7
7
import gitUrlParse from 'git-url-parse'
8
8
import { version } from '../package.json'
9
9
@@ -13,7 +13,7 @@ export interface ModuleOptions {
13
13
/**
14
14
* Enable Studio mode
15
15
* @default : 'production'
16
- ** /
16
+ */
17
17
enabled : 'production' | true
18
18
}
19
19
@@ -22,17 +22,17 @@ export interface ModuleHooks {}
22
22
export default defineNuxtModule < ModuleOptions > ( {
23
23
meta : {
24
24
name : 'studio' ,
25
- configKey : 'studio'
25
+ configKey : 'studio' ,
26
26
} ,
27
27
defaults : {
28
- enabled : 'production'
28
+ enabled : 'production' ,
29
29
} ,
30
- async setup ( options , nuxt ) {
31
- // @ts -ignore
30
+ async setup ( options , nuxt ) {
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
32
nuxt . hook ( 'schema:resolved' , ( schema : any ) => {
33
33
nuxt . options . runtimeConfig . appConfigSchema = {
34
34
properties : schema . properties ?. appConfig ,
35
- default : schema . default ?. appConfig
35
+ default : schema . default ?. appConfig ,
36
36
}
37
37
nuxt . options . runtimeConfig . contentSchema = schema . properties ?. content || { }
38
38
} )
@@ -53,25 +53,24 @@ export default defineNuxtModule<ModuleOptions>({
53
53
const contentModule = '@nuxt/content'
54
54
// Check Content module is installed
55
55
if (
56
- ! nuxt . options . runtimeConfig . content &&
57
- ! nuxt . options . modules . includes ( contentModule )
56
+ ! nuxt . options . runtimeConfig . content
57
+ && ! nuxt . options . modules . includes ( contentModule )
58
58
) {
59
59
log . warn ( 'Could not find `@nuxt/content` module. Please install it to enable preview mode.' )
60
60
return
61
61
}
62
62
// Check Content module version
63
63
const contentModuleVersion = await import ( contentModule )
64
64
. then ( m => m . default || m )
65
- . then ( ( m : any ) => m . getMeta ( ) )
65
+ . then ( m => m . getMeta ( ) )
66
66
. then ( m => m . version )
67
67
. catch ( ( ) => '0' )
68
68
if ( contentModuleVersion < '2.1.1' ) {
69
69
log . warn ( 'Please update `@nuxt/content` to version 2.1.1 or higher to enable preview mode.' )
70
70
return
71
71
}
72
72
73
- // Check Pinceau module activated
74
- // @ts -ignore
73
+ // @ts -expect-error Check Pinceau module activated
75
74
nuxt . hook ( 'pinceau:options' , ( options ) => {
76
75
options . studio = true
77
76
} )
@@ -82,34 +81,34 @@ export default defineNuxtModule<ModuleOptions>({
82
81
const publicToken = process . env . NUXT_PUBLIC_STUDIO_TOKENS
83
82
const iframeMessagingAllowedOrigins = process . env . IFRAME_MESSAGING_ALLOWED_ORIGINS
84
83
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 , {
86
85
version,
87
86
publicToken,
88
- gitInfo
87
+ gitInfo,
89
88
} )
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 } )
91
90
92
91
extendViteConfig ( ( config ) => {
93
92
config . optimizeDeps = config . optimizeDeps || { }
94
93
config . optimizeDeps . include = config . optimizeDeps . include || [ ]
95
94
config . optimizeDeps . include . push (
96
- 'socket.io-client' , 'slugify'
95
+ 'socket.io-client' , 'slugify' ,
97
96
)
98
97
} )
99
98
100
99
if ( contentModuleVersion === '2.10.0' ) {
101
100
addVitePlugin ( {
102
101
name : 'content-resolver' ,
103
102
enforce : 'pre' ,
104
- resolveId ( id , importer ) {
103
+ resolveId ( id , importer ) {
105
104
if ( id . endsWith ( '.mjs' ) && ( ( importer || '' ) . includes ( '@nuxt/content/dist' ) || id . includes ( '@nuxt/content/dist' ) ) ) {
106
105
id = id
107
106
. replace ( '.mjs' , '.js' )
108
107
. replace ( / ^ \/ n o d e _ m o d u l e s / , './node_modules/' )
109
108
110
109
return path . resolve ( path . dirname ( importer || __dirname ) , id . replace ( '.mjs' , '.js' ) )
111
110
}
112
- }
111
+ } ,
113
112
} )
114
113
}
115
114
@@ -123,29 +122,29 @@ export default defineNuxtModule<ModuleOptions>({
123
122
addServerHandler ( {
124
123
method : 'get' ,
125
124
route : '/__studio.json' ,
126
- handler : resolve ( './runtime/server/routes/studio' )
125
+ handler : resolve ( './runtime/server/routes/studio' ) ,
127
126
} )
128
127
addPrerenderRoutes ( '/__studio.json' )
129
128
130
129
// Install dependencies
131
130
await installModule ( 'nuxt-component-meta' , {
132
- globalsOnly : true
131
+ globalsOnly : true ,
133
132
} )
134
- }
133
+ } ,
135
134
} )
136
135
137
136
// --- Utilities to get git info ---
138
137
139
138
interface GitInfo {
140
139
// Repository name
141
- name : string ,
140
+ name : string
142
141
// Repository owner/organization
143
- owner : string ,
142
+ owner : string
144
143
// Repository URL
145
- url : string ,
144
+ url : string
146
145
}
147
146
148
- async function _getLocalGitInfo ( rootDir : string ) : Promise < GitInfo | void > {
147
+ async function _getLocalGitInfo ( rootDir : string ) : Promise < GitInfo | undefined > {
149
148
const remote = await _getLocalGitRemote ( rootDir )
150
149
if ( ! remote ) {
151
150
return
@@ -158,15 +157,15 @@ async function _getLocalGitInfo (rootDir: string): Promise<GitInfo | void> {
158
157
return {
159
158
name,
160
159
owner,
161
- url
160
+ url,
162
161
}
163
162
}
164
163
165
- async function _getLocalGitRemote ( dir : string ) {
164
+ async function _getLocalGitRemote ( dir : string ) {
166
165
try {
167
166
// https://www.npmjs.com/package/parse-git-config#options
168
167
const parseGitConfig = await import ( 'parse-git-config' as string ) . then (
169
- m => m . promise || m . default || m
168
+ m => m . promise || m . default || m ,
170
169
) as ( opts : { path : string } ) => Promise < Record < string , Record < string , string > > >
171
170
const gitDir = await findNearestFile ( '.git/config' , { startingFrom : dir } )
172
171
const parsed = await parseGitConfig ( { path : gitDir } )
@@ -175,30 +174,31 @@ async function _getLocalGitRemote (dir: string) {
175
174
}
176
175
const gitRemote = parsed [ 'remote "origin"' ] . url
177
176
return gitRemote
178
- } catch ( err ) {
179
-
177
+ }
178
+ catch {
179
+ // Ignore error
180
180
}
181
181
}
182
182
183
- function _getGitEnv ( ) : GitInfo {
183
+ function _getGitEnv ( ) : GitInfo {
184
184
// https://github.com/unjs/std-env/issues/59
185
185
const envInfo = {
186
186
// 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
+ || '' ,
190
190
// 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
+ || '' ,
195
195
// 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
+ || '' ,
200
200
// Url
201
- url : process . env . REPOSITORY_URL || '' // netlify
201
+ url : process . env . REPOSITORY_URL || '' , // netlify
202
202
}
203
203
204
204
if ( ! envInfo . url && envInfo . provider && envInfo . owner && envInfo . name ) {
@@ -211,12 +211,15 @@ function _getGitEnv (): GitInfo {
211
211
const { name, owner } = gitUrlParse ( envInfo . url ) as Record < string , string >
212
212
envInfo . name = name
213
213
envInfo . owner = owner
214
- } catch { }
214
+ }
215
+ catch {
216
+ // Ignore error
217
+ }
215
218
}
216
219
217
220
return {
218
221
name : envInfo . name ,
219
222
owner : envInfo . owner ,
220
- url : envInfo . url
223
+ url : envInfo . url ,
221
224
}
222
225
}
0 commit comments