|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { cliux, messageHandler } = require('@contentstack/cli-utilities'); |
| 4 | +const isEmpty = (value) => value === null || value === undefined || |
| 5 | + (typeof value === 'object' && Object.keys(value).length === 0) || |
| 6 | + (typeof value === 'string' && value.trim().length === 0);; |
| 7 | +const config = { |
| 8 | + plan: { |
| 9 | + dropdown: { optionLimit: 100 } |
| 10 | + }, |
| 11 | + cmsType: null, |
| 12 | + isLocalPath: true, |
| 13 | + awsData: { |
| 14 | + awsRegion: 'us-east-2', |
| 15 | + awsAccessKeyId: '', |
| 16 | + awsSecretAccessKey: '', |
| 17 | + awsSessionToken: '', |
| 18 | + bucketName: 'migartion-test', |
| 19 | + buketKey: 'project/package 45.zip' |
| 20 | + }, |
| 21 | + localPath: null |
| 22 | +}; |
| 23 | + |
| 24 | +const configFilePath = path.resolve(path?.join?.('upload-api', 'src', 'config', 'index.ts')); |
| 25 | + |
| 26 | +const ensureDirectoryExists = (filePath) => { |
| 27 | + const dir = path.dirname(filePath); |
| 28 | + if (!fs.existsSync(dir)) { |
| 29 | + fs.mkdirSync(dir, { recursive: true }); |
| 30 | + console.log('📂 Created missing directory:', dir); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +const inquireRequireFieldValidation = (input) => { |
| 35 | + if (isEmpty(input)) { |
| 36 | + return messageHandler.parse('Please enter the path'); |
| 37 | + } |
| 38 | + if (!fs.existsSync(input)) { |
| 39 | + return messageHandler.parse('The specified path does not exist. Please enter a valid path.'); |
| 40 | + } |
| 41 | + return true; |
| 42 | +}; |
| 43 | + |
| 44 | +const typeSwitcher = async (type) => { |
| 45 | + switch (type) { |
| 46 | + case 'Aws S3': { |
| 47 | + const awsData = { |
| 48 | + awsRegion: await cliux.inquire({ |
| 49 | + type: 'input', |
| 50 | + message: 'Enter AWS Region', |
| 51 | + name: 'awsRegion', |
| 52 | + validate: inquireRequireFieldValidation |
| 53 | + }), |
| 54 | + awsAccessKeyId: await cliux.inquire({ |
| 55 | + type: 'input', |
| 56 | + message: 'Enter AWS Access Key Id', |
| 57 | + name: 'awsAccessKeyId', |
| 58 | + validate: inquireRequireFieldValidation |
| 59 | + }), |
| 60 | + awsSecretAccessKey: await cliux.inquire({ |
| 61 | + type: 'input', |
| 62 | + message: 'Enter AWS Secret Access Key', |
| 63 | + name: 'awsSecretAccessKey', |
| 64 | + validate: inquireRequireFieldValidation |
| 65 | + }), |
| 66 | + }; |
| 67 | + const isSessionToken = await cliux.inquire({ |
| 68 | + choices: ['yes', 'no'], |
| 69 | + type: 'list', |
| 70 | + name: 'isSessionToken', |
| 71 | + message: 'Do you have a Session Token?' |
| 72 | + }); |
| 73 | + if (isSessionToken === 'yes') { |
| 74 | + awsData.awsSessionToken = await cliux.inquire({ |
| 75 | + type: 'input', |
| 76 | + message: 'Enter AWS Session Token', |
| 77 | + name: 'awsSessionToken', |
| 78 | + validate: inquireRequireFieldValidation |
| 79 | + }); |
| 80 | + } |
| 81 | + return awsData; |
| 82 | + } |
| 83 | + case 'Locale Path': { |
| 84 | + return await cliux.inquire({ |
| 85 | + type: 'input', |
| 86 | + message: 'Enter file path', |
| 87 | + name: 'filePath', |
| 88 | + validate: inquireRequireFieldValidation |
| 89 | + }); |
| 90 | + } |
| 91 | + default: |
| 92 | + console.log('⚠️ Invalid type provided'); |
| 93 | + return; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +const XMLMigration = async () => { |
| 98 | + const typeOfcms = await cliux.inquire({ |
| 99 | + choices: ['sitecore', 'contentful'], |
| 100 | + type: 'list', |
| 101 | + name: 'value', |
| 102 | + message: 'Choose the option to proceed with your legacy CMS:' |
| 103 | + }); |
| 104 | + |
| 105 | + const data = await typeSwitcher('Locale Path'); |
| 106 | + if (typeof typeOfcms === 'string') { |
| 107 | + config.cmsType = typeOfcms; |
| 108 | + } else { |
| 109 | + console.log('⚠️ Error: Expected a string for typeOfcms but got an object.'); |
| 110 | + } |
| 111 | + if (typeof data === 'string') { |
| 112 | + config.localPath = data; |
| 113 | + } else { |
| 114 | + console.log('⚠️ Error: Expected a string for localPath but got an object.'); |
| 115 | + } |
| 116 | + ensureDirectoryExists(configFilePath); |
| 117 | + fs.writeFileSync(configFilePath, `export default ${JSON.stringify(config, null, 2)};`, 'utf8'); |
| 118 | +}; |
| 119 | + |
| 120 | +XMLMigration(); |
0 commit comments