forked from andresgalante/patternfly-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
231 lines (213 loc) · 9.05 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const path = require('path');
const fs = require('fs-extra');
const inflection = require('inflection');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const WebpackNotifierPlugin = require('webpack-notifier');
const glob = require('util').promisify(require('glob'));
const resolveAliases = require('./build/resolveAliases');
const COMPONENTS_PATH = path.resolve(__dirname, './src/patternfly/components');
const DEMOS_PATH = path.resolve(__dirname, './src/patternfly/demos');
const LAYOUTS_PATH = path.resolve(__dirname, './src/patternfly/layouts');
const UTILITIES_PATH = path.resolve(__dirname, './src/patternfly/utilities');
const UPGRADE_PATH = path.resolve(__dirname, './src/patternfly/upgrade-examples');
const COMPONENT_PATHS = fs.readdirSync(COMPONENTS_PATH).map(name => path.resolve(COMPONENTS_PATH, `./${name}`));
const DEMO_PATH = fs.readdirSync(DEMOS_PATH).map(name => path.resolve(DEMOS_PATH, `./${name}`));
const LAYOUT_PATHS = fs.readdirSync(LAYOUTS_PATH).map(name => path.resolve(LAYOUTS_PATH, `./${name}`));
const UTILITIES_PATHS = fs.readdirSync(UTILITIES_PATH).map(name => path.resolve(UTILITIES_PATH, `./${name}`));
const UPGRADES_PATHS = fs.readdirSync(UPGRADE_PATH).map(name => path.resolve(UPGRADE_PATH, `./${name}`));
exports.onCreateNode = ({ node, boundActionCreators }) => {
const { createNodeField } = boundActionCreators;
const PAGES_BASE_DIR = path.resolve(__dirname, './src/site/pages');
const COMPONENTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/components');
const DEMOS_BASE_DIR = path.resolve(__dirname, './src/patternfly/demos');
const LAYOUTS_BASE_DIR = path.resolve(__dirname, './src/patternfly/layouts');
const UTILITIES_BASE_DIR = path.resolve(__dirname, './src/patternfly/utilities');
const UPGRADES_BASE_DIR = path.resolve(__dirname, './src/patternfly/upgrade-examples');
const isMarkdown = node.internal.type === 'MarkdownRemark';
if (isMarkdown) {
const isPage = node.fileAbsolutePath.includes(PAGES_BASE_DIR);
const isComponent = node.fileAbsolutePath.includes(COMPONENTS_BASE_DIR);
const isLayout = node.fileAbsolutePath.includes(LAYOUTS_BASE_DIR);
const isDemo = node.fileAbsolutePath.includes(DEMOS_BASE_DIR);
const isUtility = node.fileAbsolutePath.includes(UTILITIES_BASE_DIR);
const isUpgrade = node.fileAbsolutePath.includes(UPGRADES_BASE_DIR);
if (isPage) {
const relativePath = path.relative(PAGES_BASE_DIR, node.fileAbsolutePath);
const pagePath = `/${relativePath}`.replace(/\.md$/, '');
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'page' });
createNodeField({ node, name: 'contentType', value: 'page' });
} else if (isComponent) {
const componentName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/components/${componentName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'component' });
} else if (isLayout) {
const layoutName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/layouts/${layoutName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'layout' });
} else if (isDemo) {
const demoName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/demos/${demoName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'demo' });
} else if (isUtility) {
const utilityName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/utilities/${utilityName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'utility' });
} else if (isUpgrade) {
const upgradeName = path.basename(path.dirname(node.fileAbsolutePath));
const pagePath = `/upgrades/${upgradeName}/docs`;
createNodeField({ node, name: 'path', value: pagePath });
createNodeField({ node, name: 'type', value: 'documentation' });
createNodeField({ node, name: 'contentType', value: 'upgrade' });
}
}
};
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
type
path
contentType
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors);
}
return result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.path,
component: path.resolve(__dirname, `./src/site/templates/${node.fields.type}.js`),
layout: 'index',
context: {
pagePath: node.fields.path,
type: node.fields.type,
contentType: node.fields.contentType
}
});
});
});
};
exports.createLayouts = ({ graphql, store, boundActionCreators: { createLayout, deleteLayout } }) =>
glob(path.resolve(__dirname, 'src/site/layouts/**.js')).then(matches => {
matches.forEach(layoutFilePath => {
const id = path.parse(layoutFilePath).name;
deleteLayout(id);
createLayout({
id,
component: layoutFilePath
});
});
});
exports.onCreatePage = async ({ page, boundActionCreators }) => {
const { createPage } = boundActionCreators;
const CATEGORY_PAGE_REGEX = /^\/(components|layouts|demos|utilities)\/$/;
const CATEGORY_CHILD_PAGE_REGEX = /^\/(components|layouts|demos|utilities)\/([A-Za-z0-9_-]+)/;
const UPGRADES_PAGE_REGEX = /^\/(upgrade-examples)\/([A-Za-z0-9_-]+)/;
return new Promise((resolve, reject) => {
const isCategoryPage = page.path.match(CATEGORY_PAGE_REGEX);
const isCategoryChildPage = page.path.match(CATEGORY_CHILD_PAGE_REGEX);
const isUpgradePage = page.path.match(UPGRADES_PAGE_REGEX);
page.context.type = 'page';
page.context.category = 'page';
page.context.slug = '';
page.context.name = '';
page.context.title = '';
page.layout = 'index';
if (isCategoryPage) {
page.context.type = 'category';
page.context.category = page.path.match(CATEGORY_PAGE_REGEX)[1];
} else if (isCategoryChildPage) {
const pageCategory = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[1];
const pageSlug = page.path.match(CATEGORY_CHILD_PAGE_REGEX)[2];
const pageName = pageSlug.replace('-', ' ');
const pageTitle = inflection.titleize(pageName);
page.context.type = inflection.singularize(pageCategory);
page.context.category = pageCategory;
page.context.slug = pageSlug;
page.context.name = pageName;
page.context.title = pageTitle;
} else if (isUpgradePage) {
const pageCategory = 'upgrade';
const pageSlug = page.path.match(UPGRADES_PAGE_REGEX)[2];
const pageName = pageSlug.replace('-', ' ');
const pageTitle = inflection.titleize(pageName);
page.context.type = inflection.singularize(pageCategory);
page.context.category = pageCategory;
page.context.slug = pageSlug;
page.context.name = pageName;
page.context.title = pageTitle;
page.layout = 'upgrade';
}
createPage(page);
if (isCategoryChildPage || isUpgradePage) {
// create full demo page for each component
const demoPage = Object.assign({}, page);
demoPage.layout = 'demo';
const nodePath = demoPage.path;
demoPage.path = `${nodePath.substr(0, nodePath.length - 1)}-full/`;
createPage(demoPage);
}
resolve();
});
};
exports.modifyWebpackConfig = ({ config, stage }) => {
config.loader('markdown-loader', current => {
current.test = /\.md$/;
current.loader = 'html-loader!markdown-loader';
return current;
});
config.loader('html-loader', current => {
current.test = /\.html$/;
current.loader = 'html-loader';
return current;
});
config.loader('handlebars-loader', current => {
current.test = /\.hbs$/;
current.loader = 'handlebars-loader';
current.query = {
partialDirs: COMPONENT_PATHS.concat(LAYOUT_PATHS)
.concat(DEMO_PATH)
.concat(UTILITIES_PATHS)
.concat(UPGRADES_PATHS),
helperDirs: path.resolve(__dirname, './build/helpers')
};
return current;
});
config.merge({
resolve: {
alias: resolveAliases
},
plugins: [
new StyleLintPlugin({
configFile: './.stylelintrc',
fix: false,
failOnError: false,
emitErrors: true,
files: 'src/**/*.scss',
defaultSeverity: 'error'
}),
new WebpackNotifierPlugin({
title: 'PF-Next',
skipFirstNotification: true
})
]
});
return config;
};