-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
135 lines (112 loc) · 4.56 KB
/
gulpfile.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
const gulp = require('gulp');
const pug = require('gulp-pug');
const data = require('gulp-data');
const changed = require('gulp-changed');
const del = require('del');
const pkg = require('./projects/ng-lightning/package.json');
const pugSrc = [
'projects/ng-lightning/src/lib',
'src',
].map(path => `${path}/**/[^_]*.pug`);
gulp.task('pug:clean', function libCleanHtml () {
return del(pugSrc.map(path => path.replace('.pug', '.html')));
});
gulp.task('pug:watch', function libWatchdHtml() {
const watchSrc = pugSrc.map(path => path.replace('[^_]', ''));
gulp.watch(watchSrc, gulp.series('pug:compile'));
});
gulp.task('pug:compile', function libBuildHtml() {
const Prism = require('prismjs');
require('prismjs/components/')(['typescript']);
require('prismjs/components/')(['json']);
const path = require('path');
const fs = require('fs');
const _pug = require('pug');
const md = require('markdown-it')({ breaks: true });
const mdHtml = require('markdown-it')({
html: true, // Enable HTML tags in source
breaks: true, // Convert '\n' in paragraphs into <br>
});
function safe(string) {
const replaceChars = { '{': `{{ '{' }}`, '}': `{{ '}' }}` };
return string.replace(/{|}/g, function (match) { return replaceChars[match]; });
}
function highlightTS(src, language = 'typescript') {
return safe(Prism.highlight(`${src}`, Prism.languages[language]));
}
function highlightExample(filepath) {
// Typescript
const tsRaw = fs.readFileSync(`${filepath}.ts`, 'UTF-8');
const ts = highlightTS(tsRaw);
// HTML
const pugSrc = _pug.renderFile(`${filepath}.pug`, { pretty: true, doctype: 'html' });
const html = Prism.highlight(`${pugSrc}`.trim(), Prism.languages.markup);
// Readme
let readme = null;
const readmeFile = `${filepath}.md`;
if (fs.existsSync(readmeFile)) {
readme = safe(mdHtml.render(fs.readFileSync(readmeFile, 'UTF-8')));
}
return { ts, tsRaw: `${encodeURIComponent(tsRaw)}`, html, htmlRaw: `${encodeURIComponent(pugSrc)}`, readme };
}
return gulp.src(pugSrc, { base: './' })
.pipe(changed('./', { extension: '.html' }))
.pipe(data(function(file) {
// Intro
if (file.path.endsWith('get-started.component.pug')) {
const directory = path.dirname(file.path);
const docs = {};
[
{ file: 'install', lang: 'clike' },
{ file: 'usage', lang: 'typescript', safe: true },
{ file: 'styles', lang: 'json' },
{ file: 'icons', lang: 'json', safe: true },
{ file: 'config', lang: 'typescript', safe: true },
].forEach(({file, lang, safe}) => {
const src = fs.readFileSync(`${directory}/${file}.md`, 'UTF-8');
const md = src;
docs[file] = safe ? highlightTS(md, lang) : Prism.highlight(`${md}`, Prism.languages[lang]);
});
return { ...docs };
}
// Demo component
const metadataFile = path.dirname(file.path) + '/metadata.json';
if (fs.existsSync(metadataFile)) {
const dir = path.basename(path.dirname(file.path));
const metadata = require(metadataFile);
// Docs
const docsDir = path.dirname(file.path) + '/docs';
const readme = mdHtml.render(fs.readFileSync(`${docsDir}/README.md`, 'UTF-8'));
const api = md.render(fs.readFileSync(`${docsDir}/API.md`, 'UTF-8'));
const examplesDirectory = path.dirname(file.path) + '/examples';
const examples = Object.keys(metadata.examples).map((id) => {
return { id, ...highlightExample(examplesDirectory + '/' + id) };
});
const lds = 'lds' in metadata ? metadata.lds : dir;
const src = metadata.src || dir;
const title = metadata.title || dir.replace(/-/g, ' ');
return { dir, examples, metadata, readme: safe(readme), api: safe(api), title, lds, src };
}
// index.pug
if (file.path.endsWith('index.pug')) {
const getComponents = source => fs.readdirSync(source)
.filter(name => fs.lstatSync(path.join(source, name)).isDirectory());
return { components: getComponents('src/app/components').join(', ') };
}
}))
.pipe(pug({
doctype: 'html',
self: true,
pretty: true,
locals: {
now: +new Date(),
version: pkg.version,
},
}).on('error', function (err) { console.log(err); }))
.pipe(gulp.dest('./'))
});
gulp.task('prepublish', function prepublish_impl() {
return gulp.src(['*.md', 'LICENSE'])
.pipe(gulp.dest('dist/ng-lightning'));
});
gulp.task('pug', gulp.series('pug:clean', 'pug:compile'));