Created
October 18, 2020 15:49
-
-
Save LuigiClaudio/cd15afdf64460cc021d28cd0b41577dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
exports.onPostBuild = async ({ graphql }) => { | |
await graphql(` | |
{ | |
activeAds: allStoreProducts(filter: { cms: { isAdvertised: { eq: true } } }) { | |
nodes { | |
productId | |
cms { | |
title | |
collectionTitle | |
shortDescription | |
discount | |
slug | |
} | |
cloudinary { | |
secure_url | |
} | |
} | |
} | |
storeDetails: markdownRemark(fileAbsolutePath: { regex: "/siteLaunch/" }) { | |
frontmatter { | |
siteUrl | |
siteName | |
title | |
} | |
} | |
} | |
`).then((results) => { | |
const adsPath = './public/activeAds'; | |
const adsList = results.data.activeAds.nodes.map((item) => item); | |
const storeDetails = results.data.storeDetails.frontmatter; | |
if (!fs.existsSync(adsPath)) { | |
fs.mkdirSync(adsPath); | |
} | |
const adsItems = []; | |
adsList.forEach((ad) => { | |
const { cms, cloudinary } = ad; | |
adsItems.push({ | |
title: cms.title, | |
collectionTitle: cms.collectionTitle, | |
shortDescription: cms.shortDescription, | |
image: cloudinary[0].secure_url, | |
discount: cms.discount, | |
url: `${storeDetails.siteUrl}${cms.slug}`, | |
}); | |
}); | |
const adsData = { | |
...storeDetails, | |
adsItems, | |
}; | |
fs.writeFileSync(`${adsPath}/ads.json`, JSON.stringify(adsData)); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment