snapfail 0.0.1
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/dist/index.js +24 -0
- package/dist/init-PT3WVALP.js +120 -0
- package/package.json +33 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { outro } from "@clack/prompts";
|
|
5
|
+
var [framework, command] = process.argv.slice(2);
|
|
6
|
+
async function main() {
|
|
7
|
+
if (framework === "astro" && command === "init") {
|
|
8
|
+
const { init } = await import("./init-PT3WVALP.js");
|
|
9
|
+
await init();
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
console.log([
|
|
13
|
+
"",
|
|
14
|
+
" snapfail \u2014 error monitoring for modern web apps",
|
|
15
|
+
"",
|
|
16
|
+
" Usage:",
|
|
17
|
+
" npx snapfail astro init Add Snapfail to an Astro project",
|
|
18
|
+
""
|
|
19
|
+
].join("\n"));
|
|
20
|
+
}
|
|
21
|
+
main().catch((err) => {
|
|
22
|
+
outro(`Error: ${err.message}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/commands/astro/init.ts
|
|
4
|
+
import { intro, outro, text, spinner, log, isCancel, cancel, note } from "@clack/prompts";
|
|
5
|
+
|
|
6
|
+
// src/utils/pm.ts
|
|
7
|
+
import { existsSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { exec } from "child_process";
|
|
10
|
+
import { promisify } from "util";
|
|
11
|
+
var execAsync = promisify(exec);
|
|
12
|
+
function detectPM(cwd = process.cwd()) {
|
|
13
|
+
if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
|
|
14
|
+
if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
|
|
15
|
+
if (existsSync(join(cwd, "bun.lockb"))) return "bun";
|
|
16
|
+
return "npm";
|
|
17
|
+
}
|
|
18
|
+
async function install(pm, packages, cwd = process.cwd()) {
|
|
19
|
+
const sub = pm === "npm" ? "install" : "add";
|
|
20
|
+
await execAsync(`${pm} ${sub} ${packages.join(" ")}`, { cwd });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/utils/config.ts
|
|
24
|
+
import { readFileSync, writeFileSync } from "fs";
|
|
25
|
+
import { join as join2 } from "path";
|
|
26
|
+
var IMPORT_LINE = `import { snapfail } from '@snapfail/astro';
|
|
27
|
+
`;
|
|
28
|
+
function injectAstroConfig(configFile, dsn, cwd = process.cwd()) {
|
|
29
|
+
const filePath = join2(cwd, configFile);
|
|
30
|
+
let src = readFileSync(filePath, "utf-8");
|
|
31
|
+
if (src.includes("@snapfail/astro")) return true;
|
|
32
|
+
const lastImportIdx = [...src.matchAll(/^import .+$/gm)].at(-1);
|
|
33
|
+
if (lastImportIdx?.index != null) {
|
|
34
|
+
const insertAt = lastImportIdx.index + lastImportIdx[0].length;
|
|
35
|
+
src = src.slice(0, insertAt) + "\n" + IMPORT_LINE + src.slice(insertAt);
|
|
36
|
+
} else {
|
|
37
|
+
src = IMPORT_LINE + src;
|
|
38
|
+
}
|
|
39
|
+
if (/integrations\s*:\s*\[/.test(src)) {
|
|
40
|
+
src = src.replace(
|
|
41
|
+
/integrations\s*:\s*\[/,
|
|
42
|
+
`integrations: [
|
|
43
|
+
snapfail({ dsn: '${dsn}' }),`
|
|
44
|
+
);
|
|
45
|
+
writeFileSync(filePath, src);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if (/defineConfig\s*\(\s*\{/.test(src)) {
|
|
49
|
+
src = src.replace(
|
|
50
|
+
/defineConfig\s*\(\s*\{/,
|
|
51
|
+
`defineConfig({
|
|
52
|
+
integrations: [snapfail({ dsn: '${dsn}' })],`
|
|
53
|
+
);
|
|
54
|
+
writeFileSync(filePath, src);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
function findAstroConfig(cwd = process.cwd()) {
|
|
60
|
+
for (const name of ["astro.config.mjs", "astro.config.ts", "astro.config.js"]) {
|
|
61
|
+
try {
|
|
62
|
+
readFileSync(join2(cwd, name));
|
|
63
|
+
return name;
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/commands/astro/init.ts
|
|
71
|
+
async function init() {
|
|
72
|
+
intro("snapfail \xB7 astro init");
|
|
73
|
+
const configFile = findAstroConfig();
|
|
74
|
+
if (!configFile) {
|
|
75
|
+
log.error("No astro.config.* found. Run this inside an Astro project.");
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
log.info(`Found ${configFile}`);
|
|
79
|
+
const dsn = await text({
|
|
80
|
+
message: "Beacon endpoint (DSN)",
|
|
81
|
+
placeholder: "/api/beacon",
|
|
82
|
+
initialValue: "/api/beacon",
|
|
83
|
+
validate: (v) => !v.trim() ? "DSN cannot be empty" : void 0
|
|
84
|
+
});
|
|
85
|
+
if (isCancel(dsn)) {
|
|
86
|
+
cancel("Cancelled.");
|
|
87
|
+
process.exit(0);
|
|
88
|
+
}
|
|
89
|
+
const pm = detectPM();
|
|
90
|
+
const s = spinner();
|
|
91
|
+
s.start(`Installing @snapfail/astro and @snapfail/sdk via ${pm}`);
|
|
92
|
+
try {
|
|
93
|
+
await install(pm, ["@snapfail/astro", "@snapfail/sdk"]);
|
|
94
|
+
s.stop("Packages installed");
|
|
95
|
+
} catch (err) {
|
|
96
|
+
s.stop("Installation failed");
|
|
97
|
+
log.error(err.message);
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
const injected = injectAstroConfig(configFile, dsn);
|
|
101
|
+
if (injected) {
|
|
102
|
+
log.success(`Integration added to ${configFile}`);
|
|
103
|
+
} else {
|
|
104
|
+
log.warn(`Could not auto-inject into ${configFile}.`);
|
|
105
|
+
note(
|
|
106
|
+
[
|
|
107
|
+
`import { snapfail } from '@snapfail/astro'`,
|
|
108
|
+
``,
|
|
109
|
+
`export default defineConfig({`,
|
|
110
|
+
` integrations: [snapfail({ dsn: '${dsn}' })],`,
|
|
111
|
+
`})`
|
|
112
|
+
].join("\n"),
|
|
113
|
+
"Add this manually"
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
outro("Done! Run your dev server to start capturing errors.");
|
|
117
|
+
}
|
|
118
|
+
export {
|
|
119
|
+
init
|
|
120
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "snapfail",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "CLI for Snapfail — error monitoring for modern web apps",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"snapfail",
|
|
8
|
+
"cli",
|
|
9
|
+
"error-monitoring",
|
|
10
|
+
"astro"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"bin": {
|
|
17
|
+
"snapfail": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@clack/prompts": "^0.9.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.4.0",
|
|
27
|
+
"typescript": "^6.0.3"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"dev": "tsup --watch"
|
|
32
|
+
}
|
|
33
|
+
}
|