m2-url
v1.0.0
Published
A NodeJS CLI tool to verify product URLs in Magento 2
Downloads
5
Readme
URL Verifier
This command line utility will show all of the URL rewrites set on a product. The CLI works by retrieving product data from Magento via its APIs. This tool can also save the retrieved information to a text file for easy sharing or record keeping purposes. This tool can also quickly verify if a product has a URL rewrite that matches a given URL.
Sample Output
$ npx m2-url --env PROD --sku EXPL001 --save
Example Cotton T-Shirt
EXPL001 (Magento ID: 117)
Environment: PRODUCTION
Status: Enabled
URL key: example-cotton-t-shirt.html
Categories: (3)
└ Apparel
└ New Items
└ T-Shirts
Public URLs: (4)
└ https://example.com/example-cotton-t-shirt.html
└ https://example.com/apparel/example-cotton-t-shirt.html
└ https://example.com/apparel/t-shirts/example-cotton-t-shirt.html
└ https://example.com/new-items/example-cotton-t-shirt.html
✔ Saved results to /Users/usr/ProductData/Queries/logs/EXPL001--PRODUCTION-020121-135201.txt
The CLI will primarily call the GraphQL API. However, due to Magento core functionality that cannot be changed at this time, the GraphQL API can only respond with data for products that are enabled and fully visible. In the event that the product you are attempting to retrieve data about is disabled or not fully visible, the GraphQL API will respond with an empty result. As this isn't very helpful in figuring out if there are no results because the product doesn't actually exist or if the configuration just doesn't expose it to the GraphQL API, the CLI will attempt to call the REST API to retrieve data on the product. If there are results, the CLI will output basic product information and will display a warning that the product exists but may not be visible via the GraphQL API and the user may need to manually check the product setup in Magento. In order for the CLI to make calls to the REST API, you will need to add an access token from Magento, otherwise, the CLI will throw an error.
Installation
The CLI was built with NodeJS v15.8.0 and requires NodeJS and npm to be installed on your computer. If you do not have these installed, please visit NodeJS's official site for downloads. The installer should install both NodeJS and npm.
The URL verifier can be used straight from the command line without installing using npx
:
$ npx m2-url <options...>
You can also install the URL verifier:
$ npm i -g m2-url
$ m2-url <options...>
Configuration
Before you can use the CLI, you will need to create a .env file with some environment variables. Put the .env file in the directory from which you'll run the CLI. (Example, if you put the .env in ~/.env
, you will need to cd ~
before running the CLI)
Environment base URLs
The CLI is currently set up to have exactly three environments: SANDBOX (SAND), STAGING (STG), and PRODUCTION (PROD). You must explicitly define their base URLs in a .env file with the following format:
{environment-name}_BASE_URL={environment-URL}
.
For example:
example .env
...
SANDBOX_BASE_URL=https://dev.example.com
STAGING_BASE_URL=https://staging.example.com
PRODUCTION_BASE_URL=https://example.com
...
Magento Access Token
If you don't already have a web API access token set up in the Magento Admin panel, follow the Magento Documentation for how to generate a new integration token. Once you have an integration token set up, you will want the Access Token
value and add it to your .env in the following format:
MAGENTO_ACCESS_TOKEN={access-token}
For example:
example .env
...
MAGENTO_ACCESS_TOKEN=ujWnRVZpDfcgDiFHB8wFbZjLe89WpkP2
...
Usage
Usage: $ m2-url [options]
Options:
-e, --env <env-name> Environment to run query against. Valid options: ['PROD', 'STG', 'SAND']
-s, --sku <sku> Product SKU to query
-o, --output <dir/file-name> Output path, including filename (default: "./logs/{sku}-{envName}-{timeStamp}.txt")
-v, --verify <url> URL to verify (Valid format examples:
'https://some.website.com/page.html', 'page.html', '/page.html')
--save Save results to file (default: false)