@promakeai/cli 0.7.1 → 0.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promakeai/cli",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "promake": "dist/index.js"
@@ -1,5 +1,6 @@
1
1
  import { TooltipProvider } from "@/components/ui/tooltip";
2
2
  import { GoogleAnalytics } from "@/components/GoogleAnalytics";
3
+ import { MetriaAnalytics } from "@/components/MetriaAnalytics";
3
4
  import { ScriptInjector } from "@/components/ScriptInjector";
4
5
  import { AppDbProvider } from "@/db";
5
6
  import { Router } from "./router";
@@ -9,6 +10,7 @@ const App = () => {
9
10
  <AppDbProvider>
10
11
  <TooltipProvider>
11
12
  <GoogleAnalytics />
13
+ <MetriaAnalytics />
12
14
  <ScriptInjector />
13
15
  <Router />
14
16
  </TooltipProvider>
@@ -0,0 +1,64 @@
1
+ import { useEffect, useRef } from 'react';
2
+ const DEFAULT_TRACKER_SCRIPT_URL =
3
+ 'https://cdn.jsdelivr.net/npm/@litemetrics/tracker@latest/dist/litemetrics.global.js';
4
+
5
+ function pickFirstNonEmpty(...values: Array<string | undefined>): string | undefined {
6
+ return values.find((value) => typeof value === 'string' && value.trim().length > 0);
7
+ }
8
+
9
+ export const analyticsConfig = {
10
+ siteId: pickFirstNonEmpty(import.meta.env.VITE_LITEMETRICS_SITE_ID, import.meta.env.VITE_METRIA_SITE_ID),
11
+ serverUrl: pickFirstNonEmpty(import.meta.env.VITE_LITEMETRICS_SERVER_URL, import.meta.env.VITE_METRIA_SERVER_URL),
12
+ trackerScriptUrl: pickFirstNonEmpty(import.meta.env.VITE_LITEMETRICS_SCRIPT_URL) ?? DEFAULT_TRACKER_SCRIPT_URL,
13
+ };
14
+
15
+ export const isAnalyticsEnabled = Boolean(analyticsConfig.siteId && analyticsConfig.serverUrl);
16
+
17
+ type TrackerGlobal = {
18
+ createTracker: (config: { siteId: string; endpoint: string }) => void;
19
+ };
20
+
21
+ declare global {
22
+ interface Window {
23
+ Litemetrics?: TrackerGlobal;
24
+ }
25
+ }
26
+
27
+ function stripTrailingSlashes(value: string): string {
28
+ return value.replace(/\/+$/, '');
29
+ }
30
+
31
+ export function MetriaAnalytics() {
32
+ const injected = useRef(false);
33
+
34
+ useEffect(() => {
35
+ if (!isAnalyticsEnabled || !analyticsConfig.siteId || !analyticsConfig.serverUrl || injected.current) {
36
+ return;
37
+ }
38
+ if (document.querySelector('[data-injected="litemetrics-tracker"]')) return;
39
+
40
+ injected.current = true;
41
+
42
+ const normalizedServerUrl = stripTrailingSlashes(analyticsConfig.serverUrl);
43
+ const script = document.createElement('script');
44
+ script.async = true;
45
+ script.src = analyticsConfig.trackerScriptUrl;
46
+ script.setAttribute('data-injected', 'litemetrics-tracker');
47
+ script.onload = () => {
48
+ if (document.querySelector('[data-injected="litemetrics-init"]')) return;
49
+
50
+ window.Litemetrics?.createTracker({
51
+ siteId: analyticsConfig.siteId!,
52
+ endpoint: `${normalizedServerUrl}/api/collect`,
53
+ });
54
+
55
+ const marker = document.createElement('meta');
56
+ marker.setAttribute('data-injected', 'litemetrics-init');
57
+ document.head.appendChild(marker);
58
+ };
59
+
60
+ document.head.appendChild(script);
61
+ }, []);
62
+
63
+ return null;
64
+ }
@@ -1,8 +1,10 @@
1
1
  import path from "path";
2
+ import { execSync } from "child_process";
2
3
  import tailwindcss from "@tailwindcss/vite";
3
4
  import react from "@vitejs/plugin-react";
4
- import { defineConfig, type Plugin } from "vite";
5
+ import { defineConfig, loadEnv, type Plugin } from "vite";
5
6
  import { inspectorDebugger } from "@promakeai/inspector/plugin";
7
+ import type { IncomingMessage } from "http";
6
8
 
7
9
  // Plugin: Restart dev server when lang files are added or changed
8
10
  function langWatchPlugin(): Plugin {
@@ -28,14 +30,88 @@ function langWatchPlugin(): Plugin {
28
30
  };
29
31
  }
30
32
 
33
+ // Helper: Read request body
34
+ function readBody(req: IncomingMessage): Promise<string> {
35
+ return new Promise((resolve) => {
36
+ let data = "";
37
+ req.on("data", (chunk: string) => (data += chunk));
38
+ req.on("end", () => resolve(data));
39
+ });
40
+ }
41
+
42
+ // Plugin: Expose API for running promake commands from the panel
43
+ function promakeApiPlugin(secret?: string): Plugin {
44
+ const ALLOWED_COMMANDS = ["seo", "theme"];
45
+ const ALLOWED_ORIGINS = [
46
+ "https://promake.ai",
47
+ "https://www.promake.ai",
48
+ ];
49
+ const BASE_PATH = secret ? `/__promake/${secret}` : "/__promake";
50
+
51
+ return {
52
+ name: "promake-api",
53
+ configureServer(server) {
54
+ server.middlewares.use(`${BASE_PATH}/run`, async (req, res) => {
55
+ const origin = req.headers.origin || "";
56
+ const allowedOrigin = ALLOWED_ORIGINS.includes(origin) ? origin : ALLOWED_ORIGINS[0];
57
+ res.setHeader("Access-Control-Allow-Origin", allowedOrigin);
58
+ res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
59
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type");
60
+
61
+ if (req.method === "OPTIONS") {
62
+ res.statusCode = 204;
63
+ res.end();
64
+ return;
65
+ }
66
+
67
+ if (req.method !== "POST") {
68
+ res.statusCode = 405;
69
+ res.end();
70
+ return;
71
+ }
72
+
73
+ try {
74
+ const body = await readBody(req);
75
+ const { command, args = [] } = JSON.parse(body);
76
+
77
+ if (!ALLOWED_COMMANDS.includes(command)) {
78
+ res.statusCode = 400;
79
+ res.setHeader("Content-Type", "application/json");
80
+ res.end(JSON.stringify({ error: `Command not allowed: ${command}` }));
81
+ return;
82
+ }
83
+
84
+ const safeArgs = args.map((a: string) => `'${a.replace(/'/g, "'\\''")}'`).join(" ");
85
+ const output = execSync(`promake ${command} ${safeArgs}`, {
86
+ cwd: process.cwd(),
87
+ encoding: "utf-8",
88
+ timeout: 15000,
89
+ });
90
+
91
+ res.setHeader("Content-Type", "application/json");
92
+ res.end(JSON.stringify({ ok: true, output }));
93
+ } catch (e: any) {
94
+ res.statusCode = 500;
95
+ res.setHeader("Content-Type", "application/json");
96
+ res.end(JSON.stringify({ ok: false, error: e.message }));
97
+ }
98
+ });
99
+ },
100
+ };
101
+ }
102
+
31
103
  // https://vite.dev/config/
32
- export default defineConfig(({ mode }) => ({
104
+ export default defineConfig(({ mode }) => {
105
+ const env = loadEnv(mode, process.cwd(), "PROMAKE_");
106
+
107
+ return ({
33
108
  cacheDir: '/tmp/.vite-cache',
34
109
  plugins: [
35
110
  inspectorDebugger({ enabled: mode === "development" }),
36
111
  react(),
37
112
  tailwindcss(),
38
113
  mode === "development" && langWatchPlugin(),
114
+ mode === "development" && promakeApiPlugin(env.PROMAKE_SECRET),
39
115
  ].filter(Boolean),
40
116
  resolve: {
41
117
  alias: {
@@ -66,4 +142,5 @@ export default defineConfig(({ mode }) => ({
66
142
  },
67
143
  },
68
144
  },
69
- }));
145
+ });
146
+ });