@sleekcms/cli 1.1.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 +21 -14
- 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,12 +35,14 @@ 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({
|
|
41
42
|
baseURL: API_BASE_URL,
|
|
42
43
|
headers: { Authorization: `Bearer ${AUTH_TOKEN}` },
|
|
44
|
+
maxContentLength: Infinity,
|
|
45
|
+
maxBodyLength: Infinity,
|
|
43
46
|
});
|
|
44
47
|
|
|
45
48
|
|
|
@@ -49,14 +52,14 @@ async function fetchFiles() {
|
|
|
49
52
|
console.log("๐ฅ Fetching files from API...");
|
|
50
53
|
const response = await apiClient.get("/");
|
|
51
54
|
|
|
52
|
-
await fs.ensureDir(
|
|
55
|
+
await fs.ensureDir(VIEWS_DIR);
|
|
53
56
|
|
|
54
57
|
for (const file of response.data) {
|
|
55
58
|
if (file.file_path) {
|
|
56
|
-
const filePath =
|
|
59
|
+
const filePath = path.join(VIEWS_DIR, file.file_path);
|
|
57
60
|
await fs.outputFile(filePath, file.code);
|
|
58
|
-
fileMap[file.file_path] = file.id;
|
|
59
|
-
console.log(`โ
Created: ${filePath}`);
|
|
61
|
+
fileMap[file.file_path.replace(/\\/g,"/")] = file.id;
|
|
62
|
+
console.log(`โ
Created: ${filePath}`);
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
|
|
@@ -70,7 +73,7 @@ async function fetchFiles() {
|
|
|
70
73
|
async function cleanupFiles() {
|
|
71
74
|
console.log("๐งน Cleaning up files...");
|
|
72
75
|
try {
|
|
73
|
-
await fs.remove(
|
|
76
|
+
await fs.remove(VIEWS_DIR);
|
|
74
77
|
console.log("โ
Cleanup complete. Exiting...");
|
|
75
78
|
} catch (error) {
|
|
76
79
|
console.error("โ Error during cleanup:", error.message);
|
|
@@ -82,9 +85,14 @@ async function cleanupFiles() {
|
|
|
82
85
|
function scheduleUpdate(filePath) {
|
|
83
86
|
if (isShuttingDown) return;
|
|
84
87
|
|
|
85
|
-
const relativePath = filePath.replace(
|
|
88
|
+
const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/"); // Extract relative file path
|
|
86
89
|
const fileId = fileMap[relativePath];
|
|
87
90
|
|
|
91
|
+
if (!fileId) {
|
|
92
|
+
console.warn(`โ ๏ธ Skipping update: No matching file found in API for ${relativePath}`);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
88
96
|
// Clear previous timeout if it exists
|
|
89
97
|
if (pendingUpdates[fileId]) {
|
|
90
98
|
clearTimeout(pendingUpdates[fileId]);
|
|
@@ -94,9 +102,8 @@ function scheduleUpdate(filePath) {
|
|
|
94
102
|
pendingUpdates[fileId] = setTimeout(async () => {
|
|
95
103
|
try {
|
|
96
104
|
const code = await fs.readFile(filePath, "utf-8");
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
console.log("โ
Updated template for:", relativePath);
|
|
105
|
+
let template = await apiClient.patch(`/${fileId}`, { code: code || "foo bar" });
|
|
106
|
+
console.log(`โ
Updated template for: ${relativePath} | Length In: ${code.length}, Out: ${template.data.code.length}`);
|
|
100
107
|
|
|
101
108
|
delete pendingUpdates[fileId]; // Cleanup
|
|
102
109
|
} catch (error) {
|
|
@@ -108,7 +115,7 @@ function scheduleUpdate(filePath) {
|
|
|
108
115
|
async function createSchema(filePath) {
|
|
109
116
|
if (isShuttingDown) return;
|
|
110
117
|
try {
|
|
111
|
-
const relativePath = filePath.replace(
|
|
118
|
+
const relativePath = path.relative(VIEWS_DIR, filePath).replace(/\\/g, "/");
|
|
112
119
|
const resp = await apiClient.post("/cli", { file_path: relativePath});
|
|
113
120
|
const schema = resp.data;
|
|
114
121
|
const templateResp = await apiClient.get(`/${schema.tmpl_main_id}`);
|
|
@@ -116,13 +123,13 @@ async function createSchema(filePath) {
|
|
|
116
123
|
if (relativePath !== template.file_path) {
|
|
117
124
|
// rename the file
|
|
118
125
|
const oldPath = filePath;
|
|
119
|
-
const newPath =
|
|
126
|
+
const newPath = path.join(VIEWS_DIR, template.file_path);
|
|
120
127
|
watcher.unwatch(newPath);
|
|
121
128
|
await fs.move(oldPath, newPath);
|
|
122
129
|
watcher.add(newPath);
|
|
123
130
|
console.log(`โ
Renamed file from ${relativePath} to ${template.file_path}`);
|
|
124
131
|
}
|
|
125
|
-
fileMap[template.file_path] = schema.tmpl_main_id;
|
|
132
|
+
fileMap[template.file_path.replace(/\\/g, "/")] = schema.tmpl_main_id;
|
|
126
133
|
console.log("โ
Created model for:", template.file_path);
|
|
127
134
|
} catch (error) {
|
|
128
135
|
console.error("โ Error creating model:", error.response?.data || error.message);
|
|
@@ -135,7 +142,7 @@ async function createSchema(filePath) {
|
|
|
135
142
|
function monitorFiles() {
|
|
136
143
|
console.log("๐ Watching for file changes...");
|
|
137
144
|
|
|
138
|
-
watcher = chokidar.watch(
|
|
145
|
+
watcher = chokidar.watch(VIEWS_DIR, {
|
|
139
146
|
persistent: true,
|
|
140
147
|
ignoreInitial: true,
|
|
141
148
|
ignored: /\.vscode\//
|