cap-copilot-widget 0.1.3 → 0.1.4
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 +1 -1
- package/postinstall.js +3 -27
package/package.json
CHANGED
package/postinstall.js
CHANGED
|
@@ -1,35 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* postinstall.js — auto-configure consuming app's HTML when cap-copilot-widget is installed.
|
|
3
|
-
*
|
|
4
|
-
* What it does:
|
|
5
|
-
* 1. Detects the app's static files directory (app/, public/, webapp/, or root)
|
|
6
|
-
* 2. Copies dist/btp-copilot.js into that directory so the server can serve it
|
|
7
|
-
* 3. Finds index.html and injects:
|
|
8
|
-
* - <script src="/btp-copilot.js" defer></script> into <head>
|
|
9
|
-
* - <btp-copilot ...></btp-copilot> into <body>
|
|
10
|
-
*
|
|
11
|
-
* The frontend chatbot URL (iframe-url) is owned by this package — it is NOT
|
|
12
|
-
* written into the app's source code. Override via env var for production:
|
|
13
|
-
* BTP_COPILOT_IFRAME_URL=https://your-frontend.cfapps.eu10.hana.ondemand.com
|
|
14
|
-
*/
|
|
15
1
|
"use strict";
|
|
16
2
|
const fs = require("fs");
|
|
17
3
|
const path = require("path");
|
|
18
4
|
|
|
19
|
-
// Set BTP_COPILOT_SKIP_POSTINSTALL=1 to opt out.
|
|
20
5
|
if (process.env.BTP_COPILOT_SKIP_POSTINSTALL === "1") process.exit(0);
|
|
21
6
|
|
|
22
|
-
// INIT_CWD = directory where `npm install` was run (the consuming app root)
|
|
23
7
|
const appRoot = process.env.INIT_CWD;
|
|
24
|
-
|
|
8
|
+
|
|
9
|
+
const isInstalledPackage = __dirname.includes("node_modules");
|
|
10
|
+
if (!appRoot || !isInstalledPackage) {
|
|
25
11
|
process.exit(0);
|
|
26
12
|
}
|
|
27
13
|
|
|
28
|
-
// ── Baked-in defaults (hidden from app developer) ────────────────────────────
|
|
29
14
|
const IFRAME_URL = "http://localhost:5173";
|
|
30
15
|
|
|
31
|
-
// ── Locate the static files directory and index.html ─────────────────────────
|
|
32
|
-
// Search order: project root → app/ → public/ → webapp/ → src/
|
|
33
16
|
const HTML_CANDIDATES = [
|
|
34
17
|
"index.html",
|
|
35
18
|
"app/index.html",
|
|
@@ -48,13 +31,11 @@ for (const rel of HTML_CANDIDATES) {
|
|
|
48
31
|
}
|
|
49
32
|
|
|
50
33
|
if (!htmlPath) {
|
|
51
|
-
// No index.html found anywhere — skip silently
|
|
52
34
|
process.exit(0);
|
|
53
35
|
}
|
|
54
36
|
|
|
55
37
|
const staticDir = path.dirname(htmlPath);
|
|
56
38
|
|
|
57
|
-
// ── Copy widget bundle into static dir so the server can serve it ─────────────
|
|
58
39
|
const bundleSrc = path.join(__dirname, "dist", "btp-copilot.js");
|
|
59
40
|
const bundleDest = path.join(staticDir, "btp-copilot.js");
|
|
60
41
|
|
|
@@ -70,7 +51,6 @@ try {
|
|
|
70
51
|
process.exit(0);
|
|
71
52
|
}
|
|
72
53
|
|
|
73
|
-
// ── Read and patch index.html ──────────────────────────────────────────────────
|
|
74
54
|
let html;
|
|
75
55
|
try {
|
|
76
56
|
html = fs.readFileSync(htmlPath, "utf8");
|
|
@@ -83,12 +63,10 @@ const alreadyHasScript = html.includes("btp-copilot.js");
|
|
|
83
63
|
const alreadyHasElement = html.includes("<btp-copilot");
|
|
84
64
|
|
|
85
65
|
if (alreadyHasScript && alreadyHasElement) {
|
|
86
|
-
// Already fully configured — nothing to do
|
|
87
66
|
console.log(`\x1b[36m[cap-copilot-widget]\x1b[0m \u2714 Widget bundle updated in ${path.relative(appRoot, bundleDest)}`);
|
|
88
67
|
process.exit(0);
|
|
89
68
|
}
|
|
90
69
|
|
|
91
|
-
// Detect app metadata from consuming app's package.json
|
|
92
70
|
let appId = "my-app";
|
|
93
71
|
let appName = "My App";
|
|
94
72
|
try {
|
|
@@ -97,7 +75,6 @@ try {
|
|
|
97
75
|
appName = pkg.description ?? pkg.name ?? "My App";
|
|
98
76
|
} catch { /* ignore */ }
|
|
99
77
|
|
|
100
|
-
// Inject <script> tag into <head>
|
|
101
78
|
if (!alreadyHasScript) {
|
|
102
79
|
const scriptTag =
|
|
103
80
|
` <!-- injected by cap-copilot-widget -->\n` +
|
|
@@ -105,7 +82,6 @@ if (!alreadyHasScript) {
|
|
|
105
82
|
html = html.replace("</head>", `${scriptTag}\n</head>`);
|
|
106
83
|
}
|
|
107
84
|
|
|
108
|
-
// Inject <btp-copilot> element before </body>
|
|
109
85
|
if (!alreadyHasElement) {
|
|
110
86
|
const widgetElement = [
|
|
111
87
|
` <!-- injected by cap-copilot-widget -->`,
|