Skip to content

Commit f70c2af

Browse files
committed
refactor: extract ProjectCleanupService
1 parent 06e238f commit f70c2af

File tree

4 files changed

+97
-44
lines changed

4 files changed

+97
-44
lines changed

lib/bootstrap.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ injector.requirePublic(
1313
"projectConfigService",
1414
"./services/project-config-service"
1515
);
16+
injector.requirePublic(
17+
"projectCleanupService",
18+
"./services/project-cleanup-service"
19+
);
1620
injector.require("performanceService", "./services/performance-service");
1721
injector.requirePublic("projectService", "./services/project-service");
1822
injector.require("androidProjectService", "./services/android-project-service");

lib/commands/clean.ts

+8-44
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import * as path from "path";
21
import { ICommand, ICommandParameter } from "../common/definitions/commands";
32
import { injector } from "../common/yok";
4-
import { IFileSystem, IProjectHelper } from "../common/declarations";
53
import * as constants from "../constants";
6-
import { IProjectConfigService } from "../definitions/project";
4+
import {
5+
IProjectCleanupService,
6+
IProjectConfigService,
7+
} from "../definitions/project";
78

89
export class CleanCommand implements ICommand {
910
public allowedParameters: ICommandParameter[] = [];
1011

1112
constructor(
12-
private $fs: IFileSystem,
13-
private $logger: ILogger,
14-
private $projectHelper: IProjectHelper,
13+
private $projectCleanupService: IProjectCleanupService,
1514
private $projectConfigService: IProjectConfigService,
1615
private $terminalSpinnerService: ITerminalSpinnerService
1716
) {}
@@ -34,48 +33,13 @@ export class CleanCommand implements ICommand {
3433
if (Array.isArray(additionalPaths)) {
3534
pathsToClean.push(...additionalPaths);
3635
}
37-
38-
for (const pathToClean of pathsToClean) {
39-
const filePath = path.resolve(
40-
this.$projectHelper.projectDir,
41-
pathToClean
42-
);
43-
const displayPath = `${path.relative(
44-
this.$projectHelper.projectDir,
45-
filePath
46-
)}`.yellow;
47-
spinner.start(`Cleaning ${displayPath}`);
48-
this.$logger.trace(`Trying to clean '${filePath}'`);
49-
if (this.$fs.exists(filePath)) {
50-
const stat = this.$fs.getFsStats(filePath);
51-
52-
if (stat.isDirectory()) {
53-
this.$logger.trace(`Path '${filePath}' is a directory, deleting.`);
54-
this.$fs.deleteDirectorySafe(filePath);
55-
spinner.text = `Cleaned directory ${displayPath}`;
56-
spinner.succeed();
57-
} else {
58-
this.$logger.trace(`Path '${filePath}' is a file, deleting.`);
59-
this.$fs.deleteFile(filePath);
60-
spinner.text = `Cleaned file ${displayPath}`;
61-
spinner.succeed();
62-
}
63-
} else {
64-
spinner.text = `Skipping ${displayPath} because it doesn't exist.`;
65-
spinner.info();
66-
this.$logger.trace(`Path '${filePath}' not found, skipping.`);
67-
}
68-
}
6936
} catch (err) {
70-
// ignore any errors
71-
this.$logger.trace(
72-
`Encountered error while cleaning. Error is: ${err.message}.`,
73-
err
74-
);
37+
// ignore
7538
}
7639

40+
await this.$projectCleanupService.clean(pathsToClean);
41+
7742
spinner.succeed("Project successfully cleaned.");
78-
// this.$logger.info("Project successfully cleaned.");
7943
}
8044
}
8145

lib/definitions/project.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,20 @@ interface IProjectDataService {
293293
getNSValueFromContent(jsonData: Object, propertyName: string): any;
294294
}
295295

296+
interface IProjectCleanupService {
297+
/**
298+
* Clean multiple paths
299+
* @param {string[]} pathsToClean
300+
*/
301+
clean(pathsToClean: string[]): Promise<void>;
302+
303+
/**
304+
* Clean a single path
305+
* @param {string} pathToClean
306+
*/
307+
cleanPath(pathToClean: string): Promise<void>;
308+
}
309+
296310
interface IProjectConfigInformation {
297311
hasTSConfig: boolean;
298312
hasJSConfig: boolean;
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { IProjectCleanupService } from "../definitions/project";
2+
import { IFileSystem, IProjectHelper } from "../common/declarations";
3+
import { injector } from "../common/yok";
4+
import * as path from "path";
5+
6+
export class ProjectCleanupService implements IProjectCleanupService {
7+
private spinner: ITerminalSpinner;
8+
9+
constructor(
10+
private $fs: IFileSystem,
11+
private $logger: ILogger,
12+
private $projectHelper: IProjectHelper,
13+
private $terminalSpinnerService: ITerminalSpinnerService
14+
) {
15+
this.spinner = this.$terminalSpinnerService.createSpinner();
16+
}
17+
18+
public async clean(pathsToClean: string[]): Promise<void> {
19+
for (const pathToClean of pathsToClean) {
20+
await this.cleanPath(pathToClean).catch((error) => {
21+
this.$logger.trace(
22+
`Encountered error while cleaning. Error is: ${error.message}.`,
23+
error
24+
);
25+
});
26+
}
27+
}
28+
29+
public async cleanPath(pathToClean: string): Promise<void> {
30+
if (!pathToClean || pathToClean.trim().length === 0) {
31+
this.$logger.trace("cleanPath called with no pathToClean.");
32+
return;
33+
}
34+
35+
const filePath = path.resolve(this.$projectHelper.projectDir, pathToClean);
36+
const displayPath = `${path.relative(
37+
this.$projectHelper.projectDir,
38+
filePath
39+
)}`.yellow;
40+
41+
this.spinner.start(`Cleaning ${displayPath}`);
42+
this.$logger.trace(`Trying to clean '${filePath}'`);
43+
44+
if (this.$fs.exists(filePath)) {
45+
const stat = this.$fs.getFsStats(filePath);
46+
47+
if (stat.isDirectory()) {
48+
this.$logger.trace(`Path '${filePath}' is a directory, deleting.`);
49+
50+
this.$fs.deleteDirectorySafe(filePath);
51+
52+
this.spinner.text = `Cleaned directory ${displayPath}`;
53+
this.spinner.succeed();
54+
} else {
55+
this.$logger.trace(`Path '${filePath}' is a file, deleting.`);
56+
57+
this.$fs.deleteFile(filePath);
58+
59+
this.spinner.text = `Cleaned file ${displayPath}`;
60+
this.spinner.succeed();
61+
}
62+
return;
63+
}
64+
65+
this.$logger.trace(`Path '${filePath}' not found, skipping.`);
66+
this.spinner.text = `Skipping ${displayPath} because it doesn't exist.`;
67+
this.spinner.info();
68+
}
69+
}
70+
71+
injector.register("projectCleanupService", ProjectCleanupService);

0 commit comments

Comments
 (0)