Skip to content

Commit

Permalink
Merge pull request #12 from ItsEthanH/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
dleatherman authored Apr 19, 2023
2 parents e8409d4 + b5457c2 commit ef4ba24
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 43 deletions.
24 changes: 13 additions & 11 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ const getAllCollections = require('./src/collections');
const getAllPages = require('./src/pages');
const getAllArticles = require('./src/articles');

const config = require('./config')
const defaultConfig = require("./config");

const getShopifyContent = async (config) => {
// CHECK CONFIG VALIDATION
// defaultConfig is a standard config file with the default queries assigned
// anything that the user changes overwrites the default config
// the result from this overwriting is the below shopifyConfig variable
const shopifyConfig = Object.assign(defaultConfig, config);

console.log(chalk.yellow.bold(`SHOPIFY:GETTING SHOP INFO`))
console.log(chalk.yellow.bold(`SHOPIFY:GETTING PRODUCTS`))
console.log(chalk.yellow.bold(`SHOPIFY:GETTING COLLECTIONS`))
console.log(chalk.yellow.bold(`SHOPIFY:GETTING PAGES`))
console.log(chalk.yellow.bold(`SHOPIFY:GETTING ARTICLES`))

const shop = await getShopInfo()
const products = await getAllProducts()
const collections = await getAllCollections()
const pages = await getAllPages()
const articles = await getAllArticles()
const shop = await getShopInfo(shopifyConfig.shopQuery);
const products = await getAllProducts(shopifyConfig.productsQuery);
const collections = await getAllCollections(shopifyConfig.collectionsQuery);
const pages = await getAllPages(shopifyConfig.pagesQuery);
const articles = await getAllArticles(shopifyConfig.articlesQuery);

console.log(chalk.greenBright.bold(`SHOPIFY:SUCCESSFULLY RETRIEVED ${shop.name.toUpperCase()} INFO`))
console.log(chalk.greenBright.bold(`SHOPIFY:SUCCESSFULLY RETRIEVED ${products.length} PRODUCT${products.length > 1 || products.length == 0 ? 'S' : ''}`))
Expand Down Expand Up @@ -55,10 +58,9 @@ const getShopifyContent = async (config) => {
};
};

module.exports = (
eleventyConfig
) => {
module.exports = (eleventyConfig, pluginConfig) => {
eleventyConfig.addGlobalData(
"shopify", async () => await getShopifyContent(config ? config : {})
"shopify",
async () => await getShopifyContent(pluginConfig || {})
);
};
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ _Note: This plugin currently uses version 1.0.1-canary.3_

require("dotenv").config();

const { SHOPIFY_STORE_URL, SHOPIFY_ACCESS_TOKEN, SHOPIFY_API_VERSION } = process.env;
const { SHOPIFY_STORE_URL, SHOPIFY_ACCESS_TOKEN, SHOPIFY_API_VERSION } =
process.env;

module.exports = (eleventyConfig) => {
eleventyConfig.addPlugin(pluginShopify, {
Expand Down Expand Up @@ -67,19 +68,34 @@ _Note: This plugin currently uses version 1.0.1-canary.3_

2. Amend the `.eleventy.js` file within `demo` so it points to the source code in the parent directory:

#### When developing locally

```js
const pluginShopify = require("../");
// const pluginShopify = require("eleventy-plugin-shopify");
```

#### When using npm file

```js
// const pluginShopify = require("../");
const pluginShopify = require("eleventy-plugin-shopify");
```

3. Install the demo dependencies:
3. Install development dependencies (in root):

```text
npm install
```

4. Install the demo dependencies (in ./demo):

```text
cd demo
npm install
```

4. Run the demo locally:
5. Run the demo locally:
```text
npm run dev
```
Expand Down
13 changes: 12 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const productsQuery = require("./queries").productsQuery;
const collectionsQuery = require("./queries").collectionsQuery;
const pagesQuery = require("./queries").pagesQuery;
const articlesQuery = require("./queries").articlesQuery;
const shopQuery = require("./queries").shopQuery;

require("dotenv").config();

const { SHOPIFY_STORE_URL, SHOPIFY_ACCESS_TOKEN, SHOPIFY_API_VERSION } = process.env;
Expand All @@ -11,6 +17,11 @@ const config = {
'X-Shopify-Storefront-Access-Token': SHOPIFY_ACCESS_TOKEN,
'Content-Type': 'application/graphql'
},
}
shopQuery,
productsQuery,
collectionsQuery,
pagesQuery,
articlesQuery,
};

module.exports = config;
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"build": "eleventy"
},
"dependencies": {
"@11ty/eleventy": "^1.0.1-canary.3",
"@11ty/eleventy": "^2.0.0",
"dotenv": "^10.0.0",
"eleventy-plugin-shopify": "^0.0.5"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"node-fetch-cache": "^3.0.3"
},
"devDependencies": {
"@11ty/eleventy": "^1.0.1-canary.3",
"@11ty/eleventy": "^2.0.0",
"dotenv": "^10.0.0"
}
}
14 changes: 7 additions & 7 deletions src/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fetch = require('node-fetch-cache')
const config = require('../config')
const { articlesQuery } = require('../config/queries')

const allArticles = []
let allArticles = [];

async function getArticles(query = articlesQuery, cursor = null, previousArticles = []) {
if (previousArticles.length > 0) {
allArticles.push(...previousArticles)
allArticles = [...previousArticles];
}
const response = await fetch(config.endpoint, {
method: 'post',
Expand All @@ -23,11 +23,11 @@ async function getArticles(query = articlesQuery, cursor = null, previousArticle
}
}

async function getAllArticles() {
const articles = await getArticles()
return allArticles.map(article => {
return article.node
})
async function getAllArticles(query) {
const articles = await getArticles(query);
return allArticles.map((article) => {
return article.node;
});
}

module.exports = getAllArticles
10 changes: 5 additions & 5 deletions src/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fetch = require('node-fetch-cache')
const config = require('../config')
const { collectionsQuery } = require('../config/queries')

const allCollections = []
let allCollections = [];

async function getCollections(query = collectionsQuery, cursor = null, previousCollections = []) {
if (previousCollections.length > 0) {
allCollections.push(...previousCollections)
allCollections = [...previousCollections];
}
const response = await fetch(config.endpoint, {
method: 'post',
Expand All @@ -23,9 +23,9 @@ async function getCollections(query = collectionsQuery, cursor = null, previousC
}
}

async function getAllCollections() {
let collections = await getCollections()
return allCollections.map(collection => {
async function getAllCollections(query) {
let collections = await getCollections(query);
return allCollections.map((collection) => {
// collection.node.products = collection.node.products.edges.map(product => {
// return product.node
// })
Expand Down
14 changes: 7 additions & 7 deletions src/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fetch = require('node-fetch-cache')
const config = require('../config')
const { pagesQuery } = require('../config/queries')

const allPages = []
let allPages = [];

async function getPages(query = pagesQuery, cursor = null, previousPages = []) {
if (previousPages.length > 0) {
allPages.push(...previousPages)
allPages = [...previousPages];
}
const response = await fetch(config.endpoint, {
method: 'post',
Expand All @@ -23,11 +23,11 @@ async function getPages(query = pagesQuery, cursor = null, previousPages = []) {
}
}

async function getAllPages() {
let pages = await getPages()
return allPages.map(page => {
return page.node
})
async function getAllPages(query) {
let pages = await getPages(query);
return allPages.map((page) => {
return page.node;
});
}

module.exports = getAllPages
14 changes: 7 additions & 7 deletions src/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const fetch = require('node-fetch-cache')
const config = require('../config')
const { productsQuery } = require('../config/queries')

const allProducts = []
let allProducts = [];

async function getProducts(query = productsQuery, cursor = null, previousProducts = []) {
if (previousProducts.length > 0) {
allProducts.push(...previousProducts)
allProducts = [...previousProducts];
}
const response = await fetch(config.endpoint, {
method: 'post',
Expand All @@ -23,11 +23,11 @@ async function getProducts(query = productsQuery, cursor = null, previousProduct
}
}

async function getAllProducts() {
const products = await getProducts()
return allProducts.map(product => {
return product.node
})
async function getAllProducts(query) {
const products = await getProducts(query);
return allProducts.map((product) => {
return product.node;
});
}

module.exports = getAllProducts

0 comments on commit ef4ba24

Please sign in to comment.