Skip to content

Commit

Permalink
feat(cli): load .env files before running any command (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Feb 23, 2021
1 parent 1f11acf commit 1d6c010
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
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}`)})

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

2 comments on commit 1d6c010

@vercel
Copy link

@vercel vercel bot commented on 1d6c010 Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio-git-next.sanity.build
perf-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 1d6c010 Feb 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.