Skip to content
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

feat(cli): load .env files before running any command #2320

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"debug": "^3.1.0",
"decompress": "^4.2.0",
"deep-sort-object": "^1.0.1",
"dotenv": "^8.2.0",
"es6-promisify": "^6.0.0",
"eventsource": "^1.0.6",
"execa": "^1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/@sanity/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// eslint-disable-next-line import/no-unassigned-import
import path from 'path'
import chalk from 'chalk'
import dotenv from 'dotenv'
import fse from 'fs-extra'
import neatStack from 'neat-stack'
import resolveFrom from 'resolve-from'
Expand All @@ -24,6 +25,11 @@ module.exports = async function runCli(cliRoot) {
const cwd = checkCwdPresence()
const workDir = isInit ? process.cwd() : resolveRootDir(cwd)

// Try to load .env files from the sanity studio directory
// eslint-disable-next-line no-process-env
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV || 'development'
dotenv.config({path: path.join(workDir, `.env.${env}`)})
bjoerge marked this conversation as resolved.
Show resolved Hide resolved

await updateNotifier({pkg, cwd, workDir}).notify()

const options = {
Expand Down
14 changes: 8 additions & 6 deletions packages/@sanity/cli/src/util/clientWrapper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import client from '@sanity/client'
import generateHelpUrl from '@sanity/generate-help-url'
import getUserConfig from './getUserConfig'
import client from '@sanity/client'

/* eslint-disable no-process-env */
const envAuthToken = process.env.SANITY_AUTH_TOKEN
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production'
/* eslint-enable no-process-env */

const apiHosts = {
staging: 'https://api.sanity.work',
Expand Down Expand Up @@ -37,6 +32,13 @@ export default function clientWrapper(manifest, configPath) {
requester.use(authErrors())

return function (opts = {}) {
// Read these environment variables "late" to allow `.env` files

/* eslint-disable no-process-env */
const envAuthToken = process.env.SANITY_AUTH_TOKEN
const sanityEnv = process.env.SANITY_INTERNAL_ENV || 'production'
/* eslint-enable no-process-env */

const {requireUser, requireProject, api} = {...defaults, ...opts}
const userConfig = getUserConfig()
const token = envAuthToken || userConfig.get('authToken')
Expand Down