-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-helper.ts
72 lines (60 loc) · 2.18 KB
/
project-helper.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
import * as path from "path";
export class ProjectHelper implements IProjectHelper {
constructor(private $logger: ILogger,
private $fs: IFileSystem,
private $staticConfig: Config.IStaticConfig,
private $errors: IErrors,
private $options: IOptions) { }
private cachedProjectDir = "";
public get projectDir(): string {
if (this.cachedProjectDir !== "") {
return this.cachedProjectDir;
}
this.cachedProjectDir = null;
let projectDir = path.resolve(this.$options.path || ".");
while (true) {
this.$logger.trace("Looking for project in '%s'", projectDir);
const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
if (this.$fs.exists(projectFilePath) && this.isProjectFileCorrect(projectFilePath)) {
this.$logger.debug("Project directory is '%s'.", projectDir);
this.cachedProjectDir = projectDir;
break;
}
const dir = path.dirname(projectDir);
if (dir === projectDir) {
this.$logger.debug("No project found at or above '%s'.", this.$options.path || path.resolve("."));
break;
}
projectDir = dir;
}
return this.cachedProjectDir;
}
public generateDefaultAppId(appName: string, baseAppId: string): string {
let sanitizedName = this.sanitizeName(appName);
if (sanitizedName) {
if (/^\d+$/.test(sanitizedName)) {
sanitizedName = "the" + sanitizedName;
}
} else {
sanitizedName = "the";
}
return `${baseAppId}.${sanitizedName}`;
}
public sanitizeName(appName: string): string {
const sanitizedName = _.filter(appName.split(""), (c) => /[a-zA-Z0-9]/.test(c)).join("");
return sanitizedName;
}
private isProjectFileCorrect(projectFilePath: string): boolean {
if (this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE) {
try {
const fileContent = this.$fs.readJson(projectFilePath);
const clientSpecificData = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] && fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
return !!clientSpecificData;
} catch (err) {
this.$errors.failWithoutHelp("The project file is corrupted. Additional technical information: %s", err);
}
}
return true;
}
}
$injector.register("projectHelper", ProjectHelper);