Skip to content

Commit df64f94

Browse files
committedJun 6, 2024··
feat(files.service): update 'isDangerous' rules
1 parent 8ca964b commit df64f94

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed
 

‎__tests__/files.service.test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,38 @@ describe('File Service', () => {
147147
});
148148

149149
describe('#isDangerous', () => {
150-
it('should get false if is considered dangerous', () => {
150+
it('should return false for paths that are not considered dangerous', () => {
151151
expect(
152152
fileService.isDangerous('/home/apps/myapp/node_modules'),
153153
).toBeFalsy();
154154
expect(fileService.isDangerous('node_modules')).toBeFalsy();
155155
expect(
156156
fileService.isDangerous('/home/user/projects/a/node_modules'),
157157
).toBeFalsy();
158+
expect(
159+
fileService.isDangerous('/Applications/NotAnApp/node_modules'),
160+
).toBeFalsy();
161+
expect(
162+
fileService.isDangerous('C:\\Users\\User\\Documents\\node_modules'),
163+
).toBeFalsy();
158164
});
159165

160-
it('should get true if is not considered dangerous ', () => {
166+
it('should return true for paths that are considered dangerous', () => {
161167
expect(
162168
fileService.isDangerous('/home/.config/myapp/node_modules'),
163169
).toBeTruthy();
164170
expect(fileService.isDangerous('.apps/node_modules')).toBeTruthy();
165171
expect(
166172
fileService.isDangerous('.apps/.sample/node_modules'),
167173
).toBeTruthy();
174+
expect(
175+
fileService.isDangerous('/Applications/MyApp.app/node_modules'),
176+
).toBeTruthy();
177+
expect(
178+
fileService.isDangerous(
179+
'C:\\Users\\User\\AppData\\Local\\node_modules',
180+
),
181+
).toBeTruthy();
168182
});
169183
});
170184

‎src/services/files/files.service.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,26 @@ export abstract class FileService implements IFileService {
6464
return path.includes(targetFolder);
6565
}
6666

67-
/** We consider a directory to be dangerous if it is hidden.
68-
*
67+
/**
6968
* > Why dangerous?
7069
* It is probable that if the node_module is included in some hidden directory, it is
7170
* required by some application like "spotify", "vscode" or "Discord" and deleting it
7271
* would imply breaking the application (until the dependencies are reinstalled).
72+
*
73+
* In the case of macOS applications and Windows AppData directory, these locations often contain
74+
* application-specific data or configurations that should not be tampered with. Deleting node_modules
75+
* from these locations could potentially disrupt the normal operation of these applications.
7376
*/
7477
isDangerous(path: string): boolean {
7578
const hiddenFilePattern = /(^|\/)\.[^/.]/g;
76-
return hiddenFilePattern.test(path);
79+
const macAppsPattern = /(^|\/)Applications\/[^/]+\.app\//g;
80+
const windowsAppDataPattern = /(^|\\)AppData\\/g;
81+
82+
return (
83+
hiddenFilePattern.test(path) ||
84+
macAppsPattern.test(path) ||
85+
windowsAppDataPattern.test(path)
86+
);
7787
}
7888

7989
async getRecentModificationInDir(path: string): Promise<number> {

0 commit comments

Comments
 (0)
Please sign in to comment.