plans.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Creates data for localizedPlanPricing object in settings.overrides.saas.js
  2. // and plans object in main/plans.js
  3. const xlsx = require('xlsx')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const [fileName, sheetName] = process.argv.slice(2)
  7. // Pick the xlsx file
  8. const filePath = path.resolve(__dirname, fileName)
  9. const file = xlsx.readFile(filePath)
  10. if (!file.SheetNames.includes(sheetName)) {
  11. throw new Error('Sheet not found!')
  12. }
  13. const workSheet = Object.values(file.Sheets)[file.SheetNames.indexOf(sheetName)]
  14. // Convert to JSON
  15. const workSheetJSON = xlsx.utils.sheet_to_json(workSheet)
  16. // Mapping of [output_keys]:[actual_keys]
  17. const plansMap = {
  18. student: 'student',
  19. personal: 'paid-personal',
  20. collaborator: 'collaborator',
  21. professional: 'professional',
  22. }
  23. const currencies = {
  24. USD: {
  25. symbol: '$',
  26. placement: 'before',
  27. },
  28. EUR: {
  29. symbol: '€',
  30. placement: 'before',
  31. },
  32. GBP: {
  33. symbol: '£',
  34. placement: 'before',
  35. },
  36. SEK: {
  37. symbol: ' kr',
  38. placement: 'after',
  39. },
  40. CAD: {
  41. symbol: '$',
  42. placement: 'before',
  43. },
  44. NOK: {
  45. symbol: ' kr',
  46. placement: 'after',
  47. },
  48. DKK: {
  49. symbol: ' kr',
  50. placement: 'after',
  51. },
  52. AUD: {
  53. symbol: '$',
  54. placement: 'before',
  55. },
  56. NZD: {
  57. symbol: '$',
  58. placement: 'before',
  59. },
  60. CHF: {
  61. symbol: 'Fr ',
  62. placement: 'before',
  63. },
  64. SGD: {
  65. symbol: '$',
  66. placement: 'before',
  67. },
  68. }
  69. const buildCurrencyValue = (amount, currency) => {
  70. return currency.placement === 'before'
  71. ? `${currency.symbol}${amount}`
  72. : `${amount}${currency.symbol}`
  73. }
  74. // localizedPlanPricing object for settings.overrides.saas.js
  75. let localizedPlanPricing = {}
  76. // plans object for main/plans.js
  77. let plans = {}
  78. for (const [currency, currencyDetails] of Object.entries(currencies)) {
  79. localizedPlanPricing[currency] = {
  80. symbol: currencyDetails.symbol.trim(),
  81. free: {
  82. monthly: buildCurrencyValue(0, currencyDetails),
  83. annual: buildCurrencyValue(0, currencyDetails),
  84. },
  85. }
  86. plans[currency] = {
  87. symbol: currencyDetails.symbol.trim(),
  88. }
  89. for (const [outputKey, actualKey] of Object.entries(plansMap)) {
  90. const monthlyPlan = workSheetJSON.find(data => data.plan_code === actualKey)
  91. if (!monthlyPlan) throw new Error(`Missing plan: ${actualKey}`)
  92. const actualKeyAnnual = `${actualKey}-annual`
  93. const annualPlan = workSheetJSON.find(
  94. data => data.plan_code === actualKeyAnnual
  95. )
  96. if (!annualPlan) throw new Error(`Missing plan: ${actualKeyAnnual}`)
  97. const monthly = buildCurrencyValue(monthlyPlan[currency], currencyDetails)
  98. const monthlyTimesTwelve = buildCurrencyValue(
  99. monthlyPlan[currency] * 12,
  100. currencyDetails
  101. )
  102. const annual = buildCurrencyValue(annualPlan[currency], currencyDetails)
  103. localizedPlanPricing[currency] = {
  104. ...localizedPlanPricing[currency],
  105. [outputKey]: { monthly, monthlyTimesTwelve, annual },
  106. }
  107. plans[currency] = {
  108. ...plans[currency],
  109. [outputKey]: { monthly, annual },
  110. }
  111. }
  112. }
  113. // removes quotes from object keys
  114. const format = obj => JSON.stringify(obj, null, 2).replace(/"([^"]+)":/g, '$1:')
  115. const dir = './output'
  116. localizedPlanPricing = format(localizedPlanPricing)
  117. plans = format(plans)
  118. if (!fs.existsSync(dir)) {
  119. fs.mkdirSync(dir)
  120. }
  121. fs.writeFileSync(`${dir}/localizedPlanPricing.json`, localizedPlanPricing)
  122. fs.writeFileSync(`${dir}/plans.json`, plans)
  123. console.log('Completed!')