after_prepare.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env node
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. var path = require('path');
  23. var fs = require('fs');
  24. module.exports = function(context) {
  25. function main() {
  26. // get the file transfer server address from the specified variables
  27. var fileTransferServerAddress = getFileTransferServerAddress(context) || getDefaultFileTransferServerAddress(context);
  28. console.log('Tests will use the following file transfer server address: ' + fileTransferServerAddress);
  29. console.log('If you\'re using cordova@6.3.1 and the above address is wrong at "platform add", don\'t worry, it\'ll fix itself on "cordova run" or "cordova prepare".');
  30. // pass it to the tests
  31. writeFileTransferOptions(fileTransferServerAddress, context);
  32. }
  33. function getDefaultFileTransferServerAddress(context) {
  34. var address = null;
  35. var configNodes = context.opts.plugin.pluginInfo._et._root._children;
  36. for (var node in configNodes) {
  37. if (configNodes[node].attrib.name == 'FILETRANSFER_SERVER_ADDRESS') {
  38. address = configNodes[node].attrib.default;
  39. }
  40. }
  41. return address;
  42. }
  43. function getFileTransferServerAddress(context) {
  44. var platformJsonFile = path.join(context.opts.projectRoot, 'platforms', context.opts.platforms[0], context.opts.platforms[0] + '.json');
  45. var platformJson = JSON.parse(fs.readFileSync(platformJsonFile, 'utf8'));
  46. if (platformJson && platformJson.installed_plugins && platformJson.installed_plugins['cordova-plugin-file-transfer-tests'] && platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS) {
  47. return platformJson.installed_plugins['cordova-plugin-file-transfer-tests'].FILETRANSFER_SERVER_ADDRESS;
  48. } else {
  49. return null;
  50. }
  51. }
  52. function writeFileTransferOptions(address, context) {
  53. for (var p in context.opts.paths) {
  54. var ftOpts = {
  55. serverAddress: address
  56. };
  57. var ftOptsString = JSON.stringify(ftOpts);
  58. var ftOptsFile = path.join(context.opts.paths[p], 'fileTransferOpts.json');
  59. fs.writeFileSync(ftOptsFile, ftOptsString, 'utf8');
  60. }
  61. }
  62. main();
  63. };