const gulp = require('gulp');
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const chokidar = require('chokidar');
const path = require('path');
const fs = require('fs');
const wait = () => require('map-stream')((data, cb) => {
setTimeout(() => cb(null, data), 1000);
});
const pug = require('gulp-pug');
const sass = require('gulp-sass');
const pagePath = 'pages';
const pugSrc = [`${pagePath}/**/*.pug`];
const sassSrc = [`${pagePath}/**/*.sass`];
const dev_pug = () => gulp.src(pugSrc)
.pipe(plumber())
.pipe(pug({ pretty: true }))
.pipe(rename({ extname: '.wxml' }))
.pipe(gulp.dest(pagePath));
const dev_sass = () => gulp.src(sassSrc)
.pipe(wait())
.pipe(plumber())
.pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
.pipe(rename({ extname: '.wxss' }))
.pipe(gulp.dest(pagePath));
const dev_watch = cb => {
chokidar.watch(pagePath, {
ignoreInitial: true,
ignorePermissionErrors: true
}).on('all', (event, filePath) => {
try {
console.log(event, path.resolve(__dirname, filePath));
if (event === 'addDir') {
const fileName = filePath.split(/\/|\\/).pop();
const file = path.resolve(__dirname, filePath, fileName);
fs.writeFile(file + '.pug', '', () => {});
fs.writeFile(file + '.sass', '', () => {});
}
if (event === 'change') {
if (/\.pug$/.test(filePath)) {
dev_pug();
} else if (/\.sass$/.test(filePath)) {
dev_sass();
}
}
} catch (err) {
console.log('chokidar error', err);
}
});
cb && cb();
};
gulp.task('dev', dev_watch);