Skip to content

Commit 1cb0323

Browse files
author
DimitarTachev
committed
refactor: fix pr comments
1 parent b470c6c commit 1cb0323

File tree

5 files changed

+70
-87
lines changed

5 files changed

+70
-87
lines changed

lib/commands/create-project.ts

+41-56
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import { isInteractive } from "../common/helpers";
55
export class CreateProjectCommand implements ICommand {
66
public enableHooks = false;
77
public allowedParameters: ICommandParameter[] = [this.$stringParameter];
8-
private static NgFlavor = "Angular";
9-
private static VueFlavor = "Vue.js";
10-
private static TsFlavor = "Plain TypeScript";
11-
private static JsFlavor = "Plain JavaScript";
128
private static HelloWorldTemplateKey = "Hello World";
139
private static HelloWorldTemplateDescription = "A Hello World app";
1410
private static DrawerTemplateKey = "SideDrawer";
1511
private static DrawerTemplateDescription = "An app with pre-built pages that uses a drawer for navigation";
1612
private static TabsTemplateKey = "Tabs";
1713
private static TabsTemplateDescription = "An app with pre-built pages that uses tabs for navigation";
14+
private isInteractionIntroShown = false;
1815

1916
private createdProjectData: ICreateProjectData;
2017

@@ -49,18 +46,16 @@ export class CreateProjectCommand implements ICommand {
4946
selectedTemplate = this.$options.template;
5047
}
5148

52-
if ((!selectedTemplate || !projectName) && isInteractive()) {
53-
this.printInteractiveCreationIntro();
54-
}
55-
5649
if (!projectName && isInteractive()) {
50+
this.printInteractiveCreationIntroIfNeeded();
5751
projectName = await this.$prompter.getString(`${getNextInteractiveAdverb()}, what will be the name of your app?`, { allowEmpty: false });
5852
this.$logger.info();
5953
}
6054

6155
projectName = await this.$projectService.validateProjectName({ projectName: projectName, force: this.$options.force, pathToProject: this.$options.path });
6256

6357
if (!selectedTemplate && isInteractive()) {
58+
this.printInteractiveCreationIntroIfNeeded();
6459
selectedTemplate = await this.interactiveFlavorAndTemplateSelection(getNextInteractiveAdverb(), getNextInteractiveAdverb());
6560
}
6661

@@ -84,22 +79,25 @@ export class CreateProjectCommand implements ICommand {
8479

8580
private async interactiveFlavorSelection(adverb: string) {
8681
const flavorSelection = await this.$prompter.promptForDetailedChoice(`${adverb}, which flavor would you like to use?`, [
87-
{ key: CreateProjectCommand.NgFlavor, description: "Learn more at https://angular.io/" },
88-
{ key: CreateProjectCommand.VueFlavor, description: "Learn more at https://vuejs.org/" },
89-
{ key: CreateProjectCommand.TsFlavor, description: "Learn more at https://www.typescriptlang.org/" },
90-
{ key: CreateProjectCommand.JsFlavor, description: "Learn more at https://www.javascript.com/" },
82+
{ key: constants.NgFlavorName, description: "Learn more at https://angular.io/" },
83+
{ key: constants.VueFlavorName, description: "Learn more at https://vuejs.org/" },
84+
{ key: constants.TsFlavorName, description: "Learn more at https://www.typescriptlang.org/" },
85+
{ key: constants.JsFlavorName, description: "Learn more at https://www.javascript.com/" },
9186
]);
9287
return flavorSelection;
9388
}
9489

95-
private printInteractiveCreationIntro() {
96-
this.$logger.info();
97-
this.$logger.printMarkdown(`# Let’s create a NativeScript app!`);
98-
this.$logger.printMarkdown(`
90+
private printInteractiveCreationIntroIfNeeded() {
91+
if (!this.isInteractionIntroShown) {
92+
this.isInteractionIntroShown = true;
93+
this.$logger.info();
94+
this.$logger.printMarkdown(`# Let’s create a NativeScript app!`);
95+
this.$logger.printMarkdown(`
9996
Answer the following questions to help us build the right app for you. (Note: you
10097
can skip this prompt next time using the --template option, or the --ng, --vue, --ts,
10198
or --js flags.)
10299
`);
100+
}
103101
}
104102

105103
private async interactiveTemplateSelection(flavorSelection: string, adverb: string) {
@@ -110,19 +108,19 @@ or --js flags.)
110108
}[] = [];
111109
let selectedTemplate: string;
112110
switch (flavorSelection) {
113-
case CreateProjectCommand.NgFlavor: {
111+
case constants.NgFlavorName: {
114112
selectedFlavorTemplates.push(...this.getNgFlavors());
115113
break;
116114
}
117-
case CreateProjectCommand.VueFlavor: {
115+
case constants.VueFlavorName: {
118116
selectedFlavorTemplates.push({ value: "https://github.com/NativeScript/template-blank-vue/tarball/0.9.0" });
119117
break;
120118
}
121-
case CreateProjectCommand.TsFlavor: {
119+
case constants.TsFlavorName: {
122120
selectedFlavorTemplates.push(...this.getTsTemplates());
123121
break;
124122
}
125-
case CreateProjectCommand.JsFlavor: {
123+
case constants.JsFlavorName: {
126124
selectedFlavorTemplates.push(...this.getJsTemplates());
127125
break;
128126
}
@@ -141,74 +139,61 @@ or --js flags.)
141139
}
142140

143141
private getJsTemplates() {
144-
const templates: {
145-
key?: string;
146-
value: string;
147-
description?: string;
148-
}[] = [];
149-
templates.push({
142+
const templates = [{
150143
key: CreateProjectCommand.HelloWorldTemplateKey,
151-
value: "tns-template-hello-world",
144+
value: constants.RESERVED_TEMPLATE_NAMES.javascript,
152145
description: CreateProjectCommand.HelloWorldTemplateDescription
153-
});
154-
templates.push({
146+
},
147+
{
155148
key: CreateProjectCommand.DrawerTemplateKey,
156149
value: "tns-template-drawer-navigation",
157150
description: CreateProjectCommand.DrawerTemplateDescription
158-
});
159-
templates.push({
151+
},
152+
{
160153
key: CreateProjectCommand.TabsTemplateKey,
161154
value: "tns-template-tab-navigation",
162155
description: CreateProjectCommand.TabsTemplateDescription
163-
});
156+
}];
157+
164158
return templates;
165159
}
166160

167161
private getTsTemplates() {
168-
const templates: {
169-
key?: string;
170-
value: string;
171-
description?: string;
172-
}[] = [];
173-
templates.push({
162+
const templates = [{
174163
key: CreateProjectCommand.HelloWorldTemplateKey,
175-
value: "tns-template-hello-world-ts",
164+
value: constants.RESERVED_TEMPLATE_NAMES.typescript,
176165
description: CreateProjectCommand.HelloWorldTemplateDescription
177-
});
178-
templates.push({
166+
},
167+
{
179168
key: CreateProjectCommand.DrawerTemplateKey,
180169
value: "tns-template-drawer-navigation-ts",
181170
description: CreateProjectCommand.DrawerTemplateDescription
182-
});
183-
templates.push({
171+
},
172+
{
184173
key: CreateProjectCommand.TabsTemplateKey,
185174
value: "tns-template-tab-navigation-ts",
186175
description: CreateProjectCommand.TabsTemplateDescription
187-
});
176+
}];
177+
188178
return templates;
189179
}
190180

191181
private getNgFlavors() {
192-
const templates: {
193-
key?: string;
194-
value: string;
195-
description?: string;
196-
}[] = [];
197-
templates.push({
182+
const templates = [{
198183
key: CreateProjectCommand.HelloWorldTemplateKey,
199-
value: "tns-template-hello-world-ng",
184+
value: constants.RESERVED_TEMPLATE_NAMES.angular,
200185
description: CreateProjectCommand.HelloWorldTemplateDescription
201-
});
202-
templates.push({
186+
},
187+
{
203188
key: CreateProjectCommand.DrawerTemplateKey,
204189
value: "tns-template-drawer-navigation-ng",
205190
description: CreateProjectCommand.DrawerTemplateDescription
206-
});
207-
templates.push({
191+
},
192+
{
208193
key: CreateProjectCommand.TabsTemplateKey,
209194
value: "tns-template-tab-navigation-ng",
210195
description: CreateProjectCommand.TabsTemplateDescription
211-
});
196+
}];
212197

213198
return templates;
214199
}

lib/constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export const VUE_NAME = "vue";
106106
export const ANGULAR_NAME = "angular";
107107
export const JAVASCRIPT_NAME = "javascript";
108108
export const TYPESCRIPT_NAME = "typescript";
109+
export const NgFlavorName = "Angular";
110+
export const VueFlavorName = "Vue.js";
111+
export const TsFlavorName = "Plain TypeScript";
112+
export const JsFlavorName = "Plain JavaScript";
109113
export const BUILD_OUTPUT_EVENT_NAME = "buildOutput";
110114
export const CONNECTION_ERROR_EVENT_NAME = "connectionError";
111115
export const USER_INTERACTION_NEEDED_EVENT_NAME = "userInteractionNeeded";

lib/project-data.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export class ProjectData implements IProjectData {
2020
isDefaultProjectType: true
2121
},
2222
{
23-
type: "Angular",
23+
type: constants.NgFlavorName,
2424
requiredDependencies: ["@angular/core", "nativescript-angular"]
2525
},
2626
{
27-
type: "Vue.js",
27+
type: constants.VueFlavorName,
2828
requiredDependencies: ["nativescript-vue"]
2929
},
3030
{

lib/services/project-service.ts

+19-20
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,9 @@ export class ProjectService implements IProjectService {
3636
return projectName;
3737
}
3838

39-
private getValidProjectDir(pathToProject: string, projectName: string): string {
40-
const selectedPath = path.resolve(pathToProject || ".");
41-
const projectDir = path.join(selectedPath, projectName);
42-
43-
return projectDir;
44-
}
45-
4639
@exported("projectService")
4740
public async createProject(projectOptions: IProjectSettings): Promise<ICreateProjectData> {
48-
let projectName = projectOptions.projectName;
49-
projectName = await this.validateProjectName({ projectName, force: projectOptions.force, pathToProject: projectOptions.pathToProject });
41+
const projectName = await this.validateProjectName({ projectName: projectOptions.projectName, force: projectOptions.force, pathToProject: projectOptions.pathToProject });
5042
const projectDir = this.getValidProjectDir(projectOptions.pathToProject, projectName);
5143

5244
this.$fs.createDirectory(projectDir);
@@ -63,6 +55,24 @@ export class ProjectService implements IProjectService {
6355
return projectCreationData;
6456
}
6557

58+
@exported("projectService")
59+
public isValidNativeScriptProject(pathToProject?: string): boolean {
60+
try {
61+
const projectData = this.$projectDataService.getProjectData(pathToProject);
62+
63+
return !!projectData && !!projectData.projectDir && !!(projectData.projectIdentifiers.ios && projectData.projectIdentifiers.android);
64+
} catch (e) {
65+
return false;
66+
}
67+
}
68+
69+
private getValidProjectDir(pathToProject: string, projectName: string): string {
70+
const selectedPath = path.resolve(pathToProject || ".");
71+
const projectDir = path.join(selectedPath, projectName);
72+
73+
return projectDir;
74+
}
75+
6676
private async createProjectCore(projectCreationSettings: IProjectCreationSettings): Promise<ICreateProjectData> {
6777
const { template, projectDir, appId, projectName, ignoreScripts } = projectCreationSettings;
6878

@@ -110,17 +120,6 @@ export class ProjectService implements IProjectService {
110120
return { projectName, projectDir };
111121
}
112122

113-
@exported("projectService")
114-
public isValidNativeScriptProject(pathToProject?: string): boolean {
115-
try {
116-
const projectData = this.$projectDataService.getProjectData(pathToProject);
117-
118-
return !!projectData && !!projectData.projectDir && !!(projectData.projectIdentifiers.ios && projectData.projectIdentifiers.android);
119-
} catch (e) {
120-
return false;
121-
}
122-
}
123-
124123
private async extractTemplate(projectDir: string, templateData: ITemplateData): Promise<void> {
125124
this.$fs.ensureDirectoryExists(projectDir);
126125

test/project-commands.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import * as constants from "../lib/constants";
77
import { assert } from "chai";
88
import { PrompterStub } from "./stubs";
99

10-
const NgFlavor = "Angular";
11-
const VueFlavor = "Vue.js";
12-
const TsFlavor = "Plain TypeScript";
13-
const JsFlavor = "Plain JavaScript";
14-
1510
let selectedTemplateName: string;
1611
let isProjectCreated: boolean;
1712
let createProjectCalledWithForce: boolean;
@@ -166,7 +161,7 @@ describe("Project commands tests", () => {
166161
});
167162

168163
it("should ask for a template when ng flavor is selected.", async () => {
169-
setupAnswers({ flavorAnswer: NgFlavor, templateAnswer: "Hello World" });
164+
setupAnswers({ flavorAnswer: constants.NgFlavorName, templateAnswer: "Hello World" });
170165

171166
await createProjectCommand.execute(dummyArgs);
172167

@@ -176,7 +171,7 @@ describe("Project commands tests", () => {
176171
});
177172

178173
it("should ask for a template when ts flavor is selected.", async () => {
179-
setupAnswers({ flavorAnswer: TsFlavor, templateAnswer: "Hello World" });
174+
setupAnswers({ flavorAnswer: constants.TsFlavorName, templateAnswer: "Hello World" });
180175

181176
await createProjectCommand.execute(dummyArgs);
182177

@@ -186,7 +181,7 @@ describe("Project commands tests", () => {
186181
});
187182

188183
it("should ask for a template when js flavor is selected.", async () => {
189-
setupAnswers({ flavorAnswer: JsFlavor, templateAnswer: "Hello World" });
184+
setupAnswers({ flavorAnswer: constants.JsFlavorName, templateAnswer: "Hello World" });
190185

191186
await createProjectCommand.execute(dummyArgs);
192187

@@ -196,7 +191,7 @@ describe("Project commands tests", () => {
196191
});
197192

198193
it("should select the default vue template when the vue flavor is selected.", async () => {
199-
setupAnswers({ flavorAnswer: VueFlavor });
194+
setupAnswers({ flavorAnswer: constants.VueFlavorName });
200195

201196
await createProjectCommand.execute(dummyArgs);
202197

0 commit comments

Comments
 (0)