plans.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Creates data for localizedPlanPricing object in settings.overrides.saas.js
  2. // and plans object in main/plans.js
  3. // https://github.com/import-js/eslint-plugin-import/issues/1810
  4. // eslint-disable-next-line import/no-unresolved
  5. import * as csv from 'csv/sync'
  6. import fs from 'node:fs'
  7. import path from 'node:path'
  8. import minimist from 'minimist'
  9. import { fileURLToPath } from 'node:url'
  10. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  11. function readCSVFile(fileName) {
  12. // Pick the csv file
  13. const filePath = path.resolve(__dirname, fileName)
  14. const input = fs.readFileSync(filePath, 'utf8')
  15. const rawRecords = csv.parse(input, { columns: true })
  16. return rawRecords
  17. }
  18. function readJSONFile(fileName) {
  19. const filePath = path.resolve(__dirname, fileName)
  20. const file = fs.readFileSync(filePath)
  21. const plans = JSON.parse(file)
  22. // convert the plans JSON from recurly to an array of
  23. // objects matching the spreadsheet format
  24. const result = []
  25. for (const plan of plans) {
  26. const newRow = { plan_code: plan.code }
  27. for (const price of plan.currencies) {
  28. newRow[price.currency] = price.unitAmount
  29. }
  30. result.push(newRow)
  31. }
  32. return result
  33. }
  34. // Mapping of [output_keys]:[actual_keys]
  35. const plansMap = {
  36. student: 'student',
  37. personal: 'paid-personal',
  38. collaborator: 'collaborator',
  39. professional: 'professional',
  40. }
  41. const currencies = [
  42. 'AUD',
  43. 'BRL',
  44. 'CAD',
  45. 'CHF',
  46. 'CLP',
  47. 'COP',
  48. 'DKK',
  49. 'EUR',
  50. 'GBP',
  51. 'INR',
  52. 'MXN',
  53. 'NOK',
  54. 'NZD',
  55. 'PEN',
  56. 'SEK',
  57. 'SGD',
  58. 'USD',
  59. ]
  60. function generatePlans(workSheetJSON) {
  61. // localizedPlanPricing object for settings.overrides.saas.js
  62. const localizedPlanPricing = {}
  63. // plans object for main/plans.js
  64. for (const currency of currencies) {
  65. localizedPlanPricing[currency] = {
  66. free: {
  67. monthly: 0,
  68. annual: 0,
  69. },
  70. }
  71. for (const [outputKey, actualKey] of Object.entries(plansMap)) {
  72. const monthlyPlan = workSheetJSON.find(
  73. data => data.plan_code === actualKey
  74. )
  75. if (!monthlyPlan) throw new Error(`Missing plan: ${actualKey}`)
  76. if (!(currency in monthlyPlan))
  77. throw new Error(
  78. `Missing currency "${currency}" for plan "${actualKey}"`
  79. )
  80. const actualKeyAnnual = `${actualKey}-annual`
  81. const annualPlan = workSheetJSON.find(
  82. data => data.plan_code === actualKeyAnnual
  83. )
  84. if (!annualPlan) throw new Error(`Missing plan: ${actualKeyAnnual}`)
  85. if (!(currency in annualPlan))
  86. throw new Error(
  87. `Missing currency "${currency}" for plan "${actualKeyAnnual}"`
  88. )
  89. const monthly = Number(monthlyPlan[currency])
  90. const monthlyTimesTwelve = Number(monthlyPlan[currency] * 12)
  91. const annual = Number(annualPlan[currency])
  92. localizedPlanPricing[currency] = {
  93. ...localizedPlanPricing[currency],
  94. [outputKey]: { monthly, monthlyTimesTwelve, annual },
  95. }
  96. }
  97. }
  98. return { localizedPlanPricing }
  99. }
  100. function generateGroupPlans(workSheetJSON) {
  101. const groupPlans = workSheetJSON.filter(data =>
  102. data.plan_code.startsWith('group')
  103. )
  104. const sizes = ['2', '3', '4', '5', '10', '20', '50']
  105. const result = {}
  106. for (const type1 of ['educational', 'enterprise']) {
  107. result[type1] = {}
  108. for (const type2 of ['professional', 'collaborator']) {
  109. result[type1][type2] = {}
  110. for (const currency of currencies) {
  111. result[type1][type2][currency] = {}
  112. for (const size of sizes) {
  113. const planCode = `group_${type2}_${size}_${type1}`
  114. const plan = groupPlans.find(data => data.plan_code === planCode)
  115. if (!plan) throw new Error(`Missing plan: ${planCode}`)
  116. result[type1][type2][currency][size] = {
  117. price_in_cents: plan[currency] * 100,
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return result
  124. }
  125. const argv = minimist(process.argv.slice(2), {
  126. string: ['output', 'file'],
  127. alias: { o: 'output', f: 'file' },
  128. })
  129. let input
  130. if (argv.file) {
  131. const ext = path.extname(argv.file)
  132. switch (ext) {
  133. case '.csv':
  134. input = readCSVFile(argv.file)
  135. break
  136. case '.json':
  137. input = readJSONFile(argv.file)
  138. break
  139. default:
  140. console.log('Invalid file type: must be csv or json')
  141. }
  142. } else {
  143. console.log('usage: node plans.mjs -f <file.csv|file.json> -o <dir>')
  144. process.exit(1)
  145. }
  146. // removes quotes from object keys
  147. const formatJS = obj =>
  148. JSON.stringify(obj, null, 2).replace(/"([^"]+)":/g, '$1:')
  149. const formatJSON = obj => JSON.stringify(obj, null, 2)
  150. function writeFile(outputFile, data) {
  151. console.log(`Writing ${outputFile}`)
  152. fs.writeFileSync(outputFile, data)
  153. }
  154. const { localizedPlanPricing } = generatePlans(input)
  155. const groupPlans = generateGroupPlans(input)
  156. if (argv.output) {
  157. const dir = argv.output
  158. // check if output directory exists
  159. if (!fs.existsSync(dir)) {
  160. console.log(`Creating output directory ${dir}`)
  161. fs.mkdirSync(dir)
  162. }
  163. // check if output directory is a directory and report error if not
  164. if (!fs.lstatSync(dir).isDirectory()) {
  165. console.error(`Error: output dir ${dir} is not a directory`)
  166. process.exit(1)
  167. }
  168. writeFile(`${dir}/localizedPlanPricing.json`, formatJS(localizedPlanPricing))
  169. writeFile(`${dir}/groups.json`, formatJSON(groupPlans))
  170. } else {
  171. console.log('LOCALIZED', localizedPlanPricing)
  172. console.log('GROUP PLANS', JSON.stringify(groupPlans, null, 2))
  173. }
  174. console.log('Completed!')