Nodejs 文件操作 ¶
1. 文件夹复制 ¶
js
/** 主要版本 */
let major = process.version.match(/v([0-9]*).([0-9]*)/)[1]
/** 特性版本 */
let minor = process.version.match(/v([0-9]*).([0-9]*)/)[2]
/**
* 文件夹复制
* @param {string} source 源文件夹
* @param {string} destination 目标文件夹
*/
function cpSync(source, destination) {
// 判断node版本不是16.7.0以上的版本
// 则进入兼容处理
// 这样处理是因为16.7.0的版本支持了直接复制文件夹的操作
if (Number(major) < 16 || Number(major) == 16 && Number(minor) < 7) {
// 如果存在文件夹 先递归删除该文件夹
if (fs.existsSync(destination)) fs.rmSync(destination, { recursive: true })
// 新建文件夹 递归新建
fs.mkdirSync(destination, { recursive: true });
// 读取源文件夹
let rd = fs.readdirSync(source)
for (const fd of rd) {
// 循环拼接源文件夹/文件全名称
let sourceFullName = source + "/" + fd;
// 循环拼接目标文件夹/文件全名称
let destFullName = destination + "/" + fd;
// 读取文件信息
let lstatRes = fs.lstatSync(sourceFullName)
// 是否是文件
if (lstatRes.isFile()) fs.copyFileSync(sourceFullName, destFullName);
// 是否是文件夹
if (lstatRes.isDirectory()) cpSync(sourceFullName, destFullName);
}
}
else fs.cpSync(source, destination, { force: true, recursive: true })
}/** 主要版本 */
let major = process.version.match(/v([0-9]*).([0-9]*)/)[1]
/** 特性版本 */
let minor = process.version.match(/v([0-9]*).([0-9]*)/)[2]
/**
* 文件夹复制
* @param {string} source 源文件夹
* @param {string} destination 目标文件夹
*/
function cpSync(source, destination) {
// 判断node版本不是16.7.0以上的版本
// 则进入兼容处理
// 这样处理是因为16.7.0的版本支持了直接复制文件夹的操作
if (Number(major) < 16 || Number(major) == 16 && Number(minor) < 7) {
// 如果存在文件夹 先递归删除该文件夹
if (fs.existsSync(destination)) fs.rmSync(destination, { recursive: true })
// 新建文件夹 递归新建
fs.mkdirSync(destination, { recursive: true });
// 读取源文件夹
let rd = fs.readdirSync(source)
for (const fd of rd) {
// 循环拼接源文件夹/文件全名称
let sourceFullName = source + "/" + fd;
// 循环拼接目标文件夹/文件全名称
let destFullName = destination + "/" + fd;
// 读取文件信息
let lstatRes = fs.lstatSync(sourceFullName)
// 是否是文件
if (lstatRes.isFile()) fs.copyFileSync(sourceFullName, destFullName);
// 是否是文件夹
if (lstatRes.isDirectory()) cpSync(sourceFullName, destFullName);
}
}
else fs.cpSync(source, destination, { force: true, recursive: true })
}2. 文件复制 ¶
js
fs.writeFileSync(`file1.txt`, ''); //先创建文件 并写入内容为空
fs.copyFileSync(`file2.txt`, `file1.txt`); // file2文件内容复制到file1中fs.writeFileSync(`file1.txt`, ''); //先创建文件 并写入内容为空
fs.copyFileSync(`file2.txt`, `file1.txt`); // file2文件内容复制到file1中3. 文件夹删除 ¶
js
/**
* 文件夹删除 并删除文件夹下所有文件
* @param {string} path 源文件夹
*/
function deleteAll(path) {
var files = [];
if(fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file) {
var curPath = `${path}/${file}`;
if(fs.statSync(curPath).isDirectory()) {
deleteAll(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};/**
* 文件夹删除 并删除文件夹下所有文件
* @param {string} path 源文件夹
*/
function deleteAll(path) {
var files = [];
if(fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file) {
var curPath = `${path}/${file}`;
if(fs.statSync(curPath).isDirectory()) {
deleteAll(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};4. 压缩文件夹 .tar.gz ¶
安装
yarn add tar
js
var tar = require('tar');
/**
* 压缩文件夹
* @param {string} path 源文件夹
*/
async function compressTar(path) {
return new Promise((resolve) => {
tar.c({
gzip: true,
cwd: path,
//过滤
filter(path, stat) {
const ignoreFiles = [
'.git',
'.vscode',
'^.env*',
'.husky',
'^./node_modules', // 排除根目录下的 node_modules, 但是不排除其他 node_modules, eg: src/node_modules
'^./dist', // 排除根目录下的 dist,但是不排除其他目录的 dist eg: src/node_modules/@vant/dist
]
for (let i = 0; i < ignoreFiles.length; i++) {
let reg = new RegExp(ignoreFiles[i], 'g')
if (reg.test(path)) {
console.log(reg)
console.log('ignore file', path)
return false
}
}
return true
},
}, ['.']).pipe(fs.createWriteStream(`${path}.tar.gz`))
.on('finish', () => {
resolve()
})
})
}var tar = require('tar');
/**
* 压缩文件夹
* @param {string} path 源文件夹
*/
async function compressTar(path) {
return new Promise((resolve) => {
tar.c({
gzip: true,
cwd: path,
//过滤
filter(path, stat) {
const ignoreFiles = [
'.git',
'.vscode',
'^.env*',
'.husky',
'^./node_modules', // 排除根目录下的 node_modules, 但是不排除其他 node_modules, eg: src/node_modules
'^./dist', // 排除根目录下的 dist,但是不排除其他目录的 dist eg: src/node_modules/@vant/dist
]
for (let i = 0; i < ignoreFiles.length; i++) {
let reg = new RegExp(ignoreFiles[i], 'g')
if (reg.test(path)) {
console.log(reg)
console.log('ignore file', path)
return false
}
}
return true
},
}, ['.']).pipe(fs.createWriteStream(`${path}.tar.gz`))
.on('finish', () => {
resolve()
})
})
}