-
Notifications
You must be signed in to change notification settings - Fork 0
/
destroy.js
175 lines (152 loc) · 4.54 KB
/
destroy.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
173
174
175
const fs = require('fs');
const {execSync} = require('child_process');
const path = require('path');
const load = require('./load.js');
const os = require('os');
const ProgressBar = require('progress');
/**
* Read all *.pid files inside a project directory
* to kill the processes
* @param {String} projectName - name of the project directory
*/
const killJsLocal = (projectName) => {
const syncPath = path.join(projectName);
const files = fs.readdirSync(syncPath);
const pids = getPIDFiles(files);
const bar = new ProgressBar(`Killing all processes for ${projectName} [:bar] :percent`,
{total: pids.length});
pids.map((pidFile)=>{
const filePath = path.join(projectName, pidFile);
const pid = fs.readFileSync(filePath, 'utf-8');
process.kill(pid);
bar.tick();
});
};
/**
* Validates that the start flag was set when starting this application
*/
const validatorClean = (program) => {
return program.clean;
};
/**
* Validates that the start flag was set when starting this application
*/
const validatorKill = (program) => {
return program.kill;
};
/**
* Validates that the delete flag was set when starting this application
*/
const validatorDelete = (program) => {
return program.delete;
};
/**
* Parse the output of $(ls projectName/) output
* The output of the ls command is a \n separated string
*/
const getPIDFiles = (files) => {
// Only get files with .pid in the name.
// This includes zxcv.pidzxcv, I don't care for now
return files.filter((file)=>{return file.includes('.pid');});
};
const clean = (projectName) => {
const cleanPath = path.join(projectName);
const files = fs.readdirSync(cleanPath);
const bar = new ProgressBar(`Cleaning files ${projectName} [:bar] :percent`, {
total: 3
});
cleanJs(projectName);
bar.tick();
cleanCss(projectName);
bar.tick();
cleanPIDS(projectName);
bar.tick();
};
const cleanPIDS = (projectName) => {
const files = fs.readdirSync(projectName);
const pidFiles = files.filter((file)=>{return file.includes('.pid');});
pidFiles.map((file)=>{
const filePath = path.join(projectName, file);
fs.unlinkSync(filePath);
});
};
const cleanCss = (projectName) => {
const config = load.readConfig(projectName);
if (config.css === 'scss') {
const dir = 'css';
const cssPath = path.join(projectName, dir);
const files = fs.readdirSync(cssPath);
files.map((file)=>{
const filePath = path.join(projectName, dir, file);
fs.unlinkSync(filePath);
});
}
};
const cleanJs = (projectName) => {
const config = load.readConfig(projectName);
if (config.javascript === 'es6') {
const dir = 'browserify';
const es6Path = path.join(projectName, dir);
const files = fs.readdirSync(es6Path);
files.map((file)=>{
const filePath = path.join(projectName, dir, file);
fs.unlinkSync(filePath);
});
}
if (config.package) {
const dir = 'browserify';
const browserifyPath = path.join(projectName, dir);
const files = fs.readdirSync(browserifyPath);
files.map((file)=>{
const filePath = path.join(projectName, dir, file);
fs.unlinkSync(filePath);
});
}
};
/**
* Delete the entire project folder and all its contents
*/
const deleteProjectFiles = (projectName) => {
let numberOfDeletions = 0;
const countFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
const curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
numberOfDeletions++;
countFolderRecursive(curPath);
} else {
// file count
}
});
}
};
countFolderRecursive(projectName);
const bar = new ProgressBar(`Deleting ${projectName} [:bar] :percent`, {
total: numberOfDeletions
});
// https://geedew.com/remove-a-directory-that-is-not-empty-in-nodejs/
const deleteFolderRecursive = function(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
const curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
bar.tick();
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
deleteFolderRecursive(projectName);
};
module.exports = {
killJsLocal: killJsLocal,
clean: clean,
deleteProjectFiles: deleteProjectFiles,
validatorClean: validatorClean,
validatorKill: validatorKill,
validatorDelete: validatorDelete
};