Skip to content

Instantly share code, notes, and snippets.

@armornick
Created October 26, 2018 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armornick/84bc1fdea6eb2436165fb8d63efc7e52 to your computer and use it in GitHub Desktop.
Save armornick/84bc1fdea6eb2436165fb8d63efc7e52 to your computer and use it in GitHub Desktop.
Gulpfile.js generator
// ----------------------------------------------------------
// module imports
// const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs');
// const { execSync } = require('child_process');
// ----------------------------------------------------------
// parse commandline arguments
const argv = require('yargs')
.boolean('html')
.alias('s', 'style').alias('css', 'style')
// .alias('t', 'templates')
.boolean('babel')
.boolean('react')
.boolean('server')
.boolean('webdev').alias('w', 'webdev')
.boolean('concat').alias('c', 'concat')
.boolean('concat')
.alias('c', 'concat')
.argv;
// console.log(argv);
// ----------------------------------------------------------
// interpret commandline arguments
// default project directory to current directory
argv.path = argv._[0] || '.';
// the webdev argument forces style and templates
if (argv.webdev) {
argv.style = argv.style || 'css';
argv.html = true;
// argv.templates = argv.templates || 'templates';
}
// react compilation requires babel
argv.babel = argv.babel || argv.react;
// ----------------------------------------------------------
// calculate the gulp tasks to build
var tasks = [];
if (argv.html) {
let task = {
name: 'html',
dest: 'public',
src: 'src/*.html'
};
tasks.push(task);
}
if (argv.style) {
let task = { name: 'css', dest: 'public' };
tasks.push(task);
switch (argv.style) {
case 'sass':
task.src = 'src/*.sass';
task.package = 'gulp-sass';
task.processor = 'sass';
break;
case 'scss':
task.src = 'src/*.scss';
task.package = 'gulp-sass';
task.processor = 'sass';
break;
case 'less':
task.src = 'src/*.less';
task.package = 'gulp-less';
task.processor = 'less';
break;
case 'stylus':
task.src = 'src/*.stylus';
task.package = 'gulp-stylus';
task.processor = 'stylus';
break;
case 'css':
task.src = 'src/*.css';
break;
}
}
if (argv.babel) {
let task = {
name: 'js',
dest: 'public',
src: 'src/*.js',
package: 'gulp-babel',
processor: 'babel'
};
tasks.push(task);
if (argv.react) {
task.options = '{presets: ["@babel/preset-react","@babel/preset-env"]}';
} else {
task.options = '{presets: ["@babel/preset-env"]}';
}
}
if (argv.server) {
let task = {
name: 'webserver',
src: 'public',
package: 'gulp-webserver',
processor: 'webserver',
options: '{livereload: true}'
};
tasks.push(task);
}
// ----------------------------------------------------------
// generate gulpfile
var output = [];
output.push("var gulp = require('gulp');");
for (let task of tasks) {
if (task.processor) {
output.push(`var ${ task.processor } = require('${ task.package }');`);
}
}
output.push('');
for (let task of tasks) {
output.push(`gulp.task('${ task.name }', function(){`);
output.push(`\treturn gulp.src('${ task.src }')`);
if (task.processor) {
output.push(`\t\t.pipe(${task.processor}(${ task.options || '' }))`);
}
if (task.dest) {
output.push(`\t\t.pipe(gulp.dest('${ task.dest }'))`);
}
output.push('});')
output.push('');
}
output = output.join('\n');
console.log(output);
// ----------------------------------------------------------
// create project directory and write gulpfile
/*
if (argv.path !== '.') {
let path = argv.path;
if (!existsSync(argv.path)) {
mkdirSync(path);
}
process.chdir(path);
}
console.log('writing gulpfile.js');
writeFileSync('gulpfile.js', output, 'utf8');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment