Cordova钩子,获取项目名称 [英] Cordova hooks, Get projects name
问题描述
我在写一个cordova钩子,它的一部分需要获取项目名称。到目前为止,从其他示例中,我有这个。
I'm writing a cordova hook and part of it requires getting the projects name. So far, working from other examples, I have this.
var cordova_util = require('cordova/src/util');
var projectRoot = cordova_util.isCordova(process.cwd());
var projectXml = cordova_util.projectConfig(projectRoot);
var projectConfig = new cordova_util.config_parser(projectXml);
projectConfig.name();
// Just for example, to get things working first
console.log(projName);
但是一旦我运行 cordvoa prepare
得到此错误,
But once I run cordvoa prepare
, I get this error,
module.js:340
throw err;
^
Error: Cannot find module 'cordova/src/util'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/mhartington/Desktop/splashscreens/hooks/after_prepare/assets.js:3:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Hook failed with error code 8: /Users/mhartington/Desktop/splashscreens/hooks/after_prepare/assets.js
有什么想法我做错了什么?从 config.xml
中获取cordova项目名称并将其存储在变量中的正确方法是什么?任何帮助或想法感谢!
Any idea on what I'm doing wrong? What the correct way to get a cordova projects name from the config.xml
and store it in a variable? Any help or ideas are appreciated!
推荐答案
您可以使用模块挂钩类型:
You can use the module hook type:
config.xml:
in your config.xml:
<hook type="after_prepare" src="scripts/afterPrepareSplash.js" />
<
in your scripts/afterPrepareSplash.js
:
var path = require("path");
module.exports = function (context) {
var cordova_util = context.requireCordovaModule("cordova-lib/src/cordova/util"),
ConfigParser = context.requireCordovaModule('cordova-lib/src/configparser/ConfigParser'),
platforms = context.requireCordovaModule('cordova-lib/src/cordova/platforms'),
projectRoot = cordova_util.isCordova(),
xml = cordova_util.projectConfig(projectRoot),
cfg = new ConfigParser(xml);
function getDestFilePath(platform, destFile) {
var platform_path = path.join(projectRoot, 'platforms', platform),
parser = (new platforms[platform].parser(platform_path));
return path.join(parser.cordovaproj, destFile);
}
console.log(getDestFilePath('ios', 'res/ios/Default~iphone.png'));
};
但是,似乎你正在寻找splashscreens支持。我建议您使用< splash
标签(在cordova-cli 4.0.0中支持和测试, see here ):
However, It seems that you are looking for splashscreens support. I recommend you to use the <splash
tag (supported and tested in cordova-cli 4.0.0, see here):
<platform name="android">
<icon src="res/android/icon-ldpi.png" density="ldpi" />
<icon src="res/android/icon-mdpi.png" density="mdpi" />
<icon src="res/android/icon-hdpi.png" density="hdpi" />
<icon src="res/android/icon-xhdpi.png" density="xhdpi" />
<icon src="res/android/icon-xxhdpi.png" density="xxhdpi" />
<icon src="res/android/icon-xxxhdpi.png" density="xxxhdpi" />
<splash src="res/android/splash-land-hdpi.png" density="land-hdpi"/>
<splash src="res/android/splash-land-ldpi.png" density="land-ldpi"/>
<splash src="res/android/splash-land-mdpi.png" density="land-mdpi"/>
<splash src="res/android/splash-land-xhdpi.png" density="land-xhdpi"/>
<splash src="res/android/splash-port-hdpi.png" density="port-hdpi"/>
<splash src="res/android/splash-port-ldpi.png" density="port-ldpi"/>
<splash src="res/android/splash-port-mdpi.png" density="port-mdpi"/>
<splash src="res/android/splash-port-xhdpi.png" density="port-xhdpi"/>
</platform>
<platform name="ios">
<!-- iOS 7.0+ -->
<!-- iPhone / iPod Touch -->
<icon src="res/ios/icon-60.png" width="60" height="60" />
<icon src="res/ios/icon-60@2x.png" width="120" height="120" />
<!-- iPad -->
<icon src="res/ios/icon-76.png" width="76" height="76" />
<icon src="res/ios/icon-76@2x.png" width="152" height="152" />
<!-- iOS 6.1 -->
<!-- Spotlight Icon -->
<icon src="res/ios/icon-40.png" width="40" height="40" />
<icon src="res/ios/icon-40@2x.png" width="80" height="80" />
<!-- iPhone / iPod Touch -->
<icon src="res/ios/icon-57.png" width="57" height="57" />
<icon src="res/ios/icon-57@2x.png" width="114" height="114" />
<!-- iPad -->
<icon src="res/ios/icon-72.png" width="72" height="72" />
<icon src="res/ios/icon-72@2x.png" width="144" height="144" />
<!-- iPhone Spotlight and Settings Icon -->
<icon src="res/ios/icon-29.png" width="29" height="29" />
<icon src="res/ios/icon-29@2x.png" width="58" height="58" />
<!-- iPad Spotlight and Settings Icon -->
<icon src="res/ios/icon-50.png" width="50" height="50" />
<icon src="res/ios/icon-50@2x.png" width="100" height="100" />
<splash src="res/ios/Default~iphone.png" width="320" height="480"/>
<splash src="res/ios/Default@2x~iphone.png" width="640" height="960"/>
<splash src="res/ios/Default-Portrait~ipad.png" width="768" height="1024"/>
<splash src="res/ios/Default-Portrait@2x~ipad.png" width="1536" height="2048"/>
<splash src="res/ios/Default-Landscape~ipad.png" width="1024" height="768"/>
<splash src="res/ios/Default-Landscape@2x~ipad.png" width="2048" height="1536"/>
<splash src="res/ios/Default-568h@2x~iphone.png" width="640" height="1136"/>
<splash src="res/ios/Default-667h.png" width="750" height="1334"/>
<splash src="res/ios/Default-736h.png" width="1242" height="2208"/>
<splash src="res/ios/Default-Landscape-736h.png" width="2208" height="1242"/>
</platform>
这篇关于Cordova钩子,获取项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!