-
Notifications
You must be signed in to change notification settings - Fork 281
/
Gruntfile.js
158 lines (129 loc) · 3.95 KB
/
Gruntfile.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
'use strict'
const webpack = require('webpack');
const webpackConfig = require('./webpack.config');
const del = require('del');
const libAssets = require("./build/libAssets");
const glob = require("glob");
const {marked} = require("marked");
const Handlebars = require("handlebars");
const exec = require('child_process').exec;
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
function dirFilter(dirsToFilter) {
return (path) => {
for (let dir of dirsToFilter) {
if (path.startsWith(dir + '/') || path == dir) {
return false;
}
}
return true;
}
}
let DOCS_PATTERN = '*/features/*/docs/**';
grunt.initConfig({
watch: {
docs: {
files: ['modules/workbenches/' + DOCS_PATTERN],
tasks: ['gen-docs']
}
},
copy: {
lib_assets: {
files: libAssets.map(asset => ({
expand: true,
cwd: 'node_modules',
src: asset, //path.join('node_modules', asset),
dest: `dist/lib-assets/`
}))
},
resources: {
expand: true,
cwd: 'web',
src: '**',
dest: 'dist/',
filter: dirFilter(['web/app', 'web/test'])
},
docs: {
cwd: 'modules/workbenches',
src: DOCS_PATTERN,
dest: 'web/docs',
expand: true
},
}
});
grunt.registerTask('clean', function() {
del.sync('dist');
});
grunt.registerTask('build', function() {
const done = this.async();
webpack(webpackConfig, function (error) {
if (error) {
console.log('webpack failed');
console.log(error);
}
done();
});
});
grunt.registerTask('show-revision', function() {
const done = this.async();
exec('git rev-parse --short HEAD', (err, stdout, stderr) => {
grunt.log.writeln(stdout);
done();
});
});
grunt.registerTask('mark-revision', function() {
const done = this.async();
exec('mkdir -p dist && git rev-parse HEAD > dist/.rev', function (err, stdout, stderr) {
done(err);
});
});
grunt.registerTask('default', ['clean', 'build', 'copy:resources', 'copy:lib_assets', 'gen-docs', 'mark-revision', 'show-revision']);
grunt.registerTask('gen-docs', ['copy:docs', 'process-markdown']);
grunt.registerTask('process-markdown', function() {
const done = this.async();
const mainTemplate = Handlebars.compile(grunt.file.read("modules/doc/doc-layout.handlebars"));
glob("web/docs/**/*.md", function (er, files) {
const workbenches = new Map();
files.forEach(file => {
const parts = file.split('/');
const workbenchName = parts[2];
const operationName = parts[4];
let workbench = workbenches.get(workbenchName);
if (!workbench) {
workbench = {
workbenchName,
operations: []
};
workbenches.set(workbenchName, workbench);
}
let link = file.substring(file.indexOf('/') + 1); //drop web prefix
workbench.operations.push({
operationName,
href: '../../../../../../' + convertMdPathToHtml(link)
});
});
const sidenav = Array.from(workbenches.values());
files.forEach(file => {
const content = grunt.file.read(file);
const dest = convertMdPathToHtml(file);
const htmlContent = mainTemplate({
sidenav,
content: fixLinks(marked(content))
});
grunt.file.write(dest, htmlContent);
console.log("generated "+ dest);
})
done();
});
});
grunt.registerTask('docs-start', ['gen-docs', 'watch:docs']);
};
function convertMdPathToHtml(mdPath) {
return mdPath.substring(0, mdPath.length-('.md'.length)) + '.html';
}
function fixLinks(htmlContent) {
return htmlContent.replace(/href=['"](.+)['"]/g, function(expr, link){
return 'href="' + convertMdPathToHtml(link) + '"';
});
}