const fs = require('fs')
const path = require('path')/*** @description: 获取完整的文件路径* @param {*} url 路径* @return {*} 返回完整的文件路径*/
const getPath = (url) => {return path.join(__dirname, url)
}/*** @description: 获取参数* @return {*} target【目标文件夹】 source【源文件夹】*/
function parseArgv() {let arguments = process.argv.splice(2)const target = arguments[0] || './copy'const source = arguments[1] || './'return { target, source }
}/*** @description: 是否是文件夹* @param {*} pathDir 路径* @return {*} true 是,false 否*/
const isDir = (pathDir) => {const path = getPath(pathDir)let data = falsetry {data = fs.statSync(path).isDirectory()return data} catch (error) {return data}
}/*** @description: 获取文件夹列表* @param {*} pathDir 路径* @return {*} 文件列表*/
const getDirList = (pathDir) => {const path = getPath(pathDir)return new Promise((resolve, reject) => {fs.readdir(path, (err, data) => {if (err) {rejects(err)}resolve(data)})})
}const exclude = ['node_modules', '.gitignore']const main = async (target, source) => {let list = await getDirList(source)const except = [path.basename(target), path.parse(__filename).base]list = list.filter((item) => {return !except.includes(item)})isDir(target) ? '' : fs.mkdirSync(target, { recursive: true })const copyFile = (list, target, source) => {const result = list.filter((item) => {return !exclude.includes(item)})result.forEach(async (item) => {const path =source.endsWith('./') || source.endsWith('/')? './' + item: source + '/' + item // 原始路径const copyPath = target + '/' + item //复制的路径if (isDir(path)) {if (!fs.existsSync(copyPath)) {await fs.mkdirSync(copyPath)console.log(`正在复制:文件夹从${getPath(path)}------->复制到${getPath(copyPath)} ======成功`)}let needCopyList = await getDirList(path)await copyFile(needCopyList, copyPath, path)} else {if (!fs.existsSync(copyPath)) {fs.copyFileSync(path, copyPath)console.log(`正在复制:文件从${getPath(path)}------->复制到${getPath(copyPath)} ======成功`)}}})}copyFile(list, target, source)
}
const { target, source } = parseArgv()
main(target, source)
node快速复制文件或文件夹,排除部分文件(node_modules)
2024/11/29 14:46:58
来源:https://blog.csdn.net/qq_41893686/article/details/141999828
浏览:
次
关键词:node快速复制文件或文件夹,排除部分文件(node_modules)
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com