-
Notifications
You must be signed in to change notification settings - Fork 123
/
zero.js
172 lines (150 loc) · 4.3 KB
/
zero.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Created by jiamiu on 14-8-10.
*/
var Repo = require('git').Repo,
Git = require('git').Git,
fs = require('fs'),
fsExtra = require('fs-extra'),
async = require('async'),
_ = require('lodash'),
path = require('path'),
tosource = require('tosource'),
bundlePath = './bundle',
args = process.argv.splice(2),
repo,
mergePaths = ['config','api']
//node ./zoro.js bundle install
if( args[0] == 'bundle' && args[1] == 'install' ){
new Repo('./',function(err, r){
if( err ){
console.log("begin to init repo")
initZoroRepo( installBundle )
}else{
console.log("begin to save curent status")
repo = r
saveCurrentStatus( installBundle)
}
})
}
//TODO node ./zoro.js bundle uninstall [bundle_name]
//functions
function initZoroRepo( cb ){
createRepo( function(err, r){
if( err ){
return console.log("create repo err", err)
}
repo = r
addGitIgnore( function(){
saveCurrentStatus( cb )
})
})
}
function installBundle(){
var parallelTasks=[]
var bundles = fs.readdirSync( bundlePath).filter(function(b){return !/^\./.test(b)})
_.each( bundles, function( bundle ){
findFile( path.join(bundlePath, bundle), function(err, files){
console.log("begin to install bundle ", bundle)
files.forEach(function( bundleFilePath ){
//the file path is the same as the initial sails app
var filePath =path.relative( path.join( bundlePath,bundle) , bundleFilePath )
if( !fs.existsSync( filePath ) ){
parallelTasks.push(_.partial(fsExtra.copy,path.join( bundlePath,bundle,filePath) , filePath ))
}else{
if( _.any(mergePaths, function(p){ return (new RegExp('^'+p)).test(filePath)})){
var originContent = require( './'+filePath),
bundleContent = require( './'+ path.join( bundlePath,bundle,filePath) )
for( var i in bundleContent ){
if(originContent[i]){
console.log( "module conflict detected, we will not modify origin: ",filePath)
}else{
originContent[i] = bundleContent[i]
parallelTasks.push(_.partial( fs.writeFile,filePath, "module.exports = "+ tosource(originContent)))
}
}
}else{
console.log( "file conflict detected, we will not modify origin: ",filePath)
}
}
})
async.parallel(parallelTasks,function(err){
console.log("bundle file installed, begin to save",err)
saveCurrentStatus( function(){
tagHead(bundles.join('-'),function(err,res){
console.log("tag", err, res)
console.log("install bundle finished")
})
})
})
})
})
}
function findFile( dir, done ){
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
findFile(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
}
function saveCurrentStatus( cb ){
//call status failed
repo.git.git('add',{},'--all',function(err, res){
repo.git.git('commit',{},'-m', 'auto commit',function(err,res){
console.log("commit:",err,res)
cb()
})
})
}
function createRepo( cb ){
var git = new Git('./.git')
git.git('init',{},function(err, res){
console.log( "init err", err,res)
new Repo('./',cb)
})
}
function addGitIgnore( cb ){
var content = [
"config/local.js",
"node_modules",
"bower_components",
".tmp",
"dump.rdb",
"lib-cov",
"*.seed",
"*.log",
"*.out",
"*.pid",
"npm-debug.log",
"*~",
"*#",
".DS_STORE",
".netbeans",
"nbproject",
".idea",
".node_history",
".editorconfig",
"bundle"].join("\n")
fs.writeFile('./.gitignore', content, cb )
}
function tagHead( tag, cb ){
console.log( "begin to tag HEAD", tag)
repo.git.git('tag',{},tag,'HEAD',function(err,res){
console.log( err, res)
cb()
})
}