UNPKG

5.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cli_framework_1 = require("@ionic/cli-framework");
4const format_1 = require("@ionic/cli-framework/utils/format");
5const string_1 = require("@ionic/cli-framework/utils/string");
6const path = require("path");
7const constants_1 = require("../constants");
8const color_1 = require("../lib/color");
9const command_1 = require("../lib/command");
10const errors_1 = require("../lib/errors");
11const project_1 = require("../lib/project");
12class InitCommand extends command_1.Command {
13 async getMetadata() {
14 return {
15 name: 'init',
16 type: 'global',
17 summary: 'Initialize existing projects with Ionic',
18 description: `
19This command will initialize the current directory with an ${color_1.strong(constants_1.PROJECT_FILE)} file.
20
21${color_1.input('ionic init')} will prompt for a project name and then proceed to determine the type of your project. You can specify the ${color_1.input('name')} argument and ${color_1.input('--type')} option to provide these values via command-line.
22 `,
23 exampleCommands: [
24 '',
25 '"My App"',
26 '"My App" --type=angular',
27 ],
28 inputs: [
29 {
30 name: 'name',
31 summary: `The name of your project (e.g. ${color_1.input('myApp')}, ${color_1.input('"My App"')})`,
32 validators: [cli_framework_1.validators.required],
33 },
34 ],
35 options: [
36 {
37 name: 'type',
38 summary: `Type of project (e.g. ${constants_1.PROJECT_TYPES.map(type => color_1.input(type)).join(', ')})`,
39 },
40 {
41 name: 'force',
42 summary: 'Initialize even if a project already exists',
43 type: Boolean,
44 aliases: ['f'],
45 default: false,
46 },
47 {
48 name: 'project-id',
49 summary: 'Specify a slug for your app',
50 groups: ["advanced" /* ADVANCED */],
51 spec: { value: 'slug' },
52 },
53 ],
54 groups: ["beta" /* BETA */],
55 };
56 }
57 async preRun(inputs, options) {
58 const force = options['force'] ? true : false;
59 if (this.project && this.project.details.context === 'app' && !force) {
60 // TODO: check for existing project config in multi-app
61 throw new errors_1.FatalException(`Existing Ionic project file found: ${color_1.strong(format_1.prettyPath(this.project.filePath))}\n` +
62 `You can re-initialize your project using the ${color_1.input('--force')} option.`);
63 }
64 if (!inputs[0]) {
65 const name = await this.env.prompt({
66 type: 'input',
67 name: 'name',
68 message: 'Project name:',
69 validate: v => cli_framework_1.validators.required(v),
70 });
71 inputs[0] = name;
72 }
73 if (!options['type']) {
74 const details = new project_1.ProjectDetails({ rootDirectory: this.env.ctx.execPath, e: this.env });
75 options['type'] = await details.getTypeFromDetection();
76 }
77 if (!options['type']) {
78 if (this.env.flags.interactive) {
79 this.env.log.warn(`Could not determine project type.\n` +
80 `Please choose a project type from the list.`);
81 this.env.log.nl();
82 }
83 const type = await this.env.prompt({
84 type: 'list',
85 name: 'type',
86 message: 'Project type:',
87 choices: constants_1.PROJECT_TYPES.map(t => ({
88 name: `${project_1.prettyProjectName(t)} (${color_1.input(t)})`,
89 value: t,
90 })),
91 });
92 options['type'] = type;
93 }
94 }
95 async run(inputs, options) {
96 const name = inputs[0].trim();
97 const type = options['type'] ? String(options['type']) : undefined;
98 const projectId = options['project-id'] ? String(options['project-id']) : string_1.slugify(name); // TODO validate --project-id
99 if (!type) {
100 throw new errors_1.FatalException(`Could not determine project type.\n` +
101 `Please specify ${color_1.input('--type')}. See ${color_1.input('ionic init --help')} for details.`);
102 }
103 let project;
104 if (this.project && this.project.details.context === 'multiapp') {
105 project = await project_1.createProjectFromDetails({ context: 'multiapp', configPath: path.resolve(this.project.rootDirectory, constants_1.PROJECT_FILE), id: projectId, type, errors: [] }, this.env);
106 project.config.set('root', path.relative(this.project.rootDirectory, this.env.ctx.execPath));
107 }
108 else {
109 project = await project_1.createProjectFromDetails({ context: 'app', configPath: path.resolve(this.env.ctx.execPath, constants_1.PROJECT_FILE), type, errors: [] }, this.env);
110 }
111 project.config.set('name', name);
112 project.config.set('type', type);
113 this.env.log.ok('Your Ionic project has been initialized!');
114 }
115}
116exports.InitCommand = InitCommand;