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

Update global config #96

Merged
merged 22 commits into from
Sep 7, 2018
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"archiver": "^3.0.0",
"ascii-table": "0.0.9",
"chalk": "^2.4.1",
"ci-info": "^1.4.0",
"clean-deep": "^3.0.2",
"cli-spinners": "^1.3.1",
"cli-ux": "^4.7.3",
Expand All @@ -67,7 +68,6 @@
"flush-write-stream": "^1.0.3",
"folder-walker": "^3.1.0",
"from2-array": "0.0.4",
"ghauth": "^3.2.1",
"git-remote-origin-url": "^2.0.0",
"git-repo-info": "^2.0.0",
"hasha": "^3.0.0",
Expand Down Expand Up @@ -98,6 +98,7 @@
"update-notifier": "^2.5.0",
"util.promisify": "^1.0.0",
"util.promisify-all": "^1.0.2",
"uuid": "^3.3.2",
"write-file-atomic": "^2.3.0"
},
"devDependencies": {
Expand Down
6 changes: 0 additions & 6 deletions site/src/_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import {
import { InstantSearch, Hits, SearchBox, createConnector, Configure, Highlight } from 'react-instantsearch-dom'
import { borderColor, themeGet } from 'styled-system'

injectGlobal`
.ais-InstantSearch__root {
width: 100%;
}
`;

const breakpoint = `@media screen and (min-width: 48em)`
const repoUrl = 'https://github.com/netlify/cli'

Expand Down
11 changes: 7 additions & 4 deletions src/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ class OpenCommand extends Command {
if (isEmptyCommand(flags, args)) {
showHelp(this.id)
}
// Default open netlify admin
// Default open Netlify admin
await OpenAdminCommand.run()
}
}

OpenCommand.description = `${renderShortDesc('Opens current project urls in browser')}`

OpenCommand.examples = ['$ netlify open:admin', '$ netlify open:site']
OpenCommand.examples = [
'netlify open:admin',
'netlify open:site'
]

OpenCommand.hidden = true

Expand All @@ -42,7 +45,7 @@ Import the the base class and extend it, the same way you do with `@oclif/comman
Commands that extend this base class get access to the [same api](https://oclif.io/docs/commands.html) as `@oclif/command` plus a few extra properties:


### `this.global`
### `this.netlify.globalConfig`

Provides access to configuration stored in the users home folder under `~/.netlify`.
See [global-config](global-config/README.md).
Expand All @@ -52,7 +55,7 @@ See [global-config](global-config/README.md).
Provides access to site-level config relative to the `process.cwd`.
See [site-config](global-config/README.md)

### `this.netlify`
### `this.netlify.api`

An instance of the [`netlify`](../utils/api/README.md) api client.

Expand Down
2 changes: 1 addition & 1 deletion src/base/global-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```js
const global = require('../util/global-config')
global.get('accessToken')
global.get('userId')
global.get('foo', 'bar')
```

Expand Down
33 changes: 10 additions & 23 deletions src/base/global-config/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
const Configstore = require('configstore')
const os = require('os')
const path = require('path')
const { toEnvCase, isDotProp } = require('./util')
const uuidv4 = require('uuid/v4')

const conf = new Configstore(
null, // configPath overrides the namespace
{
// defaults
},
{ configPath: path.join(os.homedir(), '.netlify', 'config.json') }
)

const envProxy = {}

envProxy.get = (cs, prop) => {
if (prop === 'get') {
return key => {
if (isDotProp(key)) return cs.get(key)

return process.env[toEnvCase(key)] || cs.get(key)
}
}

return cs[prop]
const globalConfigDefaults = {
/* disable stats from being sent to Netlify */
telemetryDisabled: false,
/* cliId */
cliId: uuidv4()
}

const configStore = new Proxy(conf, envProxy)
const globalConfigOptions = {
configPath: path.join(os.homedir(), '.netlify', 'config.json')
}

module.exports = configStore
module.exports = new Configstore(null, globalConfigDefaults, globalConfigOptions)
108 changes: 94 additions & 14 deletions src/base/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,112 @@
const { Command } = require('@oclif/command')
const chalk = require('chalk')
const globalConfig = require('./global-config')
const SiteConfig = require('./site-config')
const siteConfig = require('./site-config')
const State = require('./state')
const openBrowser = require('../utils/open-browser')
const projectRoot = require('./utils/projectRoot')
const { track, identify } = require('../utils/telemetry')
const API = require('../utils/api')

// Netlify CLI client id
// Lives in bot@netlify.com
// Netlify CLI client id. Lives in bot@netlify.com
// Todo setup client for multiple environments
const CLIENT_ID = 'd6f37de6614df7ae58664cfca524744d73807a377f5ee71f1a254f78412e3750'

class BaseCommand extends Command {
constructor(...args) {
super(...args)
this.global = globalConfig
this.site = new SiteConfig(process.cwd())
this.netlify = new API(globalConfig.get('accessToken'))
}

// Get site id & build state
const state = new State(projectRoot)

// Pull in siteConfig from toml
const siteConf = siteConfig(projectRoot, state)

// Get current logged in user
const currentUser = globalConfig.get('userId')

// Grab netlify API token
const token = globalConfig.get(`users.${currentUser}.auth.token`)

this.netlify = {
// api methods
api: new API(token),
// current site context
site: siteConf,
// global cli config
globalConfig: globalConfig,
// state of current site dir
state: state,
// Current user
// user: {} // TODO @DWELLS
}
}
async getAuthToken() {
const currentUser = this.netlify.globalConfig.get('userId')
// TODO what should NETLIFY_AUTH_TOKEN be named
if (process.env.NETLIFY_AUTH_TOKEN) {
console.log('Using token set by env var')
return process.env.NETLIFY_AUTH_TOKEN
}
return this.netlify.globalConfig.get(`users.${currentUser}.auth.token`)
}
async authenticate() {
if (this.global.get('accessToken')) {
return
const token = await this.getAuthToken()
if (token) {
return token
}

this.log(`Logging into your Netlify account...`)
const client = this.netlify
const ticket = await client.createTicket({ clientId: CLIENT_ID })

// Create ticket for auth
const ticket = await this.netlify.api.createTicket({
clientId: CLIENT_ID
})

// Open browser for authentication
await openBrowser(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
const accessToken = await client.getAccessToken(ticket)
this.global.set('accessToken', accessToken)
this.log('Logged in...')

const accessToken = await this.netlify.api.getAccessToken(ticket)

if (accessToken) {
const accounts = await this.netlify.api.listAccountsForUser()
const accountInfo = accounts.find(account => account.type === 'PERSONAL')
const userID = accountInfo.owner_ids[0]

const userData = {
id: userID,
name: accountInfo.name || accountInfo.billing_name,
email: accountInfo.billing_email,
slug: accountInfo.slug,
auth: {
token: accessToken,
github: {
user: null,
token: null
}
}
}
// Set current userId
this.netlify.globalConfig.set('userId', userID)
// Set user data
this.netlify.globalConfig.set(`users.${userID}`, userData)

identify({
name: accountInfo.name || accountInfo.billing_name,
email: accountInfo.billing_email,
}).then(() => {
track('cli:user_login')
})

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

Expand Down
Loading