Skip to content

Commit 8c6061b

Browse files
committed
Init command
1 parent 572acff commit 8c6061b

File tree

7 files changed

+141
-4
lines changed

7 files changed

+141
-4
lines changed

docs/man_pages/project/creation/create.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ Creates a new project for native development with NativeScript from the default
1616
* `<App Name>` is the name of project. The specified name must meet the requirements of all platforms that you want to target. <% if(isConsole) { %>For more information about the `<App Name>` requirements, run `$ tns help create`<% } %><% if(isHtml) { %>For projects that target Android, you can use uppercase or lowercase letters, numbers, and underscores. The name must start with a letter.
1717
For projects that target iOS, you can use uppercase or lowercase letters, numbers, and hyphens.<% } %>
1818
* `<App ID>` is the application identifier for your project. It must be a domain name in reverse and must meet the requirements of all platforms that you want to target. If not specified, the application identifier is set to `org.nativescript.<App name>` <% if(isConsole) { %>For more information about the `<App ID>` requirements, run `$ tns help create`<% } %><% if(isHtml) { %>For projects that target Android, you can use uppercase or lowercase letters, numbers, and underscores in the strings of the reversed domain name, separated by a dot. Strings must be separated by a dot and must start with a letter. For example: `com.nativescript.My_Andro1d_App`
19-
For projects that target iOS, you can use uppercase or lowercase letters, numbers, and hyphens in the strings of the reversed domain name. Strings must be separated by a dot. For example: `com.nativescript.My-i0s-App`.<% } %>
19+
For projects that target iOS, you can use uppercase or lowercase letters, numbers, and hyphens in the strings of the reversed domain name. Strings must be separated by a dot. For example: `com.nativescript.My-i0s-App`.<% } %>
20+
21+
<% if(isHtml) { %>
22+
### Related Commands
23+
24+
Command | Description
25+
----------|----------
26+
[init](init.html) | Initializes a project for development. This will ask you a bunch of questions, and then write a package.json for you.
27+
<% } %>
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
init
2+
==========
3+
4+
Usage | Synopsis
5+
---|---
6+
General | `$ tns init [--path <Directory>] [--force]`
7+
8+
Initializes a project for development. This will ask you a bunch of questions, and then write a package.json for you.
9+
10+
### Options
11+
* `--path` - Specifies the directory where you want to initialize the project, if different from the current directory. The directory must be empty.
12+
* `--force` - Will use only defaults and not prompt you for any options.
13+
14+
<% if(isHtml) { %>
15+
### Related Commands
16+
17+
Command | Description
18+
----------|----------
19+
[create](create.html) | Creates a new project for native development with NativeScript from the default template or from an existing NativeScript project.
20+
<% } %>

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ $injector.require("pluginsService", "./services/plugins-service");
6060
$injector.requireCommand("plugin|add", "./commands/plugin/add-plugin");
6161
$injector.requireCommand("plugin|remove", "./commands/plugin/remove-plugin");
6262
$injector.requireCommand("install", "./commands/install");
63+
$injector.requireCommand("init", "./commands/init");
6364

6465
$injector.require("projectFilesManager", "./services/project-files-manager");

lib/commands/init.ts

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
///<reference path="../.d.ts"/>
2+
"use strict";
3+
4+
import constants = require("./../constants");
5+
import helpers = require("./../common/helpers");
6+
import path = require("path");
7+
8+
export class InitCommand implements ICommand {
9+
private _projectFilePath: string;
10+
11+
constructor(private $fs: IFileSystem,
12+
private $errors: IErrors,
13+
private $logger: ILogger,
14+
private $options: IOptions,
15+
private $injector: IInjector,
16+
private $staticConfig: IStaticConfig,
17+
private $projectHelper: IProjectHelper,
18+
private $prompter: IPrompter,
19+
private $npm: INodePackageManager,
20+
private $npmInstallationManager: INpmInstallationManager) { }
21+
22+
public allowedParameters: ICommandParameter[] = [];
23+
public enableHooks = false;
24+
25+
public execute(args: string[]): IFuture<void> {
26+
return (() => {
27+
let projectData: any = { };
28+
29+
if(this.$fs.exists(this.projectFilePath).wait()) {
30+
projectData = this.$fs.readJson(this.projectFilePath).wait();
31+
}
32+
33+
if(!projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) {
34+
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = { };
35+
}
36+
37+
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
38+
39+
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]["id"] = this.getProjectId().wait();
40+
41+
let $platformsData = this.$injector.resolve("platformsData");
42+
_.each($platformsData.platformsNames, platform => {
43+
let platformData: IPlatformData = $platformsData.getPlatformData(platform);
44+
if(!platformData.targetedOS || (platformData.targetedOS && _.contains(platformData.targetedOS, process.platform))) {
45+
projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][platformData.frameworkPackageName] = this.getVersionData(platformData.frameworkPackageName).wait();
46+
}
47+
});
48+
49+
this.$fs.writeJson(this.projectFilePath, projectData).wait();
50+
51+
this.$logger.out("Project successfully initialized.");
52+
}).future<void>()();
53+
}
54+
55+
public canExecute(args: string[]): IFuture<boolean> {
56+
return (() => {
57+
return true;
58+
}).future<boolean>()();
59+
}
60+
61+
private get projectFilePath(): string {
62+
if(!this._projectFilePath) {
63+
let projectPath = path.resolve(this.$options.path || ".");
64+
this._projectFilePath = path.join(projectPath, constants.PACKAGE_JSON_FILE_NAME);
65+
}
66+
67+
return this._projectFilePath;
68+
}
69+
70+
private set projectFilePath(newProjectFilePath: string) {
71+
this._projectFilePath = newProjectFilePath;
72+
}
73+
74+
private getProjectId(): IFuture<string> {
75+
return (() => {
76+
let defaultAppId = this.$projectHelper.generateDefaultAppId(path.basename(path.dirname(this.projectFilePath)), constants.DEFAULT_APP_IDENTIFIER_PREFIX);
77+
if(this.canUseDefaultValue) {
78+
return defaultAppId;
79+
}
80+
81+
return this.$prompter.getString("Id:", () => defaultAppId).wait();
82+
}).future<string>()();
83+
}
84+
85+
private getVersionData(packageName: string): IFuture<any> {
86+
return (() => {
87+
let latestVersion = this.$npmInstallationManager.getLatestVersion(packageName).wait();
88+
if(this.canUseDefaultValue) {
89+
return this.buildVersionData(latestVersion);
90+
}
91+
92+
let data = this.$npm.view(packageName, "versions").wait();
93+
let versions = data[latestVersion].versions;
94+
let sortedVersions = versions.sort(helpers.versionCompare).reverse();
95+
let version = this.$prompter.promptForChoice(`${packageName} version:`, sortedVersions).wait();
96+
return this.buildVersionData(version);
97+
}).future<any>()();
98+
}
99+
100+
private buildVersionData(version: string) {
101+
return { "version": version };
102+
}
103+
104+
private get canUseDefaultValue(): boolean {
105+
return !helpers.isInteractive() || this.$options.force;
106+
}
107+
}
108+
$injector.registerCommand("init", InitCommand);

lib/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export var NATIVESCRIPT_KEY_NAME = "nativescript";
77
export var NODE_MODULES_FOLDER_NAME = "node_modules";
88
export var PACKAGE_JSON_FILE_NAME = "package.json";
99
export var NODE_MODULE_CACHE_PATH_KEY_NAME = "node-modules-cache-path";
10+
export var DEFAULT_APP_IDENTIFIER_PREFIX = "org.nativescript";
1011

1112
export class ReleaseType {
1213
static MAJOR = "major";

lib/services/project-service.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import shell = require("shelljs");
99
import util = require("util");
1010

1111
export class ProjectService implements IProjectService {
12-
private static DEFAULT_APP_IDENTIFIER_PREFIX = "org.nativescript";
1312

1413
constructor(private $errors: IErrors,
1514
private $fs: IFileSystem,
@@ -27,7 +26,7 @@ export class ProjectService implements IProjectService {
2726
}
2827
this.$projectNameValidator.validate(projectName);
2928

30-
var projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, ProjectService.DEFAULT_APP_IDENTIFIER_PREFIX);
29+
var projectId = this.$options.appid || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX);
3130

3231
var projectDir = path.join(path.resolve(this.$options.path || "."), projectName);
3332
this.$fs.createDirectory(projectDir).wait();

0 commit comments

Comments
 (0)