-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathcreate-project.ts
456 lines (426 loc) · 13.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import * as constants from "../constants";
import * as path from "path";
import { isInteractive } from "../common/helpers";
import { ICreateProjectData, IProjectService } from "../definitions/project";
import { IOptions } from "../declarations";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { IErrors } from "../common/declarations";
import { injector } from "../common/yok";
import { color } from "../color";
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 BlankTsTemplateKey = "Blank Typescript";
private static BlankTsTemplateDescription = "A blank typescript app";
private static BlankVisionTemplateKey = "visionOS";
private static BlankVisionTemplateDescription = "A visionOS 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.react ||
this.$options.svelte ||
this.$options.js) &&
this.$options.template
) {
this.$errors.failWithHelp(
"You cannot use a flavor option like --ng, --vue, --react, --svelte, --tsc and --js together with --template."
);
}
let projectName = args[0];
let selectedTemplate: string;
if (
this.$options["vision-ng"] ||
(this.$options.vision && this.$options.ng)
) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision-ng"];
} else if (
this.$options["vision-react"] ||
(this.$options.vision && this.$options.react)
) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision-react"];
} else if (this.$options["vision-solid"]) {
// note: we don't have solid templates or --solid
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision-solid"];
} else if (
this.$options["vision-svelte"] ||
(this.$options.vision && this.$options.svelte)
) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision-svelte"];
} else if (
this.$options["vision-vue"] ||
(this.$options.vision && (this.$options.vue || this.$options.vuejs))
) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision-vue"];
} else if (
(this.$options.vue || this.$options.vuejs) &&
this.$options.tsc
) {
selectedTemplate = "@nativescript/template-blank-vue-ts";
} else if (this.$options.vision) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["vision"];
} else 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 || this.$options.vuejs) {
selectedTemplate = constants.VUE_NAME;
} else if (this.$options.react) {
selectedTemplate = constants.REACT_NAME;
} else if (this.$options.svelte) {
selectedTemplate = constants.SVELTE_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.SvelteFlavorName,
description: "Learn more at https://svelte-native.technology",
},
{
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, --svelte, --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.SvelteFlavorName: {
selectedFlavorTemplates.push(...this.getSvelteTemplates());
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: "@nativescript/template-drawer-navigation",
description: CreateProjectCommand.DrawerTemplateDescription,
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "@nativescript/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: "@nativescript/template-drawer-navigation-ts",
description: CreateProjectCommand.DrawerTemplateDescription,
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "@nativescript/template-tab-navigation-ts",
description: CreateProjectCommand.TabsTemplateDescription,
},
{
key: CreateProjectCommand.BlankVisionTemplateKey,
value: "@nativescript/template-hello-world-ts-vision",
description: CreateProjectCommand.BlankVisionTemplateDescription,
},
];
return templates;
}
private getNgTemplates() {
const templates = [
{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.angular,
description: CreateProjectCommand.HelloWorldTemplateDescription,
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "@nativescript/template-drawer-navigation-ng",
description: CreateProjectCommand.DrawerTemplateDescription,
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "@nativescript/template-tab-navigation-ng",
description: CreateProjectCommand.TabsTemplateDescription,
},
{
key: CreateProjectCommand.BlankVisionTemplateKey,
value: "@nativescript/template-hello-world-ng-vision",
description: CreateProjectCommand.BlankVisionTemplateDescription,
},
];
return templates;
}
private getReactTemplates() {
const templates = [
{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.react,
description: CreateProjectCommand.HelloWorldTemplateDescription,
},
{
key: CreateProjectCommand.BlankVisionTemplateKey,
value: "@nativescript/template-blank-react-vision",
description: CreateProjectCommand.BlankVisionTemplateDescription,
},
];
return templates;
}
private getSvelteTemplates() {
const templates = [
{
key: CreateProjectCommand.HelloWorldTemplateKey,
value: constants.RESERVED_TEMPLATE_NAMES.svelte,
description: CreateProjectCommand.HelloWorldTemplateDescription,
},
{
key: CreateProjectCommand.BlankVisionTemplateKey,
value: "@nativescript/template-blank-svelte-vision",
description: CreateProjectCommand.BlankVisionTemplateDescription,
},
];
return templates;
}
private getVueTemplates() {
const templates = [
{
key: CreateProjectCommand.BlankTemplateKey,
value: "@nativescript/template-blank-vue",
description: CreateProjectCommand.BlankTemplateDescription,
},
{
key: CreateProjectCommand.BlankTsTemplateKey,
value: "@nativescript/template-blank-vue-ts",
description: CreateProjectCommand.BlankTsTemplateDescription,
},
{
key: CreateProjectCommand.DrawerTemplateKey,
value: "@nativescript/template-drawer-navigation-vue",
description: CreateProjectCommand.DrawerTemplateDescription,
},
{
key: CreateProjectCommand.TabsTemplateKey,
value: "@nativescript/template-tab-navigation-vue",
description: CreateProjectCommand.TabsTemplateDescription,
},
{
key: CreateProjectCommand.BlankVisionTemplateKey,
value: "@nativescript/template-blank-vue-vision",
description: CreateProjectCommand.BlankVisionTemplateDescription,
},
];
return templates;
}
public async postCommandAction(args: string[]): Promise<void> {
const { projectDir, projectName } = this.createdProjectData;
const relativePath = path.relative(process.cwd(), projectDir);
const greyDollarSign = color.grey("$");
this.$logger.clearScreen();
let runDebugNotes: Array<string> = [];
if (
this.$options.vision ||
this.$options["vision-ng"] ||
this.$options["vision-react"] ||
this.$options["vision-solid"] ||
this.$options["vision-svelte"] ||
this.$options["vision-vue"]
) {
runDebugNotes = [
`Run the project on Vision Pro with:`,
"",
` ${greyDollarSign} ${color.green("ns run visionos --no-hmr")}`,
];
} else {
runDebugNotes = [
`Run the project on multiple devices:`,
"",
` ${greyDollarSign} ${color.green("ns run ios")}`,
` ${greyDollarSign} ${color.green("ns run android")}`,
"",
"Debug the project with Chrome DevTools:",
"",
` ${greyDollarSign} ${color.green("ns debug ios")}`,
` ${greyDollarSign} ${color.green("ns debug android")}`,
];
}
this.$logger.info(
[
[
color.green(`Project`),
color.cyan(projectName),
color.green(`was successfully created.`),
].join(" "),
"",
`Now you can navigate to your project with ${color.cyan(
`cd ${relativePath}`
)} and then:`,
"",
...runDebugNotes,
``,
`For more options consult the docs or run ${color.green("ns --help")}`,
"",
].join("\n")
);
// todo: add back ns preview
// this.$logger.printMarkdown(
// `After that you can preview it on device by executing \`$ ns preview\``
// );
}
}
injector.registerCommand("create", CreateProjectCommand);