This repository has been archived by the owner on Dec 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrun.js
170 lines (154 loc) · 5.25 KB
/
run.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
/**
* React Static Boilerplate
* https://github.com/kriasoft/react-static-boilerplate
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* eslint-disable no-console, global-require */
const webpack = require('webpack');
const path = require('path');
const appConfig = require('./config');
const contentIndex = require('./content.index');
const tasks = new Map(); // The collection of automation tasks ('clean', 'build', etc.)
function run(task, params) {
const start = new Date();
console.log(`Starting '${task}'...`);
return new Promise((resolve, reject) => {
tasks.get(task)(params).then(result => {
console.log(`Finished '${task}' after ${new Date().getTime() - start.getTime()}ms`);
resolve(result);
}, err => {
console.error(err.stack);
reject();
});
});
}
tasks.set('copy', require('./tasks/copy'));
tasks.set('clean', require('./tasks/clean'));
tasks.set('bundle', require('./tasks/bundle'));
tasks.set('scanMarkdownFiles', require('./tasks/scanMarkdownFiles'));
tasks.set('loadMarkdownAttributes', require('./tasks/loadMarkdownAttributes'));
tasks.set('createIndexHtmlForDev', require('./tasks/createIndexHtmlForDev'));
tasks.set('buildSiteIndex', require('./tasks/buildSiteIndex'));
tasks.set('replaceSiteIndex', require('./tasks/replaceSiteIndex'));
//
// Build website into a distributable format
// -----------------------------------------------------------------------------
tasks.set('build', () => {
global.DEBUG = process.argv.includes('--debug') || false;
return Promise.resolve()
.then(() => run('clean'))
.then(() => run('copy'))
.then(() => run('scanMarkdownFiles', {
src: path.join(__dirname, appConfig.contentDir),
}))
.then(files => run(
'bundle',
{
config: require('./webpack.config'),
extra: {
MarkdownFilePaths: JSON.stringify(files),
},
},
));
});
//
// Build website and launch it in a browser for testing (default)
// -----------------------------------------------------------------------------
tasks.set('default', () => {
let count = 0;
global.HMR = !process.argv.includes('--no-hmr'); // Hot Module Replacement (HMR)
return run('clean')
.then(() => run('copy'))
.then(() => run('createIndexHtmlForDev', {
title: appConfig.pageTitle,
dest: 'build/index.html',
}))
.then(() => run('scanMarkdownFiles', {
src: path.join(__dirname, appConfig.contentDir),
}))
.then((files) => new Promise(resolve => {
const bs = require('browser-sync').create();
const webpackConfig = require('./webpack.config');
const plugin = new webpack.DefinePlugin({
MarkdownFilePaths: JSON.stringify(files),
});
webpackConfig.plugins.push(plugin);
const compiler = webpack(webpackConfig);
// Node.js middleware that compiles application in watch mode with HMR support
// http://webpack.github.io/docs/webpack-dev-middleware.html
const webpackDevMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
stats: webpackConfig.stats,
});
compiler.plugin('done', () => {
// Launch Browsersync after the initial bundling is complete
// For more information visit https://browsersync.io/docs/options
if (++count === 1) {
const port = parseInt(process.env.PORT, 10) || 3000;
const uiPort = parseInt(process.env.UI_PORT, 10) || port + 1;
bs.init({
port: port,
ui: { port: uiPort },
server: {
baseDir: 'build',
middleware: [
webpackDevMiddleware,
require('webpack-hot-middleware')(compiler),
require('connect-history-api-fallback')(),
],
},
}, resolve);
}
});
}));
});
//
// Create / update site index
// -----------------------------------------------------------------------------
const getAlgoliaConfig = () => ({
applicationID: process.env.ALGOLIA_APP_ID || appConfig.algoliaApplicationID,
apiKey: process.env.ALGOLIA_ADMIN_KEY,
});
tasks.set('site-index', () =>
run('scanMarkdownFiles', {
src: path.join(__dirname, appConfig.contentDir),
})
.then((files) => run('loadMarkdownAttributes', {
files: files,
cwd: path.join(__dirname, appConfig.contentDir),
}))
.then((files) => {
const sections
= contentIndex.sections.reduce((allSections, eachSection) => {
if (!eachSection.id) {
return allSections;
}
return {
...allSections,
[eachSection.id]: eachSection,
};
}, {});
return run('buildSiteIndex', {
files,
sections,
config: {
...getAlgoliaConfig(),
baseUrl: appConfig.guideBaseUrl,
indexName: 'guides-new',
},
});
})
.then(() => run('replaceSiteIndex', {
config: {
...getAlgoliaConfig(),
srcIndex: 'guides-new',
destIndex: 'guides',
},
}))
);
// Execute the specified task or default one. E.g.: node run build
run(/^\w/.test(process.argv[2] || '') ? process.argv[2] : 'default');