-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcreate-project.ts
247 lines (220 loc) · 8.76 KB
/
create-project.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import * as constants from "../constants";
import * as path from "path";
import { isInteractive } from "../common/helpers";
export class CreateProjectCommand implements ICommand {
public enableHooks = false;
public allowedParameters: ICommandParameter[] = [this.$stringParameter];
private static BlankTemplateKey = "Blank";
private static BlankTemplateDescription = "A blank app";
private static HelloWorldTemplateKey = "Hello World";
private static HelloWorldTemplateDescription = "A Hello World app";
private static DrawerTemplateKey = "SideDrawer";
private static DrawerTemplateDescription = "An app with pre-built pages that uses a drawer for navigation";
private static TabsTemplateKey = "Tabs";
private static TabsTemplateDescription = "An app with pre-built pages that uses tabs for navigation";
private isInteractionIntroShown = false;
private createdProjectData: ICreateProjectData;
constructor(private $projectService: IProjectService,
private $logger: ILogger,
private $errors: IErrors,
private $options: IOptions,
private $prompter: IPrompter,
private $stringParameter: ICommandParameter) { }
public async execute(args: string[]): Promise<void> {
const interactiveAdverbs = ["First", "Next", "Finally"];
const getNextInteractiveAdverb = () => {
return interactiveAdverbs.shift() || "Next";
};
if ((this.$options.tsc || this.$options.ng || this.$options.vue || this.$options.js) && this.$options.template) {
this.$errors.failWithHelp("You cannot use a flavor option like --ng, --vue, --tsc and --js together with --template.");
}
let projectName = args[0];
let selectedTemplate: string;
if (this.$options.js) {
selectedTemplate = constants.JAVASCRIPT_NAME;
} else if (this.$options.tsc) {
selectedTemplate = constants.TYPESCRIPT_NAME;
} else if (this.$options.ng) {
selectedTemplate = constants.ANGULAR_NAME;
} else if (this.$options.vue) {
selectedTemplate = constants.VUE_NAME;
} else if (this.$options.react) {
selectedTemplate = constants.REACT_NAME;
} else {
selectedTemplate = this.$options.template;
}
if (!projectName && isInteractive()) {
this.printInteractiveCreationIntroIfNeeded();
projectName = await this.$prompter.getString(`${getNextInteractiveAdverb()}, what will be the name of your app?`, { allowEmpty: false });
this.$logger.info();
}
projectName = await this.$projectService.validateProjectName({ projectName: projectName, force: this.$options.force, pathToProject: this.$options.path });
if (!selectedTemplate && isInteractive()) {
this.printInteractiveCreationIntroIfNeeded();
selectedTemplate = await this.interactiveFlavorAndTemplateSelection(getNextInteractiveAdverb(), getNextInteractiveAdverb());
}
this.createdProjectData = await this.$projectService.createProject({
projectName: projectName,
template: selectedTemplate,
appId: this.$options.appid,
pathToProject: this.$options.path,
// its already validated above
force: true,
ignoreScripts: this.$options.ignoreScripts
});
}
private async interactiveFlavorAndTemplateSelection(flavorAdverb: string, templateAdverb: string) {
const selectedFlavor = await this.interactiveFlavorSelection(flavorAdverb);
const selectedTemplate: string = await this.interactiveTemplateSelection(selectedFlavor, templateAdverb);
return selectedTemplate;
}
private async interactiveFlavorSelection(adverb: string) {
const flavorSelection = await this.$prompter.promptForDetailedChoice(`${adverb}, which style of NativeScript project would you like to use:`, [
{ key: constants.NgFlavorName, description: "Learn more at https://nativescript.org/angular" },
{ key: constants.ReactFlavorName, description: "Learn more at https://github.com/shirakaba/react-nativescript" },
{ key: constants.VueFlavorName, description: "Learn more at https://nativescript.org/vue" },
{ key: constants.TsFlavorName, description: "Learn more at https://nativescript.org/typescript" },
{ key: constants.JsFlavorName, description: "Use NativeScript without any framework" },
]);
return flavorSelection;
}
private printInteractiveCreationIntroIfNeeded() {
if (!this.isInteractionIntroShown) {
this.isInteractionIntroShown = true;
this.$logger.info();
this.$logger.printMarkdown(`# Let’s create a NativeScript app!`);
this.$logger.printMarkdown(`
Answer the following questions to help us build the right app for you. (Note: you
can skip this prompt next time using the --template option, or the --ng, --react, --vue, --ts, or --js flags.)
`);
}
}
private async interactiveTemplateSelection(flavorSelection: string, adverb: string) {
const selectedFlavorTemplates: {
key?: string;
value: string;
description?: string;
}[] = [];
let selectedTemplate: string;
switch (flavorSelection) {
case constants.NgFlavorName: {
selectedFlavorTemplates.push(...this.getNgTemplates());
break;
}
case constants.ReactFlavorName: {
selectedFlavorTemplates.push(...this.getReactTemplates());
break;
}
case constants.VueFlavorName: {
selectedFlavorTemplates.push(...this.getVueTemplates());
break;
}
case constants.TsFlavorName: {
selectedFlavorTemplates.push(...this.getTsTemplates());
break;
}
case constants.JsFlavorName: {
selectedFlavorTemplates.push(...this.getJsTemplates());
break;
}
}
if (selectedFlavorTemplates.length > 1) {
this.$logger.info();
const templateChoices = selectedFlavorTemplates.map((template) => {
return { key: template.key, description: template.description };
});
const selectedTemplateKey = await this.$prompter.promptForDetailedChoice(`${adverb}, which template would you like to start from:`, templateChoices);
selectedTemplate = selectedFlavorTemplates.find(t => t.key === selectedTemplateKey).value;
} else {
selectedTemplate = selectedFlavorTemplates[0].value;
}
return selectedTemplate;
}
private getJsTemplates() {
const templates = [{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.javascript,
description: CreateProjectCommand.HelloWorldTemplateDescription
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "tns-template-drawer-navigation",
description: CreateProjectCommand.DrawerTemplateDescription
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "tns-template-tab-navigation",
description: CreateProjectCommand.TabsTemplateDescription
}];
return templates;
}
private getTsTemplates() {
const templates = [{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.typescript,
description: CreateProjectCommand.HelloWorldTemplateDescription
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "tns-template-drawer-navigation-ts",
description: CreateProjectCommand.DrawerTemplateDescription
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "tns-template-tab-navigation-ts",
description: CreateProjectCommand.TabsTemplateDescription
}];
return templates;
}
private getNgTemplates() {
const templates = [{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.angular,
description: CreateProjectCommand.HelloWorldTemplateDescription
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "tns-template-drawer-navigation-ng",
description: CreateProjectCommand.DrawerTemplateDescription
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "tns-template-tab-navigation-ng",
description: CreateProjectCommand.TabsTemplateDescription
}];
return templates;
}
private getReactTemplates() {
const templates = [{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.react,
description: CreateProjectCommand.HelloWorldTemplateDescription
}];
return templates;
}
private getVueTemplates() {
const templates = [{
key: CreateProjectCommand.BlankTemplateKey,
value: "tns-template-blank-vue",
description: CreateProjectCommand.BlankTemplateDescription
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "tns-template-drawer-navigation-vue",
description: CreateProjectCommand.DrawerTemplateDescription
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "tns-template-tab-navigation-vue",
description: CreateProjectCommand.TabsTemplateDescription
}];
return templates;
}
public async postCommandAction(args: string[]): Promise<void> {
const { projectDir } = this.createdProjectData;
const relativePath = path.relative(process.cwd(), projectDir);
this.$logger.printMarkdown(`Now you can navigate to your project with \`$ cd ${relativePath}\``);
this.$logger.printMarkdown(`After that you can preview it on device by executing \`$ tns preview\``);
}
}
$injector.registerCommand("create", CreateProjectCommand);