1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs_1 = require("fs");
|
4 | const path_1 = require("path");
|
5 | const common_1 = require("./common");
|
6 | const definitions_1 = require("./definitions");
|
7 | let Package;
|
8 | let ExtConfig;
|
9 | class Config {
|
10 | constructor(os, currentWorkingDir, cliBinDir) {
|
11 | this.windows = {
|
12 | androidStudioPath: 'C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe'
|
13 | };
|
14 | this.linux = {
|
15 | androidStudioPath: '/usr/local/android-studio/bin/studio.sh'
|
16 | };
|
17 | this.electron = {
|
18 | name: 'electron',
|
19 | platformDir: '',
|
20 | webDir: 'app',
|
21 | webDirAbs: '',
|
22 | assets: {
|
23 | templateName: 'electron-template',
|
24 | templateDir: ''
|
25 | }
|
26 | };
|
27 | this.android = {
|
28 | name: 'android',
|
29 | minVersion: '21',
|
30 | platformDir: '',
|
31 | webDir: 'app/src/main/assets/public',
|
32 | webDirAbs: '',
|
33 | resDir: 'app/src/main/res',
|
34 | resDirAbs: '',
|
35 | assets: {
|
36 | templateName: 'android-template',
|
37 | pluginsFolderName: 'capacitor-cordova-android-plugins',
|
38 | templateDir: '',
|
39 | pluginsDir: '',
|
40 | }
|
41 | };
|
42 | this.ios = {
|
43 | name: 'ios',
|
44 | minVersion: '11.0',
|
45 | cordovaSwiftVersion: '5.0',
|
46 | platformDir: '',
|
47 | webDir: 'public',
|
48 | webDirAbs: '',
|
49 | nativeProjectName: 'App',
|
50 | assets: {
|
51 | templateName: 'ios-template',
|
52 | pluginsFolderName: 'capacitor-cordova-ios-plugins',
|
53 | templateDir: '',
|
54 | pluginsDir: '',
|
55 | }
|
56 | };
|
57 | this.web = {
|
58 | name: 'web'
|
59 | };
|
60 | this.cli = {
|
61 | binDir: '',
|
62 | rootDir: '',
|
63 | assetsName: 'assets',
|
64 | assetsDir: '',
|
65 | package: Package,
|
66 | os: definitions_1.OS.Unknown,
|
67 | npmClient: ''
|
68 | };
|
69 | this.app = {
|
70 | rootDir: '',
|
71 | appId: '',
|
72 | appName: '',
|
73 | webDir: 'www',
|
74 | webDirAbs: '',
|
75 | package: Package,
|
76 | windowsAndroidStudioPath: 'C:\\Program Files\\Android\\Android Studio\\bin\\studio64.exe',
|
77 | linuxAndroidStudioPath: '',
|
78 | extConfigName: 'capacitor.config.json',
|
79 | extConfigFilePath: '',
|
80 | extConfig: ExtConfig,
|
81 | bundledWebRuntime: false,
|
82 | plugins: {},
|
83 | assets: {
|
84 | templateName: 'app-template',
|
85 | templateDir: '',
|
86 | pluginsTemplateDir: ''
|
87 | },
|
88 | server: {
|
89 | cleartext: false
|
90 | }
|
91 | };
|
92 | this.plugins = {
|
93 | assets: {
|
94 | templateName: 'plugin-template',
|
95 | templateDir: ''
|
96 | }
|
97 | };
|
98 | this.platforms = [];
|
99 | this.initOS(os);
|
100 | this.initCliConfig(cliBinDir);
|
101 | this.setCurrentWorkingDir(currentWorkingDir);
|
102 | }
|
103 | initOS(os) {
|
104 | switch (os) {
|
105 | case 'darwin':
|
106 | this.cli.os = definitions_1.OS.Mac;
|
107 | break;
|
108 | case 'win32':
|
109 | this.cli.os = definitions_1.OS.Windows;
|
110 | break;
|
111 | case 'linux':
|
112 | this.cli.os = definitions_1.OS.Linux;
|
113 | break;
|
114 | }
|
115 | }
|
116 | setCurrentWorkingDir(currentWorkingDir) {
|
117 | try {
|
118 | this.initAppConfig(path_1.resolve(currentWorkingDir));
|
119 | this.initPluginsConfig();
|
120 | this.loadExternalConfig();
|
121 | this.mergeConfigData();
|
122 |
|
123 | this.initAndroidConfig();
|
124 | this.initElectronConfig();
|
125 | this.initIosConfig();
|
126 | this.initWindowsConfig();
|
127 | this.initLinuxConfig();
|
128 | this.platforms.push(this.web.name);
|
129 | }
|
130 | catch (e) {
|
131 | common_1.logFatal(`Unable to load config`, e);
|
132 | }
|
133 | }
|
134 | initCliConfig(cliBinDir) {
|
135 | this.cli.binDir = cliBinDir;
|
136 | this.cli.rootDir = path_1.join(cliBinDir, '../');
|
137 | this.cli.assetsDir = path_1.join(this.cli.rootDir, this.cli.assetsName);
|
138 | this.cli.package = loadPackageJson(this.cli.rootDir);
|
139 | }
|
140 | initAppConfig(currentWorkingDir) {
|
141 | this.app.rootDir = currentWorkingDir;
|
142 | this.app.package = loadPackageJson(currentWorkingDir);
|
143 | this.app.assets.templateDir = path_1.join(this.cli.assetsDir, this.app.assets.templateName);
|
144 | }
|
145 | async updateAppPackage() {
|
146 | this.app.package = await common_1.readJSON(path_1.resolve(this.app.rootDir, 'package.json'));
|
147 | }
|
148 | initElectronConfig() {
|
149 | this.platforms.push(this.electron.name);
|
150 | this.electron.platformDir = path_1.resolve(this.app.rootDir, this.electron.name);
|
151 | this.electron.assets.templateDir = path_1.resolve(this.cli.assetsDir, this.electron.assets.templateName);
|
152 | this.electron.webDirAbs = path_1.resolve(this.electron.platformDir, this.electron.webDir);
|
153 | }
|
154 | initAndroidConfig() {
|
155 | this.platforms.push(this.android.name);
|
156 | this.android.platformDir = path_1.resolve(this.app.rootDir, this.android.name);
|
157 | this.android.assets.templateDir = path_1.resolve(this.cli.assetsDir, this.android.assets.templateName);
|
158 | this.android.assets.pluginsDir = path_1.resolve(this.cli.assetsDir, this.android.assets.pluginsFolderName);
|
159 | this.android.webDirAbs = path_1.resolve(this.android.platformDir, this.android.webDir);
|
160 | this.android.resDirAbs = path_1.resolve(this.android.platformDir, this.android.resDir);
|
161 | }
|
162 | initIosConfig() {
|
163 | this.platforms.push(this.ios.name);
|
164 | this.ios.platformDir = path_1.resolve(this.app.rootDir, this.ios.name);
|
165 | this.ios.assets.templateDir = path_1.resolve(this.cli.assetsDir, this.ios.assets.templateName);
|
166 | this.ios.assets.pluginsDir = path_1.resolve(this.cli.assetsDir, this.ios.assets.pluginsFolderName);
|
167 | this.ios.webDirAbs = path_1.resolve(this.ios.platformDir, this.ios.nativeProjectName, this.ios.webDir);
|
168 | if (this.app.extConfig && this.app.extConfig.ios && this.app.extConfig.ios.cordovaSwiftVersion) {
|
169 | this.ios.cordovaSwiftVersion = this.app.extConfig.ios.cordovaSwiftVersion;
|
170 | }
|
171 | if (this.app.extConfig && this.app.extConfig.ios && this.app.extConfig.ios.minVersion) {
|
172 | this.ios.minVersion = this.app.extConfig.ios.minVersion;
|
173 | }
|
174 | }
|
175 | initWindowsConfig() {
|
176 | if (this.cli.os !== definitions_1.OS.Windows) {
|
177 | return;
|
178 | }
|
179 | this.windows.androidStudioPath = this.app.windowsAndroidStudioPath;
|
180 | }
|
181 | initLinuxConfig() {
|
182 | if (this.app.linuxAndroidStudioPath) {
|
183 | this.linux.androidStudioPath = this.app.linuxAndroidStudioPath;
|
184 | }
|
185 | }
|
186 | initPluginsConfig() {
|
187 | this.plugins.assets.templateDir = path_1.join(this.cli.assetsDir, this.plugins.assets.templateName);
|
188 | }
|
189 | mergeConfigData() {
|
190 | const extConfig = this.app.extConfig || {};
|
191 | Object.assign(this.app, extConfig);
|
192 |
|
193 | this.app.webDirAbs = path_1.resolve(this.app.rootDir, this.app.webDir);
|
194 | }
|
195 | loadExternalConfig() {
|
196 | this.app.extConfigFilePath = path_1.join(this.app.rootDir, this.app.extConfigName);
|
197 | try {
|
198 | const extConfigStr = fs_1.readFileSync(this.app.extConfigFilePath, 'utf8');
|
199 | try {
|
200 |
|
201 | this.app.extConfig = JSON.parse(extConfigStr);
|
202 | this.cli.npmClient = this.app.extConfig.npmClient || '';
|
203 | }
|
204 | catch (e) {
|
205 | common_1.logFatal(`error parsing: ${path_1.basename(this.app.extConfigFilePath)}\n`, e);
|
206 | }
|
207 | }
|
208 | catch (_a) {
|
209 |
|
210 | }
|
211 | }
|
212 | foundExternalConfig() {
|
213 | return !!this.app.extConfig;
|
214 | }
|
215 | selectPlatforms(selectedPlatformName) {
|
216 | if (selectedPlatformName) {
|
217 |
|
218 | const platformName = selectedPlatformName.toLowerCase().trim();
|
219 | if (!this.isValidPlatform(platformName)) {
|
220 | common_1.logFatal(`Invalid platform: ${platformName}`);
|
221 | }
|
222 | else if (!this.platformDirExists(platformName)) {
|
223 | this.platformNotCreatedError(platformName);
|
224 | }
|
225 |
|
226 | return [platformName];
|
227 | }
|
228 |
|
229 |
|
230 | return this.getExistingPlatforms();
|
231 | }
|
232 | async askPlatform(selectedPlatformName, promptMessage) {
|
233 | if (!selectedPlatformName) {
|
234 | const inquirer = await Promise.resolve().then(() => require('inquirer'));
|
235 | const answer = await inquirer.prompt({
|
236 | type: 'list',
|
237 | name: 'mode',
|
238 | message: promptMessage,
|
239 | choices: this.platforms
|
240 | });
|
241 | return answer.mode.toLowerCase().trim();
|
242 | }
|
243 | const platformName = selectedPlatformName.toLowerCase().trim();
|
244 | if (!this.isValidPlatform(platformName)) {
|
245 | common_1.logFatal(`Invalid platform: "${platformName}". Valid platforms include: ${this.platforms.join(', ')}`);
|
246 | }
|
247 | return platformName;
|
248 | }
|
249 | getExistingPlatforms() {
|
250 | const platforms = [];
|
251 | if (this.platformDirExists(this.android.name)) {
|
252 | platforms.push(this.android.name);
|
253 | }
|
254 | if (this.platformDirExists(this.ios.name)) {
|
255 | platforms.push(this.ios.name);
|
256 | }
|
257 | if (this.platformDirExists(this.electron.name)) {
|
258 | platforms.push(this.electron.name);
|
259 | }
|
260 | platforms.push(this.web.name);
|
261 | return platforms;
|
262 | }
|
263 | platformDirExists(platformName) {
|
264 | let platformDir = null;
|
265 | try {
|
266 | let testDir = path_1.join(this.app.rootDir, platformName);
|
267 | if (platformName === 'web') {
|
268 | testDir = this.app.webDirAbs;
|
269 | }
|
270 | fs_1.accessSync(testDir);
|
271 | platformDir = testDir;
|
272 | }
|
273 | catch (e) { }
|
274 | return platformDir;
|
275 | }
|
276 | isValidPlatform(platform) {
|
277 | return this.platforms.includes(platform);
|
278 | }
|
279 | platformNotCreatedError(platformName) {
|
280 | const chalk = require('chalk');
|
281 | if (platformName === 'web') {
|
282 | common_1.logFatal(`Could not find the web platform directory. Make sure ${chalk.bold(this.app.webDir)} exists.`);
|
283 | }
|
284 | common_1.logFatal(`${chalk.bold(platformName)}" platform has not been created. Use "npx cap add ${platformName}" to add the platform project.`);
|
285 | }
|
286 | }
|
287 | exports.Config = Config;
|
288 | function loadPackageJson(dir) {
|
289 | let p = null;
|
290 | try {
|
291 | p = require(path_1.join(dir, 'package.json'));
|
292 | }
|
293 | catch (e) { }
|
294 | return p;
|
295 | }
|