-
Notifications
You must be signed in to change notification settings - Fork 30
perf: replace unneeded dependencies #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,47 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { homedir } from 'node:os' | ||
| import { resolve } from 'node:path' | ||
|
|
||
| import { resolve } from 'pathe' | ||
| import { destr } from 'destr' | ||
| import * as rc from 'rc9' | ||
| import { colors as c } from 'consola/utils' | ||
| import { consola } from 'consola' | ||
| import { loadNuxtConfig } from '@nuxt/kit' | ||
| import { isTest } from 'std-env' | ||
| import { parse as parseDotenv } from 'dotenv' | ||
| import { createMain, defineCommand } from 'citty' | ||
|
|
||
| import { version } from '../package.json' | ||
| import { consentVersion } from './meta' | ||
| import { ensureUserconsent } from './consent' | ||
|
|
||
| function isTruthy(val: unknown): boolean { | ||
| return val === true || val === 'true' || val === '1' || val === 1 | ||
| } | ||
|
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Environment variables are commonly set with varying cases. A user writing Suggested fix function isTruthy(val: unknown): boolean {
- return val === true || val === 'true' || val === '1' || val === 1
+ return val === true || val === 1 || (typeof val === 'string' && (val.toLowerCase() === 'true' || val === '1'))
}π€ Prompt for AI Agents |
||
|
|
||
| function parseDotenv(src: string): Record<string, string> { | ||
| const result: Record<string, string> = {} | ||
| for (const line of src.split('\n')) { | ||
| const trimmed = line.trim() | ||
| if (!trimmed || trimmed.startsWith('#')) continue | ||
| const eqIndex = trimmed.indexOf('=') | ||
| if (eqIndex === -1) continue | ||
| const key = trimmed.slice(0, eqIndex).trim() | ||
| let value = trimmed.slice(eqIndex + 1).trim() | ||
| // Remove surrounding quotes | ||
| if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith('\'') && value.endsWith('\''))) { | ||
| value = value.slice(1, -1) | ||
| } | ||
| result[key] = value | ||
| } | ||
| return result | ||
| } | ||
|
Comment on lines
+20
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Two common
Since this replaces a well-tested library, it's worth covering these edge cases to avoid subtle regressions. Suggested fix function parseDotenv(src: string): Record<string, string> {
const result: Record<string, string> = {}
for (const line of src.split('\n')) {
- const trimmed = line.trim()
+ let trimmed = line.trim()
if (!trimmed || trimmed.startsWith('#')) continue
+ // Strip optional `export` prefix
+ if (trimmed.startsWith('export ')) {
+ trimmed = trimmed.slice(7).trim()
+ }
const eqIndex = trimmed.indexOf('=')
if (eqIndex === -1) continue
const key = trimmed.slice(0, eqIndex).trim()
let value = trimmed.slice(eqIndex + 1).trim()
// Remove surrounding quotes
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith('\'') && value.endsWith('\''))) {
value = value.slice(1, -1)
}
+ else {
+ // Strip inline comments for unquoted values
+ const hashIndex = value.indexOf(' #')
+ if (hashIndex !== -1) {
+ value = value.slice(0, hashIndex).trim()
+ }
+ }
result[key] = value
}
return result
}π€ Prompt for AI Agents |
||
|
|
||
| const RC_FILENAME = '.nuxtrc' | ||
|
|
||
| const sharedArgs = { | ||
| global: { | ||
| type: 'boolean', | ||
| alias: 'g', | ||
| default: false, | ||
| description: 'Apply globally', | ||
| }, | ||
| dir: { | ||
|
|
@@ -103,14 +124,14 @@ async function _checkDisabled(dir: string): Promise<string | false | undefined> | |
| return 'because you are running in a test environment' | ||
| } | ||
|
|
||
| if (destr(process.env.NUXT_TELEMETRY_DISABLED)) { | ||
| if (isTruthy(process.env.NUXT_TELEMETRY_DISABLED)) { | ||
| return 'by the `NUXT_TELEMETRY_DISABLED` environment variable' | ||
| } | ||
|
|
||
| const dotenvFile = resolve(dir, '.env') | ||
| if (existsSync(dotenvFile)) { | ||
| const _env = parseDotenv(readFileSync(dotenvFile)) | ||
| if (destr(_env.NUXT_TELEMETRY_DISABLED)) { | ||
| const _env = parseDotenv(readFileSync(dotenvFile, 'utf8')) | ||
| if (isTruthy(_env.NUXT_TELEMETRY_DISABLED)) { | ||
| return 'by the `NUXT_TELEMETRY_DISABLED` environment variable set in ' + dotenvFile | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.