-
-
Notifications
You must be signed in to change notification settings - Fork 197
Init command #605
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
Init command #605
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
init | ||
========== | ||
|
||
Usage | Synopsis | ||
---|--- | ||
General | `$ tns init [--path <Directory>] [--force]` | ||
|
||
Initializes a project for development. The command prompts you to provide your project configuration interactively and uses the information to create a new `package.json` file or update the existing one. | ||
|
||
### Options | ||
* `--path` - Specifies the directory where you want to initialize the project, if different from the current directory. The directory must be empty. | ||
* `--force` - If set, applies the default project configuration and does not show the interactive prompt. The default project configuration targets the latest official runtimes and sets `org.nativescript.<folder_name>` for application identifier. | ||
|
||
<% if(isHtml) { %> | ||
### Related Commands | ||
|
||
Command | Description | ||
----------|---------- | ||
[create](create.html) | Creates a new project for native development with NativeScript from the default template or from an existing NativeScript project. | ||
[install](install.html) | Installs all platforms and dependencies described in the `package.json` file in the current directory. | ||
<% } %> | ||
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
///<reference path="../.d.ts"/> | ||
"use strict"; | ||
|
||
import Future = require("fibers/future"); | ||
|
||
export class InitCommand implements ICommand { | ||
constructor(private $initService: IInitService) { } | ||
|
||
public allowedParameters: ICommandParameter[] = []; | ||
public enableHooks = false; | ||
|
||
public execute(args: string[]): IFuture<void> { | ||
return this.$initService.initialize(); | ||
} | ||
} | ||
$injector.registerCommand("init", InitCommand); |
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
///<reference path="../.d.ts"/> | ||
"use strict"; | ||
|
||
import constants = require("./../constants"); | ||
import helpers = require("./../common/helpers"); | ||
import path = require("path"); | ||
import semver = require("semver"); | ||
|
||
export class InitService implements IInitService { | ||
private static MIN_SUPPORTED_FRAMEWORK_VERSIONS: IStringDictionary = { | ||
"tns-ios": "1.1.0", | ||
"tns-android": "1.1.0" | ||
}; | ||
|
||
private _projectFilePath: string; | ||
|
||
constructor(private $fs: IFileSystem, | ||
private $errors: IErrors, | ||
private $logger: ILogger, | ||
private $options: IOptions, | ||
private $injector: IInjector, | ||
private $staticConfig: IStaticConfig, | ||
private $projectHelper: IProjectHelper, | ||
private $prompter: IPrompter, | ||
private $npm: INodePackageManager, | ||
private $npmInstallationManager: INpmInstallationManager) { } | ||
|
||
public initialize(): IFuture<void> { | ||
return (() => { | ||
let projectData: any = { }; | ||
|
||
if(this.$fs.exists(this.projectFilePath).wait()) { | ||
projectData = this.$fs.readJson(this.projectFilePath).wait(); | ||
} | ||
|
||
let projectDataBackup = _.extend({}, projectData); | ||
|
||
if(!projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) { | ||
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = { }; | ||
this.$fs.writeJson(this.projectFilePath, projectData).wait(); // We need to create package.json file here in order to prevent "No project found at or above and neither was a --path specified." when resolving platformsData | ||
} | ||
|
||
try { | ||
|
||
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]["id"] = this.getProjectId().wait(); | ||
|
||
if(this.$options.frameworkName && this.$options.frameworkVersion) { | ||
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][this.$options.frameworkName] = this.buildVersionData(this.$options.frameworkVersion); | ||
} else { | ||
let $platformsData = this.$injector.resolve("platformsData"); | ||
_.each($platformsData.platformsNames, platform => { | ||
let platformData: IPlatformData = $platformsData.getPlatformData(platform); | ||
if(!platformData.targetedOS || (platformData.targetedOS && _.contains(platformData.targetedOS, process.platform))) { | ||
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][platformData.frameworkPackageName] = this.getVersionData(platformData.frameworkPackageName).wait(); | ||
} | ||
}); | ||
} | ||
|
||
this.$fs.writeJson(this.projectFilePath, projectData).wait(); | ||
} catch(err) { | ||
this.$fs.writeJson(this.projectFilePath, projectDataBackup).wait(); | ||
throw err; | ||
} | ||
|
||
this.$logger.out("Project successfully initialized."); | ||
}).future<void>()(); | ||
} | ||
|
||
private get projectFilePath(): string { | ||
if(!this._projectFilePath) { | ||
let projectDir = path.resolve(this.$options.path || "."); | ||
this._projectFilePath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME); | ||
} | ||
|
||
return this._projectFilePath; | ||
} | ||
|
||
private getProjectId(): IFuture<string> { | ||
return (() => { | ||
if(this.$options.appid) { | ||
return this.$options.appid; | ||
} | ||
|
||
let defaultAppId = this.$projectHelper.generateDefaultAppId(path.basename(path.dirname(this.projectFilePath)), constants.DEFAULT_APP_IDENTIFIER_PREFIX); | ||
if(this.useDefaultValue) { | ||
return defaultAppId; | ||
} | ||
|
||
return this.$prompter.getString("Id:", () => defaultAppId).wait(); | ||
}).future<string>()(); | ||
} | ||
|
||
private getVersionData(packageName: string): IFuture<IStringDictionary> { | ||
return (() => { | ||
let latestVersion = this.$npmInstallationManager.getLatestVersion(packageName).wait(); | ||
if(this.useDefaultValue) { | ||
return this.buildVersionData(latestVersion); | ||
} | ||
|
||
let data = this.$npm.view(packageName, "versions").wait(); | ||
let versions = _.filter(data[latestVersion].versions, (version: string) => semver.gte(version, InitService.MIN_SUPPORTED_FRAMEWORK_VERSIONS[packageName])); | ||
if(versions.length === 1) { | ||
this.$logger.info(`Only ${versions[0]} version is available for ${packageName} framework.`); | ||
return this.buildVersionData(versions[0]); | ||
} | ||
let sortedVersions = versions.sort(helpers.versionCompare).reverse(); | ||
let version = this.$prompter.promptForChoice(`${packageName} version:`, sortedVersions).wait(); | ||
return this.buildVersionData(version); | ||
}).future<IStringDictionary>()(); | ||
} | ||
|
||
private buildVersionData(version: string): IStringDictionary { | ||
return { "version": version }; | ||
} | ||
|
||
private get useDefaultValue(): boolean { | ||
return !helpers.isInteractive() || this.$options.force; | ||
} | ||
} | ||
$injector.register("initService", InitService); |
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
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.
Add the install command here, as it is actually related to the init workflow.