@nextanalytica/wizard 0.0.0-stage → 1.0.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/README.md +25 -2
- package/dist/check.js +30 -0
- package/dist/cli.js +88 -0
- package/dist/plan.js +231 -0
- package/package.json +27 -4
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nextanalytica/wizard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Adds the [nextanalytica](https://nextanalytica.app) tag to a JavaScript app. Run it in the app's folder with your
|
|
4
|
+
project token (in nextanalytica, under Tracking):
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npx -y @nextanalytica/wizard@latest na_…
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
It finds the framework (Next.js, React, Remix and React Router, Vue, Nuxt, SvelteKit, Astro, Gatsby) and the package
|
|
11
|
+
manager (from the lockfile), shows the change to one file, and on a yes writes it and installs `@nextanalytica/tag`.
|
|
12
|
+
When a file doesn't look as expected it says what to add by hand instead.
|
|
13
|
+
|
|
14
|
+
- `--yes` applies without asking
|
|
15
|
+
- `--no-install` changes the file only
|
|
16
|
+
- `--dir <path>` runs on another folder
|
|
17
|
+
|
|
18
|
+
## Check the key
|
|
19
|
+
|
|
20
|
+
No browser? Prove the key works from the terminal. This changes no file and needs no `package.json`:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npx -y @nextanalytica/wizard@latest check na_…
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
It exits 0 when nextanalytica knows the key and 1 when it doesn't (or can't be reached).
|
package/dist/check.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** `check na_…`: asks the edge whether it knows this tag key. Touches no file and needs no package.json. */
|
|
2
|
+
import { validKey } from "./plan.js";
|
|
3
|
+
export const DEFAULT_HOST = "https://t.nextanalytica.app";
|
|
4
|
+
const NO_REACH = "Couldn't reach nextanalytica.";
|
|
5
|
+
const fail = (message) => ({ code: 1, message });
|
|
6
|
+
export async function check(key, o = {}) {
|
|
7
|
+
if (!validKey(key))
|
|
8
|
+
return fail("That doesn't look like a nextanalytica key (na_…).");
|
|
9
|
+
const host = (o.host ?? DEFAULT_HOST).replace(/\/+$/, "");
|
|
10
|
+
if (!/^https?:\/\//i.test(host))
|
|
11
|
+
return fail("--api-host must be an http(s) URL.");
|
|
12
|
+
try {
|
|
13
|
+
const r = await (o.fetch ?? fetch)(`${host}/check`, {
|
|
14
|
+
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ k: key }), signal: AbortSignal.timeout(10000),
|
|
15
|
+
});
|
|
16
|
+
if (r.status === 429)
|
|
17
|
+
return fail("Couldn't check right now: try again in a minute.");
|
|
18
|
+
if (!r.ok)
|
|
19
|
+
return fail(NO_REACH);
|
|
20
|
+
const known = (await r.json())?.known;
|
|
21
|
+
if (known === true)
|
|
22
|
+
return { code: 0, message: "Installed: nextanalytica knows this key. Open the app in a browser once to see it seen." };
|
|
23
|
+
if (known === false)
|
|
24
|
+
return fail("Unknown key: copy it again from get_tag.");
|
|
25
|
+
return fail(NO_REACH);
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return fail(NO_REACH);
|
|
29
|
+
}
|
|
30
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* `npx -y @nextanalytica/wizard@latest na_…` in the app's folder (BACKLOG E3.4): finds the framework and the package
|
|
4
|
+
* manager, shows the change to one file, and on a yes writes it and installs @nextanalytica/tag.
|
|
5
|
+
* --yes apply without asking (CI, or an agent that already showed the change)
|
|
6
|
+
* --no-install change the file only
|
|
7
|
+
* --dir <path> the app's folder (default: here)
|
|
8
|
+
* `check na_…` only asks nextanalytica whether it knows the key (no folder, no change); --api-host <url> overrides the host.
|
|
9
|
+
*/
|
|
10
|
+
import { spawnSync } from "node:child_process";
|
|
11
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
import { createInterface } from "node:readline/promises";
|
|
14
|
+
import { check } from "./check.js";
|
|
15
|
+
import { diff, installCommand, packageManager, plan, validKey } from "./plan.js";
|
|
16
|
+
const args = process.argv.slice(2);
|
|
17
|
+
const flag = (f) => args.includes(f);
|
|
18
|
+
const opt = (f) => { const i = args.indexOf(f); return i >= 0 ? args[i + 1] : undefined; };
|
|
19
|
+
const dir = resolve(opt("--dir") ?? ".");
|
|
20
|
+
const say = (s = "") => process.stdout.write(s + "\n");
|
|
21
|
+
async function ask(q) {
|
|
22
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
23
|
+
try {
|
|
24
|
+
return (await rl.question(q)).trim();
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
rl.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function main() {
|
|
31
|
+
if (args[0] === "check") {
|
|
32
|
+
const r = await check(args.slice(1).find((a, i) => !a.startsWith("--") && args[i] !== "--api-host") ?? "", { host: opt("--api-host") });
|
|
33
|
+
say(r.message);
|
|
34
|
+
return r.code;
|
|
35
|
+
}
|
|
36
|
+
const pkgPath = join(dir, "package.json");
|
|
37
|
+
if (!existsSync(pkgPath)) {
|
|
38
|
+
say(`No package.json in ${dir}. Run this in your app's folder, or add the script line to your pages by hand.`);
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
// the token: the first word that isn't a flag or a flag's value
|
|
42
|
+
const positional = args.filter((a, i) => !a.startsWith("--") && !["--dir", "--project"].includes(args[i - 1] ?? ""));
|
|
43
|
+
let key = opt("--project") ?? positional[0] ?? "";
|
|
44
|
+
if (!key && process.stdin.isTTY)
|
|
45
|
+
key = await ask("Your project token (Tracking, in nextanalytica; it starts with na_): ");
|
|
46
|
+
if (!validKey(key)) {
|
|
47
|
+
say("That isn't a project token. It starts with na_ and is in nextanalytica, under Tracking.");
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
const app = {
|
|
51
|
+
pkg: JSON.parse(readFileSync(pkgPath, "utf8")),
|
|
52
|
+
has: p => existsSync(join(dir, p)),
|
|
53
|
+
read: p => (existsSync(join(dir, p)) ? readFileSync(join(dir, p), "utf8") : null),
|
|
54
|
+
list: d => (existsSync(join(dir, d)) ? readdirSync(join(dir, d)) : []),
|
|
55
|
+
};
|
|
56
|
+
const p = plan(app, key), pm = packageManager(app);
|
|
57
|
+
if (!p.ok) {
|
|
58
|
+
say(p.why);
|
|
59
|
+
if (p.byHand)
|
|
60
|
+
say(`\nBy hand: ${p.byHand}`);
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
if (p.already) {
|
|
64
|
+
say(`${p.file} already has the tag. Nothing to do.`);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
say(`Found ${p.framework}, ${pm}. This is the change to ${p.file}${p.before === null ? " (a new file)" : ""}:\n`);
|
|
68
|
+
say(diff(p.before, p.after));
|
|
69
|
+
const install = !flag("--no-install"), [cmd, cmdArgs] = installCommand(pm);
|
|
70
|
+
say(`\nThen: ${install ? `${cmd} ${cmdArgs.join(" ")}` : "no install (--no-install)"}`);
|
|
71
|
+
if (!flag("--yes") && !/^y(es)?$/i.test(await ask("\nApply it? [y/N] "))) {
|
|
72
|
+
say("Nothing changed.");
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
mkdirSync(dirname(join(dir, p.file)), { recursive: true });
|
|
76
|
+
writeFileSync(join(dir, p.file), p.after);
|
|
77
|
+
say(`Changed ${p.file}.`);
|
|
78
|
+
if (install) {
|
|
79
|
+
const r = spawnSync(cmd, cmdArgs, { cwd: dir, stdio: "inherit", shell: process.platform === "win32" });
|
|
80
|
+
if (r.status !== 0) {
|
|
81
|
+
say(`\nThe install didn't finish. Run it yourself: ${cmd} ${cmdArgs.join(" ")}`);
|
|
82
|
+
return 1;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
say("\nDone. Deploy your site; nextanalytica says when the tag is seen (Tracking).");
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
main().then(code => { process.exitCode = code; }, e => { say(`The wizard stopped: ${e.message}`); process.exitCode = 1; });
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
export const PACKAGE = "@nextanalytica/tag";
|
|
2
|
+
export const validKey = (k) => /^na_[A-Za-z0-9_-]{8,64}$/.test(k);
|
|
3
|
+
export function packageManager(app) {
|
|
4
|
+
if (app.has("pnpm-lock.yaml"))
|
|
5
|
+
return "pnpm";
|
|
6
|
+
if (app.has("yarn.lock"))
|
|
7
|
+
return "yarn";
|
|
8
|
+
if (app.has("bun.lockb") || app.has("bun.lock"))
|
|
9
|
+
return "bun";
|
|
10
|
+
return "npm";
|
|
11
|
+
}
|
|
12
|
+
export const installCommand = (pm) => pm === "npm" ? ["npm", ["install", PACKAGE]] : [pm, ["add", PACKAGE]];
|
|
13
|
+
const first = (app, paths) => paths.find(p => app.has(p));
|
|
14
|
+
export function framework(app) {
|
|
15
|
+
const deps = { ...app.pkg.dependencies, ...app.pkg.devDependencies };
|
|
16
|
+
const has = (d) => d in deps;
|
|
17
|
+
if (has("next"))
|
|
18
|
+
return first(app, ["app/layout.tsx", "app/layout.jsx", "app/layout.js", "src/app/layout.tsx", "src/app/layout.jsx", "src/app/layout.js"]) ? "next-app" : "next-pages";
|
|
19
|
+
if (has("nuxt"))
|
|
20
|
+
return "nuxt";
|
|
21
|
+
if (has("@sveltejs/kit"))
|
|
22
|
+
return "sveltekit";
|
|
23
|
+
if (has("astro"))
|
|
24
|
+
return "astro";
|
|
25
|
+
if (has("gatsby"))
|
|
26
|
+
return "gatsby";
|
|
27
|
+
if (Object.keys(deps).some(d => d.startsWith("@remix-run/")) || ((has("react-router") || has("@react-router/dev")) && first(app, ["app/root.tsx", "app/root.jsx"])))
|
|
28
|
+
return "remix";
|
|
29
|
+
if (has("vue"))
|
|
30
|
+
return "vue";
|
|
31
|
+
if (has("react"))
|
|
32
|
+
return "react";
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Adds an import after the file's last import, or at its top below any directive ("use client"). An import that runs
|
|
37
|
+
* over several lines (`import {` … `} from "x"`) ends at its closing brace; a one-line import ends on its own line,
|
|
38
|
+
* with or without a semicolon.
|
|
39
|
+
*/
|
|
40
|
+
function addImport(text, line) {
|
|
41
|
+
const lines = text.split("\n");
|
|
42
|
+
let last = -1;
|
|
43
|
+
for (let i = 0; i < lines.length; i++) {
|
|
44
|
+
if (!/^import[\s{"']/.test(lines[i]))
|
|
45
|
+
continue;
|
|
46
|
+
let end = i;
|
|
47
|
+
if (lines[i].includes("{") && !lines[i].includes("}"))
|
|
48
|
+
while (end < lines.length - 1 && !lines[end].includes("}"))
|
|
49
|
+
end++;
|
|
50
|
+
last = end;
|
|
51
|
+
i = end;
|
|
52
|
+
}
|
|
53
|
+
if (last < 0)
|
|
54
|
+
while (last + 1 < lines.length && /^\s*(["'])use [a-z ]+\1;?\s*$/.test(lines[last + 1]))
|
|
55
|
+
last++;
|
|
56
|
+
lines.splice(last + 1, 0, line);
|
|
57
|
+
return lines.join("\n");
|
|
58
|
+
}
|
|
59
|
+
/** Inserts `what` on its own line before the line holding `marker`, at that line's indentation plus one step. */
|
|
60
|
+
function beforeLine(text, marker, what) {
|
|
61
|
+
const lines = text.split("\n"), i = lines.findIndex(l => marker.test(l));
|
|
62
|
+
if (i < 0)
|
|
63
|
+
return null;
|
|
64
|
+
const indent = (lines[i].match(/^\s*/)?.[0] ?? "") + " ";
|
|
65
|
+
lines.splice(i, 0, indent + what);
|
|
66
|
+
return lines.join("\n");
|
|
67
|
+
}
|
|
68
|
+
const MANUAL = {
|
|
69
|
+
"next-app": k => `In app/layout.tsx: import { Analytics } from "${PACKAGE}/next"; and put <Analytics project="${k}" /> inside <body>.`,
|
|
70
|
+
"next-pages": k => `In pages/_app.tsx: import { Analytics } from "${PACKAGE}/next"; and render <Analytics project="${k}" /> next to <Component {...pageProps} />.`,
|
|
71
|
+
remix: k => `In app/root.tsx: import { Analytics } from "${PACKAGE}/react"; and put <Analytics project="${k}" /> inside <body>.`,
|
|
72
|
+
react: k => `Once at your app's root: import { Analytics } from "${PACKAGE}/react"; and render <Analytics project="${k}" />.`,
|
|
73
|
+
vue: k => `In src/main.ts: import { nextanalytica } from "${PACKAGE}/vue"; and createApp(App).use(nextanalytica, { project: "${k}" }).`,
|
|
74
|
+
nuxt: k => `In nuxt.config.ts: modules: ["${PACKAGE}/nuxt"], and nextanalytica: { project: "${k}" }.`,
|
|
75
|
+
sveltekit: k => `In src/routes/+layout.svelte: <script> import { inject } from "${PACKAGE}/sveltekit"; inject({ project: "${k}" }); </script>.`,
|
|
76
|
+
astro: k => `In your base layout: import Analytics from "${PACKAGE}/astro"; in its frontmatter, and <Analytics project="${k}" /> inside <head>.`,
|
|
77
|
+
gatsby: k => `In gatsby-config.js: plugins: [{ resolve: "${PACKAGE}/gatsby", options: { project: "${k}" } }].`,
|
|
78
|
+
};
|
|
79
|
+
export function plan(app, key) {
|
|
80
|
+
const fw = framework(app);
|
|
81
|
+
if (!fw)
|
|
82
|
+
return { ok: false, framework: null, why: "No Next.js, React, Remix, Vue, Nuxt, SvelteKit, Astro or Gatsby in package.json.", byHand: `Add this line to every page's <head>: <script async src="https://t.nextanalytica.app/t.js" data-project="${key}"></script>` };
|
|
83
|
+
const fail = (why) => ({ ok: false, framework: fw, why, byHand: MANUAL[fw](key) });
|
|
84
|
+
const done = (file, before, after) => after === null ? fail(`${file} doesn't look as expected.`) : { ok: true, framework: fw, file, before, after, already: false };
|
|
85
|
+
const at = (paths) => { const f = first(app, paths); return f ? { f, t: app.read(f) } : null; };
|
|
86
|
+
const tsx = (base) => ["tsx", "jsx", "js", "ts"].map(e => `${base}.${e}`);
|
|
87
|
+
const found = (f) => f && f.t.includes(PACKAGE) ? { ok: true, framework: fw, file: f.f, already: true } : null;
|
|
88
|
+
switch (fw) {
|
|
89
|
+
case "next-app":
|
|
90
|
+
case "remix": {
|
|
91
|
+
const f = at(fw === "next-app" ? [...tsx("app/layout"), ...tsx("src/app/layout")] : tsx("app/root"));
|
|
92
|
+
if (!f)
|
|
93
|
+
return fail("The root layout wasn't found.");
|
|
94
|
+
const a = found(f);
|
|
95
|
+
if (a)
|
|
96
|
+
return a;
|
|
97
|
+
const withTag = beforeLine(f.t, /<\/body>/, `<Analytics project="${key}" />`);
|
|
98
|
+
return done(f.f, f.t, withTag && addImport(withTag, `import { Analytics } from "${PACKAGE}/${fw === "next-app" ? "next" : "react"}";`));
|
|
99
|
+
}
|
|
100
|
+
case "next-pages": {
|
|
101
|
+
const f = at([...tsx("pages/_app"), ...tsx("src/pages/_app")]);
|
|
102
|
+
if (!f) {
|
|
103
|
+
// no custom App yet: the wizard writes the usual one
|
|
104
|
+
const file = app.has("src/pages") ? "src/pages/_app.tsx" : "pages/_app.tsx";
|
|
105
|
+
return done(file, null, `import type { AppProps } from "next/app";\nimport { Analytics } from "${PACKAGE}/next";\n\nexport default function App({ Component, pageProps }: AppProps) {\n return (\n <>\n <Component {...pageProps} />\n <Analytics project="${key}" />\n </>\n );\n}\n`);
|
|
106
|
+
}
|
|
107
|
+
const a = found(f);
|
|
108
|
+
if (a)
|
|
109
|
+
return a;
|
|
110
|
+
const m = f.t.match(/<Component\s+\{\.\.\.pageProps\}\s*\/>/);
|
|
111
|
+
if (!m)
|
|
112
|
+
return fail("pages/_app doesn't render <Component {...pageProps} /> as expected.");
|
|
113
|
+
return done(f.f, f.t, addImport(f.t.replace(m[0], `<>${m[0]}<Analytics project="${key}" /></>`), `import { Analytics } from "${PACKAGE}/next";`));
|
|
114
|
+
}
|
|
115
|
+
case "react": {
|
|
116
|
+
const f = at([...tsx("src/main"), ...tsx("src/index")]);
|
|
117
|
+
if (!f)
|
|
118
|
+
return fail("src/main or src/index wasn't found.");
|
|
119
|
+
const a = found(f);
|
|
120
|
+
if (a)
|
|
121
|
+
return a;
|
|
122
|
+
const m = f.t.match(/<App\s*\/>/);
|
|
123
|
+
if (!m)
|
|
124
|
+
return fail("It doesn't render <App /> as expected.");
|
|
125
|
+
return done(f.f, f.t, addImport(f.t.replace(m[0], `<><App /><Analytics project="${key}" /></>`), `import { Analytics } from "${PACKAGE}/react";`));
|
|
126
|
+
}
|
|
127
|
+
case "vue": {
|
|
128
|
+
const f = at(["src/main.ts", "src/main.js"]);
|
|
129
|
+
if (!f)
|
|
130
|
+
return fail("src/main wasn't found.");
|
|
131
|
+
const a = found(f);
|
|
132
|
+
if (a)
|
|
133
|
+
return a;
|
|
134
|
+
if (!/createApp\(App\)/.test(f.t))
|
|
135
|
+
return fail("It doesn't call createApp(App) as expected.");
|
|
136
|
+
return done(f.f, f.t, addImport(f.t.replace("createApp(App)", `createApp(App).use(nextanalytica, { project: "${key}" })`), `import { nextanalytica } from "${PACKAGE}/vue";`));
|
|
137
|
+
}
|
|
138
|
+
case "nuxt": {
|
|
139
|
+
const f = at(["nuxt.config.ts", "nuxt.config.js", "nuxt.config.mjs"]);
|
|
140
|
+
if (!f)
|
|
141
|
+
return fail("nuxt.config wasn't found.");
|
|
142
|
+
const a = found(f);
|
|
143
|
+
if (a)
|
|
144
|
+
return a;
|
|
145
|
+
if (!/defineNuxtConfig\(\{/.test(f.t))
|
|
146
|
+
return fail("It doesn't call defineNuxtConfig({ as expected.");
|
|
147
|
+
let t = f.t.replace(/defineNuxtConfig\(\{/, `defineNuxtConfig({\n nextanalytica: { project: "${key}" },`);
|
|
148
|
+
t = /modules:\s*\[/.test(t) ? t.replace(/modules:\s*\[/, `modules: ["${PACKAGE}/nuxt", `) : t.replace(/defineNuxtConfig\(\{/, `defineNuxtConfig({\n modules: ["${PACKAGE}/nuxt"],`);
|
|
149
|
+
return done(f.f, f.t, t);
|
|
150
|
+
}
|
|
151
|
+
case "sveltekit": {
|
|
152
|
+
const file = "src/routes/+layout.svelte", t = app.read(file);
|
|
153
|
+
const add = ` import { inject } from "${PACKAGE}/sveltekit";\n inject({ project: "${key}" });`;
|
|
154
|
+
if (t === null)
|
|
155
|
+
return done(file, null, `<script>\n${add}\n</script>\n\n<slot />\n`);
|
|
156
|
+
if (t.includes(PACKAGE))
|
|
157
|
+
return { ok: true, framework: fw, file, already: true };
|
|
158
|
+
const m = t.match(/<script(\s+lang="ts")?\s*>/);
|
|
159
|
+
return done(file, t, m ? t.replace(m[0], `${m[0]}\n${add}`) : `<script>\n${add}\n</script>\n\n${t}`);
|
|
160
|
+
}
|
|
161
|
+
case "astro": {
|
|
162
|
+
const layouts = ["src/layouts", "src/components"].flatMap(d => app.list(d).filter(n => n.endsWith(".astro")).map(n => `${d}/${n}`));
|
|
163
|
+
const f = [...layouts, ...app.list("src/pages").filter(n => n.endsWith(".astro")).map(n => `src/pages/${n}`)]
|
|
164
|
+
.map(p => ({ f: p, t: app.read(p) ?? "" })).find(x => /<head[\s>]/.test(x.t));
|
|
165
|
+
if (!f)
|
|
166
|
+
return fail("No layout with a <head> was found.");
|
|
167
|
+
const a = found(f);
|
|
168
|
+
if (a)
|
|
169
|
+
return a;
|
|
170
|
+
const imp = `import Analytics from "${PACKAGE}/astro";`;
|
|
171
|
+
let t = f.t.replace(/<head(\s[^>]*)?>/, m => `${m}\n <Analytics project="${key}" />`);
|
|
172
|
+
t = /^---\n/.test(t) ? t.replace(/^---\n/, `---\n${imp}\n`) : `---\n${imp}\n---\n${t}`;
|
|
173
|
+
return done(f.f, f.t, t);
|
|
174
|
+
}
|
|
175
|
+
case "gatsby": {
|
|
176
|
+
const f = at(["gatsby-config.js", "gatsby-config.ts", "gatsby-config.mjs"]);
|
|
177
|
+
const plugin = `{ resolve: "${PACKAGE}/gatsby", options: { project: "${key}" } }`;
|
|
178
|
+
if (!f)
|
|
179
|
+
return done("gatsby-config.js", null, `module.exports = {\n plugins: [${plugin}],\n};\n`);
|
|
180
|
+
const a = found(f);
|
|
181
|
+
if (a)
|
|
182
|
+
return a;
|
|
183
|
+
if (!/plugins:\s*\[/.test(f.t))
|
|
184
|
+
return fail("gatsby-config has no plugins list as expected.");
|
|
185
|
+
return done(f.f, f.t, f.t.replace(/plugins:\s*\[/, `plugins: [\n ${plugin},`));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/** A short line diff of what changes: removed lines with "-", added with "+", two lines of context. */
|
|
190
|
+
export function diff(before, after) {
|
|
191
|
+
const a = (before ?? "").split("\n"), b = after.split("\n");
|
|
192
|
+
if (before === null)
|
|
193
|
+
return b.map(l => `+ ${l}`).join("\n");
|
|
194
|
+
// longest common subsequence: files here are small (a layout, a config)
|
|
195
|
+
const n = a.length, m = b.length, L = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
|
|
196
|
+
for (let i = n - 1; i >= 0; i--)
|
|
197
|
+
for (let j = m - 1; j >= 0; j--)
|
|
198
|
+
L[i][j] = a[i] === b[j] ? L[i + 1][j + 1] + 1 : Math.max(L[i + 1][j], L[i][j + 1]);
|
|
199
|
+
const ops = [];
|
|
200
|
+
let i = 0, j = 0;
|
|
201
|
+
while (i < n || j < m) {
|
|
202
|
+
if (i < n && j < m && a[i] === b[j]) {
|
|
203
|
+
ops.push([" ", a[i]]);
|
|
204
|
+
i++;
|
|
205
|
+
j++;
|
|
206
|
+
}
|
|
207
|
+
else if (j < m && (i >= n || L[i][j + 1] >= L[i + 1][j])) {
|
|
208
|
+
ops.push(["+", b[j]]);
|
|
209
|
+
j++;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
ops.push(["-", a[i]]);
|
|
213
|
+
i++;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const keep = new Set();
|
|
217
|
+
ops.forEach(([o], k) => { if (o !== " ")
|
|
218
|
+
for (let x = k - 2; x <= k + 2; x++)
|
|
219
|
+
keep.add(x); });
|
|
220
|
+
const out = [];
|
|
221
|
+
let gap = false;
|
|
222
|
+
ops.forEach(([o, l], k) => { if (keep.has(k)) {
|
|
223
|
+
out.push(`${o} ${l}`);
|
|
224
|
+
gap = false;
|
|
225
|
+
}
|
|
226
|
+
else if (!gap) {
|
|
227
|
+
out.push(" …");
|
|
228
|
+
gap = true;
|
|
229
|
+
} });
|
|
230
|
+
return out.join("\n");
|
|
231
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextanalytica/wizard",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Adds the nextanalytica tag to a JavaScript app: finds the framework, shows the change, installs the package.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"homepage": "https://nextanalytica.app",
|
|
8
|
+
"bin": {
|
|
9
|
+
"nextanalytica-wizard": "dist/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
20
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
21
|
+
"test": "vitest run --passWithNoTests",
|
|
22
|
+
"prepublishOnly": "npm run build && vitest run"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^26.6.5",
|
|
26
|
+
"typescript": "^7.0.2",
|
|
27
|
+
"vitest": "^5.0.5"
|
|
28
|
+
}
|
|
29
|
+
}
|