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