gatsby-source-tilda
Version:
Gatsby source plugin for building websites using the tilda.cc CMS as a data source
61 lines (60 loc) • 2.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadTildaAssets = void 0;
const progress_1 = __importDefault(require("progress"));
const gatsby_source_filesystem_1 = require("gatsby-source-filesystem");
const downloadTildaAssets = async (gatsbyFunctions) => {
const { default: PQueue } = await import('p-queue');
const queue = new PQueue({ concurrency: 1 });
const { actions: { createNode, touchNode, createNodeField }, createNodeId,
// store,
cache,
// getNodes,
getNode,
// reporter,
assetNodes, } = gatsbyFunctions;
const bar = new progress_1.default('Downloading Tilda Assets [:bar] :current/:total :elapsed secs :percent', {
total: assetNodes.length,
width: 30,
});
await Promise.all(assetNodes.map(async (node) => {
let fileNodeID;
const { from: url, id } = node;
const remoteDataCacheKey = `tilda-asset-${id}-${url}`;
const cacheRemoteData = await cache.get(remoteDataCacheKey);
// Avoid downloading the asset again if it's been cached
// Note: Contentful Assets do not provide useful metadata
// to compare a modified asset to a cached version?
if (cacheRemoteData) {
const cachedNode = getNode(cacheRemoteData.fileNodeID);
if (cachedNode) {
fileNodeID = cachedNode.id;
touchNode(cachedNode);
}
}
// If we don't have cached data, download the file
if (!fileNodeID) {
const fileNode = await queue.add(() => (0, gatsby_source_filesystem_1.createRemoteFileNode)({
url: url,
// store,
cache,
createNode,
createNodeId,
// reporter,
}));
if (fileNode) {
bar.tick(0, 0);
fileNodeID = fileNode.id;
await cache.set(remoteDataCacheKey, { fileNodeID: fileNode.id });
}
}
if (fileNodeID) {
createNodeField({ node, name: 'localFile', value: fileNodeID });
}
return node;
}));
};
exports.downloadTildaAssets = downloadTildaAssets;