@commentkit/comment-kit 2.0.0 → 2.1.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/CHANGELOG.md +57 -0
- package/README.md +123 -19
- package/SECURITY.md +2 -2
- package/dist/node/cli/api.js +61 -15
- package/dist/node/cli/credentials.js +8 -0
- package/dist/node/cli/doctor.js +152 -25
- package/dist/node/cli/init.js +349 -35
- package/dist/node/cli/main.js +310 -69
- package/dist/node/cli/mcp.js +187 -6
- package/dist/node/cli/website.js +140 -0
- package/dist/node/shared/dashboard-places.js +33 -0
- package/dist/node/types/next.d.ts +4 -4
- package/dist/npm/THIRD_PARTY_NOTICES.txt +47 -0
- package/dist/npm/chunks/{AgentSend-DxRJIYiz.js → AgentSend-pA7Ufvye.js} +16 -16
- package/dist/npm/chunks/Checklist-Cn9skOYK.js +269 -0
- package/dist/npm/chunks/Composer-D7H-pzGa.js +734 -0
- package/dist/npm/chunks/DatePicker-Nta5a1xD.js +161 -0
- package/dist/npm/chunks/{Guest-vvNPz8h-.js → Guest-Bi7hlzhs.js} +63 -59
- package/dist/npm/chunks/{MarkupEditor-VCOXh9d2.js → MarkupEditor-WU3USeTk.js} +46 -39
- package/dist/npm/chunks/Panel-DhAzbUbC.js +1210 -0
- package/dist/npm/chunks/{Settings-BGoZS2iV.js → Settings-BmKqPX40.js} +91 -90
- package/dist/npm/chunks/{Shortcuts-B2OMvczr.js → Shortcuts-Bl-c0Sez.js} +25 -23
- package/dist/npm/chunks/SignInFirst-CmGvSYkQ.js +35 -0
- package/dist/npm/chunks/{StyleEditor-CJ_d8Huf.js → StyleEditor-D6fUnlwq.js} +10 -10
- package/dist/npm/chunks/{StylePanel-yQzHHlY7.js → StylePanel-C-Gikxt1.js} +61 -61
- package/dist/npm/chunks/TaskFields-CaKi523b.js +617 -0
- package/dist/npm/chunks/ThreadCard-d_3pUB5V.js +1142 -0
- package/dist/npm/chunks/Viewer-BULQsi9-.js +115 -0
- package/dist/npm/chunks/{agent-C1OQzBAm.js → agent-DHdhIJ3s.js} +1 -1
- package/dist/npm/chunks/{ar-CAJ3XT8e.js → ar-B5UszrQp.js} +53 -12
- package/dist/npm/chunks/card-bits-cV8GdFWX.js +55 -0
- package/dist/npm/chunks/{client-kNAo2TTG.js → client-Cv8ddtuM.js} +13 -11
- package/dist/npm/chunks/{confetti-CkBdSu19.js → confetti-B9zlyILU.js} +1 -1
- package/dist/npm/chunks/{context-B8UAzve0.js → context-CsHnabaA.js} +1 -1
- package/dist/npm/chunks/{de-Dl18G48t.js → de-BE0Aaq8u.js} +53 -12
- package/dist/npm/chunks/{dialog-GO04oC3D.js → dialog-DvdIkCW0.js} +1 -1
- package/dist/npm/chunks/{dpop-U48QIQd7.js → dpop-CwW9ifkq.js} +1 -1
- package/dist/npm/chunks/entrance-Bn_5M9uO.js +90 -0
- package/dist/npm/chunks/{es-Dxk79lS9.js → es-BQviao4d.js} +53 -12
- package/dist/npm/chunks/{extension-E81hWyiT.js → extension-CnQTDC1j.js} +2 -2
- package/dist/npm/chunks/{extension-CKFpPWmi.js → extension-DXhz81XD.js} +1 -1
- package/dist/npm/chunks/{files-DyD5BZgp.js → files-DLEr_wmh.js} +2 -2
- package/dist/npm/chunks/{fr-8BebGKYD.js → fr-C5gu6A06.js} +53 -12
- package/dist/npm/chunks/{hi-BIN6Yy_p.js → hi-DXfQ5EvY.js} +53 -12
- package/dist/npm/chunks/{i18n-DOBt8e9U.js → i18n-Mgs9VOuk.js} +63 -58
- package/dist/npm/chunks/icons-_C6ZWmBY.js +141 -0
- package/dist/npm/chunks/{identify-CN-3xzqL.js → identify-BnQGJOa0.js} +4 -4
- package/dist/npm/chunks/{it-Cr6EwpPQ.js → it-BPHt13FV.js} +53 -12
- package/dist/npm/chunks/{ja-Iy3s5aur.js → ja-BkCqyKR0.js} +52 -11
- package/dist/npm/chunks/late-words-D4Oo_yhJ.js +57 -0
- package/dist/npm/chunks/{live-CelVeZfP.js → live-DXcxj_Af.js} +28 -22
- package/dist/npm/chunks/{nl-B84WHqqe.js → nl-DZxdLLMU.js} +53 -12
- package/dist/npm/chunks/{page-key-DepmJzC1.js → page-key-UqAozdZi.js} +1 -1
- package/dist/npm/chunks/{pick-yTyqtxN0.js → pick-C20RVUas.js} +1 -1
- package/dist/npm/chunks/{pkce-ChKGSW_4.js → pkce-BPvWoBrr.js} +1 -1
- package/dist/npm/chunks/{pt-BR-DOkdlfoe.js → pt-BR-lA9mJ05F.js} +53 -12
- package/dist/npm/chunks/{report-CrIqQZDI.js → report-BjUgUNnT.js} +1 -1
- package/dist/npm/chunks/show-BinoJyED.js +97 -0
- package/dist/npm/chunks/signin-C2jjsxz9.js +101 -0
- package/dist/npm/chunks/{source-pXVve2Cx.js → source-NuflODx3.js} +1 -1
- package/dist/npm/chunks/src-9o2Ocq8d.js +221 -0
- package/dist/npm/chunks/start-Cbx6rMrr.js +1904 -0
- package/dist/npm/chunks/states-DLMCvwes.js +3 -0
- package/dist/npm/chunks/{upload-MgPYqPyE.js → upload-C4NpM-jE.js} +1 -1
- package/dist/npm/chunks/uploads-CY4bDbaI.js +1236 -0
- package/dist/npm/chunks/{wrap-Bl0gbnFa.js → wrap-D-xTKIzj.js} +20 -20
- package/dist/npm/chunks/{zh-CN-Z5fQCb7G.js → zh-CN-yqaDHwBx.js} +53 -12
- package/dist/npm/element.js +7 -1
- package/dist/npm/headless.js +2 -2
- package/dist/npm/index.js +2 -2
- package/dist/npm/react.js +1 -1
- package/dist/npm/svelte.js +1 -1
- package/dist/npm/types/adapters/vue.d.ts +12 -0
- package/dist/npm/types/core/auth.d.ts +1 -0
- package/dist/npm/types/core/client.d.ts +5 -1
- package/dist/npm/types/core/types.d.ts +19 -4
- package/dist/npm/vue.js +5 -1
- package/package.json +4 -1
- package/dist/npm/chunks/Checklist-CnlfeSbc.js +0 -282
- package/dist/npm/chunks/Composer-B1bBVszY.js +0 -725
- package/dist/npm/chunks/Panel-F37vIL1K.js +0 -1206
- package/dist/npm/chunks/TaskFields-fmy6_HtK.js +0 -289
- package/dist/npm/chunks/ThreadCard-D51slKST.js +0 -1113
- package/dist/npm/chunks/Viewer-BOBadqmC.js +0 -111
- package/dist/npm/chunks/card-bits-CAnzrqx3.js +0 -53
- package/dist/npm/chunks/late-words-B2bmZceJ.js +0 -12
- package/dist/npm/chunks/show-BgVC8gWY.js +0 -97
- package/dist/npm/chunks/signin-Dd1rb2BP.js +0 -75
- package/dist/npm/chunks/src-BTMy4Gyc.js +0 -208
- package/dist/npm/chunks/start-DtVlpt7p.js +0 -1878
- package/dist/npm/chunks/states-sfTbjZz8.js +0 -3
- package/dist/npm/chunks/uploads-D49mywwz.js +0 -1146
package/dist/node/cli/init.js
CHANGED
|
@@ -1,41 +1,58 @@
|
|
|
1
1
|
// `npx @commentkit/comment-kit init`: find the framework, then add the key, the build plugin and the widget.
|
|
2
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import { dirname, join, posix, resolve } from "node:path";
|
|
5
|
+
/** The Node versions the build plugins' source locations need (plugins/node-version.ts PLUGIN_NODE; a test keeps them equal). */
|
|
6
|
+
export const PLUGIN_NODE = "22.18 or 24.11";
|
|
7
|
+
export const PLUGIN_NODE_NOTE = `The build plugin's source locations need Node ${PLUGIN_NODE} or newer; on an older Node (a deploy server that builds, for example) it prints one warning and the build carries on without them.`;
|
|
8
|
+
function readJsonFile(file) {
|
|
7
9
|
try {
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
11
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null;
|
|
10
12
|
}
|
|
11
13
|
catch {
|
|
12
|
-
|
|
14
|
+
return null;
|
|
13
15
|
}
|
|
16
|
+
}
|
|
17
|
+
/** A Laravel app: `artisan` next to a composer.json that requires laravel/framework. */
|
|
18
|
+
export function isLaravel(dir) {
|
|
19
|
+
if (!existsSync(join(dir, "artisan")))
|
|
20
|
+
return false;
|
|
21
|
+
const composer = readJsonFile(join(dir, "composer.json"));
|
|
22
|
+
return Boolean(composer?.require?.["laravel/framework"] || composer?.["require-dev"]?.["laravel/framework"]);
|
|
23
|
+
}
|
|
24
|
+
function detect(dir) {
|
|
25
|
+
const pkg = readJsonFile(join(dir, "package.json"));
|
|
26
|
+
// No package.json: a plain website (or a Laravel app without a JavaScript build).
|
|
27
|
+
const deps = pkg ? { ...pkg.dependencies, ...pkg.devDependencies } : {};
|
|
14
28
|
// React Router 7 (framework mode) and Remix build with Vite but render the page from app/root: not a plain Vite app.
|
|
15
29
|
const reactRouter = deps["@react-router/dev"] || deps["@remix-run/dev"] || deps["@remix-run/react"];
|
|
16
|
-
|
|
30
|
+
// Laravel first: its apps also list vite, but keep their entries in resources/js and render pages from Blade.
|
|
31
|
+
const framework = isLaravel(dir) ? "laravel" : deps.next ? "next" : deps.astro ? "astro" : deps.nuxt ? "nuxt" : deps["@sveltejs/kit"] ? "sveltekit" : reactRouter ? "react-router" : deps.vite ? "vite" : "html";
|
|
17
32
|
return { framework, packageManager: packageManagerFor(dir), deps };
|
|
18
33
|
}
|
|
19
|
-
|
|
34
|
+
/** Lockfiles decide first: a stray pnpm-workspace.yaml next to package-lock.json is not a pnpm project. */
|
|
35
|
+
const LOCKFILES = [["package-lock.json", "npm"], ["npm-shrinkwrap.json", "npm"], ["pnpm-lock.yaml", "pnpm"], ["yarn.lock", "yarn"], ["bun.lock", "bun"], ["bun.lockb", "bun"]];
|
|
36
|
+
/** Weaker hints, used only when a folder has neither a lockfile nor "packageManager". */
|
|
37
|
+
const HINTS = [["pnpm-workspace.yaml", "pnpm"], [".yarnrc.yml", "yarn"], ["bunfig.toml", "bun"]];
|
|
20
38
|
/**
|
|
21
39
|
* The package manager of the project, or of the workspace it belongs to: the nearest folder (this one or
|
|
22
|
-
* a parent, up to the repository root or the home folder) that names one in package.json
|
|
23
|
-
* ("packageManager": "pnpm@9…") or has
|
|
40
|
+
* a parent, up to the repository root or the home folder) that has a lockfile, names one in package.json
|
|
41
|
+
* ("packageManager": "pnpm@9…") or has another hint (pnpm-workspace.yaml). In each folder a lockfile wins;
|
|
42
|
+
* with lockfiles of two managers, "packageManager" breaks the tie. npm when nothing says.
|
|
24
43
|
*/
|
|
25
44
|
export function packageManagerFor(dir, home = homedir()) {
|
|
26
45
|
let current = resolve(dir);
|
|
27
46
|
for (;;) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (lock)
|
|
38
|
-
return lock[1];
|
|
47
|
+
const declared = /^(npm|pnpm|yarn|bun)@/.exec(String(readJsonFile(join(current, "package.json"))?.packageManager ?? ""))?.[1];
|
|
48
|
+
const locks = [...new Set(LOCKFILES.filter(([file]) => existsSync(join(current, file))).map(([, pm]) => pm))];
|
|
49
|
+
if (locks.length)
|
|
50
|
+
return declared && locks.includes(declared) ? declared : locks[0];
|
|
51
|
+
if (declared)
|
|
52
|
+
return declared;
|
|
53
|
+
const hint = HINTS.find(([file]) => existsSync(join(current, file)));
|
|
54
|
+
if (hint)
|
|
55
|
+
return hint[1];
|
|
39
56
|
const parent = dirname(current);
|
|
40
57
|
if (parent === current || current === resolve(home) || existsSync(join(current, ".git")))
|
|
41
58
|
return "npm";
|
|
@@ -118,6 +135,10 @@ export function addToArray(source, key, call, importLine) {
|
|
|
118
135
|
return null;
|
|
119
136
|
const at = hits[0].index;
|
|
120
137
|
const open = source.indexOf("[", at) + 1;
|
|
138
|
+
// A list written one item per line gets commentkit() on its own line, indented like the first item.
|
|
139
|
+
const lead = /^[ \t]*\r?\n[ \t]*(?=[^\s\]])/.exec(source.slice(open))?.[0];
|
|
140
|
+
if (lead)
|
|
141
|
+
return withImport(`${source.slice(0, open)}${lead}${call},${source.slice(open)}`, importLine);
|
|
121
142
|
let out = `${source.slice(0, open)}${call}, ${source.slice(open).replace(/^\s*(?=\])/, "")}`;
|
|
122
143
|
out = out.replace(new RegExp(`\\[${call.replace(/[()]/g, "\\$&")}, \\]`), `[${call}]`);
|
|
123
144
|
return withImport(out, importLine);
|
|
@@ -147,6 +168,221 @@ function withImport(source, importLine) {
|
|
|
147
168
|
}
|
|
148
169
|
/** The widget is already set up in this file. */
|
|
149
170
|
const HAS_WIDGET = /@commentkit\/comment-kit|commentkit\.dev\/cdn\/|loader\.js["'][^>]*data-project/;
|
|
171
|
+
/** The CDN script tag (any version folder), as a page or template writes it. */
|
|
172
|
+
const LOADER_TAG = /<script\b[^>]*\bsrc\s*=\s*["']?[^"'\s>]*\/cdn\/[^"'\s>/]+\/loader\.js[^>]*>/i;
|
|
173
|
+
const VITE_CONFIGS = ["vite.config.ts", "vite.config.mts", "vite.config.js", "vite.config.mjs"];
|
|
174
|
+
const SCRIPT_FILE = /\.(?:[mc]?[jt]sx?)$/;
|
|
175
|
+
const LARAVEL_ENTRIES = ["resources/js/app.ts", "resources/js/app.tsx", "resources/js/app.js", "resources/js/app.jsx"];
|
|
176
|
+
/** The string literals between the bracket at `open` and the one that closes it. */
|
|
177
|
+
function literalsIn(source, mask, open) {
|
|
178
|
+
const close = source[open] === "[" || source[open] === "{" || source[open] === "(" ? closing(source, mask, open) : -1;
|
|
179
|
+
if (close < 0)
|
|
180
|
+
return [];
|
|
181
|
+
return [...source.slice(open, close).matchAll(/(["'`])((?:(?!\1)[^\\\n$])*)\1/g)].map((m) => m[2]);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Script entries a Vite config names: `input: "…"`, `input: [...]` or `input: { name: "…" }` (Rollup and
|
|
185
|
+
* laravel-vite-plugin), and laravel([...]) / laravel("…") shorthand. Stylesheets and other files are left out.
|
|
186
|
+
*/
|
|
187
|
+
export function viteInputs(source) {
|
|
188
|
+
const mask = codeMask(source);
|
|
189
|
+
const found = [];
|
|
190
|
+
for (const m of source.matchAll(/\b(?:input\s*:|laravel\s*\()\s*/g)) {
|
|
191
|
+
if (!mask[m.index])
|
|
192
|
+
continue;
|
|
193
|
+
const at = m.index + m[0].length;
|
|
194
|
+
const c = source[at];
|
|
195
|
+
if (c === '"' || c === "'" || c === "`") {
|
|
196
|
+
const end = source.indexOf(c, at + 1);
|
|
197
|
+
if (end > at)
|
|
198
|
+
found.push(source.slice(at + 1, end));
|
|
199
|
+
}
|
|
200
|
+
else if (c === "[" || (c === "{" && m[0].startsWith("input"))) {
|
|
201
|
+
found.push(...literalsIn(source, mask, at));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return [...new Set(found.map((f) => f.replace(/^\.?\//, "")).filter((f) => SCRIPT_FILE.test(f) && !f.includes("..")))];
|
|
205
|
+
}
|
|
206
|
+
/** The script files a Vite config's `input` names that exist in `dir`. */
|
|
207
|
+
function viteConfigEntries(dir) {
|
|
208
|
+
const config = first(dir, VITE_CONFIGS);
|
|
209
|
+
if (!config)
|
|
210
|
+
return [];
|
|
211
|
+
return viteInputs(readFileSync(join(dir, config), "utf8")).filter((f) => existsSync(join(dir, f)));
|
|
212
|
+
}
|
|
213
|
+
/** Laravel's JavaScript entries: what vite.config's input names, then resources/js/app.{ts,tsx,js,jsx}. */
|
|
214
|
+
export function laravelEntries(dir) {
|
|
215
|
+
return [...new Set([...viteConfigEntries(dir), ...LARAVEL_ENTRIES.filter((f) => existsSync(join(dir, f)))])];
|
|
216
|
+
}
|
|
217
|
+
/** The import specifiers of a module (static imports, side-effect imports and import()). */
|
|
218
|
+
function importsOf(source) {
|
|
219
|
+
const mask = codeMask(source);
|
|
220
|
+
const found = [];
|
|
221
|
+
for (const m of source.matchAll(/\b(?:import|export)\s*(?:[\w*${}\s,]+?\s*from\s*)?(["'])([^"'\n]+)\1|\bimport\s*\(\s*(["'])([^"'\n]+)\3\s*\)/g)) {
|
|
222
|
+
if (mask[m.index])
|
|
223
|
+
found.push(m[2] ?? m[4]);
|
|
224
|
+
}
|
|
225
|
+
return found;
|
|
226
|
+
}
|
|
227
|
+
/** A relative or aliased import (`./x`, `@/x` and `~/x` meaning resources/js in Laravel) as a file in `dir`, or null. */
|
|
228
|
+
function resolveImport(dir, from, spec, laravel) {
|
|
229
|
+
let base;
|
|
230
|
+
if (spec.startsWith("./") || spec.startsWith("../"))
|
|
231
|
+
base = posix.join(posix.dirname(from), spec);
|
|
232
|
+
else if (laravel && /^[@~]\//.test(spec))
|
|
233
|
+
base = posix.join("resources/js", spec.slice(2));
|
|
234
|
+
else
|
|
235
|
+
return null;
|
|
236
|
+
if (base.startsWith(".."))
|
|
237
|
+
return null;
|
|
238
|
+
const candidates = [base, ...[".ts", ".tsx", ".js", ".jsx", ".mjs", ".mts"].map((e) => base + e), ...["ts", "tsx", "js", "jsx"].map((e) => `${base}/index.${e}`)];
|
|
239
|
+
return candidates.find((f) => SCRIPT_FILE.test(f) && existsSync(join(dir, f)) && statSync(join(dir, f)).isFile()) ?? null;
|
|
240
|
+
}
|
|
241
|
+
const KEY_LITERAL = /\bpk_(?:live|test)_[a-z0-9]{20,40}\b/;
|
|
242
|
+
/** Is the widget set up in one of `entries`, or in a file one of them imports directly? */
|
|
243
|
+
export function widgetInEntries(dir, entries, laravel = false) {
|
|
244
|
+
const read = (file) => readFileSync(join(dir, file), "utf8");
|
|
245
|
+
const place = (file, text, via) => ({ file, ...(via ? { via } : {}), ...(KEY_LITERAL.test(text) ? { key: KEY_LITERAL.exec(text)[0] } : {}) });
|
|
246
|
+
for (const entry of entries) {
|
|
247
|
+
const text = read(entry);
|
|
248
|
+
if (HAS_WIDGET.test(text))
|
|
249
|
+
return place(entry, text);
|
|
250
|
+
}
|
|
251
|
+
for (const entry of entries) {
|
|
252
|
+
for (const spec of importsOf(read(entry))) {
|
|
253
|
+
const file = resolveImport(dir, entry, spec, laravel);
|
|
254
|
+
if (!file)
|
|
255
|
+
continue;
|
|
256
|
+
const text = read(file);
|
|
257
|
+
if (HAS_WIDGET.test(text))
|
|
258
|
+
return place(file, text, entry);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
const MAX_TEMPLATES = 3000;
|
|
264
|
+
/** Blade templates under resources/views (at most 3000), as paths relative to `dir`. */
|
|
265
|
+
export function bladeFiles(dir) {
|
|
266
|
+
const out = [];
|
|
267
|
+
const walk = (rel) => {
|
|
268
|
+
let names;
|
|
269
|
+
try {
|
|
270
|
+
names = readdirSync(join(dir, rel)).sort();
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
for (const name of names) {
|
|
276
|
+
if (out.length >= MAX_TEMPLATES)
|
|
277
|
+
return;
|
|
278
|
+
const path = `${rel}/${name}`;
|
|
279
|
+
let isDir = false;
|
|
280
|
+
try {
|
|
281
|
+
isDir = statSync(join(dir, path)).isDirectory();
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (isDir)
|
|
287
|
+
walk(path);
|
|
288
|
+
else if (name.endsWith(".blade.php"))
|
|
289
|
+
out.push(path);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
walk("resources/views");
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
295
|
+
/** HTML pages served as they are from public/ (top level only). */
|
|
296
|
+
function publicHtml(dir) {
|
|
297
|
+
try {
|
|
298
|
+
return readdirSync(join(dir, "public")).filter((f) => f.endsWith(".html")).sort().map((f) => `public/${f}`);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
return [];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** Blade templates and public/*.html files that hold the CommentKit script tag. */
|
|
305
|
+
export function templatesWithTag(dir) {
|
|
306
|
+
const found = [];
|
|
307
|
+
for (const file of [...bladeFiles(dir), ...publicHtml(dir)]) {
|
|
308
|
+
const text = readFileSync(join(dir, file), "utf8");
|
|
309
|
+
const tag = LOADER_TAG.exec(text)?.[0];
|
|
310
|
+
if (!tag)
|
|
311
|
+
continue;
|
|
312
|
+
const key = /\bdata-project\s*=\s*["']?(pk_(?:live|test)_[a-z0-9]{20,40})\b/.exec(tag)?.[1] ?? /[?&]project=(pk_(?:live|test)_[a-z0-9]{20,40})\b/.exec(tag)?.[1];
|
|
313
|
+
found.push({ file, ...(key ? { key } : {}) });
|
|
314
|
+
}
|
|
315
|
+
return found;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Where the widget is set up, for the frameworks whose entry init can find more than one way: a Vite app's
|
|
319
|
+
* entry (index.html's module script or the config's `input`), a Laravel app's entries, a file one of them
|
|
320
|
+
* imports directly, or (Laravel) the script tag in a Blade layout or public/*.html. Null when none is found.
|
|
321
|
+
*/
|
|
322
|
+
export function findWidget(dir, framework) {
|
|
323
|
+
if (framework === "laravel")
|
|
324
|
+
return widgetInEntries(dir, laravelEntries(dir), true) ?? templatesWithTag(dir)[0] ?? null;
|
|
325
|
+
if (framework === "vite")
|
|
326
|
+
return widgetInEntries(dir, viteEntries(dir));
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
/** A Vite app's entries: index.html's module script, the config's `input`, then src/main.* and src/index.*. */
|
|
330
|
+
function viteEntries(dir) {
|
|
331
|
+
const out = [];
|
|
332
|
+
if (existsSync(join(dir, "index.html"))) {
|
|
333
|
+
const src = /<script\b[^>]*\btype=["']module["'][^>]*\bsrc=["']\/?([^"'?#]+\.(?:[mc]?[jt]sx?))["']/i.exec(readFileSync(join(dir, "index.html"), "utf8"))?.[1];
|
|
334
|
+
if (src && !src.includes("..") && existsSync(join(dir, src)))
|
|
335
|
+
out.push(src);
|
|
336
|
+
}
|
|
337
|
+
out.push(...viteConfigEntries(dir));
|
|
338
|
+
out.push(...["src/main.ts", "src/main.tsx", "src/main.js", "src/main.jsx", "src/index.ts", "src/index.tsx", "src/index.js", "src/index.jsx"].filter((f) => existsSync(join(dir, f))));
|
|
339
|
+
return [...new Set(out)];
|
|
340
|
+
}
|
|
341
|
+
/** The quote character most of a file's imports use (Laravel's Prettier setup uses single quotes). */
|
|
342
|
+
function quoteOf(source) {
|
|
343
|
+
let single = (source.match(/\bfrom\s*'|\bimport\s*'/g) ?? []).length;
|
|
344
|
+
let double = (source.match(/\bfrom\s*"|\bimport\s*"/g) ?? []).length;
|
|
345
|
+
if (!single && !double) {
|
|
346
|
+
// No imports yet: the quotes the file's strings use.
|
|
347
|
+
single = (source.match(/'/g) ?? []).length;
|
|
348
|
+
double = (source.match(/"/g) ?? []).length;
|
|
349
|
+
}
|
|
350
|
+
return single > double ? "'" : '"';
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Add CommentKit to a Laravel JavaScript entry: the import, then init() with the key as the default, so a
|
|
354
|
+
* server builds it without a new environment variable (VITE_COMMENTKIT_KEY still overrides it). `vite`
|
|
355
|
+
* false (Laravel Mix) has no import.meta.env, so the key is passed as it is.
|
|
356
|
+
*/
|
|
357
|
+
export function laravelInit(source, key, vite = true) {
|
|
358
|
+
const q = quoteOf(source);
|
|
359
|
+
const mask = codeMask(source);
|
|
360
|
+
// A name `init` the file already has (its own function, another library's import) keeps its meaning.
|
|
361
|
+
const taken = [...source.matchAll(/\binit\b/g)].some((m) => mask[m.index]);
|
|
362
|
+
const name = taken ? "initCommentKit" : "init";
|
|
363
|
+
const semi = /;\s*$/m.test(source) || source.trim() === "" ? ";" : "";
|
|
364
|
+
const importLine = `import { ${taken ? "init as initCommentKit" : "init"} } from ${q}@commentkit/comment-kit${q}${semi}`;
|
|
365
|
+
const project = vite ? `import.meta.env.VITE_COMMENTKIT_KEY || ${q}${key}${q}` : `${q}${key}${q}`;
|
|
366
|
+
return `${withImport(source, importLine).replace(/\s*$/, "\n")}\n${name}({ project: ${project} })${semi}\n`;
|
|
367
|
+
}
|
|
368
|
+
/** `@vite(...)` directives in a template that list only stylesheets: those pages get no JavaScript entry. */
|
|
369
|
+
export function cssOnlyVite(text) {
|
|
370
|
+
const lists = [];
|
|
371
|
+
for (const m of text.matchAll(/@vite\s*\(([^)]*)\)/g)) {
|
|
372
|
+
const files = [...m[1].matchAll(/(["'])([^"'\n]+)\1/g)].map((f) => f[2]);
|
|
373
|
+
if (files.length && !files.some((f) => SCRIPT_FILE.test(f) || f.includes("{$")))
|
|
374
|
+
lists.push(files);
|
|
375
|
+
}
|
|
376
|
+
return lists;
|
|
377
|
+
}
|
|
378
|
+
/** Blade files that look like page layouts (they close </body>), mail, PDF and vendor templates left out. */
|
|
379
|
+
export function bladeLayouts(dir, files = bladeFiles(dir)) {
|
|
380
|
+
const skip = /\/(?:mail|emails?|pdfs?|vendor|notifications|errors)\//;
|
|
381
|
+
const layouts = files.filter((f) => !skip.test(f) && /<\/body>/i.test(readFileSync(join(dir, f), "utf8")));
|
|
382
|
+
const rank = (f) => (/\/layouts?\//.test(f) ? 0 : /\/(?:app|welcome)\.blade\.php$/.test(f) ? 1 : 2);
|
|
383
|
+
return layouts.sort((a, b) => rank(a) - rank(b) || a.localeCompare(b));
|
|
384
|
+
}
|
|
385
|
+
const loaderTag = (key) => `<script src="https://commentkit.dev/cdn/v1/loader.js" data-project="${key}" defer></script>`;
|
|
150
386
|
/** Insert `markup` on its own line just before the last </body>, indented like the body's content. */
|
|
151
387
|
function beforeBodyEnd(source, markup) {
|
|
152
388
|
const at = source.lastIndexOf("</body>");
|
|
@@ -192,17 +428,25 @@ export function entryEdit(dir, framework, key, svelteMajor = 5) {
|
|
|
192
428
|
});
|
|
193
429
|
}
|
|
194
430
|
case "vite": {
|
|
195
|
-
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
file = src;
|
|
200
|
-
}
|
|
201
|
-
file ??= first(dir, ["src/main.ts", "src/main.tsx", "src/main.js", "src/main.jsx", "src/index.ts", "src/index.tsx", "src/index.js", "src/index.jsx"]);
|
|
431
|
+
const entries = viteEntries(dir);
|
|
432
|
+
if (widgetInEntries(dir, entries))
|
|
433
|
+
return "done";
|
|
434
|
+
const file = entries[0];
|
|
202
435
|
if (!file)
|
|
203
436
|
return null;
|
|
204
437
|
return edit(file, (s) => `${withImport(s, 'import { init } from "@commentkit/comment-kit";').replace(/\s*$/, "\n")}\ninit({ project: import.meta.env.VITE_COMMENTKIT_KEY });\n`);
|
|
205
438
|
}
|
|
439
|
+
case "laravel": {
|
|
440
|
+
if (findWidget(dir, "laravel"))
|
|
441
|
+
return "done";
|
|
442
|
+
const entries = laravelEntries(dir);
|
|
443
|
+
// The app's main entry (loaded on every Inertia page and by most layouts), else the first script input.
|
|
444
|
+
const file = entries.find((f) => LARAVEL_ENTRIES.includes(f)) ?? entries[0];
|
|
445
|
+
if (!file)
|
|
446
|
+
return null;
|
|
447
|
+
const vite = Boolean(first(dir, VITE_CONFIGS)) || !existsSync(join(dir, "webpack.mix.js"));
|
|
448
|
+
return edit(file, (s) => laravelInit(s, key, vite));
|
|
449
|
+
}
|
|
206
450
|
case "sveltekit": {
|
|
207
451
|
const file = "src/routes/+layout.svelte";
|
|
208
452
|
const use = "<svelte:body use:commentkit={{ project: import.meta.env.VITE_COMMENTKIT_KEY }} />";
|
|
@@ -293,13 +537,24 @@ export function wrapNextConfig(source) {
|
|
|
293
537
|
}
|
|
294
538
|
export function plan(dir, key) {
|
|
295
539
|
const detected = detect(dir);
|
|
296
|
-
return { ...basePlan(dir, key, detected), entry: entryEdit(dir, detected.framework, key, major(detected.deps.svelte) ?? 5) };
|
|
540
|
+
return { ...basePlan(dir, key, detected), entry: entryEdit(dir, detected.framework, key, major(detected.deps.svelte) ?? 5), widget: findWidget(dir, detected.framework) };
|
|
541
|
+
}
|
|
542
|
+
/** "resources/js/commentkit.ts, imported by resources/js/app.ts" */
|
|
543
|
+
export function describePlace(place) {
|
|
544
|
+
return place.via ? `${place.file}, imported by ${place.via}` : place.file;
|
|
297
545
|
}
|
|
298
546
|
const VITE_PLUGIN = 'import commentkit from "@commentkit/comment-kit/vite";\n // and in the config object: plugins: [commentkit(), ...your other plugins]';
|
|
547
|
+
/** vite-plus configs that build their plugin list in lazyPlugins(() => [...]): init does not edit inside the callback. */
|
|
548
|
+
const VITE_LAZY_PLUGIN = 'import commentkit from "@commentkit/comment-kit/vite";\n // and first in the list lazyPlugins returns: plugins: lazyPlugins(() => [commentkit(), ...your other plugins])';
|
|
549
|
+
/** What to add to a Vite config by hand, in the shape it has. */
|
|
550
|
+
export function vitePluginSnippet(source) {
|
|
551
|
+
return /\blazyPlugins\s*\(/.test(source) ? VITE_LAZY_PLUGIN : VITE_PLUGIN;
|
|
552
|
+
}
|
|
299
553
|
function basePlan(dir, key, { framework, packageManager, deps }) {
|
|
300
554
|
const add = packageManager === "npm" ? ["npm", "install"] : [packageManager, "add"];
|
|
301
555
|
// One package: the widget and the build plugins (@commentkit/comment-kit/vite, /next, /astro).
|
|
302
|
-
|
|
556
|
+
// Nothing to install for a plain website, or when package.json already lists the package.
|
|
557
|
+
const install = framework === "html" || deps["@commentkit/comment-kit"] ? [] : [[...add, "@commentkit/comment-kit"]];
|
|
303
558
|
const read = (file) => (file ? readFileSync(join(dir, file), "utf8") : "");
|
|
304
559
|
switch (framework) {
|
|
305
560
|
case "next": {
|
|
@@ -308,7 +563,7 @@ function basePlan(dir, key, { framework, packageManager, deps }) {
|
|
|
308
563
|
framework, packageManager, label: "Next.js", envFile: ".env.local", envVar: "NEXT_PUBLIC_COMMENTKIT_KEY", configFile,
|
|
309
564
|
configEdit: configFile ? wrapNextConfig(read(configFile)) : null,
|
|
310
565
|
configSnippet: 'import { withCommentKit } from "@commentkit/comment-kit/next";\n // and wrap the default export: export default withCommentKit(nextConfig);',
|
|
311
|
-
manual: configFile ? [] : ['Create next.config.mjs with: import { withCommentKit } from "@commentkit/comment-kit/next"; export default withCommentKit({});'],
|
|
566
|
+
notes: [], manual: configFile ? [] : ['Create next.config.mjs with: import { withCommentKit } from "@commentkit/comment-kit/next"; export default withCommentKit({});'],
|
|
312
567
|
install,
|
|
313
568
|
snippet: 'In app/layout.tsx:\n import { CommentKit } from "@commentkit/comment-kit/react";\n <CommentKit project={process.env.NEXT_PUBLIC_COMMENTKIT_KEY!} />',
|
|
314
569
|
};
|
|
@@ -319,13 +574,14 @@ function basePlan(dir, key, { framework, packageManager, deps }) {
|
|
|
319
574
|
framework, packageManager, label: "Astro", envFile: ".env", envVar: "PUBLIC_COMMENTKIT_KEY", configFile,
|
|
320
575
|
configEdit: configFile ? addToArray(read(configFile), "integrations", "commentkit()", 'import commentkit from "@commentkit/comment-kit/astro";') : null,
|
|
321
576
|
configSnippet: 'import commentkit from "@commentkit/comment-kit/astro";\n // and in defineConfig: integrations: [commentkit(), ...your other integrations]',
|
|
322
|
-
manual: [], install,
|
|
577
|
+
manual: [], notes: [], install,
|
|
323
578
|
snippet: 'In your base layout:\n <script>\n import { init } from "@commentkit/comment-kit";\n init({ project: import.meta.env.PUBLIC_COMMENTKIT_KEY });\n </script>',
|
|
324
579
|
};
|
|
325
580
|
}
|
|
326
581
|
case "nuxt":
|
|
327
582
|
return {
|
|
328
583
|
framework, packageManager, label: "Nuxt", envFile: ".env", envVar: "NUXT_PUBLIC_COMMENTKIT_KEY", configFile: first(dir, ["nuxt.config.ts", "nuxt.config.js"]), configEdit: null, configSnippet: "",
|
|
584
|
+
notes: [],
|
|
329
585
|
manual: [
|
|
330
586
|
'In nuxt.config.ts add: import commentkit from "@commentkit/comment-kit/vite"; and vite: { plugins: [commentkit()] }',
|
|
331
587
|
'In nuxt.config.ts also add: runtimeConfig: { public: { commentkitKey: "" } }',
|
|
@@ -345,19 +601,77 @@ function basePlan(dir, key, { framework, packageManager, deps }) {
|
|
|
345
601
|
return {
|
|
346
602
|
framework, packageManager, label: framework === "sveltekit" ? "SvelteKit" : framework === "react-router" ? (remix ? "Remix" : "React Router") : "Vite", envFile: ".env.local", envVar: "VITE_COMMENTKIT_KEY", configFile,
|
|
347
603
|
configEdit: configFile ? addToArray(read(configFile), "plugins", "commentkit()", 'import commentkit from "@commentkit/comment-kit/vite";') : null,
|
|
348
|
-
configSnippet:
|
|
604
|
+
configSnippet: vitePluginSnippet(read(configFile)),
|
|
605
|
+
notes: [],
|
|
349
606
|
manual: configFile || framework !== "react-router" ? [] : ['No vite.config found. If the app builds with Vite, add to its config: import commentkit from "@commentkit/comment-kit/vite"; and plugins: [commentkit(), ...]'],
|
|
350
607
|
install,
|
|
351
608
|
snippet: snippets[framework],
|
|
352
609
|
};
|
|
353
610
|
}
|
|
611
|
+
case "laravel":
|
|
612
|
+
return laravelPlan(dir, key, packageManager, deps, install);
|
|
354
613
|
default:
|
|
355
614
|
return {
|
|
356
|
-
framework, packageManager, label: "a plain website", envFile: "", envVar: "", configFile: null, configEdit: null, configSnippet: "", manual: [], install: [],
|
|
615
|
+
framework, packageManager, label: "a plain website", envFile: "", envVar: "", configFile: null, configEdit: null, configSnippet: "", manual: [], notes: [], install: [],
|
|
357
616
|
snippet: `Add before </body> on every page:\n <script src="https://commentkit.dev/cdn/v1/loader.js" data-project="${key}" defer></script>`,
|
|
358
617
|
};
|
|
359
618
|
}
|
|
360
619
|
}
|
|
620
|
+
/** "Inertia React", "Livewire" or "Blade": what renders a Laravel app's pages. */
|
|
621
|
+
function laravelFlavour(dir, deps) {
|
|
622
|
+
if (deps["@inertiajs/react"])
|
|
623
|
+
return "Inertia React";
|
|
624
|
+
if (deps["@inertiajs/vue3"] || deps["@inertiajs/vue"])
|
|
625
|
+
return "Inertia Vue";
|
|
626
|
+
if (deps["@inertiajs/svelte"])
|
|
627
|
+
return "Inertia Svelte";
|
|
628
|
+
const composer = readJsonFile(join(dir, "composer.json"));
|
|
629
|
+
return composer?.require?.["livewire/livewire"] || composer?.require?.["livewire/flux"] ? "Livewire" : "Blade";
|
|
630
|
+
}
|
|
631
|
+
function laravelPlan(dir, key, packageManager, deps, install) {
|
|
632
|
+
const configFile = first(dir, VITE_CONFIGS);
|
|
633
|
+
const config = configFile ? readFileSync(join(dir, configFile), "utf8") : "";
|
|
634
|
+
const entries = laravelEntries(dir);
|
|
635
|
+
const entry = entries.find((f) => LARAVEL_ENTRIES.includes(f)) ?? entries[0] ?? null;
|
|
636
|
+
const templates = bladeFiles(dir);
|
|
637
|
+
const notes = [];
|
|
638
|
+
const manual = [];
|
|
639
|
+
const composer = readJsonFile(join(dir, "composer.json"));
|
|
640
|
+
const placed = findWidget(dir, "laravel");
|
|
641
|
+
if (entry && !placed) {
|
|
642
|
+
notes.push(`The key goes into ${entry} as the default value. Publishable keys are public by design, so it is safe to commit, and your server needs no new environment variable. To use another key in one place (pk_test_… on staging), set VITE_COMMENTKIT_KEY there before the build.`);
|
|
643
|
+
}
|
|
644
|
+
if (entry) {
|
|
645
|
+
// Layouts whose @vite(...) lists only stylesheets: their pages never load the entry, so the widget is not there.
|
|
646
|
+
for (const file of templates) {
|
|
647
|
+
for (const list of cssOnlyVite(readFileSync(join(dir, file), "utf8"))) {
|
|
648
|
+
const quoted = [...list, entry].map((f) => `'${f}'`).join(", ");
|
|
649
|
+
manual.push(`${file} loads only stylesheets with @vite, so its pages will not show the widget. To show it there, change that line to: @vite([${quoted}])`);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
if (composer?.require?.["filament/filament"]) {
|
|
654
|
+
notes.push(`Filament admin panels do not load ${entry ?? "your app's JavaScript entry"}, so the widget does not show in them.`);
|
|
655
|
+
}
|
|
656
|
+
// No JavaScript entry at all: pages are plain Blade. init does not edit Blade; it prints the line and where it goes.
|
|
657
|
+
const layouts = bladeLayouts(dir, templates).slice(0, 5);
|
|
658
|
+
const where = layouts.length ? `\nLayouts that look like the place for it: ${layouts.join(", ")}` : "";
|
|
659
|
+
const snippet = entry
|
|
660
|
+
? `In ${entry}:\n import { init } from "@commentkit/comment-kit";\n init({ project: import.meta.env.VITE_COMMENTKIT_KEY || "${key}" });`
|
|
661
|
+
: `Your pages load no JavaScript entry, so add the script tag before </body> in your layout (init does not edit Blade files):\n ${loaderTag(key)}${where}`;
|
|
662
|
+
return {
|
|
663
|
+
framework: "laravel", packageManager, label: `Laravel + ${laravelFlavour(dir, deps)}`,
|
|
664
|
+
// The key is written into the entry, not an env file: .env.local is git-ignored (a server that builds on deploy has none) and Laravel itself never reads it.
|
|
665
|
+
envFile: "", envVar: "VITE_COMMENTKIT_KEY",
|
|
666
|
+
configFile: entry ? configFile : null,
|
|
667
|
+
configEdit: entry && configFile ? addToArray(config, "plugins", "commentkit()", `import commentkit from ${quoteOf(config)}@commentkit/comment-kit/vite${quoteOf(config)};`) : null,
|
|
668
|
+
configSnippet: vitePluginSnippet(config),
|
|
669
|
+
manual, notes,
|
|
670
|
+
// Plain Blade pages use the script tag: nothing to install.
|
|
671
|
+
install: entry ? install : [],
|
|
672
|
+
snippet,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
361
675
|
/** Set KEY=value in an env file's text, replacing an existing line. */
|
|
362
676
|
export function setEnv(text, name, value) {
|
|
363
677
|
const line = `${name}=${value}`;
|