1 | const path = require('path');
|
2 | const { promisify } = require('util');
|
3 | const globCB = require('glob');
|
4 | const { watchDirectory } = require('gatsby-page-utils');
|
5 |
|
6 | const getOptions = require('../utils/get-options');
|
7 | const PageCreator = require('../utils/page-creator');
|
8 |
|
9 | const glob = promisify(globCB);
|
10 |
|
11 | module.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 | };
|