Cordova挂钩,获取项目名称

vlurs2pr  于 2022-11-15  发布在  其他
关注(0)|答案(3)|浏览(371)

我正在写一个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项目名称并将其存储在变量中的正确方法是什么?任何帮助或想法都将受到感谢!

6qfn3psc

6qfn3psc1#

您可以使用模块挂接类型:
在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中支持并测试,请参阅此处):

<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>
35g0bw71

35g0bw712#

这是我从不同的答案编译的,在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());

};
5cg8jx4n

5cg8jx4n3#

当前的方法是使用cordova-common

const path = require("path");

module.exports = (context) => {
  const { ConfigParser } = context.requireCordovaModule("cordova-common");
  const appConfig = new ConfigParser(path.resolve(context.opts.projectRoot, "config.xml"));
  console.log(appConfig.name());
}

如需其他指令,请参阅cordova-common NPM page

相关问题