UNPKG

942 BJavaScriptView Raw
1const path = require('path');
2const { promisify } = require('util');
3const globCB = require('glob');
4const { watchDirectory } = require('gatsby-page-utils');
5
6const getOptions = require('../utils/get-options');
7const PageCreator = require('../utils/page-creator');
8
9const glob = promisify(globCB);
10
11module.exports = async ({ store, actions }, pluginOptions) => {
12 const { pagesPath } = getOptions(pluginOptions);
13 const { createPage, deletePage } = actions;
14
15 const pagesDirectory = path.resolve(process.cwd(), pagesPath);
16 const pagesGlob = '**/*.mdx';
17 const pageCreator = new PageCreator({ pagesDirectory, store, createPage, deletePage });
18
19 const files = await glob(pagesGlob, { cwd: pagesPath });
20 await Promise.all(files.map((file) => pageCreator.create(file)));
21
22 await watchDirectory(
23 pagesPath,
24 pagesGlob,
25 (addedPath) => pageCreator.create(addedPath),
26 (removedPath) => pageCreator.remove(removedPath)
27 );
28};