-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-templates-service.ts
58 lines (47 loc) · 2.58 KB
/
project-templates-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
import * as path from "path";
import * as temp from "temp";
import * as constants from "../constants";
temp.track();
export class ProjectTemplatesService implements IProjectTemplatesService {
public constructor(private $errors: IErrors,
private $fs: IFileSystem,
private $logger: ILogger,
private $npm: INodePackageManager,
private $npmInstallationManager: INpmInstallationManager,
private $analyticsService: IAnalyticsService) { }
public prepareTemplate(originalTemplateName: string, projectDir: string): IFuture<string> {
return ((): string => {
let templateName = originalTemplateName || constants.RESERVED_TEMPLATE_NAMES["default"],
version: string = null;
if (originalTemplateName) {
// support <reserved_name>@<version> syntax
let data = originalTemplateName.split("@"),
name = data[0];
version = data[1];
templateName = constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()] || name;
}
this.$analyticsService.track("Template used for project creation", templateName).wait();
const realTemplatePath = this.prepareNativeScriptTemplate(templateName, version, projectDir).wait();
if (realTemplatePath) {
//this removes dependencies from templates so they are not copied to app folder
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
return realTemplatePath;
}
this.$errors.failWithoutHelp("Unable to find the template in temp directory. " +
`Please open an issue at https://github.com/NativeScript/nativescript-cli/issues and send the output of the same command executed with --log trace.`);
}).future<string>()();
}
/**
* Install verified NativeScript template in the npm cache.
* The "special" here is that npmInstallationManager will check current CLI version and will instal best matching version of the template.
* For example in case CLI is version 10.12.8, npmInstallationManager will try to find latest 10.12.x version of the template.
* @param {string} templateName The name of the verified NativeScript template.
* @param {string} version The version of the template specified by user.
* @return {string} Path to the directory where the template is installed.
*/
private prepareNativeScriptTemplate(templateName: string, version?: string, projectDir?: string): IFuture<string> {
this.$logger.trace(`Using NativeScript verified template: ${templateName} with version ${version}.`);
return this.$npmInstallationManager.install(templateName, projectDir, { version: version, dependencyType: "save" });
}
}
$injector.register("projectTemplatesService", ProjectTemplatesService);