Repository navigation
chore: change env filename used by generate-i18n and improve error message - #3222
Conversation
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant CLI as CLI (generate-i18n)
participant FS as FileSystem
participant Dotenv as Dotenv.loader
participant Validator as Credentials Validator
participant SDK as FastStoreSDK
CLI->>FS: check for vtex.env (existsSync)
alt vtex.env exists
CLI->>Dotenv: dotenv.config(vtex.env)
Dotenv-->>CLI: populate process.env
end
CLI->>Validator: read VTEX_ACCOUNT, FS_DISCOVERY_APP_KEY, FS_DISCOVERY_APP_TOKEN
Validator-->>CLI: valid? / error
alt valid
CLI->>SDK: initialize with validated creds
SDK-->>CLI: ready
else invalid
CLI-->>CLI: log error + exit
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/cli/src/commands/generate-i18n.ts`:
- Around line 84-103: The command currently logs and returns when VTEX
credentials are missing (vars VTEX_ACCOUNT, FS_DISCOVERY_APP_KEY,
FS_DISCOVERY_APP_TOKEN) which may exit with code 0 and doesn’t narrow types for
SDK init; replace the boolean hasCredentials check with a direct guard that
reads the three env vars into consts (VTEX_ACCOUNT, FS_DISCOVERY_APP_KEY,
FS_DISCOVERY_APP_TOKEN), log the existing message via logger.error (using
vtexEnvPath/dotenv.config as needed), then immediately call process.exit(1) to
return a non-zero status so the process fails loudly and the three consts can be
used as definitely-assigned (non-null) values when initializing the SDK.
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/cli/src/commands/generate-i18n.ts (1)
89-93: Use a direct credential guard to preserve TypeScript narrowing.Line 91-94 still uses
hasCredentials; this pattern doesn’t narrowVTEX_ACCOUNT,FS_DISCOVERY_APP_KEY, andFS_DISCOVERY_APP_TOKENas strongly as a directif (!A || !B || !C)guard.Suggested minimal refactor (no behavior change)
const { VTEX_ACCOUNT, FS_DISCOVERY_APP_KEY, FS_DISCOVERY_APP_TOKEN } = process.env - const hasCredentials = - VTEX_ACCOUNT && FS_DISCOVERY_APP_KEY && FS_DISCOVERY_APP_TOKEN - - if (!hasCredentials) { + if (!VTEX_ACCOUNT || !FS_DISCOVERY_APP_KEY || !FS_DISCOVERY_APP_TOKEN) { logger.error(`${chalk.red('[Error]')} - Missing VTEX credentials.\n ${chalk.cyan('Required Action:')}\n Check your FastStore WebOps Settings page - to work in production, it should contain the following variables: ${chalk.cyan('VTEX_ACCOUNT')}, ${chalk.cyan('FS_DISCOVERY_APP_KEY')}, ${chalk.cyan('FS_DISCOVERY_APP_TOKEN')}.\n If running locally, please check your ${chalk.bold('vtex.env')} file, it should also contain those variables. `) process.exit(1) }In TypeScript control-flow analysis, does `const ok = a && b && c; if (!ok) return;` narrow `a`, `b`, and `c` to non-undefined for subsequent lines?As per coding guidelines, "Ensure type safety and avoid type assertions when possible."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/cli/src/commands/generate-i18n.ts` around lines 89 - 93, Replace the intermediate boolean `hasCredentials` guard with a direct conditional check so TypeScript properly narrows `VTEX_ACCOUNT`, `FS_DISCOVERY_APP_KEY`, and `FS_DISCOVERY_APP_TOKEN`; specifically, after extracting those env vars (the consts named `VTEX_ACCOUNT`, `FS_DISCOVERY_APP_KEY`, `FS_DISCOVERY_APP_TOKEN`) change the flow to `if (!VTEX_ACCOUNT || !FS_DISCOVERY_APP_KEY || !FS_DISCOVERY_APP_TOKEN) return` (or the equivalent early-return guard) and remove `hasCredentials` so subsequent code can rely on narrowed, non-undefined types.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@packages/cli/src/commands/generate-i18n.ts`:
- Around line 89-93: Replace the intermediate boolean `hasCredentials` guard
with a direct conditional check so TypeScript properly narrows `VTEX_ACCOUNT`,
`FS_DISCOVERY_APP_KEY`, and `FS_DISCOVERY_APP_TOKEN`; specifically, after
extracting those env vars (the consts named `VTEX_ACCOUNT`,
`FS_DISCOVERY_APP_KEY`, `FS_DISCOVERY_APP_TOKEN`) change the flow to `if
(!VTEX_ACCOUNT || !FS_DISCOVERY_APP_KEY || !FS_DISCOVERY_APP_TOKEN) return` (or
the equivalent early-return guard) and remove `hasCredentials` so subsequent
code can rely on narrowed, non-undefined types.
What's the purpose of this pull request?
The
generate-i18nCLI command was loading credentials from.envinstead ofvtex.env, which is the FastStore-specific environment file. Additionally, the error message shown when credentials are missing was too vague, not telling developers where to look or what to do next.How to test it?
Running
node packages/cli/bin/run generate-i18n ./packages/core, test with and without the variables onvtex.env.Or through a test account, running the build command:
Starters Deploy Preview
PR: https://github.com/vtex-sites/brandless.store/pull/148

Even without the
vtex.envit worked on production since those variables were set in Webops Settings:References
Summary by CodeRabbit