Skip to content

Commit

Permalink
Merge pull request #220 from adobecom/bmarshal/utils-to-scripts
Browse files Browse the repository at this point in the history
MWPW-152082 Utils to Scripts
  • Loading branch information
Brandon32 committed Jul 11, 2024
2 parents c642703 + 63be5fe commit 00ae915
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 99 deletions.
4 changes: 2 additions & 2 deletions blocks/comparison/comparison.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

/**
* @param {Element} el
*/
const init = async (el) => {
const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);
const rows = Array.from(el.querySelectorAll(':scope > div'));
const headers = Array.from(rows.shift().children);
const headersRow = createTag('tr');
Expand Down
4 changes: 2 additions & 2 deletions blocks/faas-decode/faas-decode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

export default async function init(el) {
const { createTag, parseEncodedConfig } = await import(`${getLibs()}/utils/utils.js`);
const { createTag, parseEncodedConfig } = await import(`${LIBS}/utils/utils.js`);
const url = el.querySelector('a')?.href;
const resp = await fetch(url);

Expand Down
6 changes: 3 additions & 3 deletions blocks/redirects-formatter/redirects-formatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

export const SELECT_ALL_REGIONS = 'Select All Regions';
export const DESELECT_ALL_REGIONS = 'De-select All Regions';
Expand All @@ -12,7 +12,7 @@ const INSTRUCTIONS_TEXT = 'Select the locales you require by checking the checkb
+ ' press "Copy to clipboard" or select them with the cursor manually.';

async function createLocaleCheckboxes(prefixGroup) {
const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);

return Object.keys(prefixGroup).map((key) => {
const { prefix } = prefixGroup[key];
Expand Down Expand Up @@ -87,7 +87,7 @@ export function stringifyListForExcel(urls) {
}

export default async function init(el) {
const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);
const xlPath = './locale-config.json';
const resp = await fetch(xlPath);
if (!resp.ok) return;
Expand Down
6 changes: 3 additions & 3 deletions blocks/stats/stats.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

async function decorateRow(row, module) {
const { decorateLinkAnalytics } = module;
Expand All @@ -14,8 +14,8 @@ async function decorateRow(row, module) {
}

export default async function init(el) {
const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const module = await import(`${getLibs()}/martech/attributes.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);
const module = await import(`${LIBS}/martech/attributes.js`);

module.decorateBlockAnalytics(el);
const firstRow = el.querySelector(':scope > div');
Expand Down
4 changes: 2 additions & 2 deletions blocks/tree-view/tree-view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

const BACOM_HOSTS = ['localhost', '--bacom--adobecom.hlx.page', '--bacom--adobecom.hlx.live', 'business.adobe.com'];

Expand Down Expand Up @@ -110,7 +110,7 @@ const init = async (el) => {

if (!topList) return;

const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);
const subLists = topList.querySelectorAll('ul');
const isAccordion = subLists.length > 0;
const links = el.querySelectorAll('a');
Expand Down
6 changes: 3 additions & 3 deletions blocks/workfront-login/workfront-login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

export async function createProofForm(createTag, replaceKey, config) {
const form = createTag('form', { action: 'https://app.proofhq.com/login', method: 'post' });
Expand Down Expand Up @@ -105,8 +105,8 @@ export async function createSubdomainForm(createTag, replaceKey, config) {

/* c8 ignore next 14 */
export default async function init(el) {
const { createTag, getConfig } = await import(`${getLibs()}/utils/utils.js`);
const { replaceKey } = await import(`${getLibs()}/features/placeholders.js`);
const { createTag, getConfig } = await import(`${LIBS}/utils/utils.js`);
const { replaceKey } = await import(`${LIBS}/features/placeholders.js`);
const config = getConfig();
const isProof = el.classList.contains('proof');

Expand Down
19 changes: 12 additions & 7 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { setLibs } from './utils.js';

const LIBS = '/libs';
const STYLES = ['/styles/styles.css'];
const CONFIG = {
imsClientId: 'bacom',
Expand Down Expand Up @@ -157,18 +154,26 @@ const loadStyle = (path) => {
eagerLoad(marquee.querySelector('img'));
}());

const miloLibs = setLibs(LIBS);
export function setLibs(location) {
const { hostname, search } = location;
if (!['.hlx.', '.stage.', 'local'].some((i) => hostname.includes(i))) return '/libs';
const branch = new URLSearchParams(search).get('milolibs') || 'main';
if (branch === 'local') return 'http://localhost:6456/libs';
return branch.includes('--') ? `https://${branch}.hlx.live/libs` : `https://${branch}--milo--adobecom.hlx.live/libs`;
}

export const LIBS = setLibs(window.location);

(function loadStyles() {
const paths = [`${miloLibs}/styles/styles.css`];
const paths = [`${LIBS}/styles/styles.css`];
if (STYLES) {
paths.push(...(Array.isArray(STYLES) ? STYLES : [STYLES]));
}
paths.forEach(loadStyle);
}());

(async function loadPage() {
const { loadArea, loadLana, setConfig, createTag, getMetadata } = await import(`${miloLibs}/utils/utils.js`);
const { loadArea, loadLana, setConfig, createTag, getMetadata } = await import(`${LIBS}/utils/utils.js`);
if (getMetadata('template') === '404') window.SAMPLE_PAGEVIEWS_AT_RATE = 'high';
const metaCta = document.querySelector('meta[name="chat-cta"]');
if (metaCta && !document.querySelector('.chat-cta')) {
Expand All @@ -179,7 +184,7 @@ const miloLibs = setLibs(LIBS);
if (lastSection) lastSection.insertAdjacentElement('beforeend', chatDiv);
}
}
setConfig({ ...CONFIG, miloLibs });
setConfig({ ...CONFIG, miloLibs: LIBS });
loadLana({ clientId: 'bacom', tags: 'info' });
await loadArea();

Expand Down
15 changes: 0 additions & 15 deletions scripts/utils.js

This file was deleted.

4 changes: 2 additions & 2 deletions templates/featured-story/featured-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Templates - featured story
*/

import { getLibs } from '../../scripts/utils.js';
import { LIBS } from '../../scripts/scripts.js';

const { createTag } = await import(`${getLibs()}/utils/utils.js`);
const { createTag } = await import(`${LIBS}/utils/utils.js`);

function init() {
const i = 1;
Expand Down
2 changes: 0 additions & 2 deletions test/blocks/comparison/comparison.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import init from '../../../blocks/comparison/comparison.js';
import { setLibs } from '../../../scripts/utils.js';

setLibs('/libs');
document.body.innerHTML = await readFile({ path: './mocks/body.html' });
describe('Comparison Table', () => {
it('Verify Comparison Table Exists', async () => {
Expand Down
2 changes: 0 additions & 2 deletions test/blocks/faas-decode/faas-decode.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import init from '../../../blocks/faas-decode/faas-decode.js';
import { setLibs } from '../../../scripts/utils.js';
import waitForElement from '../../helpers/waitForElement.js';

window.lana = { log: () => {} };

describe('FaaS Decode', () => {
before(() => {
sinon.stub(window.lana, 'log');
setLibs('/libs');
});

after(() => {
Expand Down
3 changes: 0 additions & 3 deletions test/blocks/redirects-formatter/redirects-formatter.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { setLibs } from '../../../scripts/utils.js';

const {
default: init,
Expand All @@ -14,8 +13,6 @@ const {
} = await import('../../../blocks/redirects-formatter/redirects-formatter.js');
const { htmlIncluded, htmlExcluded, externalUrls, mixedSpaceTabUrls } = await import('./mocks/textAreaValues.js');

setLibs('libs');

describe('Redirects Formatter', () => {
const ogFetch = window.fetch;

Expand Down
3 changes: 0 additions & 3 deletions test/blocks/stats/stats.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { expect } from '@esm-bundle/chai';
import { setLibs } from '../../../scripts/utils.js';

const { default: init } = await import('../../../blocks/stats/stats.js');

setLibs('libs');

describe('Stats', () => {
it('Allows intro', async () => {
document.body.innerHTML = '<div class="stats"><div>Intro</div><div>Stat 1</div><div>Stat 2</div></div>';
Expand Down
2 changes: 0 additions & 2 deletions test/blocks/tree-view/tree-view.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { readFile } from '@web/test-runner-commands';
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import { setLibs } from '../../../scripts/utils.js';

const { default: init, isCurrentPage } = await import('../../../blocks/tree-view/tree-view.js');

window.lana = { log: () => {} };
setLibs('libs');

describe('Tree View', () => {
describe('simple', () => {
Expand Down
3 changes: 0 additions & 3 deletions test/blocks/workfront-login/workfront.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { expect } from '@esm-bundle/chai';
import sinon from 'sinon';
import waitForElement from '../../helpers/waitForElement.js';
import { setLibs } from '../../../scripts/utils.js';
import { createTag, replaceKey, getConfig } from './mocks/workfront-utils.js';
import { createSubdomainForm, createProofForm, location } from '../../../blocks/workfront-login/workfront-login.js';

setLibs('libs');

const config = getConfig();

const delay = (timeOut, cb) => new Promise((resolve) => {
Expand Down
36 changes: 36 additions & 0 deletions test/scripts/scripts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from '@esm-bundle/chai';
import { setLibs, LIBS } from '../../scripts/scripts.js';

describe('Libs', () => {
const tests = [
['https://business.adobe.com', '/libs'],
['https://business.adobe.com?milolibs=foo', '/libs'],
['https://business.stage.adobe.com', 'https://main--milo--adobecom.hlx.live/libs'],
['https://business.stage.adobe.com?milolibs=foo', 'https://foo--milo--adobecom.hlx.live/libs'],
['https://business.stage.adobe.com?milolibs=awesome--milo--forkedowner', 'https://awesome--milo--forkedowner.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.page/', 'https://main--milo--adobecom.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.page/?milolibs=foo', 'https://foo--milo--adobecom.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.page/?milolibs=local', 'http://localhost:6456/libs'],
['https://main--bacom--adobecom.hlx.page/?milolibs=awesome--milo--forkedowner', 'https://awesome--milo--forkedowner.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.live/', 'https://main--milo--adobecom.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.live/?milolibs=foo', 'https://foo--milo--adobecom.hlx.live/libs'],
['https://main--bacom--adobecom.hlx.live/?milolibs=local', 'http://localhost:6456/libs'],
['https://main--bacom--adobecom.hlx.live/?milolibs=awesome--milo--forkedowner', 'https://awesome--milo--forkedowner.hlx.live/libs'],
['http://localhost:3000', 'https://main--milo--adobecom.hlx.live/libs'],
['http://localhost:3000?milolibs=foo', 'https://foo--milo--adobecom.hlx.live/libs'],
['http://localhost:3000?milolibs=local', 'http://localhost:6456/libs'],
['http://localhost:3000?milolibs=awesome--milo--forkedowner', 'https://awesome--milo--forkedowner.hlx.live/libs'],
];

tests.forEach(([url, expected]) => {
it(`Sets libs for ${url}`, () => {
const location = new URL(url);
const libs = setLibs(location);
expect(libs).to.equal(expected);
});
});

it('Sets LIBS', () => {
expect(LIBS).to.equal('https://main--milo--adobecom.hlx.live/libs');
});
});
45 changes: 0 additions & 45 deletions test/scripts/utils.test.js

This file was deleted.

0 comments on commit 00ae915

Please sign in to comment.