|
| 1 | +import * as path from 'path'; |
| 2 | +import { log } from '@contentstack/cli-utilities'; |
| 3 | +import { fileExistsSync, readFile } from './file-helper'; |
| 4 | +import { askBranchSelection } from './interactive'; |
| 5 | +import { ImportConfig } from '../types'; |
| 6 | +import defaultConfig from '../config'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Selects a branch from directory structure when multiple branches are found |
| 10 | + * @param contentDir - The content directory path |
| 11 | + * @returns Promise<{ branchPath: string } | null> |
| 12 | + */ |
| 13 | +export const selectBranchFromDirectory = async (contentDir: string): Promise<{ branchPath: string } | null> => { |
| 14 | + log.debug('Selecting branch directory from directory structure'); |
| 15 | + |
| 16 | + const branchesJsonPath = path.join(contentDir, 'branches.json'); |
| 17 | + if (!fileExistsSync(branchesJsonPath)) { |
| 18 | + log.debug('No branches.json found - not a branch-enabled export'); |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + const branchesData = await readFile(branchesJsonPath); |
| 24 | + const branches = branchesData || []; |
| 25 | + |
| 26 | + if (!branches || !Array.isArray(branches) || branches.length === 0) { |
| 27 | + log.debug('No branches found in branches.json - not a branch-enabled export'); |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + if (branches.length === 1) { |
| 32 | + const singleBranch = branches[0]; |
| 33 | + const branchPath = path.join(contentDir, singleBranch.uid); |
| 34 | + |
| 35 | + if (!fileExistsSync(branchPath)) { |
| 36 | + log.warn(`Branch path does not exist: ${branchPath}, not a valid branch export`); |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + log.debug(`Single branch detected: ${singleBranch.uid} - auto-resolving to: ${branchPath}`); |
| 41 | + return { branchPath }; |
| 42 | + } else { |
| 43 | + log.debug(`Multiple branches detected: ${branches.map((b) => b.uid).join(', ')}`); |
| 44 | + |
| 45 | + const branchNames = branches.map((b) => b.uid); |
| 46 | + const selectedBranch = await askBranchSelection(branchNames); |
| 47 | + const selectedBranchPath = path.join(contentDir, selectedBranch); |
| 48 | + |
| 49 | + if (!fileExistsSync(selectedBranchPath)) { |
| 50 | + log.warn(`Selected branch path does not exist: ${selectedBranchPath}, not a valid branch export`); |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + log.debug(`User selected branch directory: ${selectedBranch} - using path: ${selectedBranchPath}`); |
| 55 | + return { branchPath: selectedBranchPath }; |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + log.error(`Error reading branches.json: ${error}`); |
| 59 | + throw new Error('Failed to read branches.json file. Please ensure the file exists and is valid JSON.'); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Resolves the import path based on directory structure and user configuration |
| 65 | + * @param importConfig - The import configuration object |
| 66 | + * @param stackAPIClient - The Contentstack API client |
| 67 | + * @returns Promise<string> - The resolved path |
| 68 | + */ |
| 69 | +export const resolveImportPath = async (importConfig: ImportConfig, stackAPIClient: any): Promise<string> => { |
| 70 | + log.debug('Resolving import path based on directory structure'); |
| 71 | + |
| 72 | + const contentDir = importConfig.contentDir || importConfig.data; |
| 73 | + log.debug(`Content directory: ${contentDir}`); |
| 74 | + |
| 75 | + if (!fileExistsSync(contentDir)) { |
| 76 | + throw new Error(`Content directory does not exist: ${contentDir}`); |
| 77 | + } |
| 78 | + |
| 79 | + if (importConfig.branchName) { |
| 80 | + log.debug(`User specified branch: ${importConfig.branchName}`); |
| 81 | + |
| 82 | + const currentDirName = path.basename(contentDir); |
| 83 | + if (currentDirName === importConfig.branchName) { |
| 84 | + log.debug(`Already in correct branch directory: ${contentDir}`); |
| 85 | + return contentDir; |
| 86 | + } |
| 87 | + |
| 88 | + const branchPath = path.join(contentDir, importConfig.branchName); |
| 89 | + if (fileExistsSync(branchPath)) { |
| 90 | + log.debug(`Navigating to specified branch directory: ${branchPath}`); |
| 91 | + return branchPath; |
| 92 | + } |
| 93 | + |
| 94 | + log.debug(`Branch directory not found: ${branchPath}, using contentDir as-is`); |
| 95 | + return contentDir; |
| 96 | + } |
| 97 | + |
| 98 | + const exportInfoPath = path.join(contentDir, 'export-info.json'); |
| 99 | + if (fileExistsSync(exportInfoPath)) { |
| 100 | + log.debug('Found export-info.json - using contentDir as-is (v2 export)'); |
| 101 | + return contentDir; |
| 102 | + } |
| 103 | + |
| 104 | + const moduleTypes = defaultConfig.modules.types; |
| 105 | + const hasModuleFolders = moduleTypes.some((moduleType) => fileExistsSync(path.join(contentDir, moduleType))); |
| 106 | + |
| 107 | + if (hasModuleFolders) { |
| 108 | + log.debug('Found module folders '); |
| 109 | + return contentDir; |
| 110 | + } |
| 111 | + |
| 112 | + const branchSelection = await selectBranchFromDirectory(contentDir); |
| 113 | + if (branchSelection) { |
| 114 | + return branchSelection.branchPath; |
| 115 | + } |
| 116 | + |
| 117 | + log.debug('No specific structure detected - using contentDir as-is'); |
| 118 | + return contentDir; |
| 119 | +}; |
| 120 | + |
| 121 | +/** |
| 122 | + * Updates the import configuration with the resolved path |
| 123 | + * @param importConfig - The import configuration object |
| 124 | + * @param resolvedPath - The resolved path |
| 125 | + */ |
| 126 | +export const updateImportConfigWithResolvedPath = (importConfig: ImportConfig, resolvedPath: string): void => { |
| 127 | + log.debug(`Updating import config with resolved path: ${resolvedPath}`); |
| 128 | + |
| 129 | + if (!fileExistsSync(resolvedPath)) { |
| 130 | + log.warn(`Resolved path does not exist: ${resolvedPath}, skipping config update`); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + importConfig.branchDir = resolvedPath; |
| 135 | + |
| 136 | + importConfig.contentDir = resolvedPath; |
| 137 | + |
| 138 | + importConfig.data = resolvedPath; |
| 139 | + |
| 140 | + log.debug( |
| 141 | + `Import config updated - contentDir: ${importConfig.contentDir}, branchDir: ${importConfig.branchDir}, data: ${importConfig.data}`, |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +/** |
| 146 | + * Executes the complete import path resolution logic |
| 147 | + * @param importConfig - The import configuration object |
| 148 | + * @param stackAPIClient - The Contentstack API client |
| 149 | + * @returns Promise<string> - The resolved path |
| 150 | + */ |
| 151 | +export const executeImportPathLogic = async (importConfig: ImportConfig, stackAPIClient: any): Promise<string> => { |
| 152 | + log.debug('Executing import path resolution logic'); |
| 153 | + |
| 154 | + const resolvedPath = await resolveImportPath(importConfig, stackAPIClient); |
| 155 | + |
| 156 | + updateImportConfigWithResolvedPath(importConfig, resolvedPath); |
| 157 | + |
| 158 | + return resolvedPath; |
| 159 | +}; |
0 commit comments