Skip to content

Commit

Permalink
Fix swatches selection, add support for basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Krzeszowiak committed Apr 3, 2020
1 parent 4c13eec commit 5a16186
Show file tree
Hide file tree
Showing 9 changed files with 914 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
magepack.build.js
magepack.config.js

# Created by https://www.gitignore.io/api/osx,node,windows,visualstudiocode
# Edit at https://www.gitignore.io/?templates=osx,node,windows,visualstudiocode
Expand Down
2 changes: 2 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ program
.requiredOption('--cms-url <url>', 'CMS page URL.')
.requiredOption('--category-url <url>', 'Category page URL.')
.requiredOption('--product-url <url>', 'Product page URL.')
.option('-u, --auth-username <user>', 'Basic authentication username.')
.option('-p, --auth-password <password>', 'Basic authentication password.')
.option('-d, --debug', 'Enable logging of debugging information.')
.action((config) => {
if (config.debug) {
Expand Down
20 changes: 20 additions & 0 deletions lib/generate/authenticate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const logger = require('../utils/logger');

/**
*
* @param {Page} page Puppeteer Page object instance.
* @param {string} username Basic auth username.
* @param {string} password Basic auth password.
*/
const authenticate = async (page, username, password) => {
if (username && password) {
logger.debug('Authenticating with given user and password.');

await page.authenticate({
username,
password,
});
}
};

module.exports = authenticate;
11 changes: 10 additions & 1 deletion lib/generate/collector/category.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const merge = require('lodash.merge');

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

const baseConfig = {
Expand All @@ -15,15 +16,23 @@ const baseConfig = {
* @param {BrowserContext} browserContext Puppeteer's BrowserContext object.
* @param {object} configuration Generation configuration object.
* @param {string} configuration.categoryUrl URL to the category page.
* @param {string} configuration.authUsername Basic auth username.
* @param {string} configuration.authPassword Basic auth password.
*/
const category = async (browserContext, { categoryUrl }) => {
const category = async (
browserContext,
{ categoryUrl, authUsername, authPassword }
) => {
const bungleConfig = merge({}, baseConfig);

const bundleName = bungleConfig.name;

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

const page = await browserContext.newPage();
console.log(authUsername, authPassword);
await authenticate(page, authUsername, authPassword);

await page.goto(categoryUrl, {
waitUntil: 'networkidle0',
});
Expand Down
31 changes: 24 additions & 7 deletions lib/generate/collector/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const merge = require('lodash.merge');

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

const baseConfig = {
Expand All @@ -17,24 +18,40 @@ const baseConfig = {
* @param {BrowserContext} browserContext Puppeteer's BrowserContext object.
* @param {object} configuration Generation configuration object.
* @param {string} configuration.productUrl URL to the product page.
* @param {string} configuration.authUsername Basic auth username.
* @param {string} configuration.authPassword Basic auth password.
*/
const checkout = async (browserContext, { productUrl }) => {
const checkout = async (
browserContext,
{ productUrl, authUsername, authPassword }
) => {
const bundleConfig = merge({}, baseConfig);

const bundleName = bundleConfig.name;

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

const page = await browserContext.newPage();

await authenticate(page, authUsername, authPassword);

await page.goto(productUrl, { waitUntil: 'networkidle0' });

// Select option for every swatch if there are any.
const swatches = await page.$$('#product_addtocart_form .swatch-attribute');
for (let swatchIndex = 1; swatchIndex <= swatches.length; swatchIndex++) {
await page.click(
`#product_addtocart_form .swatch-attribute:nth-child(${swatchIndex}) .swatch-option:not([disabled])`
);
}
await page.evaluate(() => {
Array.from(
document.querySelectorAll(
'.product-options-wrapper .swatch-attribute'
)
).forEach((swatch) => {
const swatchOption = swatch.querySelector(
'.swatch-option:not([disabled])'
);
swatch.querySelector('.swatch-input').value =
swatchOption.getAttribute('option-id') ||
swatchOption.getAttribute('data-option-id');
});
});

await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
Expand Down
8 changes: 7 additions & 1 deletion lib/generate/collector/cms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const merge = require('lodash.merge');

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

const baseConfig = {
Expand All @@ -15,15 +16,20 @@ const baseConfig = {
* @param {BrowserContext} browserContext Puppeteer's BrowserContext object.
* @param {object} configuration Generation configuration object.
* @param {string} configuration.cmsUrl URL to the CMS page.
* @param {string} configuration.authUsername Basic auth username.
* @param {string} configuration.authPassword Basic auth password.
*/
const cms = async (browserContext, { cmsUrl }) => {
const cms = async (browserContext, { cmsUrl, authUsername, authPassword }) => {
const bundleConfig = merge({}, baseConfig);

const bundleName = bundleConfig.name;

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

const page = await browserContext.newPage();

await authenticate(page, authUsername, authPassword);

await page.goto(cmsUrl, { waitUntil: 'networkidle0' });

merge(bundleConfig.modules, await collectModules(page));
Expand Down
11 changes: 10 additions & 1 deletion lib/generate/collector/product.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const merge = require('lodash.merge');

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

const baseConfig = {
Expand All @@ -15,15 +16,23 @@ const baseConfig = {
* @param {BrowserContext} browserContext Puppeteer's BrowserContext object.
* @param {object} configuration Generation configuration object.
* @param {string} configuration.productUrl URL to the product page.
* @param {string} configuration.authUsername Basic auth username.
* @param {string} configuration.authPassword Basic auth password.
*/
const product = async (browserContext, { productUrl }) => {
const product = async (
browserContext,
{ productUrl, authUsername, authPassword }
) => {
const bundleConfig = merge({}, baseConfig);

const bundleName = bundleConfig.name;

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

const page = await browserContext.newPage();

await authenticate(page, authUsername, authPassword);

await page.goto(productUrl, { waitUntil: 'networkidle0' });

merge(bundleConfig.modules, await collectModules(page));
Expand Down
26 changes: 26 additions & 0 deletions magepack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ module.exports = [
'Magento_Catalog/js/product/view/product-ids',
'Magento_Catalog/js/product/view/product-ids-resolver':
'Magento_Catalog/js/product/view/product-ids-resolver',
'Magento_Catalog/js/product/view/product-info':
'Magento_Catalog/js/product/view/product-info',
'Magento_Catalog/js/product/view/product-info-resolver':
'Magento_Catalog/js/product/view/product-info-resolver',
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin':
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin',
'Magento_ConfigurableProduct/js/product/view/product-info-resolver':
'Magento_ConfigurableProduct/js/product/view/product-info-resolver',
'Magento_Cookie/js/require-cookie':
'Magento_Cookie/js/require-cookie',
'Magento_Wishlist/js/add-to-wishlist':
Expand Down Expand Up @@ -309,6 +317,10 @@ module.exports = [
'Magento_Catalog/js/product/view/product-ids',
'Magento_Catalog/js/product/view/product-ids-resolver':
'Magento_Catalog/js/product/view/product-ids-resolver',
'Magento_Catalog/js/product/view/product-info':
'Magento_Catalog/js/product/view/product-info',
'Magento_Catalog/js/product/view/product-info-resolver':
'Magento_Catalog/js/product/view/product-info-resolver',
'Magento_Catalog/js/product/view/provider':
'Magento_Catalog/js/product/view/provider',
'Magento_Catalog/js/related-products':
Expand All @@ -317,6 +329,10 @@ module.exports = [
'Magento_Catalog/js/validate-product',
'Magento_Catalog/product/view/validation':
'Magento_Catalog/product/view/validation',
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin':
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin',
'Magento_ConfigurableProduct/js/product/view/product-info-resolver':
'Magento_ConfigurableProduct/js/product/view/product-info-resolver',
'Magento_Cookie/js/require-cookie':
'Magento_Cookie/js/require-cookie',
'Magento_InstantPurchase/js/view/instant-purchase':
Expand Down Expand Up @@ -345,6 +361,7 @@ module.exports = [
'Magento_Wishlist/js/add-to-wishlist':
'Magento_Wishlist/js/add-to-wishlist',
'fotorama/fotorama': 'fotorama/fotorama',
'mage/gallery/gallery': 'mage/gallery/gallery',
'magnifier/magnifier': 'magnifier/magnifier',
'magnifier/magnify': 'magnifier/magnify',
'text!Magento_InstantPurchase/template/confirmation.html':
Expand All @@ -353,6 +370,7 @@ module.exports = [
'Magento_InstantPurchase/template/instant-purchase.html',
'text!Magento_Theme/templates/breadcrumbs.html':
'Magento_Theme/templates/breadcrumbs.html',
'text!mage/gallery/gallery.html': 'mage/gallery/gallery.html',
},
},
{
Expand All @@ -365,6 +383,10 @@ module.exports = [
'Magento_Catalog/js/product/view/product-ids',
'Magento_Catalog/js/product/view/product-ids-resolver':
'Magento_Catalog/js/product/view/product-ids-resolver',
'Magento_Catalog/js/product/view/product-info':
'Magento_Catalog/js/product/view/product-info',
'Magento_Catalog/js/product/view/product-info-resolver':
'Magento_Catalog/js/product/view/product-info-resolver',
'Magento_Checkout/js/action/create-billing-address':
'Magento_Checkout/js/action/create-billing-address',
'Magento_Checkout/js/action/create-shipping-address':
Expand Down Expand Up @@ -457,6 +479,10 @@ module.exports = [
'Magento_CheckoutAgreements/js/model/agreements-assigner',
'Magento_CheckoutAgreements/js/model/set-payment-information-mixin':
'Magento_CheckoutAgreements/js/model/set-payment-information-mixin',
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin':
'Magento_ConfigurableProduct/js/catalog-add-to-cart-mixin',
'Magento_ConfigurableProduct/js/product/view/product-info-resolver':
'Magento_ConfigurableProduct/js/product/view/product-info-resolver',
'Magento_Customer/js/model/address-list':
'Magento_Customer/js/model/address-list',
'Magento_Customer/js/model/customer':
Expand Down
Loading

0 comments on commit 5a16186

Please sign in to comment.