landers.gulp-helper
Version:
landers.gulp-helper
562 lines (479 loc) • 18.8 kB
JavaScript
;"use strict";
let log = require('./log');
function build_task_name(type) {
return ['pack', type].filterFalse().join('-');
}
let task = {
helper: null,
use: function(helper) {
this.helper = helper;
return this;
},
data: [],
exists: function(task_name) {
return !!this.data.find(function(item){
return item === task_name;
});
},
addTask: function(task_name, handler){
if (task_name !== 'default') {
this.data.push(task_name);
}
this.helper.gulp.task(task_name, handler);
},
packDist: function(is_recursion, preppends, appends) {
var _this = this,
helper = this.helper,
build_dist_file = function(suffix) {
var filename = helper.getCurrentPackage('name') + '.' + suffix;
return helper.path.dist + '/' + filename;
};
preppends = preppends || {};
appends = appends || {};
var task_names = ['css', 'js'].map(function(suffix){
var task_name = build_task_name(suffix);
_this.addTask(task_name, function(){
var dist_file = build_dist_file(suffix);
var append_files = [
helper.path.lib + `/**/*.${suffix}`,
helper.path.src + `/${suffix}/main.${suffix}`,
helper.path.src + `/**/!(main).${suffix}`
].concat(appends[suffix] || []);
return helper.packByDependency(
task_name, suffix, dist_file, is_recursion,
preppends[suffix] || [], append_files
);
});
return task_name;
});
_this.addTask('default', function(){
task_names.push(function(){
log.showAllDone()
});
helper.runSequence.apply(helper, task_names);
});
return this;
},
useDefaultBases: function(){
this.clearProduction();
this.exeChmod();
this.cleanTemp();
this.startServer();
this.copyFonts();
return this;
},
cleanTemp: function(){
var _this = this,
task_name = 'clean-temp',
helper = this.helper;
_this.addTask(task_name, function(){
// require('del')([
// helper.path.temp,
// helper.path.buildFramework(helper.path.temp)
// ]);
// helper.cleanPaths([
// helper.path.temp,
// helper.path.buildFramework(helper.path.temp)
// ])
});
return this;
},
// 清空生产环境
// clearProduction: function(){
// var _this = this,
// task_name = 'clean-production',
// helper = this.helper;
// _this.addTask(task_name, function(){
// return helper.clearProduction();
// });
// return this;
// },
// 设置生产环境目录权限
exeChmod: function(){
var _this = this,
task_name = 'exe-chmod',
helper = this.helper;
_this.addTask(task_name, function(){
return helper.exeChmod();
});
return this;
},
startServer: function(dir){
var _this = this,
task_name = 'start-server',
helper = this.helper,
is_production = helper.options.env == 'production';
if (is_production) return this;
_this.addTask(task_name, function(){
if (!helper.options.connect) return;
helper.startServer(dir);
});
return this;
},
// 复制fonts
copyFonts: function(){
var _this = this,
helper = this.helper;
path = helper.path,
task_name = 'copy-fonts';
var is_framework_self = path.root == path.framework_root;
_this.addTask(task_name, function(){
return helper.packByDependency(
task_name, 'font', null, false, [],
[is_framework_self ? null : path.buildFramework(path.asset + '/fonts/*.*')]
);
});
return this;
},
// 打包lib.css, lib.js
packAsset: function(is_recursion, appends) {
var _this = this,
helper = this.helper,
path = helper.path;
var pathModule = require('path');
var appends = (function(array){
var appends = {};
if (array) {
array.map(function(item){
var extname = pathModule.extname(item).replace('.', '');
appends[extname] = appends[extname] || [];
appends[extname].push(item);
});
}
return appends;
})(appends);
var is_framework_self = path.root == path.framework_root;
['css', 'js'].map(function(suffix){
var task_name = `pack-asset-${suffix}`;
_this.addTask(task_name, function(){
var dist_file = path.asset + `/${suffix}/lib.${suffix}`;
return helper.packByDependency(
task_name, suffix, dist_file, is_recursion,
[is_framework_self ? null : path.buildFramework(dist_file)],
appends[suffix] || []
);
});
});
return this;
},
// 打包业务应用css
packAppCss: function(is_modules, appends) {
var _this = this,
helper = this.helper,
options = helper.options,
path = helper.path,
task_name = 'pack-app-css';
_this.addTask(task_name, function(){
var dist_file = path.asset + '/css/app.css';
var src_files = [].concat([
path.buildFramework(path.supports.self + '/**/*.css'),
path.buildFramework(path.components + '/**/*.css'),
])
.concat([
path.buildFramework(_this.getSrcFiles(path.modules, options.frameworkModules, '*.css')),
])
.concat([
path.buildApp(path.supports.self + '/**/*.css'),
path.buildApp(_this.getSrcFiles(path.components, options.appComponents, '*.css')),
is_modules !== false ? path.buildApp(_this.getSrcFiles(path.modules, options.appModules, '*.css')) : null
])
.concat(appends);
return helper.packCss(task_name, src_files, dist_file, options.env === 'production' ? {
is_mini_css: true,
} : {});
});
return this;
},
// 框架support转js到临时目录
packFrameworkSupportsHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
is_framework_self = path.root == path.framework_root,
task_name = 'pack-framework-supports-html';
if (is_framework_self) return this;
_this.addTask(task_name, function(){
var src_files = [
path.buildFramework(path.supports.partials + '/**/*.html')
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildFramework(path.supports.partials, options.is_for_crx ? '../.' : ''),
path.buildFramework(path.html2js + '/' + path.supports.partials),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
// 业务html转js到临时目录
packAppSupportsHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
task_name = 'pack-app-supports-html';
_this.addTask(task_name, function(){
var src_files = [
path.supports.partials + '/**/*.html'
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildApp(path.supports.partials, options.is_for_crx ? '../.' : ''),
path.buildApp(path.html2js + '/' + path.supports.partials),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
// 框架html转js到临时目录
packFrameworkComponentsHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
is_framework_self = path.root == path.framework_root,
task_name = 'pack-framework-components-html';
if (is_framework_self) return this;
_this.addTask(task_name, function(){
var src_files = [
path.buildFramework(path.components) + '/**/*.html'
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildFramework(path.components, options.is_for_crx ? '../.' : ''),
path.buildFramework(path.html2js + '/' + path.components),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
// 业务html转js到临时目录
packAppComponentsHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
task_name = 'pack-app-components-html';
_this.addTask(task_name, function(){
var src_files = [
path.buildApp(_this.getSrcFiles(path.components, options.appComponents, '*.html'))
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildApp(path.components, options.is_for_crx ? '../.' : ''),
path.buildApp(path.html2js + '/' + path.components),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
//框架html转js到临时目录
packFrameworkModulesHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
is_framework_self = path.root == path.framework_root,
task_name = 'pack-framework-modules-html';
if (is_framework_self) return this;
_this.addTask(task_name, function(){
var src_files = [
path.buildFramework(_this.getSrcFiles(path.modules, options.frameworkModules, '*.html'))
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildFramework(path.modules, options.is_for_crx ? '../.' : ''),
path.buildFramework(path.html2js + '/' + path.modules),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
// 业务html转js到临时目录
packAppModulesHtml: function() {
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
task_name = 'pack-app-modules-html';
_this.addTask(task_name, function(){
var src_files = [
path.buildApp(_this.getSrcFiles(path.modules, options.appModules, '*.html'))
];
return helper.packHtml(
task_name,
src_files,
'ULanConsole',
path.buildApp(path.modules, options.is_for_crx ? '../.' : ''),
path.buildApp(path.html2js + '/' + path.modules),
options.env === 'production' ? {
is_mini_html: true
} : {}
);
});
return this;
},
getSrcFiles: function(base_path, sub_paths, pattern){
// var app_components = this.helper.options.appComponents;
// var ret = app_components ?
// app_components.map(function(item){
// return path.buildApp(path.components + '/' + item + '/**/' + pattern)
// }):
// [path.buildApp(path.components + '/**/' + pattern)];
var regx = sub_paths ? '@(' + sub_paths.join('|') + ')/**' : '**';
var ret = base_path + '/' + regx + '/' + pattern;
// console.log(ret);
return ret;
},
// 打包框架js
packFrameworkJs: function(appends, preppends){
var _this = this,
helper = this.helper,
path = helper.path,
options = helper.options,
is_framework_self = path.root == path.framework_root,
task_name = 'pack-framework-js';
if (is_framework_self) return this;
this.addTask(task_name, function(){
var framework_modules = options.frameworkModules || [];
var src_files = [
path.buildApp(path.config + '/env.js'),
path.buildFramework(path.config + '/env.js'),
preppends || [],
// path.buildApp(path.config + '/env-' + options.env + '.js'),
path.buildFramework(path.config + '/config.js'),
path.buildApp(path.config + '/config.js'),
path.buildFramework(path.supports.self + '/bootstrap.js'),
path.buildFramework(path.config + '/bootstrap.js'),
path.buildApp(path.config + '/bootstrap.js'),
path.buildFramework(path.supports.self + '/**/*.js'),
path.buildApp(path.supports.self + '/**/*.js'),
path.buildFramework(path.components + '/**/*.js')
].concat([
path.buildApp(_this.getSrcFiles(path.components, options.appComponents, '*.js'))
]).concat([
path.buildFramework(path.config + '/providers.js'),
path.buildApp(path.config + '/providers.js'),
path.buildFramework(path.config + '/enums.js'),
path.buildApp(path.config + '/enums.js'),
]).concat(
framework_modules.map(function(item){
return [
// 这儿不确定 模块item 下有没有routes.js,故用 /**/routes.js
path.buildFramework(path.modules + '/' + item + '/**/routes.js'),
path.buildFramework(path.modules + '/' + item + '/**/locales*.js'),
// 常规情况下**/*.js包含了:layout.js, partials/*.js
!_this.exists('pack-framework-modules-html') ? null : path.buildFramework(path.html2js + '/' + path.modules + '/' + item + '/**/*.js') ,
!_this.exists('pack-framework-modules-html') ? null : path.buildFramework(path.modules + '/' + item + '/**/!(routes).js')
];
})
).concat(appends || []);
return helper.packJs(task_name, src_files, path.asset + '/js/framework.js', options.env === 'production' ? {
is_uglify: true,
is_obfuscate: true
} : {});
});
return this;
},
// 打包业务应用js
packAppJs: function(appends) {
var _this = this,
helper = this.helper,
options = helper.options,
path = helper.path,
task_name = 'pack-app-js';
_this.addTask(task_name, function(){
var src_files =
[_this.getSrcFiles(path.modules, options.appModules, 'routes.js')]
.concat(
[path.buildApp(path.html2js + '/**/*.js')]
).concat(
appends || []
).concat([
_this.getSrcFiles(path.modules, options.appModules, '!(routes).js')
]);
return helper.packJs(task_name, src_files, path.asset + '/js/app.js', options.env === 'production' ? {
is_uglify: true,
is_obfuscate: true
} : {});
});
return this;
},
copyToPublic: function(subpath) {
var _this = this,
helper = this.helper,
options = helper.options,
path = helper.path,
is_production = helper.options.env == 'production',
task_name = 'copy-to-public';
if (!is_production) return this;
_this.addTask(task_name, function(){
var public_sub_path = subpath || helper.options.public_sub_path;
var dist_path = path.public + '/' + public_sub_path;
helper.copy([
path.root + '/assets/css/lib.css',
path.root + '/assets/css/app.css',
], dist_path + '/assets/css');
helper.copy([
path.root + '/assets/js/lib.js',
path.root + '/assets/js/framework.js',
path.root + '/assets/js/app.js',
], dist_path + '/assets/js');
helper.copy([
path.root + '/assets/images/**/*.*',
], dist_path + '/assets/images');
helper.copy([
path.root + '/modules/**/*.jpg',
path.root + '/modules/**/*.png',
path.root + '/modules/**/*.git',
], dist_path + '/modules');
helper.copy([
path.root + '/assets/fonts/**/*.*',
], dist_path + '/assets/fonts');
return helper.copy([
path.root + '/index.php',
], dist_path);
});
return this;
},
useDefaultWithFramework: function(appends){
var _this = this;
var helper = this.helper;
var path = helper.path;
// 使用内置默认任务
this.startServer();
var is_framework_self = path.root == path.framework_root
_this.addTask('default', function(){
var task_names = _this.data;
task_names = task_names.concat(appends).filterFalse();
log.showList(task_names);
task_names = task_names.concat([
function() {helper.log.showAllDone();}
]);
return helper.runSequence.apply(helper, task_names);
});
return this;
}
};
module.exports = task;