-
Notifications
You must be signed in to change notification settings - Fork 964
/
add.ts
183 lines (165 loc) · 5.27 KB
/
add.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
import { pathExists } from '@ionic/utils-fs';
import { prettyPath } from '@ionic/utils-terminal';
import { addAndroid, createLocalProperties } from '../android/add';
import {
editProjectSettingsAndroid,
checkAndroidPackage,
} from '../android/common';
import c from '../colors';
import {
getKnownPlatforms,
check,
checkAppConfig,
checkPackage,
resolvePlatform,
runPlatformHook,
runTask,
isValidPlatform,
isValidCommunityPlatform,
promptForPlatform,
getProjectPlatformDirectory,
isValidEnterprisePlatform,
} from '../common';
import type { CheckFunction } from '../common';
import type { Config } from '../definitions';
import { fatal, isFatal } from '../errors';
import { addIOS } from '../ios/add';
import {
editProjectSettingsIOS,
checkBundler,
checkCocoaPods,
checkIOSPackage,
} from '../ios/common';
import { logger, logSuccess, output } from '../log';
import { sync } from './sync';
export async function addCommand(
config: Config,
selectedPlatformName: string,
): Promise<void> {
if (selectedPlatformName && !(await isValidPlatform(selectedPlatformName))) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:add',
);
} else {
let msg = `Platform ${c.input(selectedPlatformName)} not found.`;
if (await isValidCommunityPlatform(selectedPlatformName)) {
msg += `\nTry installing ${c.strong(
`@capacitor-community/${selectedPlatformName}`,
)} and adding the platform again.`;
}
if (await isValidEnterprisePlatform(selectedPlatformName)) {
msg +=
`\nThis is an enterprise platform and @ionic-enterprise/capacitor-${selectedPlatformName} is not installed.\n` +
`To learn how to use this platform, visit https://ionic.io/docs/${selectedPlatformName}`;
}
logger.error(msg);
}
} else {
const knownPlatforms = await getKnownPlatforms();
const platformName = await promptForPlatform(
knownPlatforms,
`Please choose a platform to add:`,
selectedPlatformName,
);
if (platformName === config.web.name) {
webWarning();
return;
}
const existingPlatformDir = await getProjectPlatformDirectory(
config,
platformName,
);
if (existingPlatformDir) {
fatal(
`${c.input(platformName)} platform already exists.\n` +
`To re-add this platform, first remove ${c.strong(
prettyPath(existingPlatformDir),
)}, then run this command again.\n` +
`${c.strong(
'WARNING',
)}: Your native project will be completely removed.`,
);
}
try {
await check([
() => checkPackage(),
() => checkAppConfig(config),
...addChecks(config, platformName),
]);
await doAdd(config, platformName);
await editPlatforms(config, platformName);
if (await pathExists(config.app.webDirAbs)) {
await sync(config, platformName, false, false);
if (platformName === config.android.name) {
await runTask('Syncing Gradle', async () => {
return createLocalProperties(config.android.platformDirAbs);
});
}
} else {
logger.warn(
`${c.success(c.strong('sync'))} could not run--missing ${c.strong(
config.app.webDir,
)} directory.`,
);
}
printNextSteps(platformName);
} catch (e: any) {
if (!isFatal(e)) {
fatal(e.stack ?? e);
}
throw e;
}
}
}
function printNextSteps(platformName: string) {
logSuccess(`${c.strong(platformName)} platform added!`);
output.write(
`Follow the Developer Workflow guide to get building:\n${c.strong(
`https://capacitorjs.com/docs/basics/workflow`,
)}\n`,
);
}
function addChecks(config: Config, platformName: string): CheckFunction[] {
if (platformName === config.ios.name) {
return [
() => checkIOSPackage(config),
() => checkBundler(config) || checkCocoaPods(config),
];
} else if (platformName === config.android.name) {
return [() => checkAndroidPackage(config)];
} else if (platformName === config.web.name) {
return [];
} else {
throw `Platform ${platformName} is not valid.`;
}
}
async function doAdd(config: Config, platformName: string): Promise<void> {
await runTask(c.success(c.strong('add')), async () => {
if (platformName === config.ios.name) {
await addIOS(config);
} else if (platformName === config.android.name) {
await addAndroid(config);
}
});
}
async function editPlatforms(config: Config, platformName: string) {
if (platformName === config.ios.name) {
await editProjectSettingsIOS(config);
} else if (platformName === config.android.name) {
await editProjectSettingsAndroid(config);
}
}
function webWarning() {
logger.error(
`Not adding platform ${c.strong('web')}.\n` +
`In Capacitor, the web platform is just your web app! For example, if you have a React or Angular project, the web platform is that project.\n` +
`To add Capacitor functionality to your web app, follow the Web Getting Started Guide: ${c.strong(
'https://capacitorjs.com/docs/web',
)}`,
);
}