发布添加copyImage步骤
This commit is contained in:
parent
b48ba9dd97
commit
3f43208997
@ -1,22 +1,25 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
|
||||||
module.exports = {
|
class Deploy {
|
||||||
/**
|
/**
|
||||||
* 发布静态化的站点
|
* 发布静态化的站点
|
||||||
* @param {String} source 源位置
|
* @param {String} source 源位置
|
||||||
* @param {String} target 目标位置
|
* @param {String} target 目标位置
|
||||||
* @param {Boolean} copyRoot 是否拷贝根目录
|
* @param {Boolean} isRemove 是否先进行删除
|
||||||
*/
|
*/
|
||||||
async exec(source, target, copyRoot) {
|
async exec(source, target, isRemove = false) {
|
||||||
await new Promise((resolve, reject) => {
|
if(isRemove) {
|
||||||
console.log(`删除${target}目录中的文件`)
|
await new Promise((resolve, reject) => {
|
||||||
this._deleteFolderRecursive(target, true)
|
console.log(`删除${target}目录中的文件`)
|
||||||
resolve()
|
this._deleteFolderRecursive(target, true)
|
||||||
})
|
resolve()
|
||||||
|
})
|
||||||
|
}
|
||||||
console.log(`拷贝${source}所有文件 -> ${target}`)
|
console.log(`拷贝${source}所有文件 -> ${target}`)
|
||||||
this._copyFolderRecursive(source, target, copyRoot)
|
this._checkDirectory(target)
|
||||||
},
|
this._copyFolderRecursive(source, target)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 递归删除目录以及子目录中的所有文件
|
* 递归删除目录以及子目录中的所有文件
|
||||||
@ -35,7 +38,7 @@ module.exports = {
|
|||||||
if(!retainRoot) { // 根目录保留
|
if(!retainRoot) { // 根目录保留
|
||||||
fs.rmdirSync(curPath)
|
fs.rmdirSync(curPath)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
/**
|
/**
|
||||||
* 递归拷贝目录
|
* 递归拷贝目录
|
||||||
* @param {String} source 源位置
|
* @param {String} source 源位置
|
||||||
@ -53,24 +56,28 @@ module.exports = {
|
|||||||
let writable = fs.createWriteStream(_target) //创建写入流
|
let writable = fs.createWriteStream(_target) //创建写入流
|
||||||
readable.pipe(writable);
|
readable.pipe(writable);
|
||||||
} else if (stats.isDirectory()) { //是目录则 递归
|
} else if (stats.isDirectory()) { //是目录则 递归
|
||||||
this._checkDirectory(_src, _target, this._copyFolderRecursive)
|
this._checkDirectory(_target, this._copyFolderRecursive, _src, _target)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验目标目录是否存在
|
* 校验目标目录是否存在
|
||||||
* @param {String} src 源目录
|
|
||||||
* @param {String} target 目标目录
|
* @param {String} target 目标目录
|
||||||
* @param {Function} callback 回调函数
|
* @param {Function} callback 回调函数
|
||||||
|
* @param {Array} args 回调函数入参
|
||||||
*/
|
*/
|
||||||
_checkDirectory (src,target,callback) {
|
_checkDirectory (target, callback, ...args) {
|
||||||
fs.access(target, fs.constants.F_OK, err => {
|
fs.access(target, fs.constants.F_OK, err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
fs.mkdirSync(target)
|
fs.mkdirSync(target)
|
||||||
}
|
}
|
||||||
callback.call(this, src, target)
|
if (typeof callback === 'function') {
|
||||||
|
callback.apply(this, args)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = new Deploy()
|
||||||
11
gulpfile.js
11
gulpfile.js
@ -49,15 +49,22 @@ gulp.task('compressHtml', () => {
|
|||||||
.pipe(gulp.dest('./public'))
|
.pipe(gulp.dest('./public'))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 拷贝图片
|
||||||
|
gulp.task('copyImage', () => {
|
||||||
|
const deploy = require('./deploy_utils/deploy')
|
||||||
|
return deploy.exec('./images', './public/images')
|
||||||
|
})
|
||||||
|
|
||||||
|
// 发布
|
||||||
gulp.task('deploy', () => {
|
gulp.task('deploy', () => {
|
||||||
if(!argv.deployPath) {
|
if(!argv.deployPath) {
|
||||||
return Promise.resolve('未获得deployPath, 跳过发布').then(log)
|
return Promise.resolve('未获得deployPath, 跳过发布').then(log)
|
||||||
}
|
}
|
||||||
const deploy = require('./deploy_utils/deploy')
|
const deploy = require('./deploy_utils/deploy')
|
||||||
return deploy.exec('./public', argv.deployPath, false)
|
return deploy.exec('./public', argv.deployPath, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 默认任务
|
// 默认任务
|
||||||
gulp.task('default',
|
gulp.task('default',
|
||||||
gulp.series('generate', 'compressHtml', 'deploy') // 串行执行任务
|
gulp.series('generate', 'compressHtml', 'copyImage', 'deploy') // 串行执行任务
|
||||||
)
|
)
|
||||||
@ -1,11 +0,0 @@
|
|||||||
// 替换markdown中图片路径的正则
|
|
||||||
const mdImageRegex = /\]\s*\((?=(?!http).*?\))/gi
|
|
||||||
// 替换所有HTML标签的正则
|
|
||||||
hexo.extend.filter.register('before_post_render', function(article){
|
|
||||||
// article.raw 是原始的文件内容
|
|
||||||
// article.content 是处理过代码块语法高亮的内容
|
|
||||||
if(hexo.config.picture_cdn) {
|
|
||||||
article.content = article.content.replace(mdImageRegex, `](${hexo.config.picture_cdn}`)
|
|
||||||
}
|
|
||||||
return article
|
|
||||||
})
|
|
||||||
Loading…
x
Reference in New Issue
Block a user