From dc9035e75b072008be46529d27158236ace0383b Mon Sep 17 00:00:00 2001 From: umeshmore45 Date: Tue, 8 Oct 2024 15:46:51 +0530 Subject: [PATCH 1/3] added updated code --- api/src/controllers/migration.controller.ts | 29 +++++- api/src/routes/migration.routes.ts | 4 +- api/src/services/migration.service.ts | 107 ++++++-------------- api/src/services/runCli.service.ts | 59 +++++++++++ api/src/utils/index.ts | 16 +++ upload-api/src/config/index.ts | 4 +- 6 files changed, 137 insertions(+), 82 deletions(-) create mode 100644 api/src/services/runCli.service.ts diff --git a/api/src/controllers/migration.controller.ts b/api/src/controllers/migration.controller.ts index 3f8ac5c8..ce5d6d18 100644 --- a/api/src/controllers/migration.controller.ts +++ b/api/src/controllers/migration.controller.ts @@ -14,17 +14,37 @@ const createTestStack = async (req: Request, res: Response): Promise => { }; /** - * Deletes the test stack. + * Start Test Migartion. * * @param {Request} req - The request object. * @param {Response} res - The response object. * @returns {Promise} - A Promise that resolves when the stack is deleted. */ -const fieldMapping = async (req: Request, res: Response): Promise => { - const resp = migrationService.fieldMapping(req); +const startTestMigration = async (req: Request, res: Response): Promise => { + const resp = migrationService.startTestMigration(req); res.status(200).json(resp); }; + +/* + * Start Final Migartion. + * + * @param {Request} req - The request object. + * @param {Response} res - The response object. + * @returns {Promise} - A Promise that resolves when the stack is deleted. + */ +const startMigration = async (req: Request, res: Response): Promise => { + const resp = migrationService.startMigration(req); + res.status(200).json(resp); +}; + +/** + * Deletes the test stack. + * + * @param {Request} req - The request object. + * @param {Response} res - The response object. + * @returns {Promise} - A Promise that resolves when the stack is deleted. + */ const deleteTestStack = async (req: Request, res: Response): Promise => { const resp = await migrationService.deleteTestStack(req); res.status(200).json(resp); @@ -33,5 +53,6 @@ const deleteTestStack = async (req: Request, res: Response): Promise => { export const migrationController = { createTestStack, deleteTestStack, - fieldMapping, + startTestMigration, + startMigration }; diff --git a/api/src/routes/migration.routes.ts b/api/src/routes/migration.routes.ts index a50ee9ec..7e4c29d9 100644 --- a/api/src/routes/migration.routes.ts +++ b/api/src/routes/migration.routes.ts @@ -18,7 +18,7 @@ const router = express.Router({ mergeParams: true }); */ router.post( "/test-stack/:orgId/:projectId", - asyncRouter(migrationController.fieldMapping) + asyncRouter(migrationController.startTestMigration) ); /** @@ -56,7 +56,7 @@ router.post( */ router.post( "/start/:orgId/:projectId", - asyncRouter(migrationController.fieldMapping) + asyncRouter(migrationController.startMigration) ); diff --git a/api/src/services/migration.service.ts b/api/src/services/migration.service.ts index 0e600457..bb7a35c3 100644 --- a/api/src/services/migration.service.ts +++ b/api/src/services/migration.service.ts @@ -1,26 +1,17 @@ import { Request } from "express"; -import fs from 'fs'; -// import cliUtilities from '@contentstack/cli-utilities'; +import ProjectModelLowdb from "../models/project-lowdb.js"; import { config } from "../config/index.js"; import { safePromise, getLogMessage } from "../utils/index.js"; import https from "../utils/https.utils.js"; -import { LoginServiceType } from "../models/types.js"; +import { LoginServiceType } from "../models/types.js" import getAuthtoken from "../utils/auth.utils.js"; import logger from "../utils/logger.js"; -import { HTTP_TEXTS, HTTP_CODES, CS_REGIONS, LOCALE_MAPPER, STEPPER_STEPS } from "../constants/index.js"; +import { HTTP_TEXTS, HTTP_CODES, LOCALE_MAPPER, STEPPER_STEPS } from "../constants/index.js"; import { ExceptionFunction } from "../utils/custom-errors.utils.js"; import { fieldAttacher } from "../utils/field-attacher.utils.js"; -import ProjectModelLowdb from "../models/project-lowdb.js"; -import shell from 'shelljs' -import path from "path"; -import AuthenticationModel from "../models/authentication.js"; import { siteCoreService } from "./sitecore.service.js"; -import { copyDirectory } from '../utils/index.js' -import { v4 } from "uuid"; -import { setLogFilePath } from "../server.js"; -import { mkdirp } from 'mkdirp'; import { testFolderCreator } from "../utils/test-folder-creator.utils.js"; - +import { utilsCli } from './runCli.service.js'; @@ -205,65 +196,12 @@ const deleteTestStack = async (req: Request): Promise => { }; -function createDirectoryAndFile(filePath: string) { - // Get the directory from the file path - const dirPath = path.dirname(filePath); - // Create the directory if it doesn't exist - mkdirp.sync(dirPath); - // Check if the file exists; if not, create it - if (!fs.existsSync(filePath)) { - fs.writeFileSync(filePath, '', { mode: 0o666 }); // Create file with read/write for everyone - console.info(`File created at: ${filePath}`); - } else { - console.info(`File already exists at: ${filePath}`); - } -} - - -const runCli = async (rg: string, user_id: string, stack_uid: any, projectId: string) => { - try { - const regionPresent = CS_REGIONS?.find((item: string) => item === rg) ?? 'NA'; - await AuthenticationModel.read(); - const userData = AuthenticationModel.chain - .get("users") - .find({ region: regionPresent, user_id }) - .value(); - if (userData?.authtoken && stack_uid) { - const sourcePath = path.join(process.cwd(), 'sitecoreMigrationData', stack_uid); - const backupPath = path.join(process.cwd(), 'migration-data', `${stack_uid}_${v4().slice(0, 4)}`); - await copyDirectory(sourcePath, backupPath); - const loggerPath = path.join(backupPath, 'logs', 'import', 'success.log'); - createDirectoryAndFile(loggerPath); - await setLogFilePath(loggerPath); - shell.cd(path.join(process.cwd(), '..', 'cli', 'packages', 'contentstack')); - shell.exec('pwd'); - shell.exec(`node bin/run config:set:region ${regionPresent}`); - shell.exec(`node bin/run login -a ${userData?.authtoken} -e ${userData?.email}`); - const exportData = shell.exec(`node bin/run cm:stacks:import -k ${stack_uid} -d ${sourcePath} --backup-dir=${backupPath} --yes`, { async: true }); - exportData.on('exit', (code) => { - console.info(`Process exited with code: ${code}`); - if (code === 1) { - const projectIndex = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); - if (projectIndex > -1) { - ProjectModelLowdb?.data.projects[projectIndex].test_stacks.map((item: any) => { - if (item?.stackUid === stack_uid) { - item.isMigrated = true; - } - return item; - }) - ProjectModelLowdb.write(); - } - } - }); - } else { - console.info('user not found.') - } - } catch (er) { - console.error("🚀 ~ runCli ~ er:", er) - } -} - -const fieldMapping = async (req: Request): Promise => { +/** + * Start Test Migration. + * + * @param req - The request object containing the necessary parameters. + */ +const startTestMigration = async (req: Request): Promise => { const { orgId, projectId } = req?.params ?? {}; const { region, user_id } = req?.body?.token_payload ?? {}; const project = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value(); @@ -274,12 +212,33 @@ const fieldMapping = async (req: Request): Promise => { await siteCoreService?.createLocale(req, project?.current_test_stack_id); await siteCoreService?.createVersionFile(project?.current_test_stack_id); await testFolderCreator?.({ destinationStackId: project?.current_test_stack_id }); - await runCli(region, user_id, project?.current_test_stack_id, projectId); + await utilsCli?.runCli(region, user_id, project?.current_test_stack_id, projectId, true); + } +} + + +/** + * Start final Migration. + * + * @param req - The request object containing the necessary parameters. + */ +const startMigration = async (req: Request): Promise => { + const { orgId, projectId } = req?.params ?? {}; + const { region, user_id } = req?.body?.token_payload ?? {}; + const project = ProjectModelLowdb.chain.get("projects").find({ id: projectId }).value(); + const packagePath = project?.extract_path; + if (packagePath && project?.destination_stack_id) { + const contentTypes = await fieldAttacher({ orgId, projectId, destinationStackId: project?.destination_stack_id }); + await siteCoreService?.createEntry({ packagePath, contentTypes, destinationStackId: project?.destination_stack_id }); + await siteCoreService?.createLocale(req, project?.destination_stack_id); + await siteCoreService?.createVersionFile(project?.destination_stack_id); + await utilsCli?.runCli(region, user_id, project?.destination_stack_id, projectId); } } export const migrationService = { createTestStack, deleteTestStack, - fieldMapping + startTestMigration, + startMigration }; diff --git a/api/src/services/runCli.service.ts b/api/src/services/runCli.service.ts new file mode 100644 index 00000000..6abbdb4c --- /dev/null +++ b/api/src/services/runCli.service.ts @@ -0,0 +1,59 @@ +import path from "path"; +import shell from 'shelljs' +import { v4 } from "uuid"; +import { setLogFilePath } from "../server.js"; +import { copyDirectory, createDirectoryAndFile } from '../utils/index.js' +import { CS_REGIONS } from "../constants/index.js"; +import ProjectModelLowdb from "../models/project-lowdb.js"; +import AuthenticationModel from "../models/authentication.js"; + + + + +export const runCli = async (rg: string, user_id: string, stack_uid: any, projectId: string, isTest = false) => { + try { + const regionPresent = CS_REGIONS?.find((item: string) => item === rg) ?? 'NA'; + await AuthenticationModel.read(); + const userData = AuthenticationModel.chain + .get("users") + .find({ region: regionPresent, user_id }) + .value(); + if (userData?.authtoken && stack_uid) { + const sourcePath = path.join(process.cwd(), 'sitecoreMigrationData', stack_uid); + const backupPath = path.join(process.cwd(), 'migration-data', `${stack_uid}_${v4().slice(0, 4)}`); + await copyDirectory(sourcePath, backupPath); + const loggerPath = path.join(backupPath, 'logs', 'import', 'success.log'); + createDirectoryAndFile(loggerPath); + await setLogFilePath(loggerPath); + shell.cd(path.join(process.cwd(), '..', 'cli', 'packages', 'contentstack')); + shell.exec('pwd'); + shell.exec(`node bin/run config:set:region ${regionPresent}`); + shell.exec(`node bin/run login -a ${userData?.authtoken} -e ${userData?.email}`); + const importData = shell.exec(`node bin/run cm:stacks:import -k ${stack_uid} -d ${sourcePath} --backup-dir=${backupPath} --yes`, { async: true }); + importData.on('exit', (code) => { + console.info(`Process exited with code: ${code}`); + if (code === 1) { + const projectIndex = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); + if (projectIndex > -1 && isTest) { + ProjectModelLowdb?.data?.projects?.[projectIndex]?.test_stacks?.map((item: any) => { + if (item?.stackUid === stack_uid) { + item.isMigrated = true; + } + return item; + }) + ProjectModelLowdb.write(); + } + } + }); + } else { + console.info('user not found.') + } + } catch (er) { + console.error("🚀 ~ runCli ~ er:", er) + } +} + + +export const utilsCli = { + runCli +}; \ No newline at end of file diff --git a/api/src/utils/index.ts b/api/src/utils/index.ts index 9404b302..71940cb5 100644 --- a/api/src/utils/index.ts +++ b/api/src/utils/index.ts @@ -1,4 +1,6 @@ import fs from 'fs-extra'; +import path from "path"; +import { mkdirp } from 'mkdirp'; /** * Throws an error with a custom message and status code. * @param message - The error message. @@ -73,4 +75,18 @@ export async function copyDirectory(srcDir: string, destDir: string): Promise Date: Wed, 9 Oct 2024 14:05:33 +0530 Subject: [PATCH 2/3] added message --- api/src/services/runCli.service.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/api/src/services/runCli.service.ts b/api/src/services/runCli.service.ts index 6abbdb4c..bc676e2f 100644 --- a/api/src/services/runCli.service.ts +++ b/api/src/services/runCli.service.ts @@ -1,4 +1,5 @@ import path from "path"; +import fs from 'fs'; import shell from 'shelljs' import { v4 } from "uuid"; import { setLogFilePath } from "../server.js"; @@ -7,7 +8,19 @@ import { CS_REGIONS } from "../constants/index.js"; import ProjectModelLowdb from "../models/project-lowdb.js"; import AuthenticationModel from "../models/authentication.js"; - +const addCustomMessageInCliLogs = async (loggerPath: string, level: string = 'info', message: string) => { + try { + const logEntry = { + level, + message, + timestamp: new Date().toISOString(), + }; + const logMessage = JSON.stringify(logEntry) + '\n'; // Convert to JSON and add a newline + await fs.promises.appendFile(loggerPath, logMessage); + } catch (error) { + console.error('Error reading the file:', error); + } +} export const runCli = async (rg: string, user_id: string, stack_uid: any, projectId: string, isTest = false) => { @@ -30,7 +43,7 @@ export const runCli = async (rg: string, user_id: string, stack_uid: any, projec shell.exec(`node bin/run config:set:region ${regionPresent}`); shell.exec(`node bin/run login -a ${userData?.authtoken} -e ${userData?.email}`); const importData = shell.exec(`node bin/run cm:stacks:import -k ${stack_uid} -d ${sourcePath} --backup-dir=${backupPath} --yes`, { async: true }); - importData.on('exit', (code) => { + importData.on('exit', async (code) => { console.info(`Process exited with code: ${code}`); if (code === 1) { const projectIndex = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); @@ -43,8 +56,10 @@ export const runCli = async (rg: string, user_id: string, stack_uid: any, projec }) ProjectModelLowdb.write(); } + await addCustomMessageInCliLogs(loggerPath, 'info', 'Test Migration Process Completed'); } }); + } else { console.info('user not found.') } From e23660b82d3cabc183a6523712e0be46ae69def8 Mon Sep 17 00:00:00 2001 From: umeshmore45 Date: Wed, 9 Oct 2024 14:07:33 +0530 Subject: [PATCH 3/3] added message --- api/src/services/runCli.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/services/runCli.service.ts b/api/src/services/runCli.service.ts index bc676e2f..94c9ee6b 100644 --- a/api/src/services/runCli.service.ts +++ b/api/src/services/runCli.service.ts @@ -45,7 +45,7 @@ export const runCli = async (rg: string, user_id: string, stack_uid: any, projec const importData = shell.exec(`node bin/run cm:stacks:import -k ${stack_uid} -d ${sourcePath} --backup-dir=${backupPath} --yes`, { async: true }); importData.on('exit', async (code) => { console.info(`Process exited with code: ${code}`); - if (code === 1) { + if (code === 1 || code === 0) { const projectIndex = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); if (projectIndex > -1 && isTest) { ProjectModelLowdb?.data?.projects?.[projectIndex]?.test_stacks?.map((item: any) => {