我正在编写一个cordova钩子,其中一部分需要获取项目名称。到目前为止,从其他例子开始,我有这个。
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
,我就会收到此错误,
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项目名称并将其存储在变量中的正确方法是什么?任何帮助或想法表示赞赏!
答案 0 :(得分:4)
您可以使用模块挂钩类型:
在config.xml中:
<hook type="after_prepare" src="scripts/afterPrepareSplash.js" />
在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'));
};
但是,您似乎正在寻找支持闪屏的设备。我建议您使用<splash
标记(在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>
答案 1 :(得分:0)
这是我从 2021 年有效的不同答案中得出的汇编。 不幸的是,cordova-utils 已经不在了,所以它可以为从谷歌登陆这里的人节省时间。 我用它来更新 Xcode 项目中的一些参数以进行插件编译。
您可以看到我从 config.xml 中获取了应用程序 ID 和名称
您可以将其添加到 after_prepare 钩子中:
<hook src="scripts/addBuildSettingsToXcode.js" type="after_prepare" />
#!/usr/bin/env node
let fs = require('fs');
let xcode = require('xcode');
let path = require('path');
let et = require('elementtree');
module.exports = function (context) {
//console.log(context);
function addBuildPropertyToDebugAndRelease(prop, value) {
console.log('Xcode Adding ' + prop + '=' + value);
myProj.addBuildProperty(prop, value, 'Debug');
myProj.addBuildProperty(prop, value, 'Release');
}
function updateBuildPropertyToDebugAndRelease(prop, value) {
console.log('Xcode Updating ' + prop + '=' + value );
myProj.updateBuildProperty(prop, value, 'Debug');
myProj.updateBuildProperty(prop, value, 'Release');
}
// Getting app id and name from config.xml
let config_xml = path.join(context.opts.projectRoot, 'config.xml');
let data = fs.readFileSync(config_xml).toString();
let etree = et.parse(data);
let appId = etree.getroot().attrib.id ;
let appName = etree.getroot().find('name')['text'];
// Building project path
let projectPath = 'platforms/ios/' + appName + '.xcodeproj/project.pbxproj';
// Opening Xcode project and parsing it
myProj = xcode.project(projectPath);
myProj = myProj.parseSync();
// Common properties
addBuildPropertyToDebugAndRelease('DEVELOPMENT_TEAM', 'CGXXXXXXX');
addBuildPropertyToDebugAndRelease('CODE_SIGN_IDENTITY', '"Apple Development"');
// Compilation properties
addBuildPropertyToDebugAndRelease('ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES', 'YES');
// Save project file
fs.writeFileSync(projectPath, myProj.writeSync());
};