magepwa-cli

MagePWA CLI Documentation

Welcome to the comprehensive documentation for MagePWA CLI. This guide covers everything you need to know to get started and make the most of the tool.

Table of Contents

  1. Getting Started
  2. Project Structure
  3. Override System
  4. Store Context
  5. Build Configuration
  6. Regions Components
  7. Tax Invoice Components
  8. Free Gift Components
  9. Import Aliases
  10. Troubleshooting

Getting Started

Installation

npm install -g magepwa-cli

Quick Setup

  1. Navigate to your existing Magento PWA project
  2. Run the initialization command
  3. Start developing with enhanced tooling
cd your-magento-pwa-project
magepwa init

Available Commands

magepwa init          # Add scaffolds to existing Magento PWA project
magepwa regions       # Copy regions manager scaffold files
magepwa tax-invoice   # Copy tax invoice scaffold files
magepwa freegift      # Copy free gift scaffold files (requires Amasty extension)
magepwa doctor        # Check environment prerequisites
magepwa --help        # Show help information

Project Structure

After running magepwa init in your existing Magento PWA project, it will add:

src/
├── targets/           # Custom build targets and interceptors
│   ├── local-intercept.js
│   ├── VeniaResolverPlugin.js
│   └── extend-configured-route.js
├── contexts/          # Store context configuration
│   └── store.js
├── overrides/         # Component override directories
│   ├── venia-ui/     # Venia UI overrides
│   ├── peregrine/    # Peregrine overrides
│   └── pagebuilder/  # Page Builder overrides
└── ...

# Root level configuration files
├── serve.js           # Development server configuration
├── theme.js           # Theme configuration with Tailwind
├── tailwind.config.js # Tailwind CSS configuration
├── webpack.config.js  # Enhanced webpack configuration
└── upward.yml         # Upward configuration

Override System

The CLI supports overriding components from three main modules:

1. Venia UI Components

Create files with the exact same path as the original:

src/overrides/venia-ui/components/AccountChip/accountChip.js

2. Peregrine Hooks

Override talons and utilities:

src/overrides/peregrine/talons/AccountMenu/useAccountMenu.js

3. Page Builder Components

Customize Page Builder elements:

src/overrides/pagebuilder/ContentTypes/ButtonItem/buttonItem.js

Store Context

Access store configuration anywhere in your app:

import { useStoreContext } from '@contexts/store';

const MyComponent = () => {
  const { product_url_suffix, store_code } = useStoreContext();
  // Use store configuration
};

Build Configuration

The CLI sets up enhanced build configuration:

Custom Webpack Configuration

Development Server

Tailwind CSS Integration

Regions Components

Adding Regions Components

# Copy regions scaffold to current directory's src folder (default)
magepwa regions

# Copy to a specific project directory
magepwa regions --dir /path/to/project

# Copy to a custom destination folder
magepwa regions --dir /path/to/project --dest components

# Copy to current directory with custom destination
magepwa regions --dest src/components

Regions Command Options

Usage Example

// Import the components in your React components
import District from '@components/Address/District';
import SubDistrict from '@components/Address/SubDistrict';
import { useDistrict } from '@talons/Address/District/useDistrict';
import { useSubDistrict } from '@talons/Address/SubDistrict/useSubDistrict';

// Use in your component
const AddressForm = () => {
  const { districts, loading } = useDistrict();
  const { subDistricts, loading: subLoading } = useSubDistrict(selectedDistrict);
  
  return (
    <div>
      <District 
        districts={districts} 
        loading={loading}
        onSelect={setSelectedDistrict}
      />
      <SubDistrict 
        subDistricts={subDistricts}
        loading={subLoading}
        onSelect={setSelectedSubDistrict}
      />
    </div>
  );
};

Tax Invoice Components

Adding Tax Invoice Components

# Copy tax invoice scaffold to current directory's src folder (default)
magepwa tax-invoice

# Copy to a specific project directory
magepwa tax-invoice --dir /path/to/project

# Copy to a custom destination folder
magepwa tax-invoice --dir /path/to/project --dest components

# Copy to current directory with custom destination
magepwa tax-invoice --dest src/components

Tax Invoice Command Options

Usage Example

// Import the components in your React components
import TaxInvoice from '@components/CheckoutPage/TaxInvoice';
import { useTaxInvoice } from '@talons/CheckoutPage/TaxInvoice/useTaxInvoice';
import { validateTaxInvoiceForm } from '@utils/validateForm';

// Use in your component
const CheckoutPage = () => {
  const { taxInvoiceData, loading, submitTaxInvoice } = useTaxInvoice();
  
  return (
    <div>
      <TaxInvoice 
        data={taxInvoiceData}
        loading={loading}
        onSubmit={submitTaxInvoice}
        validate={validateTaxInvoiceForm}
      />
    </div>
  );
};

Tax Invoice Features

Free Gift Components

⚠️ Important Prerequisites: The Free Gift components require the Amasty Free Gift Magento 2 extension to be installed on your Magento backend, along with the corresponding GraphQL module. These components will not work without the proper backend extension.

Adding Free Gift Components

# Copy free gift scaffold to current directory's src folder (default)
magepwa freegift

# Copy to a specific project directory
magepwa freegift --dir /path/to/project

# Copy to a custom destination folder
magepwa freegift --dir /path/to/project --dest components

# Copy to current directory with custom destination
magepwa freegift --dest src/components

Free Gift Command Options

Prerequisites

Before using the Free Gift components, ensure you have:

  1. Amasty Free Gift Extension: Install the Amasty Free Gift Magento 2 extension on your Magento backend
  2. GraphQL Module: Ensure the GraphQL module for the Free Gift extension is enabled
  3. Backend Configuration: Configure the Free Gift rules and promotions in your Magento admin

Usage Example

// Import the components in your React components
import FreeGift from '@components/FreeGift';
import { useFreeGift } from '@talons/FreeGift/useFreeGift';
import { useFreeGiftSelection } from '@talons/FreeGift/useFreeGiftSelection';
import { useGiftPromotions } from '@talons/FreeGift/useGiftPromotions';

// Use in your component
const CartPage = () => {
  const { freeGifts, loading } = useFreeGift();
  const { selectedGifts, selectGift } = useFreeGiftSelection();
  const { promotions, loading: promotionsLoading } = useGiftPromotions();
  
  return (
    <div>
      <FreeGift 
        gifts={freeGifts}
        loading={loading}
        onSelect={selectGift}
        selectedGifts={selectedGifts}
        promotions={promotions}
        promotionsLoading={promotionsLoading}
      />
    </div>
  );
};

Free Gift Features

Available Hooks

Component Structure

The Free Gift scaffold includes:

Import Aliases

Use these aliases for cleaner imports:

What’s Included

Pre-configured Files

Build Targets

Store Context

Regions Manager Scaffold

Tax Invoice Scaffold

Free Gift Scaffold

Troubleshooting

Common Issues

  1. Node.js Version: Ensure you’re using Node.js >= 18.0.0
  2. Permission Issues: Use sudo if needed for global installation
  3. Path Issues: Make sure you’re in the correct Magento PWA project directory

Environment Check

Use the doctor command to verify your environment:

magepwa doctor

This will check:

Getting Help

Requirements