diff --git a/packages/cli/src/cli-hosting.js b/packages/cli/src/cli-hosting.js index cd5147a8..d4ffb146 100644 --- a/packages/cli/src/cli-hosting.js +++ b/packages/cli/src/cli-hosting.js @@ -76,6 +76,7 @@ const setup = async () => { .description('Configure hosting parameters') .option('-c, --cname ', 'add CNAME to hosting') .option('-d, --remove-cname ', 'remove CNAME from hosting') + .option('-b, --browser_router', 'turn on BrowserRouter support') .action(async (...options) => { session.isAuthenticated() session.hasProject() diff --git a/packages/cli/src/commands/hosting-add.js b/packages/cli/src/commands/hosting-add.js index db3f436b..00fb7210 100644 --- a/packages/cli/src/commands/hosting-add.js +++ b/packages/cli/src/commands/hosting-add.js @@ -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 } @@ -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 } diff --git a/packages/cli/src/commands/hosting-config.js b/packages/cli/src/commands/hosting-config.js index eb4dcc6b..286b61dc 100644 --- a/packages/cli/src/commands/hosting-config.js +++ b/packages/cli/src/commands/hosting-config.js @@ -1,10 +1,12 @@ 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]) { @@ -12,14 +14,31 @@ class HostingConfig { 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) { @@ -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 diff --git a/packages/cli/src/commands/hosting-list.js b/packages/cli/src/commands/hosting-list.js index 576b6e8e..8ab260bd 100644 --- a/packages/cli/src/commands/hosting-list.js +++ b/packages/cli/src/commands/hosting-list.js @@ -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 { @@ -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')}`}`) } diff --git a/packages/cli/src/utils/hosting/hosting.js b/packages/cli/src/utils/hosting/hosting.js index 9760e37a..9b35b8c4 100644 --- a/packages/cli/src/utils/hosting/hosting.js +++ b/packages/cli/src/utils/hosting/hosting.js @@ -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) @@ -67,6 +71,9 @@ class Hosting { const paramsToAdd = { name: params.name, + config: { + browser_router: params.browser_router + }, domains } @@ -100,6 +107,9 @@ class Hosting { const paramsToUpdate = { name: this.name, + config: { + browser_router: params.browser_router + }, domains } @@ -214,12 +224,12 @@ 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() } @@ -227,7 +237,7 @@ class Hosting { debug('loadRemote()') try { const hosting = await this.getRemote() - this.setRemoteState(hosting) + await this.setRemoteState(hosting) } catch (err) { this.existRemotely = false } @@ -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) } } @@ -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 @@ -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}` }