Initial commit
This commit is contained in:
102
mcp-server/tools/helpers/fileBuilder.js
Normal file
102
mcp-server/tools/helpers/fileBuilder.js
Normal file
@@ -0,0 +1,102 @@
|
||||
import axios from "axios";
|
||||
|
||||
/**
|
||||
* Helper to save files using saveFileBuilder action
|
||||
* Used by multiple tools (save.js, saveGeneralSection.js, write.js, etc.)
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {string} params.web_url - URL base del sitio (ej: http://localhost:PORT)
|
||||
* @param {string} params.token - Session token
|
||||
* @param {string} params.tokenHash - Token hash
|
||||
* @param {string} params.path - Folder path (e.g., '/modulos/mymodule/')
|
||||
* @param {string} params.fileName - File name (e.g., 'script.js', 'style.css')
|
||||
* @param {string} params.content - File content
|
||||
* @returns {Promise<Object>} Response from the API
|
||||
*/
|
||||
export async function saveFileBuilder({
|
||||
web_url,
|
||||
token,
|
||||
tokenHash,
|
||||
path,
|
||||
fileName,
|
||||
content,
|
||||
rawDataSended = true
|
||||
}) {
|
||||
if (!content) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const viewerUrl = web_url + '/cms/lib/viewer_functions.php';
|
||||
|
||||
const payload = {
|
||||
action_ws: 'saveFileBuilder',
|
||||
token: token,
|
||||
tokenHash: tokenHash,
|
||||
fileName: fileName,
|
||||
content: content,
|
||||
rawDataSended: rawDataSended,
|
||||
rootFolder: false,
|
||||
path: path
|
||||
};
|
||||
|
||||
console.error(`[saveFileBuilder] URL: ${viewerUrl}`);
|
||||
console.error(`[saveFileBuilder] Path: ${path}`);
|
||||
console.error(`[saveFileBuilder] Content length: ${content.length} chars`);
|
||||
|
||||
try {
|
||||
const response = await axios.post(viewerUrl, payload, {
|
||||
headers: { "Content-Type": "application/json" }
|
||||
});
|
||||
|
||||
console.error(`[saveFileBuilder] Response for ${fileName}:`, JSON.stringify(response.data, null, 2));
|
||||
|
||||
return {
|
||||
success: response.data.success || false,
|
||||
message: response.data.message || (response.data.success ? 'OK' : 'Error'),
|
||||
data: response.data
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`[saveFileBuilder] Error saving ${fileName}:`, error.message);
|
||||
return {
|
||||
success: false,
|
||||
message: `Error saving ${fileName}: ${error.message}`,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to save multiple files at once
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {string} params.web_url - URL base del sitio (ej: http://localhost:PORT)
|
||||
* @param {string} params.token - Session token
|
||||
* @param {string} params.tokenHash - Token hash
|
||||
* @param {string} params.path - Folder path (e.g., '/modulos/mymodule/')
|
||||
* @param {Object} params.files - Object with fileName: content pairs
|
||||
* @returns {Promise<Object>} Results for each file
|
||||
*/
|
||||
export async function saveMultipleFiles({
|
||||
web_url,
|
||||
token,
|
||||
tokenHash,
|
||||
path,
|
||||
files
|
||||
}) {
|
||||
const results = {};
|
||||
|
||||
for (const [fileName, content] of Object.entries(files)) {
|
||||
if (content) {
|
||||
results[fileName] = await saveFileBuilder({
|
||||
web_url,
|
||||
token,
|
||||
tokenHash,
|
||||
path,
|
||||
fileName,
|
||||
content
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
Reference in New Issue
Block a user