-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
executable file
·210 lines (184 loc) · 5.14 KB
/
Copy pathcommand.js
File metadata and controls
executable file
·210 lines (184 loc) · 5.14 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* eslint no-var: 0 */
var exec = require("child_process").exec;
var fs = require("fs-extra");
function moveFile(filePath,destinationPath,callback){
if(!destinationPath){
throw new Error("Destination directory missing");
}
if(!filePath){
throw new Error("Source file missing");
}
fs.move(filePath,destinationPath,function(error){
if(error){
return console.error(error)
}else{
console.log("Move: " + filePath + " moved to " + destinationPath)
if(callback){
callback()
}
}
});
}
function copyFile(filePath,destinationPath,callback){
if(!destinationPath){
throw new Error("Destination directory missing");
}
if(!filePath){
throw new Error("Source file missing");
}
fs.copy(filePath,destinationPath,function(error){
if(error){
return console.error(error)
}else{
console.log("COPY: " + filePath + " copied to " + destinationPath)
if(callback){
callback()
}
}
});
}
function copyDir(source,dest,callback){
if(!source){
throw new Error("Source directory missing");
}
if(!dest){
throw new Error("Destination directory missing");
}
fs.copy(source,dest,function(error){
if(error){
return console.error(error)
}else{
console.log( "COPY: " + source + " copied to " + dest)
if(callback){
callback();
}
}
});
}
function createDir(dirPath,callback){
if(!dirPath){
throw new Error("directory path missing");
}
fs.ensureDir(dirPath,function(error){
if(error){
return console.error(error)
}else{
console.log( "Directory created at : " + dirPath)
if(callback){
callback();
}
}
});
}
function remove(source,callback){
fs.remove(source, function(error) {
if (error) {
return console.error(error)
}
console.log("REMOVE: " + source + " deleted")
if(callback){
callback();
}
})
}
function writeJson(source,json){
fs.writeJson(source,json, function(error) {
if (error) {
return console.error(error)
}
console.log("WRITE: " + source + " created")
})
}
function updateJson(source,newJson,callback){
fs.readJson(source,function(error,sourceJson){
var json = Object.assign({},sourceJson,newJson);
fs.outputJson(source,json, function(error) {
if (error) {
return console.error(error)
}
console.log("WRITE: " + source + " created")
if(callback){
callback();
}
})
})
}
function changeCurrentWorkingDirTemporarily(dirPath, command){
//For this Node process "bower install" working directory is changed to build/client
//upon completion Node cwd will shift back to root where we started this process.
execute(command, false,{
cwd:dirPath
});
}
function execute(commands,setNodeEnv,options,callback){
if(Array.isArray(commands)){
commands.map(function(cmdLine){
_execute(cmdLine,setNodeEnv,options,callback);
})
}else {
_execute(commands,setNodeEnv,options,callback);
}
}
function _execute(cmdLine,setNodeEnv,options,callback){
if(typeof cmdLine !== "string"){
throw new Error("String format expected for commands")
}
if(setNodeEnv){
var environ = (!process.argv[2].indexOf("development")) ? "development" : "production";
if (process.platform === "win32") {
cmdLine = "set NODE_ENV=" + environ + "&& " + cmdLine;
} else {
cmdLine = "NODE_ENV=" + environ + " " + cmdLine;
}
}
var command;
if(options) {
command = exec(cmdLine,options,(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if(callback){
callback();
}
});
}else {
command = exec(cmdLine,(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if(callback){
callback();
}
});
}
if(command){
/* eslint-disable */
command.stdout.on("data", function(data) {
process.stdout.write(data);
});
command.stderr.on("data", function(data) {
process.stderr.write(data);
});
command.on("error", function(err) {
process.stderr.write(err);
});
/* eslint-enable */
}
}
module.exports = {
moveFile,
copyFile,
copyDir,
createDir,
remove,
writeJson,
updateJson,
execute,
changeCurrentWorkingDirTemporarily
};