plans.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 csv = require('csv/sync')
  5. const fs = require('fs')
  6. const path = require('path')
  7. const minimist = require('minimist')
  8. function readXLSXFile(fileName, sheetName) {
  9. // Pick the xlsx file
  10. const filePath = path.resolve(__dirname, fileName)
  11. const file = xlsx.readFile(filePath)
  12. if (!file.SheetNames.includes(sheetName)) {
  13. console.error(
  14. `Error: sheet '${sheetName}' not found.\n` +
  15. `Valid sheet names are: ${file.SheetNames.join(',')}`
  16. )
  17. process.exit(1)
  18. }
  19. const workSheet = Object.values(file.Sheets)[
  20. file.SheetNames.indexOf(sheetName)
  21. ]
  22. // Convert to JSON
  23. const workSheetJSON = xlsx.utils.sheet_to_json(workSheet)
  24. return workSheetJSON
  25. }
  26. function readCSVFile(fileName) {
  27. // Pick the csv file
  28. const filePath = path.resolve(__dirname, fileName)
  29. const input = fs.readFileSync(filePath, 'utf8')
  30. const rawRecords = csv.parse(input, { columns: true })
  31. return rawRecords
  32. }
  33. function readJSONFile(fileName) {
  34. const filePath = path.resolve(__dirname, fileName)
  35. const file = fs.readFileSync(filePath)
  36. const plans = JSON.parse(file)
  37. // convert the plans JSON from recurly to an array of
  38. // objects matching the spreadsheet format
  39. const result = []
  40. for (const plan of plans) {
  41. const newRow = { plan_code: plan.code }
  42. for (const price of plan.currencies) {
  43. newRow[price.currency] = price.unitAmount
  44. }
  45. result.push(newRow)
  46. }
  47. return result
  48. }
  49. // Mapping of [output_keys]:[actual_keys]
  50. const plansMap = {
  51. student: 'student',
  52. personal: 'paid-personal',
  53. collaborator: 'collaborator',
  54. professional: 'professional',
  55. }
  56. const currencies = {
  57. USD: {
  58. symbol: '$',
  59. placement: 'before',
  60. },
  61. EUR: {
  62. symbol: '€',
  63. placement: 'before',
  64. },
  65. GBP: {
  66. symbol: '£',
  67. placement: 'before',
  68. },
  69. SEK: {
  70. symbol: ' kr',
  71. placement: 'after',
  72. },
  73. CAD: {
  74. symbol: '$',
  75. placement: 'before',
  76. },
  77. NOK: {
  78. symbol: ' kr',
  79. placement: 'after',
  80. },
  81. DKK: {
  82. symbol: ' kr',
  83. placement: 'after',
  84. },
  85. AUD: {
  86. symbol: '$',
  87. placement: 'before',
  88. },
  89. NZD: {
  90. symbol: '$',
  91. placement: 'before',
  92. },
  93. CHF: {
  94. symbol: 'Fr ',
  95. placement: 'before',
  96. },
  97. SGD: {
  98. symbol: '$',
  99. placement: 'before',
  100. },
  101. INR: {
  102. symbol: '₹',
  103. placement: 'before',
  104. },
  105. }
  106. const buildCurrencyValue = (amount, currency) => {
  107. return currency.placement === 'before'
  108. ? `${currency.symbol}${amount}`
  109. : `${amount}${currency.symbol}`
  110. }
  111. function generatePlans(workSheetJSON) {
  112. // localizedPlanPricing object for settings.overrides.saas.js
  113. const localizedPlanPricing = {}
  114. // plans object for main/plans.js
  115. const plans = {}
  116. for (const [currency, currencyDetails] of Object.entries(currencies)) {
  117. localizedPlanPricing[currency] = {
  118. symbol: currencyDetails.symbol.trim(),
  119. free: {
  120. monthly: buildCurrencyValue(0, currencyDetails),
  121. annual: buildCurrencyValue(0, currencyDetails),
  122. },
  123. }
  124. plans[currency] = {
  125. symbol: currencyDetails.symbol.trim(),
  126. }
  127. for (const [outputKey, actualKey] of Object.entries(plansMap)) {
  128. const monthlyPlan = workSheetJSON.find(
  129. data => data.plan_code === actualKey
  130. )
  131. if (!monthlyPlan) throw new Error(`Missing plan: ${actualKey}`)
  132. const actualKeyAnnual = `${actualKey}-annual`
  133. const annualPlan = workSheetJSON.find(
  134. data => data.plan_code === actualKeyAnnual
  135. )
  136. if (!annualPlan) throw new Error(`Missing plan: ${actualKeyAnnual}`)
  137. const monthly = buildCurrencyValue(monthlyPlan[currency], currencyDetails)
  138. const monthlyTimesTwelve = buildCurrencyValue(
  139. monthlyPlan[currency] * 12,
  140. currencyDetails
  141. )
  142. const annual = buildCurrencyValue(annualPlan[currency], currencyDetails)
  143. localizedPlanPricing[currency] = {
  144. ...localizedPlanPricing[currency],
  145. [outputKey]: { monthly, monthlyTimesTwelve, annual },
  146. }
  147. plans[currency] = {
  148. ...plans[currency],
  149. [outputKey]: { monthly, annual },
  150. }
  151. }
  152. }
  153. return { localizedPlanPricing, plans }
  154. }
  155. function generateGroupPlans(workSheetJSON) {
  156. const groupPlans = workSheetJSON.filter(data =>
  157. data.plan_code.startsWith('group')
  158. )
  159. const currencies = [
  160. 'AUD',
  161. 'CAD',
  162. 'CHF',
  163. 'DKK',
  164. 'EUR',
  165. 'GBP',
  166. 'INR',
  167. 'NOK',
  168. 'NZD',
  169. 'SEK',
  170. 'SGD',
  171. 'USD',
  172. ]
  173. const sizes = ['2', '3', '4', '5', '10', '20', '50']
  174. const result = {}
  175. for (const type1 of ['educational', 'enterprise']) {
  176. result[type1] = {}
  177. for (const type2 of ['professional', 'collaborator']) {
  178. result[type1][type2] = {}
  179. for (const currency of currencies) {
  180. result[type1][type2][currency] = {}
  181. for (const size of sizes) {
  182. const planCode = `group_${type2}_${size}_${type1}`
  183. const plan = groupPlans.find(data => data.plan_code === planCode)
  184. if (!plan) throw new Error(`Missing plan: ${planCode}`)
  185. result[type1][type2][currency][size] = {
  186. price_in_cents: plan[currency] * 100,
  187. }
  188. }
  189. }
  190. }
  191. }
  192. return result
  193. }
  194. const argv = minimist(process.argv.slice(2), {
  195. string: ['output', 'file', 'sheet'],
  196. alias: { o: 'output', f: 'file', s: 'sheet' },
  197. })
  198. let input
  199. if (argv.file) {
  200. const ext = path.extname(argv.file)
  201. switch (ext) {
  202. case '.csv':
  203. input = readCSVFile(argv.file)
  204. break
  205. case '.xls':
  206. case '.xlsx':
  207. input = readXLSXFile(argv.file, argv.sheet)
  208. break
  209. case '.json':
  210. input = readJSONFile(argv.file)
  211. break
  212. default:
  213. console.log('Invalid file type: must be csv, xls, xlsx, or json')
  214. }
  215. } else {
  216. console.log(
  217. 'usage: node plans.js -f <file.xls|file.csv|file.json> [-s <sheet>] -o <dir>'
  218. )
  219. process.exit(1)
  220. }
  221. // removes quotes from object keys
  222. const formatJS = obj =>
  223. JSON.stringify(obj, null, 2).replace(/"([^"]+)":/g, '$1:')
  224. const formatJSON = obj => JSON.stringify(obj, null, 2)
  225. function writeFile(outputFile, data) {
  226. console.log(`Writing ${outputFile}`)
  227. fs.writeFileSync(outputFile, data)
  228. }
  229. const { localizedPlanPricing, plans } = generatePlans(input)
  230. const groupPlans = generateGroupPlans(input)
  231. if (argv.output) {
  232. const dir = argv.output
  233. // check if output directory exists
  234. if (!fs.existsSync(dir)) {
  235. console.log(`Creating output directory ${dir}`)
  236. fs.mkdirSync(dir)
  237. }
  238. // check if output directory is a directory and report error if not
  239. if (!fs.lstatSync(dir).isDirectory()) {
  240. console.error(`Error: output dir ${dir} is not a directory`)
  241. process.exit(1)
  242. }
  243. writeFile(`${dir}/localizedPlanPricing.json`, formatJS(localizedPlanPricing))
  244. writeFile(`${dir}/plans.json`, formatJS(plans))
  245. writeFile(`${dir}/groups.json`, formatJSON(groupPlans))
  246. } else {
  247. console.log('PLANS', plans)
  248. console.log('LOCALIZED', localizedPlanPricing)
  249. console.log('GROUP PLANS', JSON.stringify(groupPlans, null, 2))
  250. }
  251. console.log('Completed!')