-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobsolete_functions.js
187 lines (155 loc) · 4.92 KB
/
obsolete_functions.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
176
177
178
179
180
181
182
183
184
185
186
187
// minstall.js
// this method isn't needed because npm4 doesn't create dead symlinks
// and incompatible versions created through symlinks are cleaned up
// in removeContradictingInstalledDependencies.
// The dead symlinks created with npm5 are '--clean'ed pre-install anyway.
function removeDeadSymlinksInInstalledDependencies() {
let folderPaths = [];
let flattenedFullFilePathArray = [];
let flattenedScopedFolderPaths = [];
let folderToCheck = [];
return moduletools.getAllModulesAndInstalledDependenciesDeep()
.then((result) => {
const localModules = result.modules;
folderPaths = localModules.map((localModule) => {
return path.join(localModule.fullModulePath, 'node_modules');
});
folderPaths.push('.');
return Promise.all(folderPaths.map((folderPath) => {
return systools.readdir(folderPath);
}));
})
.then((filesArray) => {
const fullFilePaths = filesArray.map((files, index) => {
return files.filter((filename) => {
return !filename.startsWith('@') && !filename.startsWith('.');
}).map((filename) => {
return path.join(folderPaths[index], filename);
});
});
const scopedFolderPaths = filesArray.map((files, index) => {
return files.filter((filename) => {
return filename.startsWith('@');
}).map((filename) => {
return path.join(folderPaths[index], filename);
});
});
// flatten the filePaths
flattenedFullFilePathArray = [].concat.apply([], fullFilePaths);
flattenedScopedFolderPaths = [].concat.apply([], scopedFolderPaths);
return Promise.all(flattenedScopedFolderPaths.map((scopedFolderPath) => {
return systools.readdir(scopedFolderPath);
}));
})
.then((scopedDependencyFolder) => {
const fullScopedFolderPaths = scopedDependencyFolder.map((files, index) => {
return files.filter((filename) => {
return !filename.startsWith('.');
}).map((filename) => {
return path.join(flattenedScopedFolderPaths[index], filename);
});
});
const flattenedScopedDependecyFolderPaths = [].concat.apply([], fullScopedFolderPaths);
folderToCheck = flattenedFullFilePathArray.concat(flattenedScopedDependecyFolderPaths);
return Promise.all(folderToCheck.map((filePath) => {
return systools.isBrokenSymlink(filePath);
}));
})
.then((brokenSymlinkInfoArray) => {
const brokenSymlinks = folderToCheck.filter((filePath, index) => {
return brokenSymlinkInfoArray[index];
});
console.log('the following symlinks are broken (point to nowhere) and will be deleted now', brokenSymlinks);
return Promise.all(brokenSymlinks.map((brokenSymlink) => {
return systools.delete(brokenSymlink);
}));
});
}
// systools.js
readdir(folderPath) {
return new Promise((resolve, reject) => {
fs.readdir(folderPath, (error, files) => {
if (error) {
if (error.code === 'ENOENT') {
return resolve([]);
}
return reject(error);
}
return resolve(files);
});
});
},
isBrokenSymlink(location) {
return this.isSymlink(location)
.then((isSymlink) => {
if (!isSymlink) {
return false;
}
return new Promise((resolve, reject) => {
fs.readlink(location, (error, result) => {
if (error) {
return reject(error);
}
fs.access(result, fs.constants.F_OK, (accessError) => {
if (accessError) {
return resolve(true);
}
return resolve(false);
});
});
});
});
},
deleteEmptyFolders(location) {
return this.getFolderNames(location)
.then((folderNames) => {
return Promise.all(folderNames.map((folderName) => {
return this.deleteIfEmptyFolder(path.join(location, folderName));
}));
});
},
deleteIfEmptyFolder(location) {
fs.readdir(location, (error, files) => {
if (error) {
return Promise.reject(error);
}
if (files.length > 0) {
return Promise.resolve();
}
return this.delete(location);
});
},
mkdir(location) {
logger.verbose('mkdir', location);
return new Promise((resolve, reject) => {
fs.mkdirs(location, (error) => {
if (error) {
return reject();
}
return resolve();
});
});
},
deleteMultiple(names, from) {
return Promise.all(names.map((name) => {
return this.delete(path.join(from, name));
}));
},
moveMultiple(names, from, to) {
return Promise.all(names.map((name) => {
return this.move(path.join(from, name), path.join(to, name), {
overwrite: true,
});
}));
},
move(from, to, options) {
logger.verbose('move', from, '->', to);
return new Promise((resolve, reject) => {
fs.move(from, to, options, (error) => {
if (error) {
return reject(error);
}
return resolve();
});
});
},