Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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));
}
});
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}
37 changes: 37 additions & 0 deletions messages.js
Original file line number Diff line number Diff line change
@@ -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: ${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})`,
},
},
};

6 changes: 4 additions & 2 deletions plugins/item-logger/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const messages = require('../../messages');

module.exports = function Plugins () {
// eslint-disable-next-line no-unused-vars
const options = Plugins.options
const log = options.log

// 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()
}
}
Loading