UNPKG

@frontitude/cli

Version:

The Frontitude CLI enables product teams to integrate their code with Frontitude (frontitude.com), thus creating a single source of truth for their product copy, from design to development.

1 lines 4.05 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.pullProjectsTextsIntoCodebase=exports.getXliffVersion=exports.setXliffVersion=exports.getIsLibraryConnected=exports.setIsLibraryConnected=exports.getConnectedProjects=exports.setConnectedProjects=exports.isOutputFileTypeSupported=void 0;const tslib_1=require("tslib"),lodash_merge_1=tslib_1.__importDefault(require("lodash.merge")),config=tslib_1.__importStar(require("../../common/config")),ErrorNames=tslib_1.__importStar(require("../../common/constants/ErrorNames")),projects_1=require("../projects"),workspace_1=require("../workspace"),files_1=require("../../utils/files"),stringsFileHandler=tslib_1.__importStar(require("./stringsFileHandler")),constants_1=require("../constants"),SUPPORTED_FILE_TYPES=stringsFileHandler.getSupportedFileTypes(),isOutputFileTypeSupported=e=>{if(!e)return!1;const{fileType:t,fileNameWithoutExtension:s}=(0,files_1.getFileDetails)(e);return s&&-1!==SUPPORTED_FILE_TYPES.indexOf(t)};exports.isOutputFileTypeSupported=isOutputFileTypeSupported;const setConnectedProjects=async e=>{config.local.set("projects",e)};exports.setConnectedProjects=setConnectedProjects;const getConnectedProjects=async()=>config.local.get("projects")||[];exports.getConnectedProjects=getConnectedProjects;const setIsLibraryConnected=async e=>{config.local.set("library",e)};exports.setIsLibraryConnected=setIsLibraryConnected;const getIsLibraryConnected=async()=>config.local.get("library")||!1;exports.getIsLibraryConnected=getIsLibraryConnected;const setXliffVersion=async e=>{config.local.set("xliffVersion",e)};exports.setXliffVersion=setXliffVersion;const getXliffVersion=async()=>config.local.get("xliffVersion")||constants_1.XliffVersions.xliff_1_2;exports.getXliffVersion=getXliffVersion;const convertTagNamesToIdsAndCheckAllExist=async e=>{const t=await(0,workspace_1.mapWorkspaceTagNamesToIds)(e),s=Object.keys(t),r=e.filter((e=>!s.includes(e)));if(r.length>0){const e=new Error(r.join(", "));throw e.name=ErrorNames.TAGS_NOT_EXIST_IN_WORKSPACE,e}return Object.values(t)},convertPageNamesToIdsAndCheckAllExist=async(e,t)=>{const s=await(0,projects_1.mapPageNamesToIds)(e,t),r=Object.keys(s),o=t.filter((e=>!r.includes(e)));if(o.length>0){const e=new Error(o.join(", "));throw e.name=ErrorNames.PAGES_NOT_EXIST_IN_PROJECTS,e}return Object.values(s).flat()},convertFrameNamesToIdsAndCheckAllExist=async(e,t)=>{const s=await(0,projects_1.mapFrameNamesToIds)(e,t),r=Object.keys(s),o=t.filter((e=>!r.includes(e)));if(o.length>0){const e=new Error(o.join(", "));throw e.name=ErrorNames.FRAMES_NOT_EXIST_IN_PROJECTS,e}return Object.values(s).flat()},pullProjectsTextsIntoCodebase=async(e,t=!1)=>{const s=await(0,exports.getIsLibraryConnected)(),r=await(0,exports.getConnectedProjects)();if(!s&&0===r.length){const e=new Error("missing sources in config");throw e.name=ErrorNames.NO_CONFIGURED_SOURCES,e}const o=config.local.get("filePath");if(!o&&!t){const e=new Error("missing output file in config");throw e.name=ErrorNames.NO_CONFIGURED_OUTPUT_FILE,e}const n=r.map((({id:e})=>e)),{tagNames:i,pageNames:a,frameNames:c}=e;if(i&&i.length>0){const t=await convertTagNamesToIdsAndCheckAllExist(i);e.tagIds=t}if(a&&a.length>0){const t=await convertPageNamesToIdsAndCheckAllExist(n,a);e.pageIds=t}if(c&&c.length>0){const t=await convertFrameNamesToIdsAndCheckAllExist(n,c);e.frameIds=t}const l=stringsFileHandler.extendTextsPullOptions(o,e),p=n.length>0?await(0,projects_1.pullProjectsTexts)(n,l):{},g=s?await(0,workspace_1.pullCopyLibrary)(l):{},d=(0,lodash_merge_1.default)(p,g),f=await stringsFileHandler.formatTextsForOutputFileType(o,d),u=await(0,workspace_1.getSourceLanguageLocaleId)();if(u&&f[u]&&(f.source=f[u],delete f[u]),t){return await stringsFileHandler.getDryRunResult(o,f)}const _=(0,files_1.getFileDetails)(o),m={};for(const e of Object.keys(f)){const t=(0,files_1.getTargetLanguageFilePath)(o,_,e);m[t]=await stringsFileHandler.updateFileWithData(t,f[e],e,f.source,u)}return m};exports.pullProjectsTextsIntoCodebase=pullProjectsTextsIntoCodebase;