-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
166 lines (128 loc) · 5.19 KB
/
build.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
const fs = require('fs-extra');
const path = require('path');
const currentFolderName = path.basename(__dirname);
// 源文件夹的路径
const sourceDir = `../${currentFolderName}`;
// 目标文件夹的路径
const targetDir = '../';
// 新文件夹的名称
const newFolderName = `${currentFolderName}-copy`;
// 构建新文件夹的完整路径
const newFolderPath = path.join(targetDir, newFolderName);
// 创建新文件夹
fs.ensureDir(newFolderPath, (err) => {
if (err) {
console.error('Error creating new folder:', err);
} else {
console.log('New folder created successfully!');
// 使用fs-extra的copy方法进行文件夹及其目录下所有文件的复制
fs.copy(sourceDir, newFolderPath, (copyErr) => {
if (copyErr) {
console.error('Error copying directory:', copyErr);
} else {
console.log('Directory copied to new folder successfully!');
function findGetTemplatePartCallsInPhp(filePath, result = []) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const pattern = /get_template_part\s*\(\s*['"]([^'"]+)['"]/g;
let match;
while ((match = pattern.exec(fileContent)) !== null) {
result.push(match[1]);
}
return result;
}
function searchPhpFiles(directoryPath) {
const allFiles = getAllFilesInDirectory(directoryPath);
const getTemplatePartResults = [];
allFiles.forEach(filePath => {
const fileExtension = path.extname(filePath).toLowerCase();
if (fileExtension === '.php') {
const templatePartCalls = findGetTemplatePartCallsInPhp(filePath);
if (templatePartCalls.length > 0) {
getTemplatePartResults.push({
file: filePath,
calls: templatePartCalls
});
}
}
});
return getTemplatePartResults;
}
function getAllFilesInDirectory(directoryPath, fileList = []) {
const files = fs.readdirSync(directoryPath);
files.forEach(file => {
const filePath = path.join(directoryPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
if (file !== 'node_modules') {
getAllFilesInDirectory(filePath, fileList);
}
} else if (stats.isFile()) {
fileList.push(filePath);
}
});
return fileList;
}
const directoryPath = '.'; // 当前目录
const results = searchPhpFiles(directoryPath);
console.log('Results:', results);
function extractLastSegments(results) {
const lastSegments = [];
results.forEach(result => {
result.calls.forEach(call => {
const segments = call.split('/');
const lastSegment = segments[segments.length - 1];
lastSegments.push(lastSegment);
});
});
return lastSegments;
}
function deleteFilesNotInLastSegmentsArray(directoryPath, lastSegmentsArray) {
const deletedFiles = [];
function deletePhpFiles(directory) {
const files = fs.readdirSync(directory);
files.forEach(file => {
const filePath = path.join(directory, file);
const fileExtension = path.extname(file).toLowerCase();
if (fs.statSync(filePath).isFile()) {
if (fileExtension === '.php') {
const fileName = path.basename(file, '.php'); // 获取文件名(不含后缀)
if (!lastSegmentsArray.includes(fileName)) {
fs.unlinkSync(filePath); // 使用 unlinkSync 删除文件
deletedFiles.push(filePath);
}
}
} else if (fs.statSync(filePath).isDirectory()) {
deletePhpFiles(filePath); // 递归处理子目录
}
});
}
deletePhpFiles(directoryPath);
return deletedFiles;
}
const lastSegmentsArray = extractLastSegments(results);
console.log('Last Segments:', lastSegmentsArray);
const deletedFiles = deleteFilesNotInLastSegmentsArray('./template_parts', lastSegmentsArray);
console.log('Deleted Files:', deletedFiles);
function deleteEmptyFoldersRecursively(directoryPath) {
if (fs.existsSync(directoryPath)) {
const files = fs.readdirSync(directoryPath);
if (files.length === 0) {
fs.rmdirSync(directoryPath); // 删除空文件夹
return;
}
files.forEach(file => {
const filePath = path.join(directoryPath, file);
if (fs.statSync(filePath).isDirectory()) {
deleteEmptyFoldersRecursively(filePath); // 递归处理子文件夹
}
});
}
}
const templatePartsPath = path.join(directoryPath, 'template_parts');
deleteEmptyFoldersRecursively(templatePartsPath);
fs.removeSync('./node_modules');
fs.removeSync('./preview')
}
});
}
});