report_runner.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const version = require('./version_compare')
  2. const iPhoneBackup = require('./iphone_backup.js').iPhoneBackup
  3. const log = require('./log')
  4. async function runSwitchedReport (report, program) {
  5. log.verbose('runner got path', program.base)
  6. async function createPromise (key, program, backup) {
  7. log.verbose('resolving using promises.')
  8. return new Promise((resolve, reject) => {
  9. report.functions[key](program, backup, resolve, reject)
  10. })
  11. }
  12. // New type of reports
  13. var backup = iPhoneBackup.fromID(program.backup, program.base)
  14. var flag = false
  15. var value
  16. // Check for a compatible reporting tool.
  17. for (var key in report.functions) {
  18. if (version.versionCheck(backup.iOSVersion, key)) {
  19. if (!report.usesPromises) {
  20. log.verbose('using synchronous call.')
  21. value = report.functions[key](program, backup)
  22. } else {
  23. // Use promises to resolve synchronously
  24. value = await createPromise(key, program, backup)
  25. }
  26. flag = true
  27. break
  28. }
  29. }
  30. if (!flag) {
  31. log.error(`Couldn't run '${report.name}'.`)
  32. log.error(`The report generator '${report.name}' does not support iOS`, backup.iOSVersion)
  33. log.error(`If you think it should, file an issue here:`)
  34. log.error(`https://github.com/richinfante/iphonebackuptools/issues`)
  35. return null
  36. }
  37. return value
  38. }
  39. async function runSingleReport (report, program) {
  40. log.verbose('runner got path', program.base)
  41. async function createPromise (program, backup, base) {
  42. log.verbose('resolving using promises.')
  43. return new Promise((resolve, reject) => {
  44. if (report.requiresBackup) {
  45. report.func(program, backup, resolve, reject)
  46. } else {
  47. report.func(program, base, resolve, reject)
  48. }
  49. })
  50. }
  51. async function runReport (backup, base) {
  52. if (!report.usesPromises) {
  53. log.verbose('using synchronous call.')
  54. // Old-style non-promise based report.
  55. if (report.requiresBackup) {
  56. return report.func(program, backup)
  57. } else {
  58. return report.func(program, base)
  59. }
  60. } else {
  61. // Create a promise to resolve this function
  62. // Use promises to resolve synchronously
  63. return createPromise(program, backup, base)
  64. }
  65. }
  66. // New type of reports
  67. var backup = iPhoneBackup.fromID(program.backup, program.base)
  68. if (report.supportedVersions !== undefined) {
  69. if (version.versionCheck(backup.iOSVersion, report.supportedVersions)) {
  70. return runReport(backup, program.base)
  71. } else {
  72. log.error(`Couldn't run '${report.name}'.`)
  73. log.error(`The report generator '${report.name}' does not support iOS`, backup.iOSVersion)
  74. log.error(`If you think it should, file an issue here:`)
  75. log.error(`https://github.com/richinfante/iphonebackuptools/issues`)
  76. return null
  77. }
  78. } else {
  79. return runReport(backup, program.base)
  80. }
  81. }
  82. module.exports.runSwitchedReport = runSwitchedReport
  83. module.exports.runSingleReport = runSingleReport