代码拉取完成,页面将自动刷新
const fs = require('fs')
const path = require('path')
const chokidar = require('chokidar');
function log(text) {
console.log(text)
}
const watcher = chokidar.watch('./example/components/PageWrapper/')
function getDistPath(originalPath) {
console.log("TCL: getDistPath -> originalPath", originalPath)
const regexp = /^example\/components\/PageWrapper\//
originalPath = originalPath.replace(regexp, '')
const distFilePath = path.join(__dirname, './src/', originalPath)
console.log("TCL: getDistPath -> distFilePath", distFilePath)
return distFilePath
}
function handleAddOrSaveFile(originalFilePath) {
const distFilePath = getDistPath(originalFilePath)
const isExist = fs.existsSync(distFilePath)
if (isExist) {
const content = fs.readFileSync(originalFilePath, { encoding: 'utf8' })
fs.writeFileSync(distFilePath, content)
log(`File ${originalFilePath} has been changed`)
} else {
fs.copyFileSync(originalFilePath, distFilePath)
log(`File ${originalFilePath} has been added`)
}
}
watcher.on('add', handleAddOrSaveFile)
watcher.on('change', handleAddOrSaveFile)
function handleAddDir(originalDirPath) {
log(`Directory ${originalDirPath} has been added`)
const distDirPath = getDistPath(originalDirPath)
const isExist = fs.existsSync(distDirPath)
if (!isExist) {
fs.mkdirSync(distDirPath)
}
}
watcher.on('addDir', handleAddDir)
function handleUnlinkFile(filePath) {
const distFilePath = getDistPath(filePath)
const isExist = fs.existsSync(distFilePath)
if (isExist) {
fs.unlinkSync(distFilePath)
log(`File ${filePath} has been removed`)
}
}
watcher.on('unlink', handleUnlinkFile);
function handleUnlinkDir(originalDirPath) {
const distDirPath = getDistPath(originalDirPath)
const isExist = fs.existsSync(distDirPath)
if (isExist) {
fs.rmdirSync(distDirPath)
log(`Directory ${originalDirPath} has been removed`)
}
}
watcher.on('unlinkDir', handleUnlinkDir)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。