From b5cf1240cad5c3afc3aea7d14c3c64a0be024061 Mon Sep 17 00:00:00 2001 From: umeshmore45 Date: Fri, 4 Oct 2024 18:30:52 +0530 Subject: [PATCH] addded test ture boolean code --- api/src/models/project-lowdb.ts | 7 ++-- api/src/services/migration.service.ts | 49 ++++++++++++--------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/api/src/models/project-lowdb.ts b/api/src/models/project-lowdb.ts index 4de76bac0..693899bab 100644 --- a/api/src/models/project-lowdb.ts +++ b/api/src/models/project-lowdb.ts @@ -1,3 +1,4 @@ +import path from 'path'; import { JSONFile } from "lowdb/node"; import LowWithLodash from "../utils/lowdb-lodash.utils.js"; @@ -27,11 +28,11 @@ interface LegacyCMS { is_localPath: boolean; } -interface StackDetails{ +interface StackDetails { uid: string; label: string; master_locale: string; - created_at: string; + created_at: string; isNewStack: boolean; } @@ -84,7 +85,7 @@ const defaultData: ProjectDocument = { projects: [] }; * Represents the database instance for the project. */ const db = new LowWithLodash( - new JSONFile("database/project.json"), + new JSONFile(path.join(process.cwd(), "database/project.json")), defaultData ); diff --git a/api/src/services/migration.service.ts b/api/src/services/migration.service.ts index e7af202b4..133f27ac4 100644 --- a/api/src/services/migration.service.ts +++ b/api/src/services/migration.service.ts @@ -204,14 +204,6 @@ const deleteTestStack = async (req: Request): Promise => { } }; -const cliLogger = (child: any) => { - if (child.code !== 0) { - console.info(`Error: Failed to install @contentstack/cli. Exit code: ${child.code}`); - console.info(`stderr: ${child.stderr}`); - } else { - console.info(child?.stdout); - } -}; function createDirectoryAndFile(filePath: string) { // Get the directory from the file path @@ -228,7 +220,7 @@ function createDirectoryAndFile(filePath: string) { } -const runCli = async (rg: string, user_id: string, stack_uid: any) => { +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(); @@ -244,14 +236,25 @@ const runCli = async (rg: string, user_id: string, stack_uid: any) => { createDirectoryAndFile(loggerPath); await setLogFilePath(loggerPath); shell.cd(path.join(process.cwd(), '..', 'cli', 'packages', 'contentstack')); - const pwd = shell.exec('pwd'); - cliLogger(pwd); - const region = shell.exec(`node bin/run config:set:region ${regionPresent}`); - cliLogger(region); - const login = shell.exec(`node bin/run login -a ${userData?.authtoken} -e ${userData?.email}`); - cliLogger(login); + 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 }); - cliLogger(exportData); + 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.') } @@ -264,22 +267,14 @@ const fieldMapping = 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(); - if (project?.extract_path && project?.current_test_stack_id) { - const packagePath = project?.extract_path; + const packagePath = project?.extract_path; + if (packagePath && project?.current_test_stack_id) { const contentTypes = await fieldAttacher({ orgId, projectId, destinationStackId: project?.current_test_stack_id }); await siteCoreService?.createEntry({ packagePath, contentTypes, destinationStackId: project?.current_test_stack_id }); 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); - // const projectIndex = ProjectModelLowdb.chain.get("projects").findIndex({ id: projectId }).value(); - // if (projectIndex > -1) { - // ProjectModelLowdb.update((data: any) => { - // const index = data.projects[projectIndex].test_stacks.findIndex((item: any) => item?.stackUid === project?.current_test_stack_id); - // console.info("🚀 ~ ProjectModelLowdb.update ~ index:", index) - // data.projects[projectIndex].current_test_stack_id = ''; - // }); - // } + await runCli(region, user_id, project?.current_test_stack_id, projectId); } }