From 835bdc0619d9959762dff3e97007567b2a0b9a0d Mon Sep 17 00:00:00 2001 From: utkarsh Date: Wed, 7 Aug 2024 12:07:43 +0530 Subject: [PATCH] updated --- actions/uploadfolder.js | 89 +++++++++-------------------------------- services/yamlService.js | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 71 deletions(-) create mode 100644 services/yamlService.js diff --git a/actions/uploadfolder.js b/actions/uploadfolder.js index f00b882..d8d8043 100644 --- a/actions/uploadfolder.js +++ b/actions/uploadfolder.js @@ -1,9 +1,4 @@ const chalk = require('chalk'); -// const { post } = require('axios'); -// const { createWriteStream, createReadStream } = require('fs'); -// const archiver = require('archiver'); -// const fs = require('fs') -// const axios = require('axios') const fs = require('fs'); const archiver = require('archiver'); const {createReadStream, createWriteStream} = require('fs'); @@ -12,19 +7,19 @@ const FormData = require('form-data'); const AdmZip = require('adm-zip'); const yaml = require('js-yaml'); const StreamZip = require('node-stream-zip'); -const path = require('path') +const path = require('path'); +const { checkUserAuth } = require('./userData'); +const { validateYAML, checkYamlDetails } = require('./yamlService'); + + // Function to check for node_modules and extract .yaml file async function processZipFile(zipFilePath) { - console.log("zipFilePath---",zipFilePath) - try { // Create a new StreamZip instance const zip = new StreamZip.async ({file: zipFilePath, storeEntries: true}); // List entries in the zip file const entries = await zip.entries(); - console.log('Zip entries:', Object.keys(entries)); - // Check for node_modules directory const containsNodeModules = Object.keys(entries).some(fileName => fileName.includes('node_modules')); @@ -62,71 +57,29 @@ async function processZipFile(zipFilePath) { } } -const validateYAML = (parsedYAML) => { - const requiredFields = [ - 'title', - 'unique_id', - 'initial_message', - 'description', - 'tags', - 'longDescription', - 'avatarSrc', - 'metadata', - 'actions' - ]; - const metadataFields = ['agent_routing']; - - // Check for missing required fields - const missingFields = requiredFields.filter(field => !parsedYAML[field]); - if (missingFields.length > 0) { - return `Missing required fields: ${ - missingFields.join(', ') - }`; - } - - // Check for missing metadata fields - if (!parsedYAML.metadata) { - return 'Missing metadata field'; - } - const missingMetadataFields = metadataFields.filter(field => !parsedYAML.metadata[field]); - if (missingMetadataFields.length > 0) { - return `Missing required metadata fields: ${ - missingMetadataFields.join(', ') - }`; - } - // Check for missing agent_routing fields - if (!parsedYAML.metadata.agent_routing) { - return 'Missing agent_routing field in metadata'; - } - const agentRoutingFields = ['supportedlanguages', 'supportedframeworks']; - const missingAgentRoutingFields = agentRoutingFields.filter(field => !parsedYAML.metadata.agent_routing[field]); - if (missingAgentRoutingFields.length > 0) { - return `Missing required agent_routing fields: ${ - missingAgentRoutingFields.join(', ') - }`; - } - return null; // No validation errors -}; const uploadFolder = async (targetPath) => { + // Check if the user is logged in + if (!checkUserAuth()) { + console.log('Error: Please login using codebolt-cli login'); + return; + } - + console.log(chalk.blue('Processing the Code....')); const folderPath = targetPath || '.'; - - // const YamlValidation = await processZipFile(folderPath) - - // console.log(YamlValidation); - - // return; - // Get all the entries (files/folders) in the ZIP file - - // Resolve folder path and create zip file path + // Resolve folder path and create zip file path const folder = path.resolve(folderPath); const zipFilePath = `${folder}.zip`; + const YamlValidation = await checkYamlDetails(folderPath); + + if (!YamlValidation) { + return; + } + // Create a file stream to write the zip file const output = createWriteStream(zipFilePath); const archive = archiver('zip', { @@ -153,14 +106,9 @@ const uploadFolder = async (targetPath) => { // Finalize the archive (i.e., finalize the zip file) archive.finalize(); - console.log(`Creating zip: ${zipFilePath}`); - // Listen for the close event to handle the upload output.on('close', async () => { try { - // Process the ZIP file and validate the YAML content - const YamlValidation = await processZipFile(zipFilePath); - if (YamlValidation.title) { // Prepare the form data for file upload const formData = new FormData(); @@ -204,7 +152,6 @@ const uploadFolder = async (targetPath) => { archive.on('error', (err) => { console.error('Archive error:', err); }); - }; diff --git a/services/yamlService.js b/services/yamlService.js new file mode 100644 index 0000000..02a7ee4 --- /dev/null +++ b/services/yamlService.js @@ -0,0 +1,80 @@ + +async function checkYamlDetails(folderPath) { + const yamlPathName = path.join(folderPath, 'codebolt.yaml'); + + // Check if the YAML file exists + try { + await fs.promises.access(yamlPathName, fs.constants.F_OK); + } catch (error) { + console.error('YAML file does not exist:', error); + return false; + } + + try { + const parsedYAML = yaml.load(ymlFileContent.toString('utf8')); + const validationError = validateYAML(parsedYAML); + if (validationError) { + console.error('Yaml Format Not Correct:', validationError); + return false; + } else { + return parsedYAML; + } + } catch (error) { + console.error('An error occurred while processing the YAML file:', error); + return false; + } + +} + +function validateYAML (parsedYAML) { + const requiredFields = [ + 'title', + 'unique_id', + 'initial_message', + 'description', + 'tags', + 'longDescription', + 'avatarSrc', + 'metadata', + 'actions' + ]; + const metadataFields = ['agent_routing']; + + // Check for missing required fields + const missingFields = requiredFields.filter(field => !parsedYAML[field]); + if (missingFields.length > 0) { + return `Missing required fields: ${ + missingFields.join(', ') + }`; + } + + // Check for missing metadata fields + if (!parsedYAML.metadata) { + return 'Missing metadata field'; + } + const missingMetadataFields = metadataFields.filter(field => !parsedYAML.metadata[field]); + if (missingMetadataFields.length > 0) { + return `Missing required metadata fields: ${ + missingMetadataFields.join(', ') + }`; + } + + // Check for missing agent_routing fields + if (!parsedYAML.metadata.agent_routing) { + return 'Missing agent_routing field in metadata'; + } + const agentRoutingFields = ['supportedlanguages', 'supportedframeworks']; + const missingAgentRoutingFields = agentRoutingFields.filter(field => !parsedYAML.metadata.agent_routing[field]); + if (missingAgentRoutingFields.length > 0) { + return `Missing required agent_routing fields: ${ + missingAgentRoutingFields.join(', ') + }`; + } + + return null; // No validation errors +}; + +module.exports = { + checkYamlDetails, + validateYAML +} \ No newline at end of file