Skip to content

Instantly share code, notes, and snippets.

@LuigiClaudio
Created October 18, 2020 15:49
Show Gist options
  • Save LuigiClaudio/cd15afdf64460cc021d28cd0b41577dd to your computer and use it in GitHub Desktop.
Save LuigiClaudio/cd15afdf64460cc021d28cd0b41577dd to your computer and use it in GitHub Desktop.
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