Skip to content
This repository was archived by the owner on Aug 10, 2020. It is now read-only.

add global flag support for —silent, —json, —auth #13

Merged
merged 6 commits into from
Jul 23, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"dependencies": {
"@iarna/toml": "^2.2.1",
"@oclif/command": "^1.5.8",
"@oclif/parser": "^3.8.3",
"chalk": "^2.4.1",
"ci-info": "^2.0.0",
"cli-ux": "^5.0.0",
Expand Down
89 changes: 82 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const { Command } = require('@oclif/command')
const chalk = require('chalk')
const API = require('netlify')
const merge = require('lodash.merge')
const { format, inspect } = require('util')
const getConfigPath = require('./utils/get-config-path')
const readConfig = require('./utils/read-config')
const globalConfig = require('./global-config')
const StateConfig = require('./state')
const chalkInstance = require('./utils/chalk')
const openBrowser = require('./utils/open-browser')
const findRoot = require('./utils/find-root')
const { track, identify } = require('./utils/telemetry')
const merge = require('lodash.merge')

const argv = require('minimist')(process.argv.slice(2))
const { NETLIFY_AUTH_TOKEN } = process.env

Expand All @@ -24,7 +26,9 @@ class BaseCommand extends Command {
async init(err) {
const projectRoot = findRoot(process.cwd())
// Grab netlify API token
const [ token ] = this.getConfigToken(argv.auth)
const authViaFlag = getAuthArg(argv)

const [ token ] = this.getConfigToken(authViaFlag)
// Get site config from netlify.toml
const configPath = getConfigPath(projectRoot)
// TODO: https://github.com/request/caseless to handle key casing issues
Expand Down Expand Up @@ -65,6 +69,69 @@ class BaseCommand extends Command {
}
}

logJson(message = '', ...args) {
/* Only run json logger when --json flag present */
if (!argv.json) {
return
}
process.stdout.write(JSON.stringify(message, null, 2))
}

log(message = '', ...args) {
/* If --silent or --json flag passed disable logger */
if (argv.silent || argv.json) {
return
}
message = typeof message === 'string' ? message : inspect(message)
process.stdout.write(format(message, ...args) + '\n')
}

/* Modified flag parser to support global --auth, --json, & --silent flags */
parse(opts, argv = this.argv) {
/* Set flags object for commands without flags */
if (!opts.flags) {
opts.flags = {}
}
/* enrich parse with global flags */
const globalFlags = {}
if (!opts.flags.silent) {
globalFlags['silent'] = {
parse: (b, _) => b,
description: 'Silence CLI output',
allowNo: false,
type: 'boolean'
}
}
if (!opts.flags.json) {
globalFlags['json'] = {
parse: (b, _) => b,
description: 'Output return values as JSON',
allowNo: false,
type: 'boolean'
}
}
if (!opts.flags.auth) {
globalFlags['auth'] = {
parse: (b, _) => b,
description: 'Netlify auth token',
input: [],
multiple: false,
type: 'option'
}
}

// enrich with flags here
opts.flags = Object.assign({}, opts.flags, globalFlags)

return require('@oclif/parser').parse(argv, Object.assign({}, {
context: this,
}, opts))
}

get chalk() {
// If --json flag disable chalk colors
return chalkInstance(argv.json)
}
/**
* Get user netlify API token
* @param {string} - [tokenFromFlag] - value passed in by CLI flag
Expand Down Expand Up @@ -150,14 +217,22 @@ class BaseCommand extends Command {

// Log success
this.log()
this.log(`${chalk.greenBright('You are now logged into your Netlify account!')}`)
this.log(`${this.chalk.greenBright('You are now logged into your Netlify account!')}`)
this.log()
this.log(`Run ${chalk.cyanBright('netlify status')} for account details`)
this.log(`Run ${this.chalk.cyanBright('netlify status')} for account details`)
this.log()
this.log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
this.log(`To see all available commands run: ${this.chalk.cyanBright('netlify help')}`)
this.log()
return accessToken
}
}

module.exports = BaseCommand
function getAuthArg(cliArgs) {
// If deploy command. Support shorthand 'a' flag
if (cliArgs && cliArgs._ && cliArgs._[0] === 'deploy') {
return cliArgs.auth || cliArgs.a
}
return cliArgs.auth
}

module.exports = BaseCommand
48 changes: 48 additions & 0 deletions src/utils/chalk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const chalk = require('chalk')

/**
* Chalk instance for CLI
* @param {boolean} noColors - disable chalk colors
* @return {object} - chalk instance or proxy noOp
*/
function safeChalk(noColors) {
/* if no colors return proxy to chalk API */
if (noColors) {
return neverNull(chalk)
}
return chalk
}

function noop() {}

function neverNull(obj) {
function match(some, none = noop) {
return obj != null ? some(obj) : none()
}
return new Proxy((some, none) => {
if (some) {
// has value return it with no chalk wrapper
return some
}
if (!some && !none) return obj
return match(some, none)
}, {
get: (target, key) => {
const obj = target()
if (obj !== null && typeof obj === 'object') {
return neverNull(obj[key])
} else {
return neverNull()
}
},
set: (target, key, val) => {
const obj = target()
if (obj !== null && typeof obj === 'object') {
obj[key] = val
}
return true
}
})
}

module.exports = safeChalk