/*
 * Copyright (C) 2021 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

const fs = require('fs')
const path = require('path')

/**
 * This returns the contents of the translations for the given package and locale
 */
async function getTranslationList(packageName, locale) {
  const jsonData = await fs.promises.readFile(
    path.resolve(__dirname, `../lib/${packageName}/${locale}.json`),
    {encoding: 'utf8'}
  )
  return jsonData
}

module.exports = getTranslationList

/*
 * Copyright (C) 2021 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

const fs = require('fs')
const path = require('path')

/**
 * This returns the contents of the translations for the given package and locale
 */
async function readTRanslationFile(packageName, locale) {
  const filePath = path.resolve(__dirname, `../lib/${packageName}/${locale}.json`)
  const jsonData = await fs.promises.readFile(filePath, {encoding: 'utf8'})
  // sanity check
  try {
    JSON.parse(jsonData)
  } catch (ex) {
    console.log(ex)
    throw new Error(`Failed parsing content from ${filePath}`)
  }
  return jsonData
}

module.exports = readTRanslationFile

Add a sanity check when reading translation json data
Add a sanity check when reading translation json data

closes MAT-200
flag=none

test plan:
  - the build succeeds
  - muck with one of the packages/translations/lib/canvas-rce/*.json
    files so it's no longer valid json
  - run `yarn installTranslations` from canvas-rce directory
  > expect to see an error message telling you what went wrong.

Change-Id: Ie46bd4c5eb0ec2b4c2b30c737efbd16d417dc9d4
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/265468
Tested-by: Service Cloud Jenkins <9144042a601061f88f1e1d7a1753ea3e2972119d@instructure.com>
Reviewed-by: Guilherme Baron <2bd19d74e693a9c19d383f14059425669349e568@instructure.com>
QA-Review: Guilherme Baron <2bd19d74e693a9c19d383f14059425669349e568@instructure.com>
Product-Review: Ed Schiebel <0ca8a83847a5889cf05cb219c0ab09f6b646243b@instructure.com>
