Last active
June 17, 2016 08:53
-
-
Save mrmlnc/c7deb53301864ae89886 to your computer and use it in GitHub Desktop.
Шаг второй. Понимание контекста
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var gulp = require('gulp'); | |
var jade = require('gulp-jade'); | |
var less = require('gulp-less'); | |
var chalk = require('chalk'); | |
// | |
// Options | |
// | |
var options = { | |
project: 'app-' + getDefaultContext('canonium') | |
}; | |
function getDefaultContext(defaultName) { | |
var argv = process.argv[2] || process.argv[3]; | |
if (typeof argv !== 'undefined' && argv.indexOf('--') < 0) { | |
argv = process.argv[3]; | |
} | |
return (typeof argv === 'undefined') ? defaultName : argv.replace('--', ''); | |
}; | |
function runInContext(filepath, cb) { | |
var context = path.relative(process.cwd(), filepath); | |
var projectName = context.split(path.sep)[0]; | |
// Console | |
console.log( | |
'[' + chalk.green(projectName.replace('app-', '')) + ']' + | |
' has been changed: ' + chalk.cyan(context) | |
); | |
// Set project | |
options.project = projectName; | |
cb(); | |
}; | |
// | |
// Jade | |
// | |
gulp.task('jade', function() { | |
return gulp.src(path.join(options.project, 'templates/*.jade')) | |
.pipe(jade({ pretty: true })) | |
.pipe(gulp.dest('build/' + options.project)) | |
}); | |
// | |
// Less | |
// | |
gulp.task('less', function() { | |
return gulp.src(path.join(options.project, 'less/*.less')) | |
.pipe(less()) | |
.pipe(gulp.dest('build/' + options.project)) | |
}); | |
// | |
// Watch | |
// | |
gulp.watch('app-*/templates/*.jade').on('change', function(filepath) { | |
runInContext(filepath, gulp.series('jade')); | |
}); | |
gulp.watch('app-*/less/*.less').on('change', function(filepath) { | |
runInContext(filepath, gulp.series('less')); | |
}); | |
gulp.task('default', gulp.parallel('jade', 'less')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment