vesh-cli
Version:
码农村nodejs版本VESH框架脚手架搭建器
298 lines (274 loc) • 10.8 kB
JavaScript
const gulp = require('gulp');
const del = require('del');
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const watch = require('gulp-watch');
const babel = require('gulp-babel');
const combiner = require('stream-combiner');
const notify = require('gulp-notify');
const uglify = require('gulp-uglify'); //js压缩
const minifyCSS = require('gulp-minify-css'); //css压缩
const gutil = require('gulp-util');
const dealLess = require('gulp-less');
const postcss = require('gulp-postcss');
const replace = require('gulp-replace');
const autoprefixer = require('autoprefixer');
const plumber = require('gulp-plumber');
const V = require('gcl');
var plugins = [
autoprefixer({
browsers: ['last 10 Chrome versions', 'last 5 Firefox versions', 'Safari >= 6', 'ie > 8']
})
];
var timeStamp = new Date().toString('yyyyMMddHHmm');
console.log('version', timeStamp);
const paths = {
static: ['./**/*', '!./*.*', '!./**/*.less', '!./**/*.js', '!./**/*.css', './**/*.min.js', './**/*.min.css', './**/ref/**', '!./bin/**/*', '!./**/*.npcf', '!./*.*', '!./node_modules/**/*', '!./log/*', '!./logic/*.js', '!./ngnix/**/*', '!./tmp/**/*', '!./transform/**'],
min: ['!./bin/**/*', '!./node_modules/**/*'],
js: ['./**/*.js', '!./**/*.min.js', '!./*.js', '!./node_modules/**/*', '!./**/action.js', '!./**/ref/**', '!./**/logic/**/*.js', '!./bin/**/*', '!./transform/**/*'],
transform: ['./transform/**/*.js'],
css: ['./**/*.css', '!./**/*.min.css', '!./node_modules/**/*', '!./bin/**/*'],
less: ['./**/*.less', '!./node_modules/**/*'],
dest: 'bin',
babel: ['./**/action.js', './**/logic/**/*.js', '!./bin/**/*', '!./node_modules/**/*'],
cleandest: 'bin/**/*.njs'
};
function clean(cb) {
return del([paths.dest], cb);
}
function static(cb) {
cb.force = true;
//原样拷贝
const combined = combiner([
gulp.src(paths.static),
watch(paths.static, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base).toLowerCase()], cb);
} else console.log('同步:' + vinyl.path);
}),
rename(function(file) {
file.dirname = file.dirname.toLowerCase();
file.basename = file.basename.toLowerCase();
}),
//html压缩
replace(/\.(min\.)?((((js)|(htm))['";])|((css)['"]))/gi, function(s) {
return (!'.html'.eq(this.file.extname) && '.htm'.eq(this.file.extname)) ? s : s.replace(/['";]$/g, '?_=' + timeStamp + s.substr(s.length - 1));
}),
gulp.dest(paths.dest)
]);
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'static 复制失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
});
setTimeout(cb, 60000);
return combined;
}
function js(cb) {
const combined = combiner([
gulp.src(paths.js),
watch(paths.js, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base)], cb);
} else console.log('JS压缩同步:' + vinyl.path);
}),
rename(function(file) {
file.dirname = file.dirname.toLowerCase();
file.basename = file.basename.toLowerCase();
}),
replace(/\.(min\.)?((((js)|(htm))['";])|((css)['"]))/gi, function(s) { return s.replace(/['";]$/g, '?_=' + timeStamp + s.substr(s.length - 1)); }),
plumber(),
// (process.argv[3] + '' == 'true') ? rename(function(file) {}) : babel({
// presets: ['@babel/env']
// }), //{ presets: ['es2015', 'stage-0'] },
// (process.argv[3] + '' == 'true') ? rename(function(file) {}) : uglify(),
//replace(/"use strict"/gi, function(s) { return ''; }),
gulp.dest(paths.dest),
rename(function(file) {
timeoutjs = 0;
}),
notify({ message: 'JS 压缩成功!' })
]);
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'JS 压缩失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
});
setTimeout(cb, 30000);
return combined;
}
function css(cb) {
const combined = combiner([
gulp.src(paths.css),
watch(paths.css, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base)], cb);
} else console.log('CSS压缩同步:' + vinyl.path);
}),
rename(function(file) {
file.dirname = file.dirname.toLowerCase();
file.basename = file.basename.toLowerCase();
}),
plumber(),
(process.argv[3] + '' == 'true') ? rename(function(file) {}) : postcss(plugins),
(process.argv[3] + '' == 'true') ? rename(function(file) {}) : minifyCSS(),
gulp.dest(paths.dest),
rename(function(file) {
timeoutcss = 0;
}),
notify({ message: 'CSS 压缩成功!' })
]);
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'CSS 压缩失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
});
setTimeout(cb, 30000);
return combined;
}
function less(cb) {
const combined = combiner([
gulp.src(paths.less),
watch(paths.less, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base)], cb);
} else console.log('Less压缩同步:' + vinyl.path);
}),
plumber(),
dealLess(),
postcss(plugins),
(process.argv[3] + '' == 'true') ? rename(function(file) {}) : minifyCSS(),
rename(function(file) {
file.basename = file.basename.toLowerCase();
file.dirname = file.dirname.toLowerCase();
}),
gulp.dest(paths.dest),
rename(function(file) {
timeoutless = 0;
}),
notify({ message: 'less 压缩成功!' })
]);
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'less 压缩失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
});
setTimeout(cb, 15000);
return combined;
}
function babel2(cb) {
const combined = combiner([
gulp.src(paths.babel),
watch(paths.babel, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
vinyl.extname = '.js';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base)], cb);
} else {
console.log('编译同步:' + vinyl.path);
}
}),
plumber(),
babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/transform-runtime']
}), //{ presets: ['es2015', 'stage-0'] },
(process.argv[3] + '' == 'true') ? rename(function(file) {}) : uglify(),
// sourcemaps.init({ loadMaps: true }),
// sourcemaps.identityMap(),
// sourcemaps.write('../bin'),
rename(function(file) {
if (file.extname != '.map')
//console.log('sssssssssssssssssssssssssssssssssssssss'+file.extname);
file.extname = '.njs';
file.basename = file.basename.toLowerCase();
file.dirname = file.dirname.toLowerCase();
timeout = 0;
console.log('timeout clear');
}),
gulp.dest(paths.dest),
notify({ message: 'VESH 编译成功!' })
]);
// any errors in the above streams will get caught
// by this listener, instead of being thrown:
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'VESH 编译失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
})
setTimeout(cb, 35000);
return combined;
}
function transform(cb) {
const combined = combiner([
gulp.src(paths.transform),
watch(paths.transform, vinyl => {
if (!vinyl.stat || !vinyl.stat.nlink) {
vinyl.base = vinyl.cwd + '/bin';
vinyl.extname = '.js';
console.log('删除:', vinyl.path.replace(vinyl.cwd, vinyl.base));
del([vinyl.path.replace(vinyl.cwd, vinyl.base)], cb);
} else {
console.log('编译同步:' + vinyl.path);
}
}),
plumber(),
babel({
presets: ['@babel/preset-env'],
plugins: ['@babel/transform-runtime']
}), //{ presets: ['es2015', 'stage-0'] },
(process.argv[3] + '' == 'true') ? rename(function(file) {}) : uglify(),
// sourcemaps.init({ loadMaps: true }),
// sourcemaps.identityMap(),
// sourcemaps.write('../bin'),
gulp.dest(paths.dest),
notify({ message: 'transform 编译成功!' })
]);
// any errors in the above streams will get caught
// by this listener, instead of being thrown:
combined.on('error', function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: 'VESH 编译失败!',
message: '<%=error.message %>'
}).apply(this, args); //替换为当前对象
this.emit();
})
setTimeout(cb, 3000);
return combined;
}
exports.default = gulp.series(
clean,
static,
less,
css,
babel2,
js,
transform
);
// exports.default = gulp.series(clean, js);
//todo action ni view moduler check vesh-cli pm2 ngnix cmds