forked from NatLibFi/finna-ui-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
365 lines (285 loc) · 9.76 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require('dotenv').config();
const config = require('./patternlab-config.json');
const helpers = require('./gulp-helpers');
const gulp = require('gulp');
const less = require('gulp-less');
const shell = require('gulp-shell');
const browserSync = require('browser-sync');
const fs = require('fs');
const chalk = require('chalk');
const path = require('path');
const autoprefixer = require('gulp-autoprefixer');
const minify = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const inject = require('gulp-inject');
const replace = require('gulp-replace');
const themesRootPath = path.resolve(process.env.THEMES_ROOT);
const themeDirectoryPath = path.resolve(process.env.THEME_DIRECTORY);
const componentsSourcePath = path.resolve(config.paths.source.patterns);
// Tasks
const cleanPublic = () => helpers.cleanDir(config.paths.public.root);
gulp.task(cleanPublic);
const patternLab = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell(['patternlab build --config ./patternlab-config.json']))
.pipe(browserSync.stream());
};
gulp.task(patternLab);
const fonts = () => {
const bootstrapFonts = `${themesRootPath}/bootstrap3/css/fonts`;
const finnaFonts = `${themeDirectoryPath}/css/fonts`;
const dest = config.paths.public.fonts;
return gulp
.src([`${bootstrapFonts}/*`, `${finnaFonts}/*`])
.pipe(gulp.dest(dest));
};
gulp.task(fonts);
const styles = () => {
const source = config.paths.source.styles;
const dest = config.paths.public.styles;
return gulp
.src(`${source}/*.less`)
.pipe(less({
modifyVars: {
'@themePath': themesRootPath
}
}))
.pipe(autoprefixer())
.pipe(replace('../../../themes/finna2/css/fonts', '../fonts'))
.pipe(minify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(dest))
.pipe(browserSync.stream());
};
gulp.task(styles);
const scripts = () => {
const source = config.paths.source.root;
const dest = config.paths.public.js;
return gulp
.src([
`${source}/js/finna.js`,
`!${source}/js/vendor/*.js`,
`${source}/components/**/*.js`,
`${source}/js/patternlab/*.js`,
])
.pipe(concat('main.js'))
.pipe(gulp.dest(dest))
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(dest))
.pipe(browserSync.stream());
};
gulp.task(scripts);
const vendorScripts = () => {
const vendor = `${config.paths.source.js}/vendor`;
const dest = `${config.paths.public.js}/vendor`;
return gulp
.src(`${vendor}/*.js`)
.pipe(gulp.dest(dest))
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(dest))
.pipe(browserSync.stream());
};
gulp.task(vendorScripts);
const watchTask = () => {
browserSync.init({
server: {
baseDir: config.paths.public.root
},
ghostMode: true,
open: 'external',
snippetOptions: {
blacklist: ['/index.html', '/']
}
});
gulp.watch(`${config.paths.source.styles}/**/*.less`, styles);
gulp.watch(`${config.paths.source.patterns}**/*.less`, styles);
gulp.watch(`${config.paths.source.js}/**/*.js`, scripts);
gulp.watch(`${config.paths.source.patterns}**/*.js`, scripts);
gulp.watch(`${config.paths.source.patterns}**/*.phtml`, patternLab);
gulp.watch(`${config.paths.source.patterns}**/*.json`, patternLab);
};
gulp.task(watchTask);
const validateImportTargetFile = (file) => {
return fs.readFile(file, (error, data) => {
if (error) {
throw error;
}
if (data.indexOf('Component imports start here') == -1 && data.indexOf('Component imports end here') == -1) {
const errorMessage = `Not able to import to target file: ${file}. Make sure that file has required starting comment /* Component imports start here */ and ending comment /* Component imports end here */`;
console.log(chalk.red(errorMessage));
throw Error(errorMessage);
}
});
};
const checkImportTargetFile = async (file) => new Promise((resolve, reject) => {
return fs.access(file, (error) => {
if (error) {
console.log(chalk.yellow(`${file} does not exist. Trying to create.`));
const componentsFileContent = '/* Component imports start here */ \r\n/* Component imports end here */';
return fs.writeFile(file, componentsFileContent, (err) => {
if (err) {
reject(err);
}
console.log(chalk.green(`${file} created successfully. Proceeding..`));
resolve(validateImportTargetFile(file));
});
} else {
resolve(validateImportTargetFile(file));
}
});
});
;
const componentImports = async () => {
const less = `${themeDirectoryPath}/less`;
try {
await checkImportTargetFile(`${less}/components.less`);
return gulp.src(`${less}/components.less`)
.pipe(inject(
gulp.src(`${less}/components/**/*.less`, { read: false }),
{
starttag: '/* Component imports start here */',
endtag: '/* Component imports end here */',
addRootSlash: false,
transform: (filepath) => {
const componentPath = filepath.split('/less/')[1];
return `@import "${componentPath}";`
}
}))
.pipe(gulp.dest(less));
}
catch (err) {
throw err;
}
};
gulp.task(componentImports);
const unlinkPatterns = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([`rm -rf ${themeDirectoryPath}/templates/components`]))
};
gulp.task(unlinkPatterns);
const unlinkStyles = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([`rm -rf ${themeDirectoryPath}/less/components`]))
};
gulp.task(unlinkStyles);
const unlinkScripts = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([`rm -rf ${themeDirectoryPath}/js/components`]))
};
gulp.task(unlinkScripts);
const unlinkTheme = gulp.series(unlinkPatterns, unlinkStyles, unlinkScripts);
const symLinkPatterns = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([
`cd ${themeDirectoryPath}/templates && ln -fs ${componentsSourcePath}`
]));
};
gulp.task(symLinkPatterns);
const symLinkStyles = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([`cd ${themeDirectoryPath}/less && ln -fs ${componentsSourcePath}`]));
};
gulp.task(symLinkStyles);
const symLinkScripts = () => {
return gulp
.src('.', { allowEmpty: true })
.pipe(shell([`cd ${themeDirectoryPath}/js && ln -fs ${componentsSourcePath}`]));
};
gulp.task(symLinkScripts);
const shouldRemoveComponents = async (callback) => {
const shouldRemove = await helpers.checkForComponents();
if (shouldRemove) {
return unlinkTheme();
}
return callback();
};
gulp.task(shouldRemoveComponents);
const symLinkTheme = gulp.series(
shouldRemoveComponents,
symLinkPatterns,
symLinkStyles,
symLinkScripts,
componentImports
);
const copyPatterns = () => {
const source = config.paths.source.patterns;
return gulp
.src(`${source}**/*.phtml`)
.pipe(gulp.dest(`${themeDirectoryPath}/templates/components`));
};
gulp.task(copyPatterns);
const copyStyles = () => {
const source = config.paths.source.patterns;
return gulp
.src(`${source}**/*.less`)
.pipe(gulp.dest(`${themeDirectoryPath}/less/components`));
};
gulp.task(copyStyles);
const copyScripts = () => {
const source = config.paths.source.patterns;
return gulp
.src(`${source}**/*.js`)
.pipe(gulp.dest(`${themeDirectoryPath}/js/components`));
};
gulp.task(copyScripts);
const shouldUnlinkTheme = async (callback) => {
const shouldUnlink = await helpers.checkForSymlinks();
if (shouldUnlink) {
return unlinkTheme();
}
return callback();
};
gulp.task(shouldUnlinkTheme);
const copyTheme = gulp.series(
shouldUnlinkTheme,
copyPatterns,
copyStyles,
copyScripts,
componentImports
);
const defaultTask = gulp.series(
cleanPublic,
gulp.parallel(patternLab, fonts, styles, scripts, vendorScripts)
);
const watch = gulp.series(defaultTask, watchTask);
// Descriptions
cleanPublic.description = "Clear all files under public directory";
patternLab.description = "Build PatternLab to public directory";
fonts.description = "Copy font files from theme directory";
styles.description = "Convert and minify Less to CSS";
scripts.description = "Build and uglify Javascript into main.js";
vendorScripts.description = "Build and uglify vendor Javascript";
watchTask.description = "Initialize BrowserSync instance and watch for changes";
componentImports.description = "Inject component imports to dedicated files";
shouldUnlinkTheme.description = "Ask if linked components should be unlinked";
symLinkPatterns.description = "Create patterns symbolic link";
symLinkStyles.description = "Create styles symbolic link";
symLinkScripts.description = "Create scripts symbolic link";
unlinkPatterns.description = "Remove symbolic link from working patterns";
unlinkStyles.description = "Remove symbolic link from working styles";
unlinkScripts.description = "Remove symbolic link from working scripts";
shouldRemoveComponents.description = "Ask if existing components in theme directory should be removed";
copyPatterns.description = "Create patterns copy";
copyStyles.description = "Create styles copy";
copyScripts.description = "Create Javascript scripts copy";
defaultTask.description = "Clear public directory and build patterns, CSS and Javascript from the source directory";
watch.description = 'Build PatternLab from source files and watch for changes.';
symLinkTheme.description = "Create symbolic links to working theme";
unlinkTheme.description = "Unlink/remove components from working theme";
copyTheme.description = "Copy components to working theme";
// Exports
exports.default = defaultTask;
exports.watch = watch;
exports.symLinkTheme = symLinkTheme;
exports.copyTheme = copyTheme;
exports.unlinkTheme = unlinkTheme;