UNPKG

tc-source-content-updater

Version:

Module that updates source content for the desktop application translationCore.

71 lines (57 loc) 3.37 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.processTranslationAcademy = processTranslationAcademy; var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); var _pathExtra = require('path-extra'); var _pathExtra2 = _interopRequireDefault(_pathExtra); var _util = require('util'); var _resourcesHelpers = require('../resourcesHelpers'); var resourcesHelpers = _interopRequireWildcard(_resourcesHelpers); var _errors = require('../../resources/errors'); var errors = _interopRequireWildcard(_errors); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @description Processes the extracted files for translationAcademy to create a single file for each * article * @param {Object} resource - Resource object * @param {String} sourcePath - Path to the extracted files that came from the zip file in the catalog * @param {String} outputPath - Path to place the processed files WITHOUT version in the path * @return {Boolean} true if success */ // heleprs function processTranslationAcademy(resource, sourcePath, outputPath) { if (!resource || !(0, _util.isObject)(resource) || !resource.languageId || !resource.resourceId) throw Error(resourcesHelpers.formatError(resource, errors.RESOURCE_NOT_GIVEN)); if (!sourcePath) throw Error(resourcesHelpers.formatError(resource, errors.SOURCE_PATH_NOT_GIVEN)); if (!_fsExtra2.default.pathExistsSync(sourcePath)) throw Error(resourcesHelpers.formatError(resource, errors.SOURCE_PATH_NOT_EXIST)); if (!outputPath) throw Error(resourcesHelpers.formatError(resource, errors.OUTPUT_PATH_NOT_GIVEN)); if (_fsExtra2.default.pathExistsSync(outputPath)) _fsExtra2.default.removeSync(outputPath); const manifest = resourcesHelpers.getResourceManifest(sourcePath); manifest.projects.forEach(project => { const folderPath = _pathExtra2.default.join(sourcePath, project.path); const isDirectory = item => _fsExtra2.default.lstatSync(_pathExtra2.default.join(folderPath, item)).isDirectory(); const articleDirs = _fsExtra2.default.readdirSync(folderPath).filter(isDirectory); articleDirs.forEach(articleDir => { const titlePath = _pathExtra2.default.join(folderPath, articleDir, 'title.md'); if (!_fsExtra2.default.existsSync(titlePath)) { console.warn(`processTranslationAcademy() - title missing: ${titlePath}`); return; } let content = '# ' + _fsExtra2.default.readFileSync(titlePath, 'utf8') + ' #\n'; const articlePath = _pathExtra2.default.join(folderPath, articleDir, '01.md'); if (!_fsExtra2.default.existsSync(articlePath)) { console.warn(`processTranslationAcademy() - file missing: ${articlePath}`); return; } content += _fsExtra2.default.readFileSync(articlePath, 'utf8'); const destinationPath = _pathExtra2.default.join(outputPath, project.path, articleDir + '.md'); _fsExtra2.default.outputFileSync(destinationPath, content); }); }); return true; } // constants /* eslint-disable curly */