diff --git a/src/up.cmd.js b/src/up.cmd.js index dab197a2a..41710b6af 100644 --- a/src/up.cmd.js +++ b/src/up.cmd.js @@ -100,11 +100,39 @@ export default class UpCommand extends AbstractServerCommand { } async verifyUrl(gitUrl, inref) { + // check if the site is on helix5 + // https://admin.hlx.page/sidekick/adobe/www-aem-live/main/config.json + // { + // "host": "aem.live", + // "liveHost": "main--www-aem-live--adobe.aem.live", + // "plugins": [ + // { + // "id": "doc", + // "title": "Documentation", + // "url": "https://www.aem.live/docs/" + // } + // ], + // "previewHost": "main--www-aem-live--adobe.aem.page", + // "project": "Helix Website (AEM Live)", + // "testProperty": "header"; + // } + const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.repo}/${gitUrl.owner}/${inref}/config.json`; + const configResp = await getFetch()(configUrl); + let previewHostBase = 'hlx.page'; + if (configResp.ok) { + // this is best effort for now + const config = await configResp.json(); + const { previewHost } = config; + if (previewHost && previewHost.endsWith('.aem.page')) { + previewHostBase = 'aem.page'; + } + } + let ref = inref; // replace `/` by `-` in ref. ref = ref.replace(/\//g, '-'); - this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.hlx.page`; + this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`; // check length limit if (this._url.split('.') .map((part) => part.replace(/^https:\/\//, '')) @@ -125,7 +153,7 @@ export default class UpCommand extends AbstractServerCommand { this.log.warn(chalk`Unable to verify {yellow main} branch via {blue ${fstabUrl}} (${resp.status}). Maybe not pushed yet?`); } else { this.log.warn(chalk`Unable to verify {yellow ${ref}} branch on {blue ${fstabUrl}} (${resp.status}). Fallback to {yellow main} branch.`); - this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.hlx.page`; + this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`; } } } diff --git a/test/up-cmd.test.js b/test/up-cmd.test.js index 56bf8b314..adc810746 100644 --- a/test/up-cmd.test.js +++ b/test/up-cmd.test.js @@ -65,6 +65,73 @@ describe('Integration test for up command with helix pages', function suite() { .get('/not-found.txt') .reply(404); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(404); + + let port; + await new Promise((resolve, reject) => { + let error = null; + cmd + .on('started', async () => { + try { + port = cmd.project.server.port; + let ret = await assertHttp(`http://127.0.0.1:${port}/index.html`, 200); + assert.strictEqual(ret.trim(), '## Welcome'); + ret = await assertHttp(`http://127.0.0.1:${port}/local.txt`, 200); + assert.strictEqual(ret.trim(), 'Hello, world.'); + await assertHttp(`http://127.0.0.1:${port}/not-found.txt`, 404); + } catch (e) { + error = e; + } + await cmd.stop(); + }) + .on('stopped', () => { + if (error) { + reject(error); + } + resolve(); + }) + .run() + .catch(reject); + }); + assert.strictEqual(opened, `http://localhost:${port}/`); + }); + + it('up command opens browser and delivers correct response on helix 5.', async () => { + let opened; + const MockedCommand = await esmock('../src/up.cmd.js', { + '../src/abstract-server.cmd.js': await esmock('../src/abstract-server.cmd.js', { + open: (url) => { + opened = url; + }, + }), + }); + initGit(testDir, 'https://github.com/adobe/dummy-foo.git'); + const cmd = new MockedCommand() + .withLiveReload(false) + .withDirectory(testDir) + .withOpen('/') + .withHttpPort(0); + + nock('https://master--dummy-foo--adobe.aem.page') + .get('/fstab.yaml') + .reply(200, 'dummy') + .get('/index.html') + .reply(200, '## Welcome') + .get('/not-found.txt') + .reply(404); + + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(200, { + host: 'example.com', + liveHost: 'master--dummy-foo--adobe.aem.live', + previewHost: 'master--dummy-foo--adobe..aem.page', + project: 'Example Project on Helix 5', + testProperty: 'header', + }); + let port; await new Promise((resolve, reject) => { let error = null; @@ -116,6 +183,10 @@ describe('Integration test for up command with helix pages', function suite() { .get('/not-found.txt') .reply(404); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/tripod/test/config.json') + .reply(404); + cmd .on('started', async () => { try { @@ -160,6 +231,10 @@ describe('Integration test for up command with helix pages', function suite() { .get('/fstab.yaml') .reply(404, 'dummy'); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(404); + cmd .on('started', async () => { try { @@ -208,6 +283,14 @@ describe('Integration test for up command with helix pages', function suite() { .get('/fstab.yaml') .reply(200, 'yep!'); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(404); + + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/new-branch/config.json') + .reply(404); + let timer; cmd .on('started', async () => { @@ -257,6 +340,14 @@ describe('Integration test for up command with helix pages', function suite() { .get('/fstab.yaml') .reply(404, 'dummy'); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(404); + + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/new-and-totally-unreasonably-long-in-fact-too-long-branch/config.json') + .reply(404); + let timer; cmd .on('started', async () => { @@ -336,6 +427,10 @@ describe('Integration test for up command with cache', function suite() { .get('/head.html') .reply(200, ''); + nock('https://admin.hlx.page:443') + .get('/sidekick/dummy-foo/adobe/master/config.json') + .reply(404); + nock.enableNetConnect(/127.0.0.1/); cmd