@sleekcms/cli 1.2.0 โ†’ 1.3.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 +18 -12
  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({
@@ -51,14 +52,14 @@ async function fetchFiles() {
51
52
  console.log("๐Ÿ“ฅ Fetching files from API...");
52
53
  const response = await apiClient.get("/");
53
54
 
54
- await fs.ensureDir(`./${VIEWS_DIR}`);
55
+ await fs.ensureDir(VIEWS_DIR);
55
56
 
56
57
  for (const file of response.data) {
57
58
  if (file.file_path) {
58
- const filePath = `./${VIEWS_DIR}${file.file_path}`;
59
+ const filePath = path.join(VIEWS_DIR, file.file_path);
59
60
  await fs.outputFile(filePath, file.code);
60
- fileMap[file.file_path] = file.id;
61
- console.log(`โœ… Created: ${filePath}`);
61
+ fileMap[file.file_path.replace(/\\/g,"/")] = file.id;
62
+ console.log(`โœ… Created: ${filePath}`);
62
63
  }
63
64
  }
64
65
 
@@ -72,7 +73,7 @@ async function fetchFiles() {
72
73
  async function cleanupFiles() {
73
74
  console.log("๐Ÿงน Cleaning up files...");
74
75
  try {
75
- await fs.remove(`./${VIEWS_DIR}`);
76
+ await fs.remove(VIEWS_DIR);
76
77
  console.log("โœ… Cleanup complete. Exiting...");
77
78
  } catch (error) {
78
79
  console.error("โŒ Error during cleanup:", error.message);
@@ -84,9 +85,14 @@ async function cleanupFiles() {
84
85
  function scheduleUpdate(filePath) {
85
86
  if (isShuttingDown) return;
86
87
 
87
- const relativePath = filePath.replace(VIEWS_DIR, ""); // Extract relative file path
88
+ const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/"); // Extract relative file path
88
89
  const fileId = fileMap[relativePath];
89
90
 
91
+ if (!fileId) {
92
+ console.warn(`โš ๏ธ Skipping update: No matching file found in API for ${relativePath}`);
93
+ return;
94
+ }
95
+
90
96
  // Clear previous timeout if it exists
91
97
  if (pendingUpdates[fileId]) {
92
98
  clearTimeout(pendingUpdates[fileId]);
@@ -97,7 +103,7 @@ function scheduleUpdate(filePath) {
97
103
  try {
98
104
  const code = await fs.readFile(filePath, "utf-8");
99
105
  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}`);
106
+ console.log(`โœ… Updated template for: ${relativePath} | Length In: ${code.length}, Out: ${template.data.code.length}`);
101
107
 
102
108
  delete pendingUpdates[fileId]; // Cleanup
103
109
  } catch (error) {
@@ -109,7 +115,7 @@ function scheduleUpdate(filePath) {
109
115
  async function createSchema(filePath) {
110
116
  if (isShuttingDown) return;
111
117
  try {
112
- const relativePath = filePath.replace(VIEWS_DIR, ""); // Extract relative file path
118
+ const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/");
113
119
  const resp = await apiClient.post("/cli", { file_path: relativePath});
114
120
  const schema = resp.data;
115
121
  const templateResp = await apiClient.get(`/${schema.tmpl_main_id}`);
@@ -117,13 +123,13 @@ async function createSchema(filePath) {
117
123
  if (relativePath !== template.file_path) {
118
124
  // rename the file
119
125
  const oldPath = filePath;
120
- const newPath = `./${VIEWS_DIR}${template.file_path}`;
126
+ const newPath = path.join(VIEWS_DIR, template.file_path);
121
127
  watcher.unwatch(newPath);
122
128
  await fs.move(oldPath, newPath);
123
129
  watcher.add(newPath);
124
130
  console.log(`โœ… Renamed file from ${relativePath} to ${template.file_path}`);
125
131
  }
126
- fileMap[template.file_path] = schema.tmpl_main_id;
132
+ fileMap[template.file_path.replace(/\\/g, "/")] = schema.tmpl_main_id;
127
133
  console.log("โœ… Created model for:", template.file_path);
128
134
  } catch (error) {
129
135
  console.error("โŒ Error creating model:", error.response?.data || error.message);
@@ -136,7 +142,7 @@ async function createSchema(filePath) {
136
142
  function monitorFiles() {
137
143
  console.log("๐Ÿ‘€ Watching for file changes...");
138
144
 
139
- watcher = chokidar.watch(`./${VIEWS_DIR}`, {
145
+ watcher = chokidar.watch(VIEWS_DIR, {
140
146
  persistent: true,
141
147
  ignoreInitial: true,
142
148
  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.3.0",
4
4
  "description": "SleekCMS CLI for locally editing SleekCMS site template code",
5
5
  "main": "index.js",
6
6
  "bin": {