Browse Source

Now asking for value of missing environment variables (#645)

* Ask for missing environment variables

* Prettified

* Use `info` helper

* Only apply fields if `now` is available

* Don't log

* Isolate our Inquirer patch

* Patch Inquirer before asking for the env fields

* Don't ask for env vars that are already defined
master
Leo Lamprecht 8 years ago
committed by GitHub
parent
commit
ac2a75fe41
  1. 45
      bin/now-deploy.js
  2. 15
      lib/utils/input/list.js
  3. 20
      lib/utils/input/patch-inquirer.js

45
bin/now-deploy.js

@ -14,6 +14,7 @@ const flatten = require('arr-flatten')
const dotenv = require('dotenv')
const { eraseLines } = require('ansi-escapes')
const { write: copy } = require('clipboardy')
const inquirer = require('inquirer')
// Ours
const login = require('../lib/login')
@ -221,6 +222,37 @@ const stopDeployment = msg => {
process.exit(1)
}
const envFields = async list => {
const questions = []
for (const field of list) {
questions.push({
name: field,
message: field
})
}
// eslint-disable-next-line import/no-unassigned-import
require('../lib/utils/input/patch-inquirer')
info('Please enter the values for the following environment variables:')
const answers = await inquirer.prompt(questions)
for (const answer in answers) {
if (!{}.hasOwnProperty.call(answers, answer)) {
continue
}
const content = answers[answer]
if (content === '') {
stopDeployment(`Enter a value for ${answer}`)
}
}
return answers
}
// Create a new deployment if user changed the name or made `_src` public.
// This works fine because it doesn't force a new sync,
// it just forces a new deployment.
@ -415,12 +447,21 @@ async function sync({ token, config: { currentTeam, user } }) {
dotenvConfig = dotenv.parse(dotenvFile)
}
// Merge `now.env` from package.json with `-e` arguments.
const pkgEnv = nowConfig && nowConfig.env
const argEnv = [].concat(argv.env || [])
if (pkgEnv && Array.isArray(nowConfig.env)) {
const defined = argEnv.join()
const askFor = nowConfig.env.filter(item => !defined.includes(`${item}=`))
nowConfig.env = await envFields(askFor)
}
// Merge `now.env` from package.json with `-e` arguments
const envs = [
...Object.keys(dotenvConfig || {}).map(k => `${k}=${dotenvConfig[k]}`),
...Object.keys(pkgEnv || {}).map(k => `${k}=${pkgEnv[k]}`),
...[].concat(argv.env || [])
...argEnv
]
let secrets

15
lib/utils/input/list.js

@ -1,19 +1,8 @@
const chalk = require('chalk')
const inquirer = require('inquirer')
const stripAnsi = require('strip-ansi')
/* eslint-disable no-multiple-empty-lines, no-var, no-undef, no-eq-null, eqeqeq, semi */
inquirer.prompt.prompts.list.prototype.getQuestion = function() {
var message = chalk.bold('> ' + this.opt.message) + ' '
// Append the default if available, and if question isn't answered
if (this.opt.default != null && this.status !== 'answered') {
message += chalk.dim('(' + this.opt.default + ') ')
}
return message
}
/* eslint-enable */
// eslint-disable-next-line import/no-unassigned-import
require('./patch-inquirer')
function getLength(string) {
let biggestLength = 0

20
lib/utils/input/patch-inquirer.js

@ -0,0 +1,20 @@
const inquirer = require('inquirer')
const chalk = require('chalk')
// Here we patch inquirer to use a `>` instead of the ugly green `?`
/* eslint-disable no-multiple-empty-lines, no-var, no-undef, no-eq-null, eqeqeq, semi */
const getQuestion = function() {
var message = chalk.bold('> ' + this.opt.message) + ' '
// Append the default if available, and if question isn't answered
if (this.opt.default != null && this.status !== 'answered') {
message += chalk.dim('(' + this.opt.default + ') ')
}
return message
}
/* eslint-enable */
inquirer.prompt.prompts.input.prototype.getQuestion = getQuestion
inquirer.prompt.prompts.list.prototype.getQuestion = getQuestion
Loading…
Cancel
Save