forked from seb-oss/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
91 lines (80 loc) · 3.62 KB
/
gulpfile.babel.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
'use strict';
const gulp = require('gulp');
const markdownToJSON = require('gulp-markdown-to-json');
const marked = require('marked');
const jsoncombine = require("gulp-jsoncombine");
const rename = require("gulp-rename");
const jsonFormat = require('gulp-json-format');
const log = require('fancy-log');
const output = "./dist/bootstrap/sdl";
const libraryName = "sdl-bootstrap";
marked.setOptions({
pedantic: true,
smartypants: true
});
gulp.task('build-sdl', function() {
return gulp.src(output + '/components/**/*.md')
// parse markdown file and return json
.pipe(markdownToJSON(marked, (data, file) => {
const path = file.path.replace(/\\/g, '/'); // convert windows file path to normal path
const filePathArray = path.split('/'); // get file path as an array
log(path);
delete data.body; // remove body
delete data.updatedAt; // remove updated
const relativePath = path.split('/sdl/')[1];
data.filename = filePathArray[filePathArray.length-1]; // set file name
data.filepath = relativePath; // set file path
data.shortpath = relativePath.slice(0,-3); // set short file path
data.longpath = relativePath; // set long file path
data.type = "file"; // set file type
data.guid = libraryName + '-' + data.componentid + '-' + data.variantid; // set guid
data.private = false; // set private property
return data;
}))
// rename to .json
.pipe(rename(function (path) {
path.extname = ".json"
}))
// combine files into single config file
.pipe(jsoncombine((libraryName + "-config.json"), (data, meta) => {
const componentGroups = {}; // create placeholder for configuration
// each file has it's own key in the data, loop through them using map
Object.keys(data)
.map((key, index) => {
const componentGroup = key.replace(/\\/g, '/').split('/')[0]; // get component group name
// if component group doesn't exist...
if(!componentGroups[componentGroup]) {
// ...create component group and add component (object)
componentGroups[componentGroup] = {
"title": componentGroup.charAt(0).toUpperCase() + componentGroup.slice(1), // capitalize
"shortpath": componentGroup,
"longpath": "src/components/" + componentGroup,
"items": [data[key]]
}
} else {
// push component (object) to existing group
componentGroups[componentGroup].items.push(data[key]);
}
});
const items = Object.keys(componentGroups)
.map((key) => componentGroups[key]); // return items as an array
const outputTemplate = {
"structure": [
{
"title": "Components",
"shortpath": "components",
"longpath": "src/components",
"items": items,
"filecount": 0,
"directorycount": items.length,
"type": "directory"
}
]
};
return Buffer.from(JSON.stringify(outputTemplate));
}))
.pipe(jsonFormat(4))
.pipe(gulp.dest(output));
});
gulp.task('default', function() {
});