-
-
Notifications
You must be signed in to change notification settings - Fork 197
Add support for different templates #1218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,101 @@ | ||
///<reference path="../.d.ts"/> | ||
"use strict"; | ||
import * as path from "path"; | ||
import * as temp from "temp"; | ||
import * as constants from "../constants"; | ||
import {EOL} from "os"; | ||
temp.track(); | ||
|
||
export class ProjectTemplatesService implements IProjectTemplatesService { | ||
private static NPM_DEFAULT_TEMPLATE_NAME = "tns-template-hello-world"; | ||
private static RESERVED_TEMPLATE_NAMES: IStringDictionary = { | ||
"default": "tns-template-hello-world", | ||
"tsc": "tns-template-hello-world-ts", | ||
"typescript": "tns-template-hello-world-ts" | ||
}; | ||
|
||
public constructor(private $npmInstallationManager: INpmInstallationManager) { } | ||
public constructor(private $errors: IErrors, | ||
private $fs: IFileSystem, | ||
private $logger: ILogger, | ||
private $npm: INodePackageManager, | ||
private $npmInstallationManager: INpmInstallationManager) { } | ||
|
||
public get defaultTemplatePath(): IFuture<string> { | ||
return this.$npmInstallationManager.install(ProjectTemplatesService.NPM_DEFAULT_TEMPLATE_NAME); | ||
return this.prepareNativeScriptTemplate(ProjectTemplatesService.RESERVED_TEMPLATE_NAMES["default"]); | ||
} | ||
|
||
public prepareTemplate(originalTemplateName: string): IFuture<string> { | ||
return ((): string => { | ||
let realTemplatePath: string; | ||
if(originalTemplateName) { | ||
let templateName = originalTemplateName.toLowerCase(); | ||
|
||
// support <reserved_name>@<version> syntax | ||
let [name, version] = templateName.split("@"); | ||
if(ProjectTemplatesService.RESERVED_TEMPLATE_NAMES[name]) { | ||
realTemplatePath = this.prepareNativeScriptTemplate(ProjectTemplatesService.RESERVED_TEMPLATE_NAMES[name], version).wait(); | ||
} else { | ||
let tempDir = temp.mkdirSync("nativescript-template-dir"); | ||
try { | ||
// Use the original template name, specified by user as it may be case-sensitive. | ||
this.$npm.install(originalTemplateName, tempDir, {production: true, silent: true}).wait(); | ||
} catch(err) { | ||
this.$logger.trace(err); | ||
this.$errors.failWithoutHelp(`Unable to use template ${originalTemplateName}. Make sure you've specified valid name, github URL or path to local dir.` + | ||
`${EOL}Error is: ${err.message}.`); | ||
} | ||
|
||
realTemplatePath = this.getTemplatePathFromTempDir(tempDir).wait(); | ||
} | ||
} else { | ||
realTemplatePath = this.defaultTemplatePath.wait(); | ||
} | ||
|
||
if(realTemplatePath) { | ||
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME)).wait(); | ||
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): IFuture<string> { | ||
this.$logger.trace(`Using NativeScript verified template: ${templateName} with version ${version}.`); | ||
return this.$npmInstallationManager.install(templateName, {version: version}); | ||
} | ||
|
||
private getTemplatePathFromTempDir(tempDir: string): IFuture<string> { | ||
return ((): string => { | ||
let templatePath: string; | ||
let tempDirContents = this.$fs.readDirectory(tempDir).wait(); | ||
this.$logger.trace(`TempDir contents: ${tempDirContents}.`); | ||
|
||
// We do not know the name of the package that will be installed, so after installation to temp dir, | ||
// there should be node_modules dir there and its only subdir should be our package. | ||
// In case there's some other dir instead of node_modules, consider it as our package. | ||
if(tempDirContents && tempDirContents.length === 1) { | ||
let tempDirSubdir = _.first(tempDirContents); | ||
if(tempDirSubdir === constants.NODE_MODULES_FOLDER_NAME) { | ||
let templateDirName = _.first(this.$fs.readDirectory(path.join(tempDir, constants.NODE_MODULES_FOLDER_NAME)).wait()); | ||
if(templateDirName) { | ||
templatePath = path.join(tempDir, tempDirSubdir, templateDirName); | ||
} | ||
} else { | ||
templatePath = path.join(tempDir, tempDirSubdir); | ||
} | ||
} | ||
|
||
return templatePath; | ||
}).future<string>()(); | ||
} | ||
} | ||
$injector.register("projectTemplatesService", ProjectTemplatesService); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current code is written to be case sensitive for template names. I think it is more user friendly to make it case insensitive but do not insist. Opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact the default templates are case-insensitive:
https://github.com/NativeScript/nativescript-cli/pull/1218/files#diff-123e2a8a4e8daddf9ac8a60b8c79546dR29
In case the lowercased value is not one of the reserved template names, we'll call
npm install
with original value specified by user. We do not want to set toLowerCase() there as the specified value might be case sensitive URL.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!