@sleekcms/cli 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +24 -9
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -45,6 +45,18 @@ const apiClient = axios.create({
45
45
  maxBodyLength: Infinity,
46
46
  });
47
47
 
48
+ async function refreshFile(filePath) {
49
+ try {
50
+ const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/");
51
+ const resp = await apiClient.get(`/${fileMap[relativePath].id}`);
52
+ const template = resp.data;
53
+ fileMap[relativePath] = template;
54
+ await fs.outputFile(filePath, template.code);
55
+ console.log(`✅ Refreshed template for: ${relativePath}`);
56
+ } catch (error) {
57
+ console.error("❌ Error refreshing template:", error.response?.data || error.message);
58
+ }
59
+ }
48
60
 
49
61
  // Function to fetch and save files
50
62
  async function fetchFiles() {
@@ -58,7 +70,7 @@ async function fetchFiles() {
58
70
  if (file.file_path) {
59
71
  const filePath = path.join(VIEWS_DIR, file.file_path);
60
72
  await fs.outputFile(filePath, file.code);
61
- fileMap[file.file_path.replace(/\\/g,"/")] = file.id;
73
+ fileMap[file.file_path.replace(/\\/g,"/")] = file;
62
74
  console.log(`✅ Created: ${filePath}`);
63
75
  }
64
76
  }
@@ -86,28 +98,31 @@ function scheduleUpdate(filePath) {
86
98
  if (isShuttingDown) return;
87
99
 
88
100
  const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/"); // Extract relative file path
89
- const fileId = fileMap[relativePath];
101
+ const file = fileMap[relativePath];
90
102
 
91
- if (!fileId) {
103
+ if (!file?.id) {
92
104
  console.warn(`⚠️ Skipping update: No matching file found in API for ${relativePath}`);
93
105
  return;
94
106
  }
95
107
 
96
108
  // Clear previous timeout if it exists
97
- if (pendingUpdates[fileId]) {
98
- clearTimeout(pendingUpdates[fileId]);
109
+ if (pendingUpdates[file.id]) {
110
+ clearTimeout(pendingUpdates[file.id]);
99
111
  }
100
112
 
101
113
  // Schedule a new update after the debounce delay
102
- pendingUpdates[fileId] = setTimeout(async () => {
114
+ pendingUpdates[file.id] = setTimeout(async () => {
103
115
  try {
104
116
  const code = await fs.readFile(filePath, "utf-8");
105
- let template = await apiClient.patch(`/${fileId}`, { code: code || "foo bar" });
117
+ let template = await apiClient.patch(`/${file.id}`, { code: code || "foo bar", updated_at: file.updated_at });
118
+ fileMap[relativePath] = template.data;
106
119
  console.log(`✅ Updated template for: ${relativePath} | Length In: ${code.length}, Out: ${template.data.code.length}`);
107
120
 
108
- delete pendingUpdates[fileId]; // Cleanup
121
+ delete pendingUpdates[file.id]; // Cleanup
109
122
  } catch (error) {
110
123
  console.error("❌ Error updating API:", error.response?.data || error.message);
124
+ // refresh file
125
+ await refreshFile(filePath);
111
126
  }
112
127
  }, DEBOUNCE_DELAY);
113
128
  }
@@ -129,7 +144,7 @@ async function createSchema(filePath) {
129
144
  watcher.add(newPath);
130
145
  console.log(`✅ Renamed file from ${relativePath} to ${template.file_path}`);
131
146
  }
132
- fileMap[template.file_path.replace(/\\/g, "/")] = schema.tmpl_main_id;
147
+ fileMap[template.file_path.replace(/\\/g, "/")] = template;
133
148
  console.log("✅ Created model for:", template.file_path);
134
149
  } catch (error) {
135
150
  console.error("❌ Error creating model:", error.response?.data || error.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleekcms/cli",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "SleekCMS CLI for locally editing SleekCMS site template code",
5
5
  "main": "index.js",
6
6
  "bin": {