-
Notifications
You must be signed in to change notification settings - Fork 41
/
dev-server.js
158 lines (133 loc) · 4.37 KB
/
dev-server.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
require('dotenv').config();
// Set Debugging up
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
if (!process.env.DEBUG) {
process.env.DEBUG = 'docs-server*,*:info,*:error';
}
// check for no-clean switch
const no_clean = !!process.argv.find((val) => val === '--no-clean');
// Spawns a dev server with Metalsmith & Webpack live reloading via Browsersync
const debug = require('debug')('docs-server');
// To mitigate a Windows specific issue
if (!debug.extend) {
debug.extend = (ns) => (msg) => {
debug(ns, msg);
};
}
const error = debug.extend('error');
const info = debug.extend('info');
info('no-clean', no_clean);
const browser_sync = require('browser-sync');
const strip_ansi = require('strip-ansi');
const thenify = require('thenify');
const exec = thenify(require('child_process').exec);
const fs = require('fs');
const path = require('path');
const yaml_config = require('node-yaml-config');
const co = require('co');
const extend = require('lodash.defaultsdeep');
const { pre_builder, builder } = require('nuxeo-docs-builder');
// Working copy
const target_repo_path = path.join(__dirname, 'src');
const source_repo_path = __dirname;
const target_repo_site = path.join(__dirname, 'site');
info('target_repo_src: %s', target_repo_path);
const instance_config = yaml_config.load(path.join(__dirname, 'config.yml'));
const branch = instance_config && instance_config.site && instance_config.site.branch;
const repo_id = instance_config && instance_config.site && instance_config.site.repo_id;
const dev_browser_path = (instance_config && instance_config.site && instance_config.site.dev_browser_path) || '';
info(`dev_browser_path: ${dev_browser_path}`);
if (!branch) {
throw new Error('Could not get current branch from `config.yml` site.branch');
}
if (!repo_id) {
throw new Error('Could not get Repo ID from `config.yml` site.repo_id');
}
// Get 404 info page
const content_404 = fs.readFileSync(path.join(__dirname, '404.html'));
// Initialize Browsersync and webpack
const sync = browser_sync.create();
const get_filter = (file) => {
const file_relative = path.relative(__dirname, file);
debug(`File relative: ${file_relative}`);
const first_dir = file_relative.split('/').shift();
if (first_dir === 'src') {
return file_relative.split('/').slice(1).join('/');
} else {
return '';
}
};
// Run metalsmith and reload browsers
// or send a fullscreen error message to the browser instead
const build_docs = () => {
info('Building Docs');
co(function* () {
// Pre-build
const pre_build = [pre_builder({ branch, repo_id, source_path: target_repo_path })];
const metadata = {};
const pre_build_result = yield pre_build;
pre_build_result.forEach((data) => extend(metadata, data));
// Build
yield builder(target_repo_path, metadata, target_repo_site, {
branch,
repo_id,
repo_path: source_repo_path,
});
yield exec('npm run copy_assets', { encoding: 'utf8', cwd: __dirname });
info('Docs build successfully finished! Reloading browsers.');
sync.reload();
}).catch((err) => {
error('Docs build error');
error(err);
return sync.sockets.emit('fullscreen:message', {
title: 'Docs Build Error:',
body: strip_ansi(`${err.message}\n\n${err.stack}`),
timeout: 100000,
});
});
};
// Run full build to start
if (!no_clean) {
build_docs();
}
// Run Browsersync for server and watching
// Use webpack dev middleware for Hot Module Replacement
// Apply custom chokidar function to rebuild metalsmith when files changed
sync.init(
{
server: path.join(__dirname, 'site'),
startPath: dev_browser_path,
open: true,
logFileChanges: true,
plugins: ['bs-fullscreen-message'],
files: [
{
match: [
path.join(__dirname, 'src', '**', '*'),
path.join(__dirname, 'assets', '**', '*'),
path.join(__dirname, 'config.yml'),
],
fn: (event, file) => {
debug(`File changed: ${file}`);
if (!file.startsWith('assets/nx_assets')) {
process.env.FILTER = get_filter(file);
build_docs();
}
},
},
],
},
(err, bs) => {
if (err) {
error(err);
}
bs.addMiddleware('*', (req, res) => {
// res.writeHead(302, {
// "location": "404.html"
// });
// res.end("Redirecting!");
res.write(content_404);
res.end();
});
}
);