Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions lib/generate/authenticate.js

This file was deleted.

18 changes: 7 additions & 11 deletions lib/generate/collector/category.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const merge = require('lodash.merge');

const logger = require('../../utils/logger');
const authenticate = require('../authenticate');
const blockMagepack = require('../blockMagepack');
const collectModules = require('../collectModules');
const createPage = require('../createPage');

const baseConfig = {
url: '',
Expand All @@ -30,15 +29,12 @@ const category = async (

logger.info(`Collecting modules for bundle "${bundleName}".`);

const page = await browserContext.newPage();

blockMagepack(page);

await authenticate(page, authUsername, authPassword);

await page.goto(categoryUrl, {
waitUntil: 'networkidle0',
});
const page = await createPage(
browserContext,
authUsername,
authPassword,
categoryUrl
);

merge(bungleConfig.modules, await collectModules(page));

Expand Down
20 changes: 8 additions & 12 deletions lib/generate/collector/checkout.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* global BASE_URL */

const merge = require('lodash.merge');

const logger = require('../../utils/logger');
const authenticate = require('../authenticate');
const blockMagepack = require('../blockMagepack');
const collectModules = require('../collectModules');
const createPage = require('../createPage');

const baseConfig = {
url: {},
Expand All @@ -32,13 +29,12 @@ const checkout = async (

logger.info(`Collecting modules for bundle "${bundleName}".`);

const page = await browserContext.newPage();

blockMagepack(page);

await authenticate(page, authUsername, authPassword);

await page.goto(productUrl, { waitUntil: 'networkidle0' });
const page = await createPage(
browserContext,
authUsername,
authPassword,
productUrl
);

// Select option for every swatch if there are any.
await page.evaluate(() => {
Expand Down Expand Up @@ -85,7 +81,7 @@ const checkout = async (
),
]);

const baseUrl = await page.evaluate(() => BASE_URL);
const baseUrl = await page.evaluate(() => window.BASE_URL);

await page.goto(`${baseUrl}checkout/cart`, { waitUntil: 'networkidle0' });
const cartModules = await collectModules(page);
Expand Down
16 changes: 7 additions & 9 deletions lib/generate/collector/cms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const merge = require('lodash.merge');

const logger = require('../../utils/logger');
const authenticate = require('../authenticate');
const blockMagepack = require('../blockMagepack');
const collectModules = require('../collectModules');
const createPage = require('../createPage');

const baseConfig = {
url: '',
Expand All @@ -27,13 +26,12 @@ const cms = async (browserContext, { cmsUrl, authUsername, authPassword }) => {

logger.info(`Collecting modules for bundle "${bundleName}".`);

const page = await browserContext.newPage();

blockMagepack(page);

await authenticate(page, authUsername, authPassword);

await page.goto(cmsUrl, { waitUntil: 'networkidle0' });
const page = await createPage(
browserContext,
authUsername,
authPassword,
cmsUrl
);

merge(bundleConfig.modules, await collectModules(page));

Expand Down
16 changes: 7 additions & 9 deletions lib/generate/collector/product.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const merge = require('lodash.merge');

const logger = require('../../utils/logger');
const authenticate = require('../authenticate');
const blockMagepack = require('../blockMagepack');
const collectModules = require('../collectModules');
const createPage = require('../createPage');

const baseConfig = {
url: [],
Expand All @@ -30,13 +29,12 @@ const product = async (

logger.info(`Collecting modules for bundle "${bundleName}".`);

const page = await browserContext.newPage();

blockMagepack(page);

await authenticate(page, authUsername, authPassword);

await page.goto(productUrl, { waitUntil: 'networkidle0' });
const page = await createPage(
browserContext,
authUsername,
authPassword,
productUrl
);

merge(bundleConfig.modules, await collectModules(page));

Expand Down
46 changes: 46 additions & 0 deletions lib/generate/createPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const blockMagepack = require('./blockMagepack');
const logger = require('../utils/logger');

/**
* Set up a page to be ready to collect data
*
* @param {import('puppeteer').BrowserContext} browserContext Puppeteer browser context instance
* @param {string} authUsername Basic auth username
* @param {string} authPassword Basic auth password
* @param {string} url URL of page to navigate to initially
*/
module.exports = async (browserContext, authUsername, authPassword, url) => {
const page = await browserContext.newPage();

blockMagepack(page);

if (authUsername && authPassword) {
logger.debug('Authenticating with given user and password.');

await page.authenticate({ authUsername, authPassword });
}

if (url) {
const result = await page.goto(url, { waitUntil: 'networkidle0' });

if (url !== result.url()) {
logger.info(
'Requested',
url,
'but navigation ended on',
result.url()
);
}

if (!result.ok()) {
throw new Error(
'HTTP status code ' +
result.status() +
' returned from ' +
result.url()
);
}
}

return page;
};