eapp
Version:
192 lines (178 loc) • 5.8 kB
JavaScript
const fs = require('fs'); //文件模块
const path = require('path');
const config = require('../FilePathConfig.js')
const self = this;
/**
* 配置信息
*/
exports.config = {};
/**
* 相对路径转绝对路径 相对路径是相对自己的js文件的
* @param {*相对路径} path
*/
exports.getWholePath = function (relative_path, flag) {
if (flag == 0) { //相对于本JS文件的路径
return path.join(__dirname, "../" + relative_path);
} else { //相对于执行命令的路径
return path.resolve("./" + relative_path);
}
}
/**
* 复制文件 相对路径的文件
*/
exports.copyFile = function (originPath, targetPath) {
var fullOriginPath = this.getWholePath(originPath, 0);
var fullTargetPath = this.getWholePath(targetPath, 1);
fs.copyFileSync(fullOriginPath, fullTargetPath, 0);
}
/**
* 复制文件 绝对路径的文件
*/
exports.copyFileForFullPath = function (originPath, targetPath) {
fs.copyFileSync(originPath, targetPath, 0);
}
/**
* 删除文件
*/
exports.deleteFile = function (filePath) {
fs.unlinkSync(filePath);
}
/**
* 删除目录
*/
exports.deleteDir = function (filePath) {
fs.rmdirSync(filePath);
}
/**
* 生成目录
*/
exports.makeDir = function (dir) {
var response = fs.mkdirSync(dir, 0777);
return 0;
}
/**
* 判断文件/目录是否存在
* @param {路径} path
* 1 文件存在 0 文件不存在
*/
exports.fileExist = function (path) {
try {
fs.accessSync(path, fs.constants.R_OK | fs.constants.W_OK);
return 1;
} catch (error) {
if (error.errno == -2) { //文件不存在的
return error.message;
} else { //文件存在,可能权限不能访问
return 1;
}
}
}
/**
* 读取目录下所有文件 并复制到目标文件里
* @param {*} dir 绝对路径
* @param {*} targetPath 绝对路径
* @param {*} level
*/
exports.copyDir = function (dir, targetPath, level) {
if (self.fileExist(dir) != 1) { //文件不存在
console.log("资源文件不存在,无法进行创建工程 dir = " + dir);
return;
}
//读取文件信息
var fileInfo = fs.statSync(dir);
if (fileInfo.isFile()) { //文件
self.copyFileForFullPath(dir, targetPath);
// console.log("生成文件---" + targetPath);
if (level == 0) {
// console.log("复制文件成功!");
}
} else { //是目录
var response = self.makeDir(targetPath);
// console.log("创建目录---" + targetPath + ",返回值 = " + response);
var dirs = fs.readdirSync(dir);
for (var i = 0; i < dirs.length; i++) {
var sub_dir = dirs[i];
var fullOriginPath = path.join(dir, sub_dir);
var fullTargetPath = path.join(targetPath, sub_dir);
var newTargetPathDir = fullTargetPath.replace(config.projectConfig.dirReplaceStr, config.pathConfig.projectEnName);
var fileName = self.getFileNameByProjectEnName(config.pathConfig.projectEnName);
var newTargetFilePathDir = newTargetPathDir.replace(config.projectConfig.fileReplaceStr, fileName);
self.copyDir(fullOriginPath, newTargetFilePathDir, level + 1);
}
if (level == 0) {
// console.log("复制文件成功!");
}
}
}
/**
* 删除目录及下所有文件
* @param {要删除文件目录路径} filePath
* @param {删除深度} level
*/
exports.removeDir = function (filePath, level) {
// 先判断当前filePath的类型(文件还是文件夹,如果是文件直接删除, 如果是文件夹, 去取当前文件夹下的内容, 拿到每一个递归)
var responseS = self.fileExist(filePath);
if (responseS != 1) { //文件不存在
console.log("filepath==>" + responseS);
return;
}
//读取文件信息
var fileInfo = fs.statSync(filePath);
if (fileInfo.isFile()) {
self.deleteFile(filePath);
// console.log("删除文件==" + filePath);
if (level == 0) {
console.log("删除原工程目录成功");
}
} else {
var dirs = fs.readdirSync(filePath);
for (var i = 0; i < dirs.length; i++) {
var sub_dir = dirs[i];
var newPath = path.join(filePath, sub_dir);
self.removeDir(newPath, level + 1);
}
//删除当前目录
self.deleteDir(filePath);
if (level == 0) {
console.log("删除原工程目录成功");
}
}
}
/**
* 读取文件的内容
*/
exports.readLocalFile = function (pathFile) {
if (self.fileExist(pathFile) != 1) { //文件不存在
console.log("读取的文件不存在 == >" + pathFile);
return null;
}
var data = fs.readFileSync(pathFile, 'utf-8');
return data;
}
/**
* 向文件里写入内容
* @param {写文件路径} pathFile
*/
exports.writeLocalFile = function (pathFile, data) {
if (self.fileExist(pathFile) == 1) //文件存在了 ,进行删除,再写入
{
console.log("写入的文件已存在 == >" + pathFile);
self.deleteFile(pathFile);
}
var options = { encoding: 'utf8', mode: 0777 /*=0666*/, flag: 'w' };
fs.writeFileSync(pathFile, data, options);
return;
}
/**
* 将工程英文名转换成首字母大写的英文名
* @param {英文名} projectEnName
*/
exports.getFileNameByProjectEnName = function (projectEnName) {
if (projectEnName.length > 0) {
var firstLetter = projectEnName.slice(0, 1);
var afterStr = projectEnName.slice(1, projectEnName.length);
var upperLetter = firstLetter.toLocaleUpperCase();
return upperLetter + afterStr;
}
return projectEnName;
}