Skip to content

Commit

Permalink
feat(cordova): move Cordova prefs from config.xml to capacitor.config…
Browse files Browse the repository at this point in the history
….json (#1977)
  • Loading branch information
jcesarmobile authored Sep 23, 2019
1 parent d08ec9f commit 2655f0f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
43 changes: 39 additions & 4 deletions cli/src/cordova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { copy as fsCopy, existsSync } from 'fs-extra';
import { getAndroidPlugins } from './android/common';
import { getIOSPlugins } from './ios/common';
import { copy } from './tasks/copy';
import * as inquirer from 'inquirer';

const plist = require('plist');
const chalk = require('chalk');
Expand Down Expand Up @@ -172,11 +173,19 @@ export async function autoGenerateConfig(config: Config, cordovaPlugins: Plugin[
const xmlString = await writeXML(item);
return xmlString;
}));
let pluginPreferencesString: Array<string> = [];
if (config.app.extConfig && config.app.extConfig.cordova && config.app.extConfig.cordova.preferences) {
pluginPreferencesString = await Promise.all(Object.keys(config.app.extConfig.cordova.preferences).map(async (key): Promise<string> => {
return `
<preference name="${key}" value="${config.app.extConfig.cordova.preferences[key]}" />`;
}));
}
const content = `<?xml version='1.0' encoding='utf-8'?>
<widget version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<widget version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<access origin="*" />
${pluginEntriesString.join('\n')}
</widget>`;
${pluginEntriesString.join('')}
${pluginPreferencesString.join('')}
</widget>`;
await writeFileAsync(cordovaConfigXMLFile, content);
}

Expand Down Expand Up @@ -317,7 +326,7 @@ export async function checkAndInstallDependencies(config: Config, plugins: Plugi
return needsUpdate;
}

export function getIncompatibleCordovaPlugins(platform: string){
export function getIncompatibleCordovaPlugins(platform: string) {
let pluginList = ["cordova-plugin-statusbar", "cordova-plugin-splashscreen", "cordova-plugin-ionic-webview",
"cordova-plugin-crosswalk-webview", "cordova-plugin-wkwebview-engine", "cordova-plugin-console",
"cordova-plugin-compat", "cordova-plugin-music-controls", "cordova-plugin-add-swift-support",
Expand All @@ -327,3 +336,29 @@ export function getIncompatibleCordovaPlugins(platform: string){
}
return pluginList;
}

export async function getCordovaPreferences(config: Config) {
const configXml = join(config.app.rootDir, 'config.xml');
let cordova: any = {};
if (existsSync(configXml)) {
cordova.preferences = {};
const xmlMeta = await readXML(configXml);
xmlMeta.widget.preference.map((pref: any) => {
cordova.preferences[pref.$.name] = pref.$.value;
});
}
if (config.app.extConfig && config.app.extConfig.cordova && config.app.extConfig.cordova.preferences && cordova.preferences) {
const answer = await inquirer.prompt({
type: 'confirm',
name: 'confirm',
message: 'capacitor.config.json already contains cordova preferences. Overwrite with values from config.xml?'
});
if (!answer.confirm) {
cordova = config.app.extConfig.cordova;
}
}
if (config.app.extConfig && !cordova.preferences) {
cordova = config.app.extConfig.cordova;
}
return cordova;
}
1 change: 1 addition & 0 deletions cli/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ExternalConfig {
cordovaLinkerFlags?: string[];
};
npmClient?: string;
cordova?: any;
}

export interface AppPluginsConfig {
Expand Down
6 changes: 5 additions & 1 deletion cli/src/tasks/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
printNextSteps,
runTask,
} from '../common';
import { getCordovaPreferences } from '../cordova';
import { emoji as _e } from '../util/emoji';
import { checkInteractive } from '../util/term';

Expand Down Expand Up @@ -43,6 +44,8 @@ export async function initCommand(config: Config, name: string, id: string, webD
]
);

const cordova = await getCordovaPreferences(config);

await runTask(`Initializing Capacitor project in ${chalk.blue(config.app.rootDir)}`, async () => {
config.app.appId = appId;
config.app.appName = appName;
Expand All @@ -55,7 +58,8 @@ export async function initCommand(config: Config, name: string, id: string, webD
appId,
appName,
webDir,
npmClient
npmClient,
cordova
});
});

Expand Down

0 comments on commit 2655f0f

Please sign in to comment.