@agility/cli
Version:
Agility CLI for working with your content. (Public Beta)
245 lines • 9.66 kB
JavaScript
;
/**
* Timestamp tracking system for incremental pull operations
*
* Stores last successful pull timestamps per entity type to enable
* incremental downloading of only changed entities.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadLastPullTimestamps = loadLastPullTimestamps;
exports.saveLastPullTimestamps = saveLastPullTimestamps;
exports.updateEntityTypeTimestamp = updateEntityTypeTimestamp;
exports.getLastPullTimestamp = getLastPullTimestamp;
exports.isEntityModifiedSinceLastPull = isEntityModifiedSinceLastPull;
exports.markPullStart = markPullStart;
exports.markPushStart = markPushStart;
exports.clearTimestamps = clearTimestamps;
exports.getIncrementalPullDecision = getIncrementalPullDecision;
var fs = __importStar(require("fs"));
var path = __importStar(require("path"));
/**
* Get the path to the timestamp tracking file for an instance
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @returns Path to the .last-pull-timestamps.json file
*/
function getTimestampFilePath(guid, rootPath) {
return path.join(process.cwd(), rootPath, guid, '.last-pull-timestamps.json');
}
/**
* Load last pull timestamps for an instance
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @returns LastPullTimestamps object or empty object if file doesn't exist
*/
function loadLastPullTimestamps(guid, rootPath) {
try {
var timestampFile = getTimestampFilePath(guid, rootPath);
if (!fs.existsSync(timestampFile)) {
// No timestamp file exists, return empty timestamps (will trigger full pull)
return {};
}
var content = fs.readFileSync(timestampFile, 'utf-8');
var timestamps = JSON.parse(content);
// Validate that all timestamps are valid ISO 8601 dates
var validatedTimestamps = {};
for (var _i = 0, _a = Object.entries(timestamps); _i < _a.length; _i++) {
var _b = _a[_i], entityType = _b[0], timestamp = _b[1];
if (timestamp && typeof timestamp === 'string') {
var parsed = new Date(timestamp);
if (!isNaN(parsed.getTime())) {
validatedTimestamps[entityType] = timestamp;
}
else {
console.warn("Invalid timestamp for ".concat(entityType, ": ").concat(timestamp));
}
}
}
return validatedTimestamps;
}
catch (error) {
console.warn("Error loading last pull timestamps for ".concat(guid, ":"), error);
return {};
}
}
/**
* Save last pull timestamps for an instance
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @param timestamps Timestamps to save
*/
function saveLastPullTimestamps(guid, rootPath, timestamps) {
try {
var timestampFile = getTimestampFilePath(guid, rootPath);
var instanceDir = path.dirname(timestampFile);
// Ensure instance directory exists
if (!fs.existsSync(instanceDir)) {
fs.mkdirSync(instanceDir, { recursive: true });
}
// Sort keys for consistent file format
var sortedTimestamps = {};
var entityTypes = ['models', 'containers', 'content', 'assets', 'pages', 'galleries', 'templates'];
for (var _i = 0, entityTypes_1 = entityTypes; _i < entityTypes_1.length; _i++) {
var entityType = entityTypes_1[_i];
var timestamp = timestamps[entityType];
if (timestamp) {
sortedTimestamps[entityType] = timestamp;
}
}
var content = JSON.stringify(sortedTimestamps, null, 2);
fs.writeFileSync(timestampFile, content, 'utf-8');
console.log("Saved last pull timestamps for ".concat(guid));
}
catch (error) {
console.error("Error saving last pull timestamps for ".concat(guid, ":"), error);
}
}
/**
* Update timestamp for a specific entity type
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @param entityType Entity type to update
* @param timestamp ISO 8601 timestamp
*/
function updateEntityTypeTimestamp(guid, rootPath, entityType, timestamp) {
try {
var currentTimestamps = loadLastPullTimestamps(guid, rootPath);
currentTimestamps[entityType] = timestamp;
saveLastPullTimestamps(guid, rootPath, currentTimestamps);
}
catch (error) {
console.error("Error updating timestamp for ".concat(entityType, ":"), error);
}
}
/**
* Get the last pull timestamp for a specific entity type
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @param entityType Entity type to check
* @returns ISO 8601 timestamp or null if no previous pull
*/
function getLastPullTimestamp(guid, rootPath, entityType) {
var timestamps = loadLastPullTimestamps(guid, rootPath);
return timestamps[entityType] || null;
}
/**
* Check if an entity has been modified since the last pull
* @param entityModifiedDate Entity's modified date (ISO 8601)
* @param lastPullTimestamp Last pull timestamp (ISO 8601) or null
* @returns true if entity was modified since last pull, false otherwise
*/
function isEntityModifiedSinceLastPull(entityModifiedDate, lastPullTimestamp) {
// If no entity modified date, we can't determine if it was modified
if (!entityModifiedDate) {
return true; // Default to "modified" to be safe
}
// If no last pull timestamp, this is the first pull
if (!lastPullTimestamp) {
return true; // First pull, consider everything "modified"
}
try {
var entityDate = new Date(entityModifiedDate);
var lastPullDate = new Date(lastPullTimestamp);
if (isNaN(entityDate.getTime()) || isNaN(lastPullDate.getTime())) {
console.warn("Invalid dates for comparison: entity=".concat(entityModifiedDate, ", lastPull=").concat(lastPullTimestamp));
return true; // Default to "modified" on parsing errors
}
// Entity is modified if its modified date is after the last pull
return entityDate > lastPullDate;
}
catch (error) {
console.warn("Error comparing dates: entity=".concat(entityModifiedDate, ", lastPull=").concat(lastPullTimestamp), error);
return true; // Default to "modified" on errors
}
}
/**
* Mark the start of a pull operation with current timestamp
* @returns Current ISO 8601 timestamp
*/
function markPullStart() {
return new Date().toISOString();
}
/**
* Mark the start of a push operation with current timestamp
* @returns Current ISO 8601 timestamp
*/
function markPushStart() {
return new Date().toISOString();
}
/**
* Clear all timestamps for an instance (used with --reset flag)
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
*/
function clearTimestamps(guid, rootPath) {
try {
var timestampFile = getTimestampFilePath(guid, rootPath);
if (fs.existsSync(timestampFile)) {
fs.unlinkSync(timestampFile);
console.log("Cleared timestamps for ".concat(guid, " (--reset mode)"));
}
}
catch (error) {
console.warn("Error clearing timestamps for ".concat(guid, ":"), error);
}
}
/**
* Get incremental pull decision for an entity type
* @param guid Instance GUID
* @param rootPath Root path (e.g., "agility-files")
* @param entityType Entity type to check
* @returns "incremental" | "full" | "skip"
*/
function getIncrementalPullDecision(guid, rootPath, entityType) {
try {
// Templates always require full refresh (no modified dates)
if (entityType.toLowerCase() === 'templates') {
return "full";
}
var lastPullTimestamp = getLastPullTimestamp(guid, rootPath, entityType);
// No previous pull recorded
if (!lastPullTimestamp) {
return "full";
}
// Previous pull recorded, can do incremental
return "incremental";
}
catch (error) {
console.warn("Error determining pull decision for ".concat(entityType, ":"), error);
return "full"; // Default to full on errors
}
}
//# sourceMappingURL=timestamp-tracker.js.map