forked from chocolatey/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
205 lines (176 loc) · 6.69 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
const concat = require('gulp-concat');
const cleancss = require('gulp-clean-css');
const uglify = require('gulp-uglify-es').default;
const sass = require('gulp-sass')(require('sass'));
const clean = require('gulp-clean');
const purgecss = require('gulp-purgecss');
const rename = require('gulp-rename');
const merge = require('merge-stream');
const injectstring = require('gulp-inject-string');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const util = require('gulp-util');
const bundleconfig = require('./bundleconfig.json');
const fs = require('fs');
const editFilePartial = 'Edit this file at https://github.com/chocolatey/choco-theme/partials';
const { series, parallel, src, dest } = require('gulp');
const regex = {
css: /\.css$/,
js: /\.js$/
};
const paths = {
input: 'input/',
assets: 'input/assets/',
partials: 'input/global-partials',
node_modules: 'node_modules/',
theme: 'node_modules/choco-theme/'
};
const getBundles = regexPattern => {
return bundleconfig.filter(bundle => {
return regexPattern.test(bundle.outputFileName);
});
};
const del = () => {
return src([
`${paths.assets}css`,
`${paths.assets}js`,
`${paths.assets}fonts`,
`${paths.assets}images/global-shared`,
paths.partials
], { allowEmpty: true })
.pipe(clean({ force: true }));
};
const copyTheme = () => {
const copyFontAwesome = src(`${paths.node_modules}@fortawesome/fontawesome-free/webfonts/*.*`)
.pipe(dest(`${paths.assets}fonts/fontawesome-free`));
const copyImages = src(`${paths.theme}images/global-shared/*.*`)
.pipe(dest(`${paths.assets}images/global-shared`));
const copyIcons = src(`${paths.theme}images/icons/*.*`)
.pipe(dest(paths.input));
const copyPartials = src([
`${paths.theme}partials/*.*`,
`!${paths.theme}partials/AlertText.txt`,
`!${paths.theme}partials/svgstyles.txt`,
`!${paths.theme}partials/partials/TermsLastUpdated.txt`,
`!${paths.theme}partials/TermsToc.html`,
`!${paths.theme}partials/TermsContent.html`
])
.pipe(injectstring.prepend(`@* ${editFilePartial} *@\n`))
.pipe(injectstring.replace(/topNoticeText = ""/, `topNoticeText = "${fs.readFileSync(`${paths.theme}partials/AlertText.txt`, 'utf8')}"`))
.pipe(injectstring.replace(/<input id="themeToggle" \/>/, fs.readFileSync(`${paths.theme}partials/ThemeToggle.txt`)))
.pipe(injectstring.replace(//, '')) // eslint-disable-line
.pipe(rename({ prefix: '_', extname: '.cshtml' }))
.pipe(dest(paths.partials));
const copyTermsLastUpdated = src(`${paths.theme}partials/TermsLastUpdated.txt`)
.pipe(injectstring.prepend(`<!-- ${editFilePartial} -->\n`))
.pipe(rename({ basename: 'terms-last-updated' }))
.pipe(dest(paths.partials));
const copyTermsContent = src(`${paths.theme}partials/TermsContent.html`)
.pipe(injectstring.prepend(`<!-- ${editFilePartial} -->\n`))
.pipe(rename({ basename: 'terms-content', extname: '.txt' }))
.pipe(dest(paths.partials));
const copyChocoThemeJs = src(`${paths.theme}js/**/*.js`)
.pipe(dest(`${paths.assets}js/temp`));
return merge(copyFontAwesome, copyImages, copyIcons, copyPartials, copyTermsLastUpdated, copyTermsContent, copyChocoThemeJs);
};
const compileSass = () => {
return src(`${paths.theme}scss/*.scss`)
.pipe(sass().on('error', sass.logError))
.pipe(dest(`${paths.assets}css`));
};
const compileJs = () => {
const tasks = getBundles(regex.js).map(bundle => {
const b = browserify({
entries: bundle.inputFiles,
debug: true,
transform: [babelify.configure({
presets: [
'@babel/preset-env',
['@babel/preset-react', { runtime: 'automatic' }]
],
compact: false
})]
});
return b.bundle()
.pipe(source(bundle.outputFileName))
.pipe(buffer())
.on('error', util.log)
.pipe(dest('.'));
});
return merge(tasks);
};
const compileCss = () => {
const tasks = getBundles(regex.css).map(bundle => {
return src(bundle.inputFiles, { base: '.' })
.pipe(concat(bundle.outputFileName))
.pipe(dest('.'));
});
return merge(tasks);
};
const purgeCss = () => {
return src(`${paths.assets}css/chocolatey.bundle.css`)
.pipe(purgecss({
content: [
`${paths.input}**/*.cshtml`,
`${paths.input}**/*.md`,
`${paths.assets}js/*.*`,
`${paths.theme}scss/_algolia.scss`
],
safelist: [
'::-webkit-scrollbar',
'::-webkit-scrollbar-thumb',
'link-light',
'bg-purple',
'fa-check',
'fa-triangle-exclamation',
'fa-info',
'fa-xmark',
'text-bg-warning',
'text-bg-danger',
'text-bg-success',
'text-bg-info'
]
}))
.pipe(dest(`${paths.assets}css/`));
};
const minCss = () => {
const tasks = getBundles(regex.css).map(bundle => {
return src(bundle.outputFileName, { base: '.' })
.pipe(cleancss({
level: 2,
compatibility: 'ie8'
}))
.pipe(rename({ suffix: '.min' }))
.pipe(dest('.'));
});
return merge(tasks);
};
const minJs = () => {
const tasks = getBundles(regex.js).map(bundle => {
return src(bundle.outputFileName, { base: '.' })
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('.'));
});
return merge(tasks);
};
const delEnd = () => {
return src([
`${paths.assets}css/*.css`,
`!${paths.assets}css/*.min.css`,
`${paths.assets}js/*.js`,
`!${paths.assets}js/*.min.js`,
`${paths.assets}js/temp`
], { allowEmpty: true })
.pipe(clean({ force: true }));
};
// Independent tasks
exports.del = del;
// Gulp series
exports.compileSassJs = parallel(compileSass, compileJs);
exports.minCssJs = parallel(minCss, minJs);
// Gulp default
exports.default = series(del, copyTheme, exports.compileSassJs, compileCss, purgeCss, exports.minCssJs, delEnd);