UNPKG

gatsby-source-wordpress

Version:

Source data from WordPress in an efficient and scalable way.

82 lines (80 loc) 3.04 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chalk = _interopRequireDefault(require("chalk")); var _formatLogMessage = require("../../../../utils/format-log-message"); var _store = require("../../../../store"); var _helpers = require("../../../create-schema-customization/helpers"); var _fetchGraphql = require("../../../../utils/fetch-graphql"); var _helpers2 = require("../../helpers"); var _constants = require("../../../../constants"); var _cache = require("../../../../utils/cache"); var _gatsbyFeatures = require("../../../../utils/gatsby-features"); const wpActionDELETE = async ({ helpers, wpAction }) => { const { reporter, actions, getNode } = helpers; try { const cachedNodeIds = _gatsbyFeatures.needToTouchNodes ? await (0, _cache.getPersistentCache)({ key: _constants.CREATED_NODE_IDS }) : null; // get the node ID from the WPGQL id const nodeId = wpAction.referencedNodeGlobalRelayID; const node = await getNode(nodeId); const { typeInfo } = (0, _helpers2.getQueryInfoBySingleFieldName)(wpAction.referencedNodeSingularName) || {}; if (!typeInfo) { reporter.info((0, _formatLogMessage.formatLogMessage)(`A ${wpAction.referencedNodeSingularName} was deleted, but this node type is excluded in plugin options.`)); reporter.log(``); return; } const typeSettings = (0, _helpers.getTypeSettingsByType)({ name: typeInfo.nodesTypeName }); if (typeSettings.beforeChangeNode && typeof typeSettings.beforeChangeNode === `function`) { const { additionalNodeIds } = (await typeSettings.beforeChangeNode({ actionType: `DELETE`, remoteNode: node, actions, helpers, typeInfo, fetchGraphql: _fetchGraphql.fetchGraphql, typeSettings, buildTypeName: _helpers.buildTypeName, wpStore: (0, _store.getStore)() })) || {}; if (_gatsbyFeatures.needToTouchNodes && additionalNodeIds && additionalNodeIds.length) { additionalNodeIds.forEach(id => cachedNodeIds.push(id)); } } if (node) { await actions.touchNode(node); await actions.deleteNode(node); reporter.log(``); reporter.info((0, _formatLogMessage.formatLogMessage)(`${_chalk.default.bold(`deleted ${wpAction.referencedNodeSingularName}`)} ${wpAction.title} (#${wpAction.referencedNodeID})`)); reporter.log(``); } if (_gatsbyFeatures.needToTouchNodes) { // Remove this from cached node id's so we don't try to touch it const validNodeIds = cachedNodeIds.filter(cachedId => cachedId !== nodeId); await (0, _cache.setPersistentCache)({ key: _constants.CREATED_NODE_IDS, value: validNodeIds }); } } catch (e) { Object.entries(wpAction).forEach(([key, value]) => { reporter.warn(`${key} ${JSON.stringify(value)}`); }); throw Error(e); } }; module.exports = wpActionDELETE; //# sourceMappingURL=delete.js.map