|
| 1 | +importtype{ConsolaInstance}from'consola' |
| 2 | +importtype{RegistryScript}from'./runtime/types' |
| 3 | + |
| 4 | +constUPPER_RE=/([A-Z])/g |
| 5 | +consttoScreamingSnake=(s: string)=>s.replace(UPPER_RE,'_$1').toUpperCase() |
| 6 | + |
| 7 | +constENV_PREFIX='NUXT_PUBLIC_SCRIPTS_' |
| 8 | + |
| 9 | +functionlevenshtein(a: string,b: string): number{ |
| 10 | +if(a===b) |
| 11 | +return0 |
| 12 | +if(!a.length) |
| 13 | +returnb.length |
| 14 | +if(!b.length) |
| 15 | +returna.length |
| 16 | +constprev: number[]=[] |
| 17 | +for(letj=0;j<=b.length;j++)prev.push(j) |
| 18 | +for(leti=1;i<=a.length;i++){ |
| 19 | +letprevDiag=prev[0]! |
| 20 | +prev[0]=i |
| 21 | +for(letj=1;j<=b.length;j++){ |
| 22 | +consttmp=prev[j]! |
| 23 | +prev[j]=a[i-1]===b[j-1] |
| 24 | + ? prevDiag |
| 25 | + : Math.min(prevDiag,prev[j]!,prev[j-1]!)+1 |
| 26 | +prevDiag=tmp |
| 27 | +} |
| 28 | +} |
| 29 | +returnprev[b.length]! |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Warn for `NUXT_PUBLIC_SCRIPTS_*` env vars that don't map to a valid registry |
| 34 | + * key + field. Nuxt resolves env vars against runtimeConfig before modules run, |
| 35 | + * so a misspelled key (e.g. `NUXT_PUBLIC_SCRIPTS_MICROSOFT_CLARITY_ID` instead |
| 36 | + * of `NUXT_PUBLIC_SCRIPTS_CLARITY_ID`) is silently dropped with no error. |
| 37 | + */ |
| 38 | +exportfunctionvalidateScriptsEnvVars( |
| 39 | +scripts: RegistryScript[], |
| 40 | +enabledRegistryKeys: Set<string>, |
| 41 | +logger: ConsolaInstance, |
| 42 | +): void{ |
| 43 | +// Build a map from screaming-snake registry key to its envDefaults fields |
| 44 | +constvalidByKey=newMap<string,{camel: string,fields: Set<string>}>() |
| 45 | +for(constsofscripts){ |
| 46 | +if(!s.registryKey||!s.envDefaults||!Object.keys(s.envDefaults).length) |
| 47 | +continue |
| 48 | +constscreaming=toScreamingSnake(s.registryKey) |
| 49 | +constfields=newSet(Object.keys(s.envDefaults).map(toScreamingSnake)) |
| 50 | +validByKey.set(screaming,{camel: s.registryKey, fields }) |
| 51 | +} |
| 52 | + |
| 53 | +if(!validByKey.size) |
| 54 | +return |
| 55 | + |
| 56 | +constallValidEnvKeys: string[]=[] |
| 57 | +for(const[screaming,{ fields }]ofvalidByKey){ |
| 58 | +for(constfoffields) |
| 59 | +allValidEnvKeys.push(`${ENV_PREFIX}${screaming}_${f}`) |
| 60 | +} |
| 61 | + |
| 62 | +for(constenvKeyofObject.keys(process.env)){ |
| 63 | +if(!envKey.startsWith(ENV_PREFIX)) |
| 64 | +continue |
| 65 | +if(allValidEnvKeys.includes(envKey)) |
| 66 | +continue |
| 67 | + |
| 68 | +constsegment=envKey.slice(ENV_PREFIX.length) |
| 69 | + |
| 70 | +// Case 1: matches a valid registry key but unknown field |
| 71 | +letmatchedKey: {screaming: string,camel: string,fields: Set<string>}|undefined |
| 72 | +for(const[screaming,info]ofvalidByKey){ |
| 73 | +if(segment===screaming||segment.startsWith(`${screaming}_`)){ |
| 74 | +matchedKey={ screaming, ...info} |
| 75 | +break |
| 76 | +} |
| 77 | +} |
| 78 | + |
| 79 | +if(matchedKey){ |
| 80 | +constfield=segment.slice(matchedKey.screaming.length+1) |
| 81 | +logger.warn( |
| 82 | +`[scripts] env var \`${envKey}\` does not match any option on \`${matchedKey.camel}\`. ` |
| 83 | ++`Valid fields: ${[...matchedKey.fields].map(f=>`\`${ENV_PREFIX}${matchedKey!.screaming}_${f}\``).join(', ')}.${ |
| 84 | +field ? ` Got: \`${field}\`.` : ''}`, |
| 85 | +) |
| 86 | +continue |
| 87 | +} |
| 88 | + |
| 89 | +// Case 2: registry key appears as a substring of the segment (e.g. |
| 90 | +// `MICROSOFT_CLARITY_ID` contains `CLARITY`). Likely a marketing-name |
| 91 | +// prefix; suggest the canonical key, and if the remainder is a valid |
| 92 | +// field, suggest the full corrected env var. |
| 93 | +constsegmentParts=segment.split('_') |
| 94 | +letsubstringMatch: {screaming: string,camel: string,fields: Set<string>,remainder: string}|undefined |
| 95 | +for(const[screaming,info]ofvalidByKey){ |
| 96 | +constkeyParts=screaming.split('_') |
| 97 | +for(leti=0;i<=segmentParts.length-keyParts.length;i++){ |
| 98 | +letok=true |
| 99 | +for(letj=0;j<keyParts.length;j++){ |
| 100 | +if(segmentParts[i+j]!==keyParts[j]){ |
| 101 | +ok=false |
| 102 | +break |
| 103 | +} |
| 104 | +} |
| 105 | +if(ok){ |
| 106 | +substringMatch={ |
| 107 | + screaming, |
| 108 | +camel: info.camel, |
| 109 | +fields: info.fields, |
| 110 | +remainder: segmentParts.slice(i+keyParts.length).join('_'), |
| 111 | +} |
| 112 | +break |
| 113 | +} |
| 114 | +} |
| 115 | +if(substringMatch) |
| 116 | +break |
| 117 | +} |
| 118 | + |
| 119 | +letsuggestion='' |
| 120 | +if(substringMatch){ |
| 121 | +if(substringMatch.remainder&&substringMatch.fields.has(substringMatch.remainder)){ |
| 122 | +suggestion=` Did you mean \`${ENV_PREFIX}${substringMatch.screaming}_${substringMatch.remainder}\` (registry key \`${substringMatch.camel}\`)?` |
| 123 | +} |
| 124 | +else{ |
| 125 | +suggestion=` Did you mean registry key \`${substringMatch.camel}\` (\`${ENV_PREFIX}${substringMatch.screaming}_*\`)?` |
| 126 | +} |
| 127 | +} |
| 128 | +else{ |
| 129 | +// Fallback: closest registry key by edit distance on the leading parts |
| 130 | +letbest: {key: string,camel: string,dist: number}|undefined |
| 131 | +for(const[screaming,info]ofvalidByKey){ |
| 132 | +consthead=segmentParts.slice(0,screaming.split('_').length).join('_') |
| 133 | +constd=levenshtein(head,screaming) |
| 134 | +if(!best||d<best.dist) |
| 135 | +best={key: screaming,camel: info.camel,dist: d} |
| 136 | +} |
| 137 | +if(best&&best.dist<=Math.max(2,Math.floor(best.key.length/2))) |
| 138 | +suggestion=` Did you mean registry key \`${best.camel}\` (\`${ENV_PREFIX}${best.key}_*\`)?` |
| 139 | +} |
| 140 | + |
| 141 | +logger.warn( |
| 142 | +`[scripts] env var \`${envKey}\` does not map to any registered script.${ |
| 143 | +suggestion}`, |
| 144 | +) |
| 145 | +} |
| 146 | + |
| 147 | +// Case 3: env var maps to a known script that the user hasn't enabled in |
| 148 | +// their nuxt.config registry. The value gets resolved into runtimeConfig |
| 149 | +// but won't be consumed unless the script is registered. |
| 150 | +for(const[screaming,info]ofvalidByKey){ |
| 151 | +if(enabledRegistryKeys.has(info.camel)) |
| 152 | +continue |
| 153 | +for(constfieldofinfo.fields){ |
| 154 | +constenvKey=`${ENV_PREFIX}${screaming}_${field}` |
| 155 | +if(process.env[envKey]!==undefined){ |
| 156 | +logger.warn( |
| 157 | +`[scripts] env var \`${envKey}\` is set but \`${info.camel}\` is not registered in \`scripts.registry\`. ` |
| 158 | ++`Add \`registry: { ${info.camel}: {} }\` to your nuxt.config for it to take effect.`, |
| 159 | +) |
| 160 | +} |
| 161 | +} |
| 162 | +} |
| 163 | +} |
0 commit comments