/
watch.js
55 lines (42 loc) · 1.59 KB
/
watch.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
import gulp from 'gulp';
import ThemeRegistry from '../utils/theme-registry';
import config from '../config';
import { initSync } from '../utils/sync';
import path from 'path';
import gutil from 'gulp-util';
import chalk from 'chalk';
function runThemeTask(task, file) {
const themeRegistry = new ThemeRegistry();
const theme = themeRegistry.getThemeKeyByFile(file);
if (!theme) {
gutil.log(chalk.red(`Theme task not found for this file!`));
}
if (gulp.tasks[`${task}:${theme}`]) {
gulp.start(`${task}:${theme}`);
return;
}
gutil.log(chalk.red(`Task ${task}:${theme} not found!`));
}
function relativizePath(absolutePath) {
return path.relative(process.cwd(), absolutePath);
}
export default function (done, theme) {
const pubPath = `${config.projectPath}/pub/static`;
initSync();
if (theme) {
const themeRegistry = new ThemeRegistry();
const themeConfig = themeRegistry.getTheme(theme);
gulp.watch(`${themeConfig.path}**/*.${themeConfig.dsl}`, [`${themeConfig.dsl}:${theme}`]).on('change', stream => {
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`));
});
return;
}
gulp.watch(`${pubPath}/**/*.less`).on('change', stream => {
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`));
runThemeTask(`less`, stream.path);
});
gulp.watch(`${pubPath}/**/*.scss`).on('change', stream => {
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`));
runThemeTask(`scss`, stream.path);
});
}