Skip to content

Commit

Permalink
feat(hosting): support for BrowserRouter in hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucharz committed Dec 27, 2017
1 parent 258e41b commit 297eab2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/cli-hosting.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const setup = async () => {
.description('Configure hosting parameters')
.option('-c, --cname <domain_name>', 'add CNAME to hosting')
.option('-d, --remove-cname <domain_name>', 'remove CNAME from hosting')
.option('-b, --browser_router', 'turn on BrowserRouter support')
.action(async (...options) => {
session.isAuthenticated()
session.hasProject()
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/commands/hosting-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class HostingAdd {
} else {
const params = {
name: this.hostingName,
browser_router: responses.browser_router,
src: path.relative(this.session.projectPath, path.join(process.cwd(), this.folder)),
cname: this.cname || responses.CNAME || null
}
Expand Down Expand Up @@ -100,6 +101,13 @@ class HostingAdd {
message: p(2)('Set CNAME now (your own domain) or leave it empty')
})
}
if (!this.browser_router) {
questions.push({
type: 'confirm',
name: 'browser_router',
message: p(2)('Do you want to use BrowserRouter for this hosting?')
})
}

return questions
}
Expand Down
51 changes: 46 additions & 5 deletions packages/cli/src/commands/hosting-config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
import format from 'chalk'
import { echo, error, warning } from '../utils/print-tools'
import inquirer from 'inquirer'
import { p, echo, error, warning } from '../utils/print-tools'
import Hosting from '../utils/hosting'

class HostingConfig {
constructor (context) {
this.context = context
this.hosting = null
}

async run ([hostingName, cmd]) {
this.cname = cmd.cname
this.fullPath = null

try {
const hosting = await Hosting.get(hostingName)
if (cmd.removeCname && !hosting.hasCNAME(cmd.removeCname)) {
this.hosting = await Hosting.get(hostingName)

if (!this.hosting.existLocally) {
warning(4)('No such hosting!')
echo()
process.exit(1)
}
if (cmd.removeCname && !this.hosting.hasCNAME(cmd.removeCname)) {
warning(4)('This hosting doesn\'t have such CNAME!')
echo()
process.exit(0)
process.exit(1)
}

let responses = {}
if (!(cmd.removeCname || cmd.cname || cmd.browser_router)) {
responses = await inquirer.prompt(this.getQuestions()) || {}
}

const paramsToUpdate = {
cname: this.cname || responses.CNAME,
removeCNAME: cmd.removeCname,
browser_router: cmd.browser_router || responses.browser_router
}

await hosting.configure({ cname: this.cname, removeCNAME: cmd.removeCname })
await this.hosting.configure(paramsToUpdate)
echo(4)(format.green('Configuration successfully updated!'))
echo()
} catch (err) {
Expand All @@ -31,6 +50,28 @@ class HostingConfig {
echo()
}
}

getQuestions () {
const questions = []

if (!this.cname) {
questions.push({
name: 'CNAME',
message: p(2)('Set CNAME now (your own domain) or leave it empty'),
default: this.hosting.getCNAME()
})
}
if (!this.browser_router) {
questions.push({
type: 'confirm',
name: 'browser_router',
message: p(2)('Do you want to use BrowserRouter for this hosting?'),
default: this.hosting.config.browser_router
})
}

return questions
}
}

export default HostingConfig
13 changes: 8 additions & 5 deletions packages/cli/src/commands/hosting-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import format from 'chalk'

import { echo } from '../utils/print-tools'
import { echo, echon } from '../utils/print-tools'
import Hosting from '../utils/hosting'

class HostingListCmd {
Expand Down Expand Up @@ -35,18 +35,21 @@ class HostingListCmd {

static printHosting (hosting) {
const cname = typeof hosting.getCnameURL === 'function' && hosting.getCnameURL()
echo(4)(`${format.dim('name')}: ${format.cyan(hosting.name)}`)
echo(11)(`${format.dim('name')}: ${format.cyan(hosting.name)}`)

if (hosting.existRemotely) {
echo(4)(`${format.dim('url')}: ${format.cyan(hosting.getURL())}`)
echo(12)(`${format.dim('URL')}: ${format.cyan(hosting.getURL())}`)
}

if (hosting.getCnameURL()) {
echo(4)(`${format.dim('cname')}: ${format.cyan(cname)}`)
echo(10)(`${format.dim('CNAME')}: ${format.cyan(cname)}`)
}

echon(2)(`${format.dim('BrowserRouter')}:`)
echo(` ${format.cyan(hosting.config.browser_router ? format.green('✓') : format.red('x'))}`)

if (!hosting.existRemotely) {
echo(4)(`${format.dim('status')}: ${format.red('x')}`)
echo(9)(`${format.dim('status')}: ${format.magenta('not synced')}`)
} else {
echo(4)(`${format.dim('sync status')}: ${hosting.isUpToDate ? `${format.green('✓')}` : `${format.red('x')}`}`)
}
Expand Down
38 changes: 28 additions & 10 deletions packages/cli/src/utils/hosting/hosting.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ class Hosting {
this.hostingURL = `/v2/instances/${session.project.instance}/hosting/`
this.editHostingURL = `https://${session.getHost()}${this.hostingURL}${this.name}/`
this.hostingHost = session.getHost() === 'api.syncano.rocks' ? 'syncano.ninja' : 'syncano.site'
this.config = {}

this.loadLocal()
}

static async add (params) {
const configParams = {
src: params.src
src: params.src,
config: {
browser_router: params.browser_router || false
}
}
session.settings.project.addHosting(params.name, configParams)

Expand All @@ -67,6 +71,9 @@ class Hosting {

const paramsToAdd = {
name: params.name,
config: {
browser_router: params.browser_router
},
domains
}

Expand Down Expand Up @@ -100,6 +107,9 @@ class Hosting {

const paramsToUpdate = {
name: this.name,
config: {
browser_router: params.browser_router
},
domains
}

Expand Down Expand Up @@ -214,20 +224,20 @@ class Hosting {
this.name = hosting.name
this.description = hosting.description
this.domains = hosting.domains

const status = await this.areFilesUpToDate()
this.isUpToDate = status
this.config.browser_router = hosting.config.browser_router || false
this.isUpToDate = await this.areFilesUpToDate()
} else {
this.existRemotely = false
this.error = hosting
}
this.existRemotely = false
this.error = hosting
return Promise.resolve()
}

async loadRemote () {
debug('loadRemote()')
try {
const hosting = await this.getRemote()
this.setRemoteState(hosting)
await this.setRemoteState(hosting)
} catch (err) {
this.existRemotely = false
}
Expand All @@ -253,7 +263,8 @@ class Hosting {
this.existLocally = true
this.src = localHostingSettings.src
this.cname = localHostingSettings.cname
this.path = path.join(session.projectPath, this.src, '/')
this.config.browser_router = localHostingSettings.config.browser_router
this.path = path.join(session.projectPath, this.src, path.sep)
this.url = this.getURL(this.name)
}
}
Expand Down Expand Up @@ -385,7 +396,10 @@ class Hosting {
async listRemoteFiles () {
debug('listRemoteFiles()')
const files = await session.connection.hosting.listFiles(this.name)
return files.map((file) => new HostingFile().loadRemote(file))
return files.map(async file => {
const hostingFile = new HostingFile(file)
return hostingFile.loadRemote(file)
})
}

// Get info about hostings first, then get the files list for given one
Expand Down Expand Up @@ -416,8 +430,12 @@ class Hosting {
return files
}

getCNAME () {
return _.find(this.domains, (domain) => domain.indexOf('.') !== -1)
}

getCnameURL () {
const cname = _.find(this.domains, (domain) => domain.indexOf('.') !== -1)
const cname = this.getCNAME()
if (cname) {
return `http://${cname}`
}
Expand Down

0 comments on commit 297eab2

Please sign in to comment.