UNPKG

gatsby-plugin-stork

Version:

A Gatsby plugin for generating Stork search indexes.

172 lines (140 loc) 6.58 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _require = require("child_process"), execSync = _require.execSync; var fs = require("fs").promises; var tmp = require("tmp"); var pathUtil = require("path"); var TOML = require("@iarna/toml"); var _require2 = require("./defaults"), DEFAULTS = _require2.DEFAULTS, DEFAULT_OUTPUT_FILE_NAME = _require2.DEFAULT_OUTPUT_FILE_NAME; var _require3 = require("./schema"), createSchema = _require3.createSchema; exports.pluginOptionsSchema = function (_ref) { var Joi = _ref.Joi; return createSchema(Joi); }; exports.onPostBootstrap = /*#__PURE__*/function () { var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(_ref2, pluginOptions) { var getNodes, _DEFAULTS$pluginOptio, indexes, nodes; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: getNodes = _ref2.getNodes; assertStorkIsInstalled(); _DEFAULTS$pluginOptio = (0, _extends2.default)({}, DEFAULTS, pluginOptions), indexes = _DEFAULTS$pluginOptio.indexes; nodes = getNodes(); _context2.next = 6; return Promise.all(indexes.map( /*#__PURE__*/function () { var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref4) { var _ref4$filename, filename, resolvers, files, _tmp$fileSync, tempFileName, removeCallback, testForInvalidFile, invalidFiles, outputObject, tomlString; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _ref4$filename = _ref4.filename, filename = _ref4$filename === void 0 ? DEFAULT_OUTPUT_FILE_NAME : _ref4$filename, resolvers = _ref4.resolvers; files = []; nodes.forEach(function (node) { var resolver = resolvers[node.internal.type]; if (!resolver) return; var resolvedValues = {}; Object.entries(resolver).forEach(function (_ref6) { var key = _ref6[0], resolveFunc = _ref6[1]; resolvedValues[key] = resolveFunc(node); }); files.push(resolvedValues); }); // Serialize and write to TOML _tmp$fileSync = tmp.fileSync({ postfix: ".toml" }), tempFileName = _tmp$fileSync.name, removeCallback = _tmp$fileSync.removeCallback; // Throw if any files are invalid // N.B. either path or contents is valid testForInvalidFile = function testForInvalidFile(_ref7) { var path = _ref7.path, contents = _ref7.contents, url = _ref7.url, title = _ref7.title; return !(path || contents) || !url || !title; }; if (!files.some(testForInvalidFile)) { _context.next = 10; break; } invalidFiles = files.filter(testForInvalidFile); console.error("The following node inputs were missing one or more of the required fields (path/contents, url, and title):"); invalidFiles.forEach(console.error); throw new Error("Could not generate index from invalid files"); case 10: console.log("Writing temporary TOML to " + tempFileName); outputObject = { input: { base_directory: __dirname, files: files }, output: { filename: pathUtil.join("public", filename) } }; tomlString = TOML.stringify(outputObject); _context.next = 15; return fs.writeFile(tempFileName, tomlString); case 15: buildStorkIndex(tempFileName, tomlString); // Clean up temp file removeCallback(); case 17: case "end": return _context.stop(); } } }, _callee); })); return function (_x3) { return _ref5.apply(this, arguments); }; }())); case 6: case "end": return _context2.stop(); } } }, _callee2); })); return function (_x, _x2) { return _ref3.apply(this, arguments); }; }(); /** * Checks that stork is installed on the host machine. * @throws if Stork is not present. */ function assertStorkIsInstalled() { if (process.env.GATSBY_STORK_EXECUTABLE_PATH) { // if the user provides an executable path, we trust // that it's properly installed there return; } // Check if Stork is present try { execSync("which -s stork"); // `-s` omits output and just returns a 0 or 1 exit code } catch (e) { console.error("It looks like the Stork executable is not installed. For some instructions on how to install Stork, see the documentation: https://stork-search.net/docs/install"); throw e; } } /** * Builds stork index. * @throws if Stork returns a non-zero exit code. */ function buildStorkIndex(tempFileName, tomlString) { try { execSync((process.env.GATSBY_STORK_EXECUTABLE_PATH || "stork") + " --build " + tempFileName); } catch (e) { console.error("Could not generate index for generated TOML file:"); console.error(tomlString); throw e; } }