Skip to content

Commit

Permalink
filter git env.
Browse files Browse the repository at this point in the history
  • Loading branch information
tangkunyin committed Sep 7, 2017
1 parent bb4c3b9 commit 7acb9ee
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ $.isFile = function(file){
* @return {[type]} [description]
*/
$.writeFile = function(filepath, content) {
$.mkdir(path.dirname(filepath));
return fs.writeFileSync(filepath, content);
}
var dir = path.dirname(filepath);
if ($.filterGitEnv(dir)){
$.mkdir(dir);
return fs.writeFileSync(filepath, content);
}
};

/**
* 读取文件
Expand Down Expand Up @@ -175,7 +178,6 @@ $.copy = function(distDir, destDir, judgeFunction) {
*/
$.renderFile = function(filepath, data) {
var str = $.readFile(filepath);
// console.log(filepath, str);
return ejs.render(str, data || {});
};
/**
Expand All @@ -195,22 +197,24 @@ $.renderStr = function(str, data) {
* @return {[type]} [description]
*/
$.mkdir = function(dirpath, mode) {
if (mode == null) {
mode = parseInt('0777', 8) & (~process.umask());
}
dirpath.split(pathSeparatorRe).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!$.exists(subpath)) {
try {
fs.mkdirSync(subpath, mode);
} catch (e) {
console.log('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
if ($.filterGitEnv(dirpath)){
if (mode == null) {
mode = parseInt('0777', 8) & (~process.umask());
}
return parts;
}, '');
}
dirpath.split(pathSeparatorRe).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!$.exists(subpath)) {
try {
fs.mkdirSync(subpath, mode);
} catch (e) {
console.log('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
}
return parts;
}, '');
}
};

/**
* 判断是否存在
Expand All @@ -221,4 +225,18 @@ $.exists = function() {
return fs.existsSync(filepath);
};

/**
* 滤掉Git环境
* @param dir
* @returns {boolean}
*/
$.filterGitEnv = function (dir) {
var reg = new RegExp(".git");
if (reg.test(dir)){
console.log(('路径 "' + dir + '" 为Git环境,已自动过滤').bold.yellow);
return false;
}
return true;
};

module.exports = $;

0 comments on commit 7acb9ee

Please sign in to comment.