-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinit.ts
More file actions
252 lines (231 loc) · 8.1 KB
/
init.ts
File metadata and controls
252 lines (231 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* sentry init
*
* Initialize Sentry in a project using the remote wizard workflow.
* Communicates with the Mastra API via suspend/resume to perform
* local filesystem operations and interactive prompts.
*
* Supports two optional positionals with smart disambiguation:
* sentry init — auto-detect everything, dir = cwd
* sentry init . — dir = cwd, auto-detect org
* sentry init ./subdir — dir = subdir, auto-detect org
* sentry init acme/ — explicit org, dir = cwd
* sentry init acme/my-app — explicit org + project, dir = cwd
* sentry init my-app — search for project across orgs
* sentry init acme/ ./subdir — explicit org, dir = subdir
* sentry init acme/my-app ./subdir — explicit org + project, dir = subdir
* sentry init ./subdir acme/ — swapped, auto-correct with warning
*/
import path from "node:path";
import type { SentryContext } from "../context.js";
import { looksLikePath, parseOrgProjectArg } from "../lib/arg-parsing.js";
import { buildCommand } from "../lib/command.js";
import { ContextError } from "../lib/errors.js";
import { warmOrgDetection } from "../lib/init/prefetch.js";
import { runWizard } from "../lib/init/wizard-runner.js";
import { validateResourceId } from "../lib/input-validation.js";
import { logger } from "../lib/logger.js";
import { resolveProjectBySlug } from "../lib/resolve-target.js";
const log = logger.withTag("init");
const FEATURE_DELIMITER = /[,+ ]+/;
const USAGE_HINT = "sentry init <org>/<project> [directory]";
type InitFlags = {
readonly yes: boolean;
readonly "dry-run": boolean;
readonly features?: string[];
readonly team?: string;
};
/**
* Classify and separate two optional positional args into a target and a directory.
*
* Uses {@link looksLikePath} to distinguish filesystem paths from org/project targets.
* Detects swapped arguments and emits a warning when auto-correcting.
*
* @returns Resolved target string (or undefined) and directory string (or undefined)
*/
function classifyArgs(
first?: string,
second?: string
): { target: string | undefined; directory: string | undefined } {
// No args — auto-detect everything
if (!first) {
return { target: undefined, directory: undefined };
}
const firstIsPath = looksLikePath(first);
// Single arg
if (!second) {
return firstIsPath
? { target: undefined, directory: first }
: { target: first, directory: undefined };
}
const secondIsPath = looksLikePath(second);
// Two paths → error
if (firstIsPath && secondIsPath) {
throw new ContextError("Arguments", USAGE_HINT, [
"Two directory paths provided. Only one directory is allowed.",
]);
}
// Two targets → error
if (!(firstIsPath || secondIsPath)) {
throw new ContextError("Arguments", USAGE_HINT, [
"Two targets provided. Use <org>/<project> for the target and a path (e.g., ./dir) for the directory.",
]);
}
// (TARGET, PATH) — correct order
if (!firstIsPath && secondIsPath) {
return { target: first, directory: second };
}
// (PATH, TARGET) — swapped, auto-correct with warning
log.warn(`Arguments appear reversed. Interpreting as: ${second} ${first}`);
return { target: second, directory: first };
}
/**
* Resolve the parsed org/project target into explicit org and project values.
*
* For `project-search` (bare slug), calls {@link resolveProjectBySlug} to search
* across all accessible orgs and determine both org and project from the match.
*/
async function resolveTarget(targetArg: string | undefined): Promise<{
org: string | undefined;
project: string | undefined;
}> {
const parsed = parseOrgProjectArg(targetArg);
switch (parsed.type) {
case "explicit":
// Validate user-provided slugs before they reach API calls
validateResourceId(parsed.org, "organization slug");
validateResourceId(parsed.project, "project name");
return { org: parsed.org, project: parsed.project };
case "org-all":
validateResourceId(parsed.org, "organization slug");
return { org: parsed.org, project: undefined };
case "project-search": {
// Bare slug — search for a project with this name across all orgs.
// resolveProjectBySlug handles not-found, ambiguity, and org-name-collision errors.
const resolved = await resolveProjectBySlug(
parsed.projectSlug,
USAGE_HINT,
`sentry init ${parsed.projectSlug}/ (if '${parsed.projectSlug}' is an org)`
);
return { org: resolved.org, project: resolved.project };
}
case "auto-detect":
return { org: undefined, project: undefined };
default: {
const _exhaustive: never = parsed;
throw new ContextError("Target", String(_exhaustive));
}
}
}
export const initCommand = buildCommand<
InitFlags,
[string?, string?],
SentryContext
>({
docs: {
brief: "Initialize Sentry in your project",
fullDescription:
"Runs the Sentry setup wizard to detect your project's framework, " +
"install the SDK, and configure Sentry.\n\n" +
"Supports org/project syntax and a directory positional. Path-like\n" +
"arguments (starting with . / ~) are treated as the directory;\n" +
"everything else is treated as the target.\n\n" +
"Examples:\n" +
" sentry init\n" +
" sentry init acme/\n" +
" sentry init acme/my-app\n" +
" sentry init my-app\n" +
" sentry init acme/my-app ./my-project\n" +
" sentry init ./my-project",
},
parameters: {
positional: {
kind: "tuple",
parameters: [
{
placeholder: "target",
brief: "<org>/<project>, <org>/, <project>, or a directory path",
parse: String,
optional: true,
},
{
placeholder: "directory",
brief: "Project directory (default: current directory)",
parse: String,
optional: true,
},
],
},
flags: {
yes: {
kind: "boolean",
brief: "Non-interactive mode (accept defaults)",
default: false,
},
"dry-run": {
kind: "boolean",
brief: "Preview changes without applying them",
default: false,
},
features: {
kind: "parsed",
parse: String,
brief:
"Features to enable: errors,tracing,logs,replay,metrics,profiling,sourcemaps,crons,ai-monitoring,user-feedback",
variadic: true,
optional: true,
},
team: {
kind: "parsed",
parse: String,
brief: "Team slug to create the project under",
optional: true,
},
},
aliases: {
y: "yes",
t: "team",
},
},
async *func(
this: SentryContext,
flags: InitFlags,
first?: string,
second?: string
) {
// 1. Classify positionals into target vs directory
const { target: targetArg, directory: dirArg } = classifyArgs(
first,
second
);
// 2. Resolve directory
const targetDir = dirArg ? path.resolve(this.cwd, dirArg) : this.cwd;
// 3. Parse features
const featuresList = flags.features
?.flatMap((f) => f.split(FEATURE_DELIMITER))
.map((f) => f.trim())
.filter(Boolean);
// 4. Resolve target → org + project
// Validation of user-provided slugs happens inside resolveTarget.
// API-resolved values (from resolveProjectBySlug) are already valid.
const { org: explicitOrg, project: explicitProject } =
await resolveTarget(targetArg);
// 5. Start background org detection when org is not yet known.
// The prefetch runs concurrently with the preamble, the wizard startup,
// and all early suspend/resume rounds — by the time the wizard needs the
// org (inside createSentryProject), the result is already cached.
if (!explicitOrg) {
warmOrgDetection(targetDir);
}
// 6. Run the wizard
await runWizard({
directory: targetDir,
yes: flags.yes,
dryRun: flags["dry-run"],
features: featuresList,
team: flags.team,
org: explicitOrg,
project: explicitProject,
});
},
});