@sleekcms/cli 1.2.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 +39 -18
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,6 +4,7 @@ const fs = require("fs-extra");
4
4
  const axios = require("axios");
5
5
  const chokidar = require("chokidar");
6
6
  const { program } = require("commander");
7
+ const path = require("path");
7
8
 
8
9
  const API_BASE_URLS = {
9
10
  localhost: "http://localhost:9000/api/template",
@@ -34,7 +35,7 @@ if (!AUTH_TOKEN) {
34
35
 
35
36
  const API_BASE_URL = API_BASE_URLS[ENV] || API_BASE_URLS.production;
36
37
 
37
- const VIEWS_DIR = AUTH_TOKEN.split('-')[0] + "-views/";
38
+ const VIEWS_DIR = path.resolve(AUTH_TOKEN.split('-')[0] + "-views");
38
39
 
39
40
  // Axios instance with authorization
40
41
  const apiClient = axios.create({
@@ -44,6 +45,18 @@ const apiClient = axios.create({
44
45
  maxBodyLength: Infinity,
45
46
  });
46
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
+ }
47
60
 
48
61
  // Function to fetch and save files
49
62
  async function fetchFiles() {
@@ -51,14 +64,14 @@ async function fetchFiles() {
51
64
  console.log("📥 Fetching files from API...");
52
65
  const response = await apiClient.get("/");
53
66
 
54
- await fs.ensureDir(`./${VIEWS_DIR}`);
67
+ await fs.ensureDir(VIEWS_DIR);
55
68
 
56
69
  for (const file of response.data) {
57
70
  if (file.file_path) {
58
- const filePath = `./${VIEWS_DIR}${file.file_path}`;
71
+ const filePath = path.join(VIEWS_DIR, file.file_path);
59
72
  await fs.outputFile(filePath, file.code);
60
- fileMap[file.file_path] = file.id;
61
- console.log(`✅ Created: ${filePath}`);
73
+ fileMap[file.file_path.replace(/\\/g,"/")] = file;
74
+ console.log(`✅ Created: ${filePath}`);
62
75
  }
63
76
  }
64
77
 
@@ -72,7 +85,7 @@ async function fetchFiles() {
72
85
  async function cleanupFiles() {
73
86
  console.log("🧹 Cleaning up files...");
74
87
  try {
75
- await fs.remove(`./${VIEWS_DIR}`);
88
+ await fs.remove(VIEWS_DIR);
76
89
  console.log("✅ Cleanup complete. Exiting...");
77
90
  } catch (error) {
78
91
  console.error("❌ Error during cleanup:", error.message);
@@ -84,24 +97,32 @@ async function cleanupFiles() {
84
97
  function scheduleUpdate(filePath) {
85
98
  if (isShuttingDown) return;
86
99
 
87
- const relativePath = filePath.replace(VIEWS_DIR, ""); // Extract relative file path
88
- const fileId = fileMap[relativePath];
100
+ const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/"); // Extract relative file path
101
+ const file = fileMap[relativePath];
102
+
103
+ if (!file?.id) {
104
+ console.warn(`⚠️ Skipping update: No matching file found in API for ${relativePath}`);
105
+ return;
106
+ }
89
107
 
90
108
  // Clear previous timeout if it exists
91
- if (pendingUpdates[fileId]) {
92
- clearTimeout(pendingUpdates[fileId]);
109
+ if (pendingUpdates[file.id]) {
110
+ clearTimeout(pendingUpdates[file.id]);
93
111
  }
94
112
 
95
113
  // Schedule a new update after the debounce delay
96
- pendingUpdates[fileId] = setTimeout(async () => {
114
+ pendingUpdates[file.id] = setTimeout(async () => {
97
115
  try {
98
116
  const code = await fs.readFile(filePath, "utf-8");
99
- let template = await apiClient.patch(`/${fileId}`, { code: code || "foo bar" });
100
- console.log("✅ Updated template for: ", relativePath, `In: ${code.length}, Out: ${template.data.code.length}`);
117
+ let template = await apiClient.patch(`/${file.id}`, { code: code || "foo bar", updated_at: file.updated_at });
118
+ fileMap[relativePath] = template.data;
119
+ console.log(`✅ Updated template for: ${relativePath} | Length In: ${code.length}, Out: ${template.data.code.length}`);
101
120
 
102
- delete pendingUpdates[fileId]; // Cleanup
121
+ delete pendingUpdates[file.id]; // Cleanup
103
122
  } catch (error) {
104
123
  console.error("❌ Error updating API:", error.response?.data || error.message);
124
+ // refresh file
125
+ await refreshFile(filePath);
105
126
  }
106
127
  }, DEBOUNCE_DELAY);
107
128
  }
@@ -109,7 +130,7 @@ function scheduleUpdate(filePath) {
109
130
  async function createSchema(filePath) {
110
131
  if (isShuttingDown) return;
111
132
  try {
112
- const relativePath = filePath.replace(VIEWS_DIR, ""); // Extract relative file path
133
+ const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/");
113
134
  const resp = await apiClient.post("/cli", { file_path: relativePath});
114
135
  const schema = resp.data;
115
136
  const templateResp = await apiClient.get(`/${schema.tmpl_main_id}`);
@@ -117,13 +138,13 @@ async function createSchema(filePath) {
117
138
  if (relativePath !== template.file_path) {
118
139
  // rename the file
119
140
  const oldPath = filePath;
120
- const newPath = `./${VIEWS_DIR}${template.file_path}`;
141
+ const newPath = path.join(VIEWS_DIR, template.file_path);
121
142
  watcher.unwatch(newPath);
122
143
  await fs.move(oldPath, newPath);
123
144
  watcher.add(newPath);
124
145
  console.log(`✅ Renamed file from ${relativePath} to ${template.file_path}`);
125
146
  }
126
- fileMap[template.file_path] = schema.tmpl_main_id;
147
+ fileMap[template.file_path.replace(/\\/g, "/")] = template;
127
148
  console.log("✅ Created model for:", template.file_path);
128
149
  } catch (error) {
129
150
  console.error("❌ Error creating model:", error.response?.data || error.message);
@@ -136,7 +157,7 @@ async function createSchema(filePath) {
136
157
  function monitorFiles() {
137
158
  console.log("👀 Watching for file changes...");
138
159
 
139
- watcher = chokidar.watch(`./${VIEWS_DIR}`, {
160
+ watcher = chokidar.watch(VIEWS_DIR, {
140
161
  persistent: true,
141
162
  ignoreInitial: true,
142
163
  ignored: /\.vscode\//
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleekcms/cli",
3
- "version": "1.2.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": {