发布添加copyImage步骤

This commit is contained in:
灌糖包子 2022-04-03 20:05:32 +08:00
parent b48ba9dd97
commit 3f43208997
Signed by: sookie
GPG Key ID: 6805B2A09C8E0B47
3 changed files with 33 additions and 30 deletions

View File

@ -1,22 +1,25 @@
const fs = require('fs')
const path = require('path')
module.exports = {
class Deploy {
/**
* 发布静态化的站点
* @param {String} source 源位置
* @param {String} target 目标位置
* @param {Boolean} copyRoot 是否拷贝根目录
* @param {Boolean} isRemove 是否先进行删除
*/
async exec(source, target, copyRoot) {
await new Promise((resolve, reject) => {
console.log(`删除${target}目录中的文件`)
this._deleteFolderRecursive(target, true)
resolve()
})
async exec(source, target, isRemove = false) {
if(isRemove) {
await new Promise((resolve, reject) => {
console.log(`删除${target}目录中的文件`)
this._deleteFolderRecursive(target, true)
resolve()
})
}
console.log(`拷贝${source}所有文件 -> ${target}`)
this._copyFolderRecursive(source, target, copyRoot)
},
this._checkDirectory(target)
this._copyFolderRecursive(source, target)
}
/**
* 递归删除目录以及子目录中的所有文件
@ -35,7 +38,7 @@ module.exports = {
if(!retainRoot) { // 根目录保留
fs.rmdirSync(curPath)
}
},
}
/**
* 递归拷贝目录
* @param {String} source 源位置
@ -53,24 +56,28 @@ module.exports = {
let writable = fs.createWriteStream(_target) //创建写入流
readable.pipe(writable);
} else if (stats.isDirectory()) { //是目录则 递归
this._checkDirectory(_src, _target, this._copyFolderRecursive)
this._checkDirectory(_target, this._copyFolderRecursive, _src, _target)
}
})
})
},
}
/**
* 校验目标目录是否存在
* @param {String} src 源目录
* @param {String} target 目标目录
* @param {Function} callback 回调函数
* @param {Array} args 回调函数入参
*/
_checkDirectory (src,target,callback) {
_checkDirectory (target, callback, ...args) {
fs.access(target, fs.constants.F_OK, err => {
if (err) {
fs.mkdirSync(target)
}
callback.call(this, src, target)
if (typeof callback === 'function') {
callback.apply(this, args)
}
})
}
}
}
module.exports = new Deploy()

View File

@ -49,15 +49,22 @@ gulp.task('compressHtml', () => {
.pipe(gulp.dest('./public'))
})
// 拷贝图片
gulp.task('copyImage', () => {
const deploy = require('./deploy_utils/deploy')
return deploy.exec('./images', './public/images')
})
// 发布
gulp.task('deploy', () => {
if(!argv.deployPath) {
return Promise.resolve('未获得deployPath, 跳过发布').then(log)
}
const deploy = require('./deploy_utils/deploy')
return deploy.exec('./public', argv.deployPath, false)
return deploy.exec('./public', argv.deployPath, true)
})
// 默认任务
gulp.task('default',
gulp.series('generate', 'compressHtml', 'deploy') // 串行执行任务
gulp.series('generate', 'compressHtml', 'copyImage', 'deploy') // 串行执行任务
)

View File

@ -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
})