-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathadd-platform-service.ts
65 lines (53 loc) · 2.82 KB
/
add-platform-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
import * as path from "path";
import * as temp from "temp";
import { PROJECT_FRAMEWORK_FOLDER_NAME } from "../../constants";
import { performanceLog } from "../../common/decorators";
export class AddPlatformService implements IAddPlatformService {
constructor(
private $fs: IFileSystem,
private $pacoteService: IPacoteService,
private $projectDataService: IProjectDataService,
private $terminalSpinnerService: ITerminalSpinnerService
) { }
public async addPlatformSafe(projectData: IProjectData, platformData: IPlatformData, packageToInstall: string, nativePrepare: INativePrepare): Promise<string> {
const spinner = this.$terminalSpinnerService.createSpinner();
try {
spinner.start();
const frameworkDirPath = await this.extractPackage(packageToInstall);
const frameworkPackageJsonContent = this.$fs.readJson(path.join(frameworkDirPath, "..", "package.json"));
const frameworkVersion = frameworkPackageJsonContent.version;
await this.setPlatformVersion(platformData, projectData, frameworkVersion);
if (!nativePrepare || !nativePrepare.skipNativePrepare) {
await this.addNativePlatform(platformData, projectData, frameworkDirPath, frameworkVersion);
}
return frameworkVersion;
} catch (err) {
const platformPath = path.join(projectData.platformsDir, platformData.platformNameLowerCase);
this.$fs.deleteDirectory(platformPath);
throw err;
} finally {
spinner.stop();
}
}
public async setPlatformVersion(platformData: IPlatformData, projectData: IProjectData, frameworkVersion: string): Promise<void> {
const frameworkPackageNameData = { version: frameworkVersion };
this.$projectDataService.setNSValue(projectData.projectDir, platformData.frameworkPackageName, frameworkPackageNameData);
}
private async extractPackage(pkg: string): Promise<string> {
temp.track();
const downloadedPackagePath = temp.mkdirSync("runtimeDir");
await this.$pacoteService.extractPackage(pkg, downloadedPackagePath);
const frameworkDir = path.join(downloadedPackagePath, PROJECT_FRAMEWORK_FOLDER_NAME);
return path.resolve(frameworkDir);
}
@performanceLog()
private async addNativePlatform(platformData: IPlatformData, projectData: IProjectData, frameworkDirPath: string, frameworkVersion: string): Promise<void> {
const platformDir = path.join(projectData.platformsDir, platformData.normalizedPlatformName.toLowerCase());
this.$fs.deleteDirectory(platformDir);
await platformData.platformProjectService.createProject(path.resolve(frameworkDirPath), frameworkVersion, projectData);
platformData.platformProjectService.ensureConfigurationFileInAppResources(projectData);
await platformData.platformProjectService.interpolateData(projectData);
platformData.platformProjectService.afterCreateProject(platformData.projectRoot, projectData);
}
}
$injector.register("addPlatformService", AddPlatformService);