diff --git a/clean.js b/clean.js index 831e08b..2ca0639 100644 --- a/clean.js +++ b/clean.js @@ -4,6 +4,7 @@ const path = require("path"); const rimraf = require("rimraf"); const config = require("./config/all"); const { merge } = require("lodash"); +const messages = require("./messages"); // Set application's environment const env = process.env.NODE_ENV || config.environment || "development"; @@ -28,8 +29,8 @@ pathsToDelete.forEach((fileOrDir) => { const fullPath = path.join(__dirname, fileOrDir); if (fs.existsSync(fullPath)) { rimraf.sync(fullPath); - console.log("Deleted:", fileOrDir); + console.log(messages.clean.deleted(fileOrDir)); } else { - console.log("Not found (skipped):", fileOrDir); + console.log(messages.clean.notFound(fileOrDir)); } }); diff --git a/index.js b/index.js index bb45d58..7bfb00d 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const path = require("path"); const _ = require("lodash"); const datasyncManager = require("@contentstack/datasync-manager"); const config = require("./config/all"); +const messages = require("./messages"); /** * Listener module. Currently Contentstack offers * - @contentstack/webhook-listener @@ -37,9 +38,9 @@ if (appConfig.checkpoint?.enabled) { const checkpointPath = path.join(__dirname, appConfig.checkpoint.filePath || ".checkpoint"); const checkPoint = readHiddenFile(checkpointPath); if (checkPoint) { - console.log("Found sync token in checkpoint file:", checkPoint); + console.log(messages.sync.tokenFound(checkPoint)); appConfig.contentstack.sync_token = checkPoint.token; - console.log("Using sync token:", appConfig.contentstack.sync_token); + console.log(messages.sync.usingToken(appConfig.contentstack.sync_token)); } } @@ -51,21 +52,21 @@ datasyncManager.setListener(listener); datasyncManager .start() .then(() => { - console.log("Boilerplate: DataSync started successfully!"); + console.log(messages.sync.startSuccess()); }) .catch((error) => { - console.error(error); + console.error(messages.sync.error(error)); }); function readHiddenFile(filePath) { try { if (!fs.existsSync(filePath)) { - console.error("File does not exist:", filePath); + console.error(messages.file.notExist(filePath)); return; } const data = fs.readFileSync(filePath, "utf8"); return JSON.parse(data); } catch (err) { - console.error("Error reading file:", err?.message); + console.error(messages.file.readError(err?.message)); } } diff --git a/messages.js b/messages.js new file mode 100644 index 0000000..3f6db52 --- /dev/null +++ b/messages.js @@ -0,0 +1,37 @@ +/** + * Centralized message store for datasync-boilerplate + * This file contains all user-facing messages including info, error, and warning messages. + */ + +module.exports = { + // Clean script messages + clean: { + deleted: (item) => `Deleted: ${item}`, + notFound: (item) => `Item not found! Skipping: ${item}`, + }, + + // Index/Main application messages + sync: { + tokenFound: (checkpoint) => `Sync token found in the checkpoint file: ${JSON.stringify(checkpoint)}`, + usingToken: (token) => `Using sync token: ${token}`, + startSuccess: () => `Boilerplate: Data sync started successfully.`, + error: (error) => `An error occurred: ${error}`, + }, + + // File operation messages + file: { + notExist: (filePath) => `File does not exist: ${filePath}`, + readError: (errorMessage) => `An error occurred while reading the file: ${errorMessage}`, + }, + + // Plugin messages + plugin: { + itemLogger: { + beforeSync: (action, uid, contentType) => + `Item logger - Before sync: ${action.toUpperCase()} operation on '${uid}' (content type: ${contentType})`, + afterSync: (action, uid, contentType) => + `Item logger - After sync: ${action.toUpperCase()} operation on '${uid}' (content type: ${contentType})`, + }, + }, +}; + diff --git a/plugins/item-logger/index.js b/plugins/item-logger/index.js index 135598b..60fe84a 100644 --- a/plugins/item-logger/index.js +++ b/plugins/item-logger/index.js @@ -1,3 +1,5 @@ +const messages = require('../../messages'); + module.exports = function Plugins () { // eslint-disable-next-line no-unused-vars const options = Plugins.options @@ -5,13 +7,13 @@ module.exports = function Plugins () { // eslint-disable-next-line no-unused-vars Plugins.beforeSync = function (action, data, schema) { - log(`item-logger before-sync logging: ${action.toUpperCase()} on ${data.uid} of ${data._content_type_uid}`) + log(messages.plugin.itemLogger.beforeSync(action, data.uid, data._content_type_uid)) return Promise.resolve() } // eslint-disable-next-line no-unused-vars Plugins.afterSync = function (action, data, schema) { - log(`item-logger after-sync logging: ${action.toUpperCase()} on ${data.uid} of ${data._content_type_uid}`) + log(messages.plugin.itemLogger.afterSync(action, data.uid, data._content_type_uid)) return Promise.resolve() } }