Javascript Cordova:在config.xml中使用环境变量
我认为应该避免公开API密钥。在构建期间,如何在不同的环境中使用环境变量作为Javascript Cordova:在config.xml中使用环境变量,javascript,cordova,webpack,environment-variables,Javascript,Cordova,Webpack,Environment Variables,我认为应该避免公开API密钥。在构建期间,如何在不同的环境中使用环境变量作为config.xml中的参数 比如说, <preference name="TwitterConsumerKey" value="$TWITTER_KEY" /> <preference name="TwitterConsumerSecret" value="$TWITTER_SECRET" /> 我应该在我的自定义配置中包含什么样的内容才能使事情发生 编辑 环境。ts export const
config.xml
中的参数
比如说,
<preference name="TwitterConsumerKey" value="$TWITTER_KEY" />
<preference name="TwitterConsumerSecret" value="$TWITTER_SECRET" />
我应该在我的自定义配置中包含什么样的内容才能使事情发生
编辑
环境。ts
export const environment = {
mode: 'Production',
production: true,
firebase: {
apiKey: "SOME KEY",
authDomain: "prod.firebaseapp.com",
databaseURL: "https://prod.firebaseio.com",
projectId: "prod",
storageBucket: "prod.appspot.com",
messagingSenderId: "SOME ID"
},
// I'd like to use these key values in config.xml during build time
TWITTER_KEY: "SOME KEY",
TWITTER_SECRET: "SOME SECRET KEY"
};
environment.dev.ts
export const environment = {
mode: 'Development',
production: false,
firebase: {
apiKey: "SOME KEY",
authDomain: "dev.firebaseapp.com",
databaseURL: "https://dev.firebaseio.com",
projectId: "dev",
storageBucket: "dev.appspot.com",
messagingSenderId: "SOME ID"
},
// Use these key values as well
TWITTER_KEY: "SOME KEY",
TWITTER_SECRET: "SOME SECRET KEY"
};
例如,
ionic cordova build ios--dev
将使用environment.dev.ts
变量。另一方面,ionic cordova build ios--prod
将使用环境.ts
变量。我通过编程方式加载和解析config.xml
(或任何我需要的,如.git/HEAD
resp./git/refs/heads/*
以获得最新的提交哈希)来解决这类问题;考虑到您的环境{dev,prod}.ts
(在您的网页配置中):
可能可以用更优雅的方式编写,但这就是想法。您的代码如何从每个
dev环境.ts
和prod环境.ts
中获取密钥和秘密值?我理解您的问题,因为您在shell变量中有密钥/秘密(如value=“$TWITTER\u key”
shape所建议);如果涉及environment.dev/prod.ts
文件,请告诉我们您是如何将密钥/机密存储在其中的,无论如何,我会用建议的解决方案更新答案。我会尝试一下,然后返回给您。我应该将代码粘贴到自定义配置文件中的任何位置吗?另外,我应该如何更改config.xml文件?就这样吧?我说的是config.xml中的首选项名称和值。建议将import/require语句放在文件的顶部,其余的可以在使用收集的变量之前粘贴,或者将其包含在函数中以提高清晰度(如果愿意,甚至可以移动到另一个文件);config.xml可能没有问题(变量名称前的$
符号是多余的,但是如果您决定删除它们,请不要忘记从行中删除\$
。替换regexp)。
export const environment = {
mode: 'Development',
production: false,
firebase: {
apiKey: "SOME KEY",
authDomain: "dev.firebaseapp.com",
databaseURL: "https://dev.firebaseio.com",
projectId: "dev",
storageBucket: "dev.appspot.com",
messagingSenderId: "SOME ID"
},
// Use these key values as well
TWITTER_KEY: "SOME KEY",
TWITTER_SECRET: "SOME SECRET KEY"
};
// just get the environment files:
const devEnvironment = require('/path/to/environment.dev.ts').environment;
const prodEnvironment = require('/path/to/environment.prod.ts').environment;
//...
const envConfig = env === 'prod' ? prodEnvironment : devEnvironment;
var twitterKey;
var twitterSecret;
// load the config.xml
var configXmlFile = fs.readFileSync(path.join(__dirname, './config.xml'));
// get it's rows
var configRows = configXmlFile.toString().split(/\n/);
// traverse the rows
configRows.forEach(function (row) {
// check if the row do have what we're looking for
if (row.indexOf('TwitterConsumerKey') !== -1) {
// get the environment variable name
var twitterKeyVarName = row.replace(/.*value="\$([^"]*)".*/, '$1');
// get the variable value - environment file variant
twitterKey = envConfig[twitterKeyVarName];
}
else if (row.indexOf('TwitterConsumerSecret') !== -1) {
var twitterSecretVarName = row.replace(/.*value="\$([^"]*)".*/, '$1');
twitterSecret = envConfig[twitterSecretVarName];
}
});