antv-l7/node_modules/gatsby-plugin-page-creator/gatsby-node.js

119 lines
4.1 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var globCB = require("glob");
var Promise = require("bluebird");
var _ = require("lodash");
var systemPath = require("path");
var existsSync = require("fs-exists-cached").sync;
var glob = Promise.promisify(globCB);
var _require = require("gatsby-page-utils"),
createPath = _require.createPath,
validatePath = _require.validatePath,
ignorePath = _require.ignorePath,
watchDirectory = _require.watchDirectory; // Path creator.
// Auto-create pages.
// algorithm is glob /pages directory for js/jsx/cjsx files *not*
// underscored. Then create url w/ our path algorithm *unless* user
// takes control of that page component in gatsby-node.
exports.createPagesStatefully = function _callee(_ref, _ref2, doneCb) {
var store, actions, reporter, pagesPath, _ref2$pathCheck, pathCheck, ignore, createPage, deletePage, program, exts, pagesDirectory, pagesGlob, files;
return _regenerator["default"].async(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
store = _ref.store, actions = _ref.actions, reporter = _ref.reporter;
pagesPath = _ref2.path, _ref2$pathCheck = _ref2.pathCheck, pathCheck = _ref2$pathCheck === void 0 ? true : _ref2$pathCheck, ignore = _ref2.ignore;
createPage = actions.createPage, deletePage = actions.deletePage;
program = store.getState().program;
exts = program.extensions.map(function (e) {
return "" + e.slice(1);
}).join(",");
if (!pagesPath) {
reporter.panic("\n \"path\" is a required option for gatsby-plugin-page-creator\n\n See docs here - https://www.gatsbyjs.org/plugins/gatsby-plugin-page-creator/\n ");
} // Validate that the path exists.
if (pathCheck && !existsSync(pagesPath)) {
reporter.panic("\n The path passed to gatsby-plugin-page-creator does not exist on your file system:\n\n " + pagesPath + "\n\n Please pick a path to an existing directory.\n ");
}
pagesDirectory = systemPath.resolve(process.cwd(), pagesPath);
pagesGlob = "**/*.{" + exts + "}"; // Get initial list of files.
_context.next = 11;
return _regenerator["default"].awrap(glob(pagesGlob, {
cwd: pagesPath
}));
case 11:
files = _context.sent;
files.forEach(function (file) {
return _createPage(file, pagesDirectory, createPage, ignore);
});
watchDirectory(pagesPath, pagesGlob, function (addedPath) {
if (!_.includes(files, addedPath)) {
_createPage(addedPath, pagesDirectory, createPage, ignore);
files.push(addedPath);
}
}, function (removedPath) {
// Delete the page for the now deleted component.
var componentPath = systemPath.join(pagesDirectory, removedPath);
store.getState().pages.forEach(function (page) {
if (page.component === componentPath) {
deletePage({
path: createPath(removedPath),
component: componentPath
});
}
});
files = files.filter(function (f) {
return f !== removedPath;
});
}).then(function () {
return doneCb();
});
case 14:
case "end":
return _context.stop();
}
}
});
};
var _createPage = function _createPage(filePath, pagesDirectory, createPage, ignore) {
// Filter out special components that shouldn't be made into
// pages.
if (!validatePath(filePath)) {
return;
} // Filter out anything matching the given ignore patterns and options
if (ignorePath(filePath, ignore)) {
return;
} // Create page object
var createdPath = createPath(filePath);
var page = {
path: createdPath,
component: systemPath.join(pagesDirectory, filePath)
}; // Add page
createPage(page);
};