Skip to content

Instantly share code, notes, and snippets.

@Ben02
Last active April 30, 2017 06:54
Show Gist options
  • Save Ben02/e7a29d2a237a9ad35766b546683571b7 to your computer and use it in GitHub Desktop.
Save Ben02/e7a29d2a237a9ad35766b546683571b7 to your computer and use it in GitHub Desktop.
利用 Git Hooks 自动克隆 Git 仓库进行部署并推送到另一仓库。详细:https://munen.cc/tech/Git-Hooks.html
var gulp = require('gulp');
var minifycss = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
var concat = require('gulp-concat');
var del = require('del');
var copy = require('gulp-copy');
var runSequence = require('run-sequence');
var csscomb = require('gulp-csscomb');
// 清除public文件夹
gulp.task('clean', function() {
return del(['public/**/*']);
});
// 复制到临时目录
gulp.task('temp-copy', function() {
return gulp.src('./public/**/*')
.pipe(copy('./gulp-temp', {
prefix: 1
}))
});
// 复制不压缩的文件到 public
gulp.task('public-copy', function() {
return gulp.src([
'./gulp-temp/**/*.xml',
'./gulp-temp/fonts/*',
'./gulp-temp/**/*.txt'
])
.pipe(copy('./public', {
prefix: 1
}))
});
// 一气呵成
gulp.task('copy', function(cb) {
runSequence('temp-copy', 'clean', 'public-copy', cb);
});
// 压缩css
gulp.task('minify-css', function() {
return gulp.src([
'./gulp-temp/css/nav.css',
'./gulp-temp/css/screen.css'
])
.pipe(csscomb())
.pipe(minifycss({
compatibility: '*',
level: 2
}))
.pipe(concat('main.css'))
.pipe(gulp.dest('./public/css'));
});
// 压缩所有html
gulp.task('minify-html', function() {
return gulp.src('./gulp-temp/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 压缩js
gulp.task('minify-js', function() {
return gulp.src([
'./gulp-temp/js/jquery.js',
'./gulp-temp/js/jquery.fitvids.js',
'./gulp-temp/js/index.js'
])
.pipe(uglify())
.pipe(concat('main.js'))
.pipe(gulp.dest('./public/js'));
});
// compress:同时处理html,css,js,img
gulp.task('compress', function(cb) {
runSequence(['minify-html', 'minify-css', 'minify-js'], cb);
});
// 一气呵成
gulp.task('build', function(cb) {
runSequence('copy', 'compress', cb)
});
gulp.task('default', ['build'])
{
"name": "tomoki.cc",
"version": "1.0.0",
"description": "![Hugo](https://raw.githubusercontent.com/spf13/hugo/master/docs/static/img/hugo-logo.png)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "/home/git/tomoki.git"
},
"author": "",
"license": "ISC",
"devDependencies": {
"del": "^2.2.2",
"gulp": "^3.9.1",
"gulp-clean-css": "^3.0.4",
"gulp-concat": "^2.6.0",
"gulp-copy": "^1.0.0",
"gulp-csscomb": "^3.0.7",
"gulp-htmlclean": "^2.7.8",
"gulp-htmlmin": "^2.0.0",
"gulp-uglify": "^2.0.0",
"run-sequence": "^1.2.2"
}
}
#!/bin/bash
rm -rf /home/tmk/tomokicc
git clone /home/tmk/tomoki.git /home/tmk/tomokicc
cd /home/tmk/tomokicc
git clone [email protected]:Ben02/tomoki.git /home/tmk/tomokicc/public
cp -rf /home/tmk/package.json /home/tmk/tomokicc
cp -rf /home/tmk/gulpfile.js /home/tmk/tomokicc
cnpm install
hugo && gulp
#!/bin/bash
cd /home/tmk/tomokicc/public && /usr/bin/git add . && /usr/bin/git commit -m "post" && /usr/bin/git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment