blessing-skin-server/gulpfile.js

244 lines
7.5 KiB
JavaScript
Raw Normal View History

2017-01-18 23:05:32 +08:00
'use strict';
var gulp = require('gulp'),
babel = require('gulp-babel'),
2017-07-13 17:02:31 +08:00
eslint = require('gulp-eslint'),
uglify = require('gulp-uglify'),
sass = require('gulp-sass'),
cleanCss = require('gulp-clean-css'),
del = require('del'),
2017-11-09 20:50:56 +08:00
exec = require('child_process').exec,
concat = require('gulp-concat'),
zip = require('gulp-zip'),
replace = require('gulp-batch-replace'),
notify = require('gulp-notify'),
2017-11-08 23:18:47 +08:00
sourcemaps = require('gulp-sourcemaps'),
2017-11-08 13:27:35 +08:00
merge = require('merge2'),
runSequence = require('run-sequence');
2016-08-28 10:05:21 +08:00
2017-01-20 18:17:56 +08:00
var version = require('./package.json').version;
2016-07-21 22:01:57 +08:00
var srcPath = 'resources/assets/src';
var distPath = 'resources/assets/dist';
2017-01-20 18:17:56 +08:00
var vendorScripts = [
2016-09-25 09:00:40 +08:00
'jquery/dist/jquery.min.js',
'bootstrap/dist/js/bootstrap.min.js',
2017-06-30 12:13:45 +08:00
'admin-lte/dist/js/app.min.js',
2016-09-25 09:00:40 +08:00
'bootstrap-fileinput/js/fileinput.min.js',
2017-06-30 12:13:45 +08:00
'admin-lte/plugins/datatables/jquery.dataTables.min.js',
'admin-lte/plugins/datatables/dataTables.bootstrap.min.js',
'icheck/icheck.min.js',
'toastr/build/toastr.min.js',
'es6-promise/dist/es6-promise.auto.min.js',
2016-09-25 09:00:40 +08:00
'sweetalert2/dist/sweetalert2.min.js',
2017-06-30 12:13:45 +08:00
'jqPaginator/dist/1.2.0/jqPaginator.min.js',
2016-08-28 11:49:22 +08:00
];
2017-11-08 13:27:35 +08:00
var vendorScriptsToBeMinified = [
'regenerator-runtime/runtime.js',
2017-11-09 20:51:28 +08:00
'resources/assets/dist/js/common.js',
2017-11-08 13:27:35 +08:00
];
2017-01-20 18:17:56 +08:00
var vendorStyles = [
2016-09-25 09:00:40 +08:00
'bootstrap/dist/css/bootstrap.min.css',
2017-06-30 12:13:45 +08:00
'admin-lte/dist/css/AdminLTE.min.css',
'admin-lte/plugins/datatables/dataTables.bootstrap.css',
2016-09-25 09:00:40 +08:00
'bootstrap-fileinput/css/fileinput.min.css',
'font-awesome/css/font-awesome.min.css',
'icheck/skins/square/blue.css',
2017-06-30 12:13:45 +08:00
'toastr/build/toastr.min.css',
'sweetalert2/dist/sweetalert2.min.css',
];
2016-08-28 11:49:22 +08:00
var styleReplacements = [
['blue.png', '"../images/blue.png"'],
['blue@2x.png', '"../images/blue@2x.png"'],
2016-09-24 23:48:52 +08:00
['../img/loading.gif', '"../images/loading.gif"'],
['../img/loading-sm.gif', '"../images/loading-sm.gif"'],
[/@import url\((.*)italic\);/g, ''],
];
var scriptReplacements = [
['$.AdminLTE.layout.activate(),', '']
];
2017-01-20 18:17:56 +08:00
var fonts = [
'font-awesome/fonts/**',
'bootstrap/dist/fonts/**',
2017-01-20 18:17:56 +08:00
'resources/assets/src/fonts/**',
];
2017-01-20 18:17:56 +08:00
var images = [
'icheck/skins/square/blue.png',
'icheck/skins/square/blue@2x.png',
2017-01-20 18:17:56 +08:00
'resources/assets/src/images/**',
'bootstrap-fileinput/img/loading.gif',
'bootstrap-fileinput/img/loading-sm.gif',
];
2016-08-28 11:49:22 +08:00
// aka. `yarn run build`
gulp.task('default', ['build']);
// Build the things!
2017-07-13 17:02:31 +08:00
gulp.task('build', callback => {
runSequence('clean', 'lint', ['compile-es6', 'compile-sass'], 'publish-vendor', 'notify', callback);
});
// Send a notification
gulp.task('notify', () => {
return gulp.src('').pipe(notify('Assets compiled!'));
});
// Check JavaScript files with ESLint
gulp.task('lint', () => {
return gulp.src(`${srcPath}/js/**/*.js`)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// Concentrate all vendor scripts & styles to one dist file
gulp.task('publish-vendor', ['compile-es6'], callback => {
// JavaScript files
2017-11-08 13:27:35 +08:00
var js = gulp.src(convertNpmRelativePath(vendorScripts))
.pipe(replace(scriptReplacements));
var jsToBeMinified = gulp.src(convertNpmRelativePath(vendorScriptsToBeMinified))
.pipe(uglify());
merge(js, jsToBeMinified)
.pipe(concat('app.js'))
.pipe(gulp.dest(`${distPath}/js/`));
// CSS files
gulp.src(convertNpmRelativePath(vendorStyles))
.pipe(concat('style.css'))
.pipe(replace(styleReplacements))
.pipe(gulp.dest(`${distPath}/css/`));
// Fonts
gulp.src(convertNpmRelativePath(fonts))
.pipe(gulp.dest(`${distPath}/fonts/`));
// Images
gulp.src(convertNpmRelativePath(images))
.pipe(gulp.dest(`${distPath}/images/`));
// AdminLTE skins
2017-08-06 22:14:14 +08:00
gulp.src('node_modules/admin-lte/dist/css/skins/*.min.css')
.pipe(gulp.dest(`${distPath}/css/skins/`));
// 3D skin preview
gulp.src(['skin-preview/**', 'Chart.min.js'].map(path => `${srcPath}/vendor/${path}`))
.pipe(gulp.dest(`${distPath}/js/`));
callback();
2016-07-21 22:01:57 +08:00
});
// Compile sass to css
gulp.task('compile-sass', () => {
return gulp.src(`${srcPath}/sass/*.scss`)
2017-11-08 23:18:47 +08:00
.pipe(sourcemaps.init())
2016-08-28 11:49:22 +08:00
.pipe(sass().on('error', sass.logError))
2016-07-21 22:01:57 +08:00
.pipe(cleanCss())
2017-11-08 23:18:47 +08:00
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest(`${distPath}/css`));
2016-07-21 22:01:57 +08:00
});
// Compile ES6 scripts to ES5
2017-07-13 17:02:31 +08:00
gulp.task('compile-es6', callback => {
['common', 'admin', 'auth', 'skinlib', 'user'].forEach(moduleName => {
return gulp.src(`${srcPath}/js/${moduleName}/*.js`)
2017-11-08 23:18:47 +08:00
.pipe(sourcemaps.init())
2017-11-06 16:48:53 +08:00
.pipe(babel())
2017-07-13 17:02:31 +08:00
.pipe(concat(`${moduleName}.js`))
.pipe(uglify())
2017-11-08 23:18:47 +08:00
.pipe(sourcemaps.write('./maps'))
2017-07-13 17:02:31 +08:00
.pipe(gulp.dest(`${distPath}/js`));
});
callback();
2016-07-21 22:01:57 +08:00
});
// Delete cache files
gulp.task('clean', () => {
2016-12-30 16:18:13 +08:00
clearCache();
return clearDist();
2016-12-30 16:18:13 +08:00
});
// Release archive file
// aka. `yarn run release`
2017-01-18 23:05:32 +08:00
gulp.task('zip', () => {
2016-12-30 16:18:13 +08:00
clearCache();
2017-11-09 20:50:56 +08:00
console.log('Cache file deleted');
exec('composer dump-autoload --no-dev', () => {
console.log('Autoload files generated without autoload-dev');
});
2016-08-28 18:34:41 +08:00
let zipPath = `blessing-skin-server-v${version}.zip`;
console.log(`Zip archive will be saved to ${zipPath}.`);
2016-07-23 16:24:44 +08:00
return gulp.src([
'**/*.*',
2017-11-09 20:50:56 +08:00
'artisan',
2016-07-29 16:13:21 +08:00
'LICENSE',
2017-11-08 13:27:35 +08:00
'!.babelrc',
'!.eslintrc.js',
'!.travis.yml',
2017-11-09 20:50:56 +08:00
'!{.env,.env.testing}',
'!{.git,.git/**}',
'!{.github,.github/**}',
'!{.gitignore,.gitmodules,.gitattributes}',
'!gulpfile.js',
'!composer.*',
'!yarn.lock',
2017-11-09 20:50:56 +08:00
'!plugins/**',
2017-11-06 11:24:01 +08:00
'!phpunit.xml',
2017-11-09 20:50:56 +08:00
'!package.json',
'!{tests,tests/**}',
'!{coverage,coverage/**}',
'!{node_modules,node_modules/**}',
'!storage/textures/**',
'!resources/assets/{src,src/**}',
'!resources/assets/dist/**/{maps,maps/**}',
2017-01-20 18:17:56 +08:00
// do not pack packages for developments
2017-11-09 20:50:56 +08:00
'!vendor/fzaninotto/**',
'!vendor/mockery/**',
'!vendor/phpunit/**',
'!vendor/symfony/css-selector/**',
2017-11-19 12:49:24 +08:00
'!vendor/symfony/dom-crawler/**',
'!vendor/mikey179/vfsStream/**',
2016-07-23 16:24:44 +08:00
], { dot: true })
.pipe(zip(zipPath))
2017-08-10 14:25:29 +08:00
.pipe(notify('Don\'t forget to compile Sass & ES2015 files before publishing a release!'))
.pipe(gulp.dest('../'))
.pipe(notify({ message: `Zip archive saved to ${zipPath}!` }));
2016-07-23 16:24:44 +08:00
});
gulp.task('watch', ['compile-sass', 'compile-es6'], () => {
2017-01-20 18:17:56 +08:00
// watch .scss files
2017-07-13 17:02:31 +08:00
gulp.watch(`${srcPath}/sass/*.scss`, ['compile-sass'], () => notify('Sass files compiled!'));
2017-01-20 18:17:56 +08:00
// watch .js files
2017-11-08 13:27:35 +08:00
gulp.watch(`${srcPath}/js/**/*.js`, ['compile-es6'], () => notify('ES6 scripts compiled!'));
2017-07-13 17:02:31 +08:00
gulp.watch(`${srcPath}/js/general.js`, ['publish-vendor']);
});
2017-06-30 12:13:45 +08:00
function convertNpmRelativePath(paths) {
2017-01-20 18:17:56 +08:00
return paths.map(relativePath => {
return relativePath.startsWith('resources') ? relativePath : `node_modules/${relativePath}`;
2017-01-20 18:17:56 +08:00
});
}
function clearCache() {
return del([
'storage/logs/*',
'storage/debugbar/*',
'storage/update_cache/*',
'storage/yaml-translation/*',
'storage/framework/cache/*',
'storage/framework/sessions/*',
'storage/framework/views/*'
]);
}
2017-01-20 18:17:56 +08:00
function clearDist() {
return del([`${distPath}/**/*`]);
2017-01-20 18:17:56 +08:00
}