Skip to content

Commit

Permalink
Merge pull request #239 from zendesk/luis.themes_preview_respects_env…
Browse files Browse the repository at this point in the history
…_vars

fix: `themes:preview` should respect the env variables
  • Loading branch information
luis-almeida authored Jul 10, 2024
2 parents 6563e10 + d6f560a commit b876397
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
8 changes: 1 addition & 7 deletions packages/zcli-themes/src/commands/themes/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import * as morgan from 'morgan'
import * as chalk from 'chalk'
import * as cors from 'cors'
import * as chokidar from 'chokidar'
import { Auth, getBaseUrl } from '@zendesk/zcli-core'
import preview from '../../lib/preview'
import getManifest from '../../lib/getManifest'
import getVariables from '../../lib/getVariables'
Expand Down Expand Up @@ -56,8 +55,7 @@ export default class Preview extends Command {
}
}

await preview(themePath, flags)

const baseUrl = await preview(themePath, flags)
const app = express()
const server = httpsServerOptions === null ? http.createServer(app) : https.createServer(httpsServerOptions, app)
const wss = new WebSocket.Server({ server, path: '/livereload' })
Expand Down Expand Up @@ -87,10 +85,6 @@ export default class Preview extends Command {
})

server.listen(port, host, async () => {
// preview requires authentication so we're sure
// to have a logged in profile at this point
const { subdomain, domain } = await new Auth().getLoggedInProfile()
const baseUrl = getBaseUrl(subdomain, domain)
this.log(chalk.bold.green('Ready', chalk.blueBright(`${baseUrl}/hc/admin/local_preview/start`, '🚀')))
this.log(`You can exit preview mode in the UI or by visiting ${baseUrl}/hc/admin/local_preview/stop`)
tailLogs && this.log(chalk.bold('Tailing logs'))
Expand Down
9 changes: 6 additions & 3 deletions packages/zcli-themes/src/lib/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('preview', () => {
sinon.restore()
})

it('calls the local_preview endpoint with the correct payload', async () => {
it('calls the local_preview endpoint with the correct payload and returns the used baseURL', async () => {
const getManifestStub = sinon.stub(getManifest, 'default')
const getTemplatesStub = sinon.stub(getTemplates, 'default')
const getVariablesStub = sinon.stub(getVariables, 'default')
Expand Down Expand Up @@ -63,10 +63,11 @@ describe('preview', () => {

requestStub.returns(Promise.resolve({
status: 200,
statusText: 'OK'
statusText: 'OK',
config: { baseURL: 'https://z3ntest.zendesk.com' }
}) as axios.AxiosPromise)

await preview('theme/path', flags)
const baseUrl = await preview('theme/path', flags)

expect(requestStub.calledWith('/hc/api/internal/theming/local_preview', sinon.match({
method: 'put',
Expand All @@ -92,6 +93,8 @@ describe('preview', () => {
}
}
}))).to.equal(true)

expect(baseUrl).to.equal('https://z3ntest.zendesk.com')
})

it('throws a comprehensive error when validation fails', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/zcli-themes/src/lib/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import validationErrorsToString from './validationErrorsToString'
import { getLocalServerBaseUrl } from './getLocalServerBaseUrl'
import type { AxiosError } from 'axios'

export default async function preview (themePath: string, flags: Flags): Promise<void> {
export default async function preview (themePath: string, flags: Flags): Promise<string | void> {
const manifest = getManifest(themePath)
const templates = getTemplates(themePath)
const variables = getVariables(themePath, manifest.settings, flags)
Expand All @@ -32,7 +32,7 @@ export default async function preview (themePath: string, flags: Flags): Promise

try {
CliUx.ux.action.start('Uploading theme')
await request.requestAPI('/hc/api/internal/theming/local_preview', {
const { config: { baseURL } } = await request.requestAPI('/hc/api/internal/theming/local_preview', {
method: 'put',
headers: {
'X-Zendesk-Request-Originator': 'zcli themes:preview'
Expand All @@ -56,6 +56,7 @@ export default async function preview (themePath: string, flags: Flags): Promise
validateStatus: (status: number) => status === 200
})
CliUx.ux.action.stop('Ok')
return baseURL
} catch (e) {
CliUx.ux.action.stop(chalk.bold.red('!'))
const { response, message } = e as AxiosError
Expand Down
7 changes: 7 additions & 0 deletions packages/zcli-themes/tests/functional/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('themes:preview', function () {
let server

const preview = test
.stdout()
.env({
ZENDESK_SUBDOMAIN: 'z3ntest',
ZENDESK_EMAIL: 'admin@z3ntest.com',
Expand All @@ -33,6 +34,12 @@ describe('themes:preview', function () {
nock.cleanAll()
})

preview
.it('should provide links and instructions to start and exit preview', async (ctx) => {
expect(ctx.stdout).to.contain('Ready https://z3ntest.zendesk.com/hc/admin/local_preview/start 🚀')
expect(ctx.stdout).to.contain('You can exit preview mode in the UI or by visiting https://z3ntest.zendesk.com/hc/admin/local_preview/stop')
})

preview
.it('should serve assets on the defined host and port', async () => {
expect((await axios.get('http://0.0.0.0:9999/guide/style.css')).status).to.eq(200)
Expand Down

0 comments on commit b876397

Please sign in to comment.