-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-multi-move.js
132 lines (97 loc) · 4.49 KB
/
index-multi-move.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
//KEEP: # 1.1.4
// https://chatgpt.com/share/67758592-d174-8011-be5b-642a87e71019
'use strict';
// const fs = require('fs');
const path = require('path');
const fs = require('fs-extra');
// Function to remove comment lines but keep specific ones
function removeCommentsFromFile(fileContent) {
//KEEP REF: https://chatgpt.com/share/67758592-d174-8011-be5b-642a87e71019
const step1 = fileContent
.replace(/(http:\/\/)/gm, 'http:--')
.replace(/(https:\/\/)/gm, 'https:--'); // ilk yapılan tüm http(s) leri -- ile koruma altına alıyoruz? Nedenki sebebi?
// "KEEP" ve "gm;" etiketlerini koruyan düzenli ifade buda olması lazım //g;
const commentRegex = /\/\/(?!KEEP\b|gm;).*?$|\/\*(?!.*KEEP\b|gm;)[\s\S]*?\*\//gm;
// //g; da eklendi aşağıda.
const commentRegex2 = /\/\/(?!KEEP\b|gm;|g;).*?$|\/\*(?!.*KEEP\b|gm;|g;)[\s\S]*?\*\//gm;
const step2 = step1
.replace(commentRegex2, ''); // ikinci yapılan tüm yorum satırları siliniyor iken "KEEP" ve "gm;" ve "g;" etiketleri de korunuyor.
const step3 = step2
.replace(/(http:--)/gm, 'http://')
.replace(/(https:--)/gm, 'https://');
// const falseIfBlockRegex = /if\s*\(\s*false\s*\)\s*\{[\s\S]*?\}/gm;
const step4 = step3
// .replace(falseIfBlockRegex, '');
return step4;
}
// Recursive function to process files in a directory
function processDirectory(sourceDir, targetDir) {
const HEDEFMEVCUT = fs.existsSync(targetDir)
if (!HEDEFMEVCUT) {
fs.mkdirSync(targetDir); // Create target directory if not exists
// console.log(`Target directory: ${targetDir}`, "CREATED");
}
const items = fs.readdirSync(sourceDir, { withFileTypes: true });
for (const item of items) {
const sourcePath = path.join(sourceDir, item.name);
const targetPath = path.join(targetDir, item.name);
// console.log("TEST(1)",{ sourcePath, targetPath }, {itemisdir:item.isDirectory(),itname:item.name})
if (item.isDirectory() && (item.name !== 'node_modules' && item.name !== 'clean_dist')) {
processDirectory(sourcePath, targetPath);
} else if (item.isFile() && path.extname(item.name) === '.js') {
const fileContent = fs.readFileSync(sourcePath, 'utf8');
const cleanedContent = removeCommentsFromFile(fileContent);
if (targetPath && cleanedContent)
try {
fs.writeFileSync(targetPath, cleanedContent, 'utf8');
// console.log({ targetPath })
} catch (_writeFileSyncERROR) {
console.log({ _writeFileSyncERROR })
}
else {
console.log({ targetPath, cleanedContent });
}
// console.log(sourceDir, "\t\t\t->", targetPath);
}
}
// console.log("Bitti", { sourceDir, targetDir })
}
// Get command line arguments
const args = process.argv.slice(2);
// console.log({ args });
// if (args.length !== 2) {
// console.error('Usage: node [your-js-comments-eraser] <sourceDir> <targetDir>');
// process.exit(1);
// }
if (args.length !== 1) {
console.error('Usage: node [your-js-comments-eraser] <sourceDir>');
process.exit(1);
}
let sourceDir = args[0]; // basit ifade
let targetDir = "clean_dist"; // basit ifade
console.log()
console.log("***********************")
// console.log("Start processing", { sourceDir })
processDirectory(sourceDir, targetDir);
console.log(`The comment lines of all javascript files in the "${sourceDir}" folder were deleted and copied to the "${targetDir}" folder created in the same folder.`);
console.log("***********************")
const exportet = (user__dirname) => {
if (true) {
sourceDir = path.join(user__dirname, targetDir)//** gerçek/tam ifade */
targetDir = path.join(user__dirname, args[0])//** gerçek/tam ifade */
// console.log({ sourceDir, targetDir, user__dirname });
const finalDestination = path.join(targetDir, path.basename(sourceDir));
// console.log("input params", { sourceDir, targetDir })
// console.log("move params2", { sourceDir, finalDestination })
setTimeout(() => {
fs.move(sourceDir, finalDestination, { overwrite: true })
.then(() => {
console.log('Successfully moved.');
})
.catch(err => {
console.error('Some things went wrong:', err);
});
}, 1);
}
}
module.exports.dirname = exportet