forked from fsystech/cwserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.js
More file actions
99 lines (99 loc) · 3.38 KB
/
Copy pathclean.js
File metadata and controls
99 lines (99 loc) · 3.38 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
const _fs = require('fs');
const _fsRmdirSync = typeof (_fs.rmSync) === "function" ? _fs.rmSync : _fs.rmdirSync;
const _path = require('path');
console._info = console.info;
console._log = console.log;
console.log = (color, msg) => {
if (!msg) return console._log(color);
console._log(color, `-- ${msg}`)
}
//const os = require( "os" );
//const tempPath = _path.resolve( `${os.tmpdir()}/cwserver/` );
//_fs.exists( tempPath, ( exists ) => {
// if ( !exists ) {
// _fs.mkdir( tempPath, ( err ) => {
// console.log( err || "Success" );
// } );
// }
// console.log( `exists=>${exists}` );
// console.log( tempPath );
//} );
console.success = (msg) => {
console.log('\x1b[32m', msg);
}
console.info = (msg) => {
console.log('\x1b[33m%s\x1b[0m', msg);
}
console.error = (msg) => {
console.log('\x1b[31m', msg);
}
console.reset = () => {
console.log('\x1b[0m');
}
const deleteFolderRecursive = function (path, count) {
if (!count) count = 0;
if (_fs.existsSync(path)) {
_fs.readdirSync(path).forEach((file, index) => {
const curPath = _path.join(path, file);
if (_fs.statSync(curPath).isDirectory()) {
deleteFolderRecursive(curPath, count + 1);
} else {
//console.error( `Deleting:\r\n${curPath}` );
_fs.unlinkSync(curPath);
}
});
_fsRmdirSync(path, { recursive: true });
} else {
if (count > 0) return;
return console.info(`Doesn't exist: ${path}`);
}
};
const getRootFiles = (rootPath, filter) => {
const files = _fs.readdirSync(rootPath);
const matchFiles = [];
for (const path of files) {
const absPath = _path.join(rootPath, path);
if (!_fs.existsSync(absPath)) continue;
const stat = _fs.statSync(absPath);
if (stat.isDirectory()) continue;
const p = _path.parse(path);
if (p.ext.indexOf(filter) >= 0) {
console.info(`found:\r\n${absPath}`);
matchFiles.push({
absPath, parsedPath: p
});
}
}
return matchFiles;
}
const deleteRootFiles = (rootPath, filter) => {
const matchFiles = getRootFiles(rootPath, filter);
if (matchFiles.length === 0) {
return console.info(`No ${filter} file found in ${rootPath}`);
}
matchFiles.forEach(a => {
console.info(`Working file:\r\n${a.absPath}`);
const index = a.parsedPath.name.lastIndexOf(".");
if (index > 0) {
const jsfileName = `${a.parsedPath.name.substring(0, index)}.js`;
//.js.map
const jsFile = _path.resolve(`${rootPath}/${jsfileName}`);
if (_fs.existsSync(jsFile)) {
//console.error( `Deleting file:\r\n${jsFile}` );
_fs.unlinkSync(jsFile);
}
const jsMapFile = _path.resolve(`${rootPath}/${jsfileName}.map`);
if (_fs.existsSync(jsMapFile)) {
//console.error( `Deleting file:\r\n${jsMapFile}` );
_fs.unlinkSync(jsMapFile);
}
}
//console.error( `Deleting file:\r\n${a.absPath}` );
_fs.unlinkSync(a.absPath);
});
}
deleteFolderRecursive(_path.resolve('dist/lib'));
deleteFolderRecursive(_path.resolve('dist/test'));
deleteRootFiles(_path.resolve('dist'), "ts");
deleteRootFiles(_path.resolve('dist/error_page'), "cach");
console.reset();