@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.
- package/index.js +18 -12
- 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(
|
|
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 =
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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 =
|
|
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(
|
|
145
|
+
watcher = chokidar.watch(VIEWS_DIR, {
|
|
140
146
|
persistent: true,
|
|
141
147
|
ignoreInitial: true,
|
|
142
148
|
ignored: /\.vscode\//
|