@sleekcms/cli 2.0.1 → 2.2.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 +62 -44
  2. package/package.json +1 -2
package/index.js CHANGED
@@ -5,7 +5,6 @@ const axios = require("axios");
5
5
  const chokidar = require("chokidar");
6
6
  const { program } = require("commander");
7
7
  const path = require("path");
8
- const express = require("express");
9
8
  const { execSync, spawn } = require("child_process");
10
9
  const readline = require("readline");
11
10
  const agentMdContent = require("./agent.js");
@@ -39,29 +38,58 @@ Examples:
39
38
  .parse(process.argv);
40
39
 
41
40
  const options = program.opts();
42
- const AUTH_TOKEN = options.token;
43
- const tokenParts = AUTH_TOKEN ? AUTH_TOKEN.split('-') : [];
44
- const ENV = (tokenParts[2] || options.env || "production").toLowerCase();
45
41
 
46
- if (!AUTH_TOKEN) {
47
- console.error("❌ Missing required --token (-t) parameter. Use -h for help.");
48
- process.exit(1);
42
+ // Will be set after prompting if needed
43
+ let AUTH_TOKEN;
44
+ let ENV;
45
+ let API_BASE_URL;
46
+ let VIEWS_DIR;
47
+ let apiClient;
48
+
49
+ function prompt(question) {
50
+ const rl = readline.createInterface({
51
+ input: process.stdin,
52
+ output: process.stdout
53
+ });
54
+ return new Promise(resolve => {
55
+ rl.question(question, answer => {
56
+ rl.close();
57
+ resolve(answer.trim());
58
+ });
59
+ });
49
60
  }
50
61
 
51
- const API_BASE_URL = API_BASE_URLS[ENV] || API_BASE_URLS.production;
62
+ async function initConfig() {
63
+ AUTH_TOKEN = options.token;
64
+ if (!AUTH_TOKEN) {
65
+ AUTH_TOKEN = await prompt("Enter SleekCMS CLI auth token: ");
66
+ if (!AUTH_TOKEN) {
67
+ console.error("❌ Token is required.");
68
+ process.exit(1);
69
+ }
70
+ }
71
+
72
+ const tokenParts = AUTH_TOKEN.trim().split('-');
73
+ ENV = (tokenParts[2] || options.env || "production").toLowerCase();
74
+ API_BASE_URL = API_BASE_URLS[ENV] || API_BASE_URLS.production;
52
75
 
53
- const viewsFolder = tokenParts[0] + "-views";
54
- const VIEWS_DIR = options.path
55
- ? path.resolve(options.path, viewsFolder)
56
- : path.resolve(viewsFolder);
76
+ let customPath = options.path;
77
+ if (!customPath) {
78
+ customPath = await prompt("Enter workspace folder path (or press Enter for current directory): ");
79
+ }
80
+
81
+ const viewsFolder = tokenParts[0] + "-views";
82
+ VIEWS_DIR = customPath
83
+ ? path.resolve(customPath, viewsFolder)
84
+ : path.resolve(viewsFolder);
57
85
 
58
- // Axios instance with authorization
59
- const apiClient = axios.create({
60
- baseURL: API_BASE_URL,
61
- headers: { Authorization: `Bearer ${AUTH_TOKEN}` },
62
- maxContentLength: Infinity,
63
- maxBodyLength: Infinity,
64
- });
86
+ apiClient = axios.create({
87
+ baseURL: API_BASE_URL,
88
+ headers: { Authorization: `Bearer ${AUTH_TOKEN}` },
89
+ maxContentLength: Infinity,
90
+ maxBodyLength: Infinity,
91
+ });
92
+ }
65
93
 
66
94
  async function refreshFile(filePath) {
67
95
  try {
@@ -135,9 +163,16 @@ function scheduleUpdate(filePath) {
135
163
  pendingUpdates[file.id] = setTimeout(async () => {
136
164
  try {
137
165
  const code = await fs.readFile(filePath, "utf-8");
166
+
167
+ // Skip API call if content hasn't changed
168
+ if (code === file.code) {
169
+ delete pendingUpdates[file.id];
170
+ return;
171
+ }
172
+
138
173
  let template = await apiClient.patch(`/${file.id}`, { code: code || "foo bar", updated_at: file.updated_at });
139
174
  fileMap[relativePath] = template.data;
140
- console.log(`✅ Updated template for: ${relativePath} | Length In: ${code.length}, Out: ${template.data.code.length}`);
175
+ console.log(`✅ Updated template for: ${relativePath}`);
141
176
 
142
177
  delete pendingUpdates[file.id]; // Cleanup
143
178
  } catch (error) {
@@ -174,29 +209,6 @@ async function createSchema(filePath) {
174
209
  }
175
210
  }
176
211
 
177
- // Start an Express server to serve files in VIEWS_DIR
178
- function startServer() {
179
- const app = express();
180
-
181
- // Serve static files from VIEWS_DIR
182
- app.use(express.static(VIEWS_DIR));
183
-
184
- // Optional: Custom 404 for files not found
185
- app.use((req, res) => {
186
- res.status(404).send("File not found");
187
- });
188
-
189
- const port = process.env.PORT || 3000;
190
- app.listen(port, () => {
191
- console.log(`\n✅ Ready! Editing session started.`);
192
- console.log(`\n📁 Templates directory: ${VIEWS_DIR}`);
193
- console.log(`🌐 Environment: ${ENV}`);
194
- console.log(`🔗 Static server: http://localhost:${port}`);
195
- console.log(`\n⚠️ Files will be cleaned up on exit (Ctrl+C).`);
196
- showEditorMenu();
197
- });
198
- }
199
-
200
212
  // Function to monitor file changes
201
213
  function monitorFiles() {
202
214
  watcher = chokidar.watch(VIEWS_DIR, {
@@ -283,9 +295,15 @@ async function handleExit() {
283
295
 
284
296
  // Main function
285
297
  async function main() {
298
+ await initConfig();
286
299
  await fetchFiles();
287
300
  monitorFiles();
288
- startServer();
301
+
302
+ console.log(`\n✅ Ready! Editing session started.`);
303
+ console.log(`\n📁 Templates directory: ${VIEWS_DIR}`);
304
+ console.log(`🌐 Environment: ${ENV}`);
305
+ console.log(`\n⚠️ Files will be cleaned up on exit (Ctrl+C).`);
306
+ showEditorMenu();
289
307
 
290
308
  process.on("SIGINT", async () => {
291
309
  console.log("\n🛑 Caught interrupt signal (Ctrl+C)");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sleekcms/cli",
3
- "version": "2.0.1",
3
+ "version": "2.2.0",
4
4
  "description": "SleekCMS CLI for locally editing SleekCMS site template code",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -15,7 +15,6 @@
15
15
  "axios": "^1.7.9",
16
16
  "chokidar": "^4.0.3",
17
17
  "commander": "^13.1.0",
18
- "express": "^4.21.0",
19
18
  "fs-extra": "^11.3.0"
20
19
  }
21
20
  }