const path = require('path')
const fs = require('fs')
const shelljs = require('shelljs')
const { sftpPath } = require('../src/api/config')
function resolve(fakepath) {
return path.resolve(__dirname, '..', fakepath)
}
function update(rootpath) {
fs.readdir(resolve(rootpath), 'utf8', function(err, dirList) {
if (err) {
throw err
}
dirList.forEach(function(item) {
let stats = fs.statSync(resolve(`${rootpath}/${item}`))
if (stats.isDirectory()) {
// 文件夹
if (item === 'npm') return undefined
update(`${rootpath}/${item}`)
} else {
// 文件
if (stats.size > 200 * 1024) return undefined
fs.readFile(resolve(`${rootpath}/${item}`), 'utf8', function(err, file) {
if (err) {
throw err
}
// 文件
let newFile = ''
if (/\.json$/.test(item)) {
newFile = file.replace(/(['"]{1})([./]*assets\/images\/)/g, function($0, $1) {
return `${$1}${sftpPath}/ebank/images/`
})
} else if (/\.js$/.test(item)) {
newFile = file.replace(/(['"]{1})([./]*assets\/images\/)/g, function($0, $1) {
return `${$1}${sftpPath}/ebank/images/`
})
} else if (/\.wxss|\.css/.test(item)) {
newFile = file.replace(/(url\(['"]?)([./]*assets\/images\/)/g, function($0, $1) {
return `${$1}${sftpPath}/ebank/images/`
})
} else if (/\.wxml/.test(item)) {
newFile = file.replace(/(['"]{1})([./]*assets\/images\/)/g, function($0, $1) {
return `${$1}${sftpPath}/ebank/images/`
})
}
if (newFile) {
fs.writeFile(resolve(`${rootpath}/${item}`), newFile, 'utf8', function(err) {
if (err) {
console.log(err)
throw err
}
})
}
})
}
})
})
}
update('dist')
shelljs.rm('-rf', resolve('dist/assets/images'))
2019-04-04 使用 nodejs 批量替换文件夹下内容
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言:实际工作中遇到一个问题:需要在某一个文件下,将所有包含aaa字符串全部替换为bbb字符串。之前处理这种方式是...
- 直接上代码: import osimport sys#递归遍历文件夹名,筛选符合要求的文件夹,并执行替换重命名de...
- sed命令介绍 sed是一种流编辑器,它是文本处理中非常有用的工具,能够完美的配合正则表达式使用,功能不同凡响。处...
- (2018-06-26-周二 22:06:02) 在打开对应的空间时。