forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject-cleanup-service.ts
135 lines (114 loc) · 3.51 KB
/
project-cleanup-service.ts
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
import {
IProjectCleanupOptions,
IProjectCleanupResult,
IProjectCleanupService,
IProjectPathCleanupResult,
} from "../definitions/project";
import { IFileSystem, IProjectHelper } from "../common/declarations";
import { injector } from "../common/yok";
import * as path from "path";
import {
ITerminalSpinner,
ITerminalSpinnerService,
} from "../definitions/terminal-spinner-service";
export class ProjectCleanupService implements IProjectCleanupService {
private spinner: ITerminalSpinner;
constructor(
private $fs: IFileSystem,
private $logger: ILogger,
private $projectHelper: IProjectHelper,
private $terminalSpinnerService: ITerminalSpinnerService
) {}
public async clean(
pathsToClean: string[],
options?: IProjectCleanupOptions
): Promise<IProjectCleanupResult> {
this.spinner = this.$terminalSpinnerService.createSpinner({
isSilent: options?.silent,
});
let stats = options?.stats ? new Map<string, number>() : false;
let success = true;
for (const pathToClean of pathsToClean) {
const cleanRes = await this.cleanPath(pathToClean, options).catch(
(error) => {
this.$logger.trace(
`Encountered error while cleaning. Error is: ${error.message}.`,
error
);
return { ok: false };
}
);
if (stats && "size" in cleanRes) {
stats.set(pathToClean, cleanRes.size);
}
success = success && cleanRes.ok;
}
if (!options?.silent) {
// required to print an empty line for the spinner to not replace the last status... (probably a bug in the spinners)
console.log();
}
if (stats) {
return { ok: success, stats };
}
return { ok: success };
}
public async cleanPath(
pathToClean: string,
options?: IProjectCleanupOptions
): Promise<IProjectPathCleanupResult> {
const dryRun = options?.dryRun ?? false;
const logPrefix = dryRun ? "(dry run) ".grey : "";
this.spinner.clear();
let fileType: string;
if (!pathToClean || pathToClean.trim().length === 0) {
this.$logger.trace(`${logPrefix}cleanPath called with no pathToClean.`);
return { ok: true };
}
const filePath = path.resolve(this.$projectHelper.projectDir, pathToClean);
const displayPath = `${path.relative(
this.$projectHelper.projectDir,
filePath
)}`.yellow;
this.$logger.trace(`${logPrefix}Trying to clean '${filePath}'`);
if (this.$fs.exists(filePath)) {
const stat = this.$fs.getFsStats(filePath);
let size = 0;
if (options?.stats) {
size = this.$fs.getSize(filePath);
}
if (stat.isDirectory()) {
this.$logger.trace(
`${logPrefix}Path '${filePath}' is a directory, deleting.`
);
!dryRun && this.$fs.deleteDirectorySafe(filePath);
fileType = "directory";
} else {
this.$logger.trace(
`${logPrefix}Path '${filePath}' is a file, deleting.`
);
!dryRun && this.$fs.deleteFile(filePath);
fileType = "file";
}
const success = dryRun || !this.$fs.exists(filePath);
if (success) {
this.spinner.succeed(`${logPrefix}Cleaned ${fileType} ${displayPath}`);
} else {
const message = `Failed to Clean ${fileType}`.red;
this.spinner.fail(`${logPrefix}${message} ${displayPath}`);
}
if (options?.stats) {
return { ok: success, size };
}
return { ok: success };
}
this.$logger.trace(`${logPrefix}Path '${filePath}' not found, skipping.`);
this.spinner.info(
`${logPrefix}Skipping ${displayPath} because it doesn't exist.`
);
if (options?.stats) {
return { ok: true, size: 0 };
}
return { ok: true };
}
}
injector.register("projectCleanupService", ProjectCleanupService);