( config: Config, name: string, id: string, webDirFromCLI?: string, skipAppIDValidation?: boolean, )
| 14 | import { checkInteractive, isInteractive } from '../util/term'; |
| 15 | |
| 16 | export async function initCommand( |
| 17 | config: Config, |
| 18 | name: string, |
| 19 | id: string, |
| 20 | webDirFromCLI?: string, |
| 21 | skipAppIDValidation?: boolean, |
| 22 | ): Promise<void> { |
| 23 | try { |
| 24 | if (!checkInteractive(name, id)) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | if (config.app.extConfigType !== 'json') { |
| 29 | fatal( |
| 30 | `Cannot run ${c.input('init')} for a project using a non-JSON configuration file.\n` + |
| 31 | `Delete ${c.strong(config.app.extConfigName)} and try again.`, |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | const isNewConfig = Object.keys(config.app.extConfig).length === 0; |
| 36 | const tsInstalled = !!resolveNode(config.app.rootDir, 'typescript'); |
| 37 | const appName = await getName(config, name); |
| 38 | const appId = await getAppId(config, id); |
| 39 | const webDir = isInteractive() |
| 40 | ? await getWebDir(config, webDirFromCLI) |
| 41 | : (webDirFromCLI ?? config.app.extConfig.webDir ?? 'www'); |
| 42 | |
| 43 | if (skipAppIDValidation === true) { |
| 44 | await check([() => checkAppName(config, appName)]); |
| 45 | } else { |
| 46 | await check([() => checkAppName(config, appName), () => checkAppId(config, appId)]); |
| 47 | } |
| 48 | |
| 49 | const cordova = await getCordovaPreferences(config); |
| 50 | |
| 51 | await runMergeConfig( |
| 52 | config, |
| 53 | { |
| 54 | appId, |
| 55 | appName, |
| 56 | webDir, |
| 57 | cordova, |
| 58 | }, |
| 59 | isNewConfig && tsInstalled ? 'ts' : 'json', |
| 60 | ); |
| 61 | } catch (e: any) { |
| 62 | if (!isFatal(e)) { |
| 63 | output.write('Usage: npx cap init appName appId\n' + 'Example: npx cap init "My App" "com.example.myapp"\n\n'); |
| 64 | |
| 65 | fatal(e.stack ?? e); |
| 66 | } |
| 67 | |
| 68 | throw e; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | async function getName(config: Config, name: string) { |
| 73 | if (!name) { |
no test coverage detected