Skip to content
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"dependencies": {
"color": "^0.11.3",
"execa": "1.0.0",
"git-state": "^4.0.0",
"json-loader": "^0.5.4",
"left-pad": "^1.1.3",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import battery from './battery'
// Import Time from './time'
// Import Docker from './docker'
import spotify from './spotify'
import now from './now'

export default [hostname, ip, memory, battery, cpu, network, spotify]
export default [hostname, ip, memory, battery, cpu, network, spotify, now]
106 changes: 106 additions & 0 deletions src/lib/plugins/now.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react'
import Component from 'hyper/component'
import execa from 'execa'

class Now extends Component {
static displayName() {
return "now"
}

constructor(props) {
super(props)

this.interval = null
this.fetchTeam = this.fetchTeam.bind(this)

this.state = {
team: null,
fetching: false,
}
}

async osUser() {
const {stdout: user} = await execa('whoami')
return user
}

async readNowToken() {

const user = await this.osUser()

const path = `/Users/${user}/.now/auth.json`
const {stdout} = await execa('cat', [path])

const config = JSON.parse(stdout)

return config.token
}

async readCurrentTeam() {
const user = await this.osUser()

const path = `/Users/${user}/.now/config.json`
const {stdout} = await execa('cat', [path])

const config = JSON.parse(stdout)

return config.currentTeam
}

async fetchTeams(token) {
const res = await fetch('https://api.zeit.co/teams', { method: "GET", headers: { Authorization: `bearer ${token}`}})
const json = await res.json()
return json.teams
}

async fetchUser(token) {
const res = await fetch('https://api.zeit.co/www/user', { method: "GET", headers: { Authorization: `bearer ${token}`}})
const json = await res.json()

return json.user.username
}

async fetchTeam() {
if (this.state.fetching) {
return
}

try {

await new Promise(resolve => this.setState(() => ({ fetching: true }), resolve))

const token = await this.readNowToken()
const teams = await this.fetchTeams(token)
const teamId = await this.readCurrentTeam()

const team = teams.find(team => team.id === teamId) || null;

let update = {fetching: false}
if (team) {
update.team = team.name
} else {
update.team = await this.fetchUser(token)
}

this.setState(() => update)
} catch(err) {
console.error('Hyperline::now plugin', err)
this.setState(() => ({ fetching: false }))
}
}

componentDidMount() {
this.fetchTeam()
this.interval = setInterval(this.fetchTeam.bind(this), 60000)
}

componentWillUnmount() {
clearInterval(this.interval)
}

render() {
return this.state.team ? `▲ ${this.state.team}` : '▲'
}
}

export default Now
Loading