-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-service.ts
104 lines (86 loc) · 3.84 KB
/
project-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
///<reference path="../.d.ts"/>
import path = require("path");
import options = require("./../options");
import shell = require("shelljs");
import osenv = require("osenv");
export class ProjectService implements IProjectService {
private static DEFAULT_ID = "com.telerik.tns.HelloWorld";
private static DEFAULT_NAME = "HelloNativescript";
private static APP_FOLDER_NAME = "app";
constructor(private $logger: ILogger,
private $errors: IErrors,
private $fs: IFileSystem,
private $projectTemplatesService: IProjectTemplatesService) { }
public createProject(projectName: string, projectId: string): IFuture<void> {
return(() => {
var projectDir = path.resolve(options.path || ".");
projectId = projectId || ProjectService.DEFAULT_ID;
projectName = projectName || ProjectService.DEFAULT_NAME;
projectDir = path.join(projectDir, projectName);
this.$fs.createDirectory(projectDir).wait();
var customAppPath = this.getCustomAppPath();
if(customAppPath) {
customAppPath = path.resolve(customAppPath);
}
if(this.$fs.exists(projectDir).wait() && !this.$fs.isEmptyDir(projectDir).wait()) {
this.$errors.fail("Path already exists and is not empty %s", projectDir);
}
this.$logger.trace("Creating a new NativeScript project with name %s and id at location", projectName, projectId, projectDir);
var appDirectory = path.join(projectDir, ProjectService.APP_FOLDER_NAME);
var appPath: string = null;
if(customAppPath) {
this.$logger.trace("Using custom app from %s", customAppPath);
// Make sure that the source app/ is not a direct ancestor of a target app/
var relativePathFromSourceToTarget = path.relative(customAppPath, appDirectory);
var doesRelativePathGoUpAtLeastOneDir = relativePathFromSourceToTarget.split(path.sep)[0] == "..";
if(!doesRelativePathGoUpAtLeastOneDir) {
this.$errors.fail("Project dir %s must not be created at/inside the template used to create the project %s.", projectDir, customAppPath);
}
this.$logger.trace("Copying custom app into %s", appDirectory);
appPath = customAppPath;
} else {
// No custom app - use nativescript hello world application
this.$logger.trace("Using NativeScript hello world application");
var defaultTemplatePath = this.$projectTemplatesService.defaultTemplatePath.wait();
this.$logger.trace("Copying Nativescript hello world application into %s", appDirectory);
appPath = defaultTemplatePath;
}
this.createProjectCore(projectDir, appPath, false).wait();
}).future<void>()();
}
private createProjectCore(projectDir: string, appPath: string, symlink?: boolean): IFuture<void> {
return (() => {
if(!this.$fs.exists(projectDir).wait()) {
this.$fs.createDirectory(projectDir).wait();
}
if(symlink) {
// TODO: Implement support for symlink the app folder instead of copying
} else {
var appDir = path.join(projectDir, ProjectService.APP_FOLDER_NAME);
this.$fs.createDirectory(appDir).wait();
shell.cp('-R', path.join(appPath, "*"), appDir);
}
this.createBasicProjectStructure(projectDir).wait();
}).future<void>()();
}
private createBasicProjectStructure(projectDir: string): IFuture<void> {
return (() => {
this.$fs.createDirectory(path.join(projectDir, "platforms")).wait();
this.$fs.createDirectory(path.join(projectDir, "tns_modules")).wait();
this.$fs.createDirectory(path.join(projectDir, "hooks")).wait();
}).future<void>()();
}
private getCustomAppPath(): string {
var customAppPath = options["copy-from"] || options["link-to"];
if(customAppPath) {
if(customAppPath.indexOf("http") >= 0) {
this.$errors.fail("Only local paths for custom app are supported.");
}
if(customAppPath.substr(0, 1) === '~') {
customAppPath = path.join(osenv.home(), customAppPath.substr(1));
}
}
return customAppPath;
}
}
$injector.register("projectService", ProjectService);