cap-copilot-widget 0.1.2 → 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 +4 -3
- package/postinstall.js +111 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cap-copilot-widget",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "BTP Copilot floating chat widget — Web Component (iframe launcher)",
|
|
5
5
|
"main": "dist/btp-copilot.js",
|
|
6
6
|
"module": "dist/btp-copilot.esm.js",
|
|
@@ -12,13 +12,14 @@
|
|
|
12
12
|
"types": "./dist/btp-copilot.d.ts"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": ["dist"],
|
|
15
|
+
"files": ["dist", "postinstall.js"],
|
|
16
16
|
"scripts": {
|
|
17
17
|
"dev": "vite --port 5174 --open",
|
|
18
18
|
"build": "tsc --emitDeclarationOnly && vite build",
|
|
19
19
|
"prepare": "tsc --emitDeclarationOnly && vite build",
|
|
20
20
|
"preview": "vite preview",
|
|
21
|
-
"clean": "rm -rf dist"
|
|
21
|
+
"clean": "rm -rf dist",
|
|
22
|
+
"postinstall": "node postinstall.js"
|
|
22
23
|
},
|
|
23
24
|
"keywords": ["sap", "fiori", "ui5", "cap", "btp", "ai", "chatbot", "web-component"],
|
|
24
25
|
"license": "MIT",
|
package/postinstall.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
|
|
5
|
+
if (process.env.BTP_COPILOT_SKIP_POSTINSTALL === "1") process.exit(0);
|
|
6
|
+
|
|
7
|
+
const appRoot = process.env.INIT_CWD;
|
|
8
|
+
|
|
9
|
+
const isInstalledPackage = __dirname.includes("node_modules");
|
|
10
|
+
if (!appRoot || !isInstalledPackage) {
|
|
11
|
+
process.exit(0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const IFRAME_URL = "http://localhost:5173";
|
|
15
|
+
|
|
16
|
+
const HTML_CANDIDATES = [
|
|
17
|
+
"index.html",
|
|
18
|
+
"app/index.html",
|
|
19
|
+
"public/index.html",
|
|
20
|
+
"webapp/index.html",
|
|
21
|
+
"src/index.html",
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
let htmlPath = null;
|
|
25
|
+
for (const rel of HTML_CANDIDATES) {
|
|
26
|
+
const candidate = path.join(appRoot, rel);
|
|
27
|
+
if (fs.existsSync(candidate)) {
|
|
28
|
+
htmlPath = candidate;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!htmlPath) {
|
|
34
|
+
process.exit(0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const staticDir = path.dirname(htmlPath);
|
|
38
|
+
|
|
39
|
+
const bundleSrc = path.join(__dirname, "dist", "btp-copilot.js");
|
|
40
|
+
const bundleDest = path.join(staticDir, "btp-copilot.js");
|
|
41
|
+
|
|
42
|
+
if (!fs.existsSync(bundleSrc)) {
|
|
43
|
+
console.warn("\x1b[33m[cap-copilot-widget]\x1b[0m Warning: dist/btp-copilot.js not found — run `npm run build` in the widget package first.");
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
fs.copyFileSync(bundleSrc, bundleDest);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
console.warn(`\x1b[33m[cap-copilot-widget]\x1b[0m Warning: Could not copy bundle to ${path.relative(appRoot, bundleDest)}: ${e.message}`);
|
|
51
|
+
process.exit(0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let html;
|
|
55
|
+
try {
|
|
56
|
+
html = fs.readFileSync(htmlPath, "utf8");
|
|
57
|
+
} catch (e) {
|
|
58
|
+
console.warn(`\x1b[33m[cap-copilot-widget]\x1b[0m Warning: Could not read ${path.relative(appRoot, htmlPath)}: ${e.message}`);
|
|
59
|
+
process.exit(0);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const alreadyHasScript = html.includes("btp-copilot.js");
|
|
63
|
+
const alreadyHasElement = html.includes("<btp-copilot");
|
|
64
|
+
|
|
65
|
+
if (alreadyHasScript && alreadyHasElement) {
|
|
66
|
+
console.log(`\x1b[36m[cap-copilot-widget]\x1b[0m \u2714 Widget bundle updated in ${path.relative(appRoot, bundleDest)}`);
|
|
67
|
+
process.exit(0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let appId = "my-app";
|
|
71
|
+
let appName = "My App";
|
|
72
|
+
try {
|
|
73
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(appRoot, "package.json"), "utf8"));
|
|
74
|
+
appId = (pkg.name ?? "my-app").replace(/[^a-zA-Z0-9_-]/g, "-");
|
|
75
|
+
appName = pkg.description ?? pkg.name ?? "My App";
|
|
76
|
+
} catch { /* ignore */ }
|
|
77
|
+
|
|
78
|
+
if (!alreadyHasScript) {
|
|
79
|
+
const scriptTag =
|
|
80
|
+
` <!-- injected by cap-copilot-widget -->\n` +
|
|
81
|
+
` <script src="/btp-copilot.js" defer></script>`;
|
|
82
|
+
html = html.replace("</head>", `${scriptTag}\n</head>`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!alreadyHasElement) {
|
|
86
|
+
const widgetElement = [
|
|
87
|
+
` <!-- injected by cap-copilot-widget -->`,
|
|
88
|
+
` <btp-copilot`,
|
|
89
|
+
` iframe-url="${IFRAME_URL}"`,
|
|
90
|
+
` app-id="${appId}"`,
|
|
91
|
+
` app-name="${appName}"`,
|
|
92
|
+
` position="bottom-right"`,
|
|
93
|
+
` theme="auto">`,
|
|
94
|
+
` </btp-copilot>`,
|
|
95
|
+
].join("\n");
|
|
96
|
+
|
|
97
|
+
if (html.includes("</body>")) {
|
|
98
|
+
html = html.replace("</body>", `${widgetElement}\n</body>`);
|
|
99
|
+
} else {
|
|
100
|
+
html += `\n${widgetElement}\n`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
fs.writeFileSync(htmlPath, html, "utf8");
|
|
106
|
+
console.log(`\x1b[36m[cap-copilot-widget]\x1b[0m \u2714 Copied widget bundle \u2192 ${path.relative(appRoot, bundleDest)}`);
|
|
107
|
+
console.log(`\x1b[36m[cap-copilot-widget]\x1b[0m \u2714 Injected <btp-copilot> into ${path.relative(appRoot, htmlPath)}`);
|
|
108
|
+
console.log(`\x1b[36m[cap-copilot-widget]\x1b[0m iframe-url="${IFRAME_URL}" (set BTP_COPILOT_IFRAME_URL for production)`);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
console.warn(`\x1b[33m[cap-copilot-widget]\x1b[0m Warning: Could not write ${path.relative(appRoot, htmlPath)}: ${e.message}`);
|
|
111
|
+
}
|