chcpBuildOptions.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. Module helps to generate building options for plugin.
  3. Those options then injected into platform-specific config.xml.
  4. */
  5. var fs = require('fs');
  6. var path = require('path');
  7. var OPTIONS_FILE_NAME = 'chcpbuild.options';
  8. module.exports = {
  9. getBuildConfigurationByName: getBuildConfigurationByName
  10. };
  11. // region Public API
  12. /**
  13. * Generate build options depending on the options, provided in console.
  14. *
  15. * @param {String} buildName - build identifier
  16. * @return {Object} build options; null - if none are found
  17. */
  18. function getBuildConfigurationByName(ctx, buildName) {
  19. // load options from the chcpbuild.options file
  20. var chcpBuildOptions = getBuildOptionsFromConfig(ctx);
  21. if (chcpBuildOptions == null) {
  22. return null;
  23. }
  24. var resultConfig = chcpBuildOptions[buildName];
  25. if (!resultConfig) {
  26. return null;
  27. }
  28. // backwards capability
  29. // TODO: remove this in the next versions
  30. if (resultConfig['config-file'] && !resultConfig['config-file']['url']) {
  31. var url = resultConfig['config-file'];
  32. resultConfig['config-file'] = {
  33. 'url': url
  34. };
  35. }
  36. return resultConfig;
  37. }
  38. // endregion
  39. // region Private API
  40. /**
  41. * Read options, listed in chcpbuild.options file.
  42. *
  43. * @return {Object} options from chcpbuild.options file
  44. */
  45. function getBuildOptionsFromConfig(ctx) {
  46. var chcpBuildOptionsFilePath = path.join(ctx.opts.projectRoot, OPTIONS_FILE_NAME);
  47. return readObjectFromFile(chcpBuildOptionsFilePath);
  48. };
  49. function readObjectFromFile(filePath) {
  50. var objData = null;
  51. try {
  52. var data = fs.readFileSync(filePath, 'utf-8');
  53. objData = JSON.parse(data, 'utf-8');
  54. } catch (err) {}
  55. return objData;
  56. }
  57. // endregion