@tailor-platform/sdk-codemod 0.3.0-next.2 → 0.3.0-next.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.
@@ -0,0 +1,1087 @@
1
+ import { Lang, parse } from "@ast-grep/napi";
2
+ import * as path from "pathe";
3
+ //#region codemods/v2/rename-bin/scripts/transform.ts
4
+ const SOURCE_ARG_VALUE = `(?:[^\\s'"\`;|&]+|'[^']*'|"(?:(?:\\\\.)|[^"\\\\])*")`;
5
+ const RUNNER_OPTION_VALUE_FLAG_LIST = [
6
+ "--registry",
7
+ "--cache",
8
+ "--userconfig",
9
+ "--prefix",
10
+ "--filter",
11
+ "-F",
12
+ "--dir",
13
+ "-C",
14
+ "--cwd"
15
+ ];
16
+ const RUNNER_OPTION_VALUE_FLAG_PATTERN = `(?:${RUNNER_OPTION_VALUE_FLAG_LIST.join("|")})`;
17
+ const PACKAGE_RUNNER_OPTION = `(?:${RUNNER_OPTION_VALUE_FLAG_PATTERN}(?:=${SOURCE_ARG_VALUE}|\\s+${SOURCE_ARG_VALUE})|${`(?!(?:${RUNNER_OPTION_VALUE_FLAG_PATTERN})(?:=|\\s|$))(?:-\\w+|--\\w[\\w-]*)(?:=${SOURCE_ARG_VALUE})?`})`;
18
+ const PACKAGE_RUNNER_COMMAND = `(?:npx|bunx|(?:pnpm|yarn)(?:\\s+${PACKAGE_RUNNER_OPTION})*\\s+dlx)`;
19
+ const PKG_RUNNER_RE = new RegExp(`\\b(${PACKAGE_RUNNER_COMMAND}(?:\\s+(?:-\\w+|--\\w[\\w-]*))*)\\s+tailor-sdk(?![\\w-])(@[^\\s'"\`;|&)]+)?`, "g");
20
+ const TAILOR_SDK_RE = /(?<![.\w-])tailor-sdk(?![\w-])(@[^\s'"`;|&)]+)?/g;
21
+ const SOURCE_EXTENSIONS = /* @__PURE__ */ new Set([
22
+ ".ts",
23
+ ".tsx",
24
+ ".mts",
25
+ ".cts",
26
+ ".js",
27
+ ".jsx",
28
+ ".mjs",
29
+ ".cjs"
30
+ ]);
31
+ const TAILOR_CLI_COMMAND_PATTERN = `(?:${[
32
+ "api",
33
+ "apply",
34
+ "authconnection",
35
+ "completion",
36
+ "crash-report",
37
+ "crashreport",
38
+ "deploy",
39
+ "executor",
40
+ "function",
41
+ "generate",
42
+ "init",
43
+ "login",
44
+ "logout",
45
+ "machineuser",
46
+ "oauth2client",
47
+ "open",
48
+ "organization",
49
+ "profile",
50
+ "query",
51
+ "remove",
52
+ "secret",
53
+ "setup",
54
+ "show",
55
+ "skills",
56
+ "staticwebsite",
57
+ "tailordb",
58
+ "upgrade",
59
+ "user",
60
+ "workflow",
61
+ "workspace"
62
+ ].join("|")})`;
63
+ const TAILOR_CLI_VALUE_FLAG = "(?:--env-file-if-exists|--env-file|--profile|--config|--workspace-id|--arg|--query|--file|--name|--namespace|--dir|-e|-p|-c|-w|-a|-q|-f|-n)";
64
+ const TAILOR_CLI_VALUE_FLAGS = /* @__PURE__ */ new Set([
65
+ "--env-file-if-exists",
66
+ "--env-file",
67
+ "--profile",
68
+ "--config",
69
+ "--workspace-id",
70
+ "--arg",
71
+ "--query",
72
+ "--file",
73
+ "--name",
74
+ "--namespace",
75
+ "--dir",
76
+ "-e",
77
+ "-p",
78
+ "-c",
79
+ "-w",
80
+ "-a",
81
+ "-q",
82
+ "-f",
83
+ "-n"
84
+ ]);
85
+ const TAILOR_CLI_COMMAND_VALUE_FLAGS = /* @__PURE__ */ new Set(["--value", "-v"]);
86
+ const SOURCE_CLI_ARG_VALUE = `(?:${String.raw`\\"(?:\\\\.|[^"\\])*\\"|\\'(?:\\\\.|[^'\\])*\\'`}|${SOURCE_ARG_VALUE})`;
87
+ const SOURCE_COMMAND_GAP = `(?:\\s+--?[\\w-]+(?:=${SOURCE_CLI_ARG_VALUE})?(?:\\s+${SOURCE_CLI_ARG_VALUE})?)*`;
88
+ const SOURCE_RUNNER_OPTION_GAP = `(?:\\s+${PACKAGE_RUNNER_OPTION})*`;
89
+ const SOURCE_OPTION_VALUE_REFERENCE_RE = new RegExp(`((?:--[\\w-]+|-\\w)(?:=|\\s+))(${SOURCE_CLI_ARG_VALUE})`, "g");
90
+ const SOURCE_TEMPLATE_EXPR_PLACEHOLDER = "__TAILOR_SDK_TEMPLATE_EXPR_\\d+_\\d+__";
91
+ const SOURCE_TEMPLATE_EXPR_PLACEHOLDER_RE = /^__TAILOR_SDK_TEMPLATE_EXPR_\d+_\d+__$/;
92
+ const SOURCE_TEMPLATE_DYNAMIC_ARGS = `\\s+${SOURCE_TEMPLATE_EXPR_PLACEHOLDER}(?:\\s+${SOURCE_ARG_VALUE})*(?=\\s*(?:$|[;&|]))`;
93
+ const SOURCE_DIRECT_INVOCATION_LOOKAHEAD = `(?:${SOURCE_COMMAND_GAP}\\s+${TAILOR_CLI_COMMAND_PATTERN}\\b|${`${SOURCE_COMMAND_GAP}\\s+(?:--help|-h|--version|-v)\\b`})`;
94
+ const SOURCE_PKG_RUNNER_COMMAND_LOOKAHEAD = `(?:${SOURCE_DIRECT_INVOCATION_LOOKAHEAD}|${SOURCE_TEMPLATE_DYNAMIC_ARGS}|\\s*$)`;
95
+ const SOURCE_PKG_RUNNER_INVOCATION_LOOKAHEAD = `(?:${SOURCE_PKG_RUNNER_COMMAND_LOOKAHEAD}|\\s+tailor-sdk(?![\\w-])(?:@[^\\s'"\`;|&)]+)?${SOURCE_PKG_RUNNER_COMMAND_LOOKAHEAD})`;
96
+ const SOURCE_DYNAMIC_OPTION_VALUE_LOOKAHEAD = `(?=\\s+${TAILOR_CLI_VALUE_FLAG}(?:=|\\s+)\\s*$)`;
97
+ const SOURCE_TAILOR_SDK_COMMAND = `tailor-sdk(?:(\\.(?:cmd|ps1|exe))|(@[^\\s'"\`;|&)]+))?(?![\\w-])`;
98
+ const SOURCE_PKG_RUNNER_RE = new RegExp(`\\b(${PACKAGE_RUNNER_COMMAND}${SOURCE_RUNNER_OPTION_GAP})\\s+tailor-sdk(?![\\w-])(@[^\\s'"\`;|&)]+)?(?=${SOURCE_PKG_RUNNER_INVOCATION_LOOKAHEAD})`, "g");
99
+ const SOURCE_PACKAGE_FLAG_VALUE_RE = new RegExp(`((?:-p|--package)(?:=|\\s+))tailor-sdk(?![\\w-])(@[^\\s'"\`;|&)]+)?`, "g");
100
+ const SOURCE_PACKAGE_FLAG_EQUALS_QUOTED_VALUE_RE = /((?:-p|--package)=)(\\"|\\'|"|')tailor-sdk(?![\w-])(@[^\s'"`;|&)]+)?(\\"|\\'|"|')/g;
101
+ const SOURCE_PACKAGE_FLAG_BINARY_RE = new RegExp(`\\b(${PACKAGE_RUNNER_COMMAND}(?:(?!\\s+tailor-sdk(?![\\w-])(?:@[^\\s'"\`;|&)]+)?${SOURCE_PKG_RUNNER_COMMAND_LOOKAHEAD})\\s+${SOURCE_ARG_VALUE})*\\s+)tailor-sdk(?![\\w-])(@[^\\s'"\`;|&)]+)?(?=${SOURCE_PKG_RUNNER_COMMAND_LOOKAHEAD})`, "g");
102
+ const SOURCE_TAILOR_SDK_RE = new RegExp(`(?<![.\\w-])${SOURCE_TAILOR_SDK_COMMAND}(?=${SOURCE_DIRECT_INVOCATION_LOOKAHEAD})`, "g");
103
+ const SOURCE_DYNAMIC_TAILOR_SDK_RE = new RegExp(`(^\\s*|[;&|]\\s*|\\b(?:pnpm|npm|yarn)(?:\\s+exec)?\\s+)${SOURCE_TAILOR_SDK_COMMAND}(?=${SOURCE_TEMPLATE_DYNAMIC_ARGS}|\\s+$)`, "g");
104
+ const SOURCE_DYNAMIC_OPTION_TAILOR_SDK_RE = new RegExp(`(^\\s*|[;&|]\\s*|\\b(?:pnpm|npm|yarn)(?:\\s+exec)?\\s+)${SOURCE_TAILOR_SDK_COMMAND}${SOURCE_DYNAMIC_OPTION_VALUE_LOOKAHEAD}`, "g");
105
+ const TAILOR_SDK_TOKEN_RE = /^tailor-sdk(@[^\s'"`;|&)]+)?$/;
106
+ const TAILOR_SDK_COMMAND_TOKEN_RE = /^tailor-sdk(@[^\s'"`;|&)]+)?(?:\.(?:cmd|ps1|exe))?$/;
107
+ const TAILOR_COMMAND_TOKEN_RE = /^tailor(?:\.(?:cmd|ps1|exe))?$/;
108
+ const TAILOR_SDK_PATH_RE = /(?:^|[\\/])tailor-sdk(?:\.(?:cmd|ps1|exe))?$/;
109
+ const TAILOR_CLI_TOKEN_RE = /^(?:tailor(?:\.(?:cmd|ps1|exe))?|tailor-sdk(?:@[^\s'"`;|&)]+)?(?:\.(?:cmd|ps1|exe))?|@tailor-platform\/sdk(?:@[^\s'"`;|&)]+)?)$/;
110
+ const CLI_ARGUMENT_CALLEE_RE = /(?:^|\.)(?:spawn|spawnSync|execFile|execFileSync|execa|execaSync)$/;
111
+ const SOURCE_EXEC_PACKAGE_MANAGERS = /* @__PURE__ */ new Set([
112
+ "npm",
113
+ "pnpm",
114
+ "yarn"
115
+ ]);
116
+ const SOURCE_PACKAGE_RUNNERS = /* @__PURE__ */ new Set(["bunx", "npx"]);
117
+ const SOURCE_DLX_PACKAGE_RUNNERS = /* @__PURE__ */ new Set(["pnpm", "yarn"]);
118
+ const SOURCE_NPM_EXEC_PACKAGE_RUNNERS = /* @__PURE__ */ new Set(["npm"]);
119
+ const NPM_OPTION_VALUE_FLAGS = /* @__PURE__ */ new Set(["--workspace", "-w"]);
120
+ const SOURCE_PACKAGE_FLAG_RE = /^(?:-p|--package)(?:=.*)?$/;
121
+ const NPX_PACKAGE_FLAG_CONTEXT_RE = new RegExp(`(?:^|[;&|]\\s*)npx(?:\\s+(?:(?:--registry|--cache|--userconfig|--prefix)\\s+${SOURCE_ARG_VALUE}|-\\w+|--\\w[\\w-]*(?:=${SOURCE_ARG_VALUE})?))*\\s*$`);
122
+ const SOURCE_TOKEN_RE = new RegExp(SOURCE_CLI_ARG_VALUE, "g");
123
+ const CLI_RENAME_LEGACY_RE = /(?<![\w-])(?:apply|crash-report|--machineuser)(?![\w-])/;
124
+ const TAILOR_PLATFORM_SDK_TOKEN_RE = /^@tailor-platform\/sdk(@[^\s'"`;|&)]+)?$/;
125
+ const RUNNER_OPTION_VALUE_FLAGS = new Set(RUNNER_OPTION_VALUE_FLAG_LIST);
126
+ function renameBinary(value) {
127
+ return value.replace(PKG_RUNNER_RE, (_, runner, version) => version ? `${runner} @tailor-platform/sdk${version}` : `${runner} @tailor-platform/sdk`).replace(TAILOR_SDK_RE, (_match, version) => version ? `@tailor-platform/sdk${version}` : "tailor");
128
+ }
129
+ function renamePackageName(value) {
130
+ return value.replace(TAILOR_SDK_TOKEN_RE, (_match, version) => version ? `@tailor-platform/sdk${version}` : "@tailor-platform/sdk");
131
+ }
132
+ function renameSourcePackageToken(token) {
133
+ const value = sourceTokenValue(token);
134
+ if (!TAILOR_SDK_TOKEN_RE.test(value)) return null;
135
+ return replaceSourceTokenValue(token, renamePackageName(value));
136
+ }
137
+ function renameSourceBinaryToken(token) {
138
+ const value = sourceTokenValue(token);
139
+ if (!TAILOR_SDK_COMMAND_TOKEN_RE.test(value)) return null;
140
+ return replaceSourceTokenValue(token, value.includes("@") && !/\.(?:cmd|ps1|exe)$/.test(value) ? renamePackageName(value) : renameBinary(value));
141
+ }
142
+ function isTailorPackageValue(value) {
143
+ return TAILOR_SDK_TOKEN_RE.test(value) || TAILOR_PLATFORM_SDK_TOKEN_RE.test(value);
144
+ }
145
+ function sourcePathBasename(value) {
146
+ return value.split(/[\\/]/).at(-1) ?? value;
147
+ }
148
+ function isTailorSdkBinaryTokenValue(value) {
149
+ return TAILOR_SDK_COMMAND_TOKEN_RE.test(sourcePathBasename(value));
150
+ }
151
+ function isTailorCliTokenValue(value) {
152
+ const basename = sourcePathBasename(value);
153
+ return TAILOR_CLI_TOKEN_RE.test(value) || TAILOR_COMMAND_TOKEN_RE.test(basename) || TAILOR_SDK_COMMAND_TOKEN_RE.test(basename);
154
+ }
155
+ function replaceSourceSpans(value, replacements, tokens) {
156
+ let updated = value;
157
+ for (const [index, replacement] of [...replacements.entries()].toSorted(([a], [b]) => b - a)) {
158
+ const token = tokens[index];
159
+ if (token == null) continue;
160
+ updated = `${updated.slice(0, token.start)}${replacement}${updated.slice(token.end)}`;
161
+ }
162
+ return updated;
163
+ }
164
+ function sourceValuePlaceholder(index, source, usedPlaceholders) {
165
+ let attempt = 0;
166
+ while (true) {
167
+ const placeholder = `__TAILOR_SDK_SOURCE_VALUE_${index}_${attempt}__`;
168
+ if (!source.includes(placeholder) && !usedPlaceholders.has(placeholder)) {
169
+ usedPlaceholders.add(placeholder);
170
+ return placeholder;
171
+ }
172
+ attempt += 1;
173
+ }
174
+ }
175
+ function protectSourceCliValueReferences(value) {
176
+ const protectedValues = [];
177
+ const usedPlaceholders = /* @__PURE__ */ new Set();
178
+ return {
179
+ source: value.replace(SOURCE_OPTION_VALUE_REFERENCE_RE, (match, prefix, arg, offset) => {
180
+ if (!arg.includes("tailor-sdk")) return match;
181
+ const flag = sourceOptionFlag(prefix);
182
+ const afterPackageRunner = isAfterPackageRunnerPrefix(value, offset);
183
+ if (afterPackageRunner && !isPackageRunnerOptionValue(value, offset, flag)) return match;
184
+ if (!afterPackageRunner && !isAfterTailorCliToken(value, offset)) return match;
185
+ if (isPackageFlag(flag) && NPX_PACKAGE_FLAG_CONTEXT_RE.test(value.slice(0, offset))) return match;
186
+ const placeholder = sourceValuePlaceholder(protectedValues.length, value, usedPlaceholders);
187
+ protectedValues.push({
188
+ placeholder,
189
+ value: arg
190
+ });
191
+ return `${prefix}${placeholder}`;
192
+ }),
193
+ protectedValues
194
+ };
195
+ }
196
+ function restoreSourceCliValueReferences(value, protectedValues) {
197
+ let restored = value;
198
+ for (const protectedValue of protectedValues) restored = restored.replaceAll(protectedValue.placeholder, () => protectedValue.value);
199
+ return restored;
200
+ }
201
+ function sourceOptionFlag(prefix) {
202
+ return prefix.trim().replace(/[=\s].*$/, "");
203
+ }
204
+ function isPackageFlag(value) {
205
+ return value === "-p" || value === "--package";
206
+ }
207
+ function sourceTokens(value) {
208
+ const tokens = sourceTokenSpans(value);
209
+ return tokens == null ? null : tokens.map((token) => token.value);
210
+ }
211
+ function sourceTokenSpans(value) {
212
+ const tokens = [];
213
+ SOURCE_TOKEN_RE.lastIndex = 0;
214
+ let lastEnd = 0;
215
+ for (const match of value.matchAll(SOURCE_TOKEN_RE)) {
216
+ if (value.slice(lastEnd, match.index).trim() !== "") return null;
217
+ tokens.push(sourceTokenValue(match[0]));
218
+ lastEnd = (match.index ?? 0) + match[0].length;
219
+ }
220
+ if (value.slice(lastEnd).trim() !== "") return null;
221
+ return mergeSourceEqualsQuotedTokenSpans([...value.matchAll(new RegExp(SOURCE_CLI_ARG_VALUE, "g"))].map((match) => ({
222
+ start: match.index ?? 0,
223
+ end: (match.index ?? 0) + match[0].length,
224
+ text: match[0],
225
+ value: sourceTokenValue(match[0])
226
+ })));
227
+ }
228
+ function mergeSourceEqualsQuotedTokenSpans(spans) {
229
+ const merged = [];
230
+ for (const span of spans) {
231
+ const previous = merged.at(-1);
232
+ if (previous != null && previous.end === span.start && previous.text.endsWith("=")) {
233
+ previous.end = span.end;
234
+ previous.text += span.text;
235
+ previous.value += span.value;
236
+ continue;
237
+ }
238
+ merged.push({ ...span });
239
+ }
240
+ return merged;
241
+ }
242
+ function sourceTokenValue(token) {
243
+ if (token.startsWith("\\\"") && token.endsWith("\\\"")) return token.slice(2, -2);
244
+ if (token.startsWith("\\'") && token.endsWith("\\'")) return token.slice(2, -2);
245
+ if (token.startsWith("\"") && token.endsWith("\"")) return token.slice(1, -1);
246
+ if (token.startsWith("'") && token.endsWith("'")) return token.slice(1, -1);
247
+ return token;
248
+ }
249
+ function replaceSourceTokenValue(token, replacement) {
250
+ if (token.startsWith("\\\"") && token.endsWith("\\\"")) return `\\"${replacement}\\"`;
251
+ if (token.startsWith("\\'") && token.endsWith("\\'")) return `\\'${replacement}\\'`;
252
+ if (token.startsWith("\"") && token.endsWith("\"")) return `"${replacement}"`;
253
+ if (token.startsWith("'") && token.endsWith("'")) return `'${replacement}'`;
254
+ return replacement;
255
+ }
256
+ function activeQuoteStart(source, start, offset) {
257
+ let quote = null;
258
+ for (let index = start; index < offset; index += 1) {
259
+ const char = source[index];
260
+ if (quote != null) {
261
+ if (quote.escaped) {
262
+ if (char === "\\") {
263
+ let runEnd = index + 1;
264
+ while (source[runEnd] === "\\") runEnd += 1;
265
+ if (runEnd === index + 1 && source[runEnd] === quote.delimiter) {
266
+ quote = null;
267
+ index = runEnd;
268
+ } else index = runEnd - 1;
269
+ }
270
+ } else if (char === "\\") index += 1;
271
+ else if (char === quote.delimiter) quote = null;
272
+ continue;
273
+ }
274
+ if (char === "\\" && (source[index + 1] === "\"" || source[index + 1] === "'")) {
275
+ quote = {
276
+ delimiter: source[index + 1],
277
+ escaped: true,
278
+ start: index
279
+ };
280
+ index += 1;
281
+ } else if (char === "\"" || char === "'") quote = {
282
+ delimiter: char,
283
+ escaped: false,
284
+ start: index
285
+ };
286
+ }
287
+ return quote?.start ?? null;
288
+ }
289
+ function skipsRunnerOptionValue(token, executable) {
290
+ const flag = token.split("=", 1)[0];
291
+ return (RUNNER_OPTION_VALUE_FLAGS.has(flag) || executable === "npm" && NPM_OPTION_VALUE_FLAGS.has(flag)) && !token.includes("=");
292
+ }
293
+ function isPotentialValueFlag(value) {
294
+ const flag = value.split("=", 1)[0];
295
+ return /^--[\w-]+$/.test(flag) || /^-\w$/.test(flag);
296
+ }
297
+ function packageRunnerPackageStartTokenIndex(tokens) {
298
+ const executable = tokens[0];
299
+ if (executable === "npx" || executable === "bunx") return 1;
300
+ else if (executable === "npm") {
301
+ let index = 1;
302
+ for (; index < tokens.length; index += 1) {
303
+ const token = tokens[index];
304
+ if (token === "exec") return index + 1;
305
+ if (token.startsWith("-")) {
306
+ if (skipsRunnerOptionValue(token, executable)) index += 1;
307
+ continue;
308
+ }
309
+ return null;
310
+ }
311
+ } else if (executable === "pnpm" || executable === "yarn") {
312
+ let index = 1;
313
+ for (; index < tokens.length; index += 1) {
314
+ const token = tokens[index];
315
+ if (token === "dlx") return index + 1;
316
+ if (token.startsWith("-")) {
317
+ if (skipsRunnerOptionValue(token, executable)) index += 1;
318
+ continue;
319
+ }
320
+ return null;
321
+ }
322
+ }
323
+ return null;
324
+ }
325
+ function renameSourcePackageRunnerTokens(value) {
326
+ return transformSourceCommandSegments(value, renameSourcePackageRunnerTokenSegment);
327
+ }
328
+ function transformSourceCommandSegments(value, transform) {
329
+ let result = "";
330
+ let segmentStart = 0;
331
+ let quote = null;
332
+ for (let index = 0; index < value.length; index += 1) {
333
+ const char = value[index];
334
+ if (quote != null) {
335
+ if (char === "\\") index += 1;
336
+ else if (char === quote) quote = null;
337
+ continue;
338
+ }
339
+ if (char === "\\" && (value[index + 1] === "\"" || value[index + 1] === "'")) {
340
+ index += 1;
341
+ continue;
342
+ }
343
+ if (char === "\"" || char === "'") {
344
+ quote = char;
345
+ continue;
346
+ }
347
+ if (char === ";" || char === "&" || char === "|") {
348
+ result += transform(value.slice(segmentStart, index)) + char;
349
+ segmentStart = index + 1;
350
+ }
351
+ }
352
+ return result + transform(value.slice(segmentStart));
353
+ }
354
+ function renameSourcePackageRunnerTokenSegment(value) {
355
+ const spans = sourceTokenSpans(value);
356
+ if (spans == null) return value;
357
+ const tokens = spans.map((span) => span.value);
358
+ const start = packageRunnerPackageStartTokenIndex(tokens);
359
+ if (start == null) return value;
360
+ const replacements = /* @__PURE__ */ new Map();
361
+ let hasPackageFlag = false;
362
+ let hasTailorPackageFlag = false;
363
+ for (let index = start; index < tokens.length; index += 1) {
364
+ const token = tokens[index];
365
+ if (SOURCE_PACKAGE_FLAG_RE.test(token)) {
366
+ hasPackageFlag = true;
367
+ if (token.includes("=")) {
368
+ const [flag, rawValue = ""] = spans[index].text.split(/=(.*)/s, 2);
369
+ const packageReplacement = renameSourcePackageToken(rawValue);
370
+ if (packageReplacement != null) {
371
+ replacements.set(index, `${flag}=${packageReplacement}`);
372
+ hasTailorPackageFlag = true;
373
+ } else if (isTailorPackageValue(sourceTokenValue(rawValue))) hasTailorPackageFlag = true;
374
+ } else {
375
+ const valueIndex = index + 1;
376
+ const value = spans[valueIndex];
377
+ if (value != null) {
378
+ const packageReplacement = renameSourcePackageToken(value.text);
379
+ if (packageReplacement != null) {
380
+ replacements.set(valueIndex, packageReplacement);
381
+ hasTailorPackageFlag = true;
382
+ } else if (isTailorPackageValue(value.value)) hasTailorPackageFlag = true;
383
+ }
384
+ index += 1;
385
+ }
386
+ continue;
387
+ }
388
+ if (token.startsWith("-")) {
389
+ if (skipsRunnerOptionValue(token, tokens[0])) index += 1;
390
+ continue;
391
+ }
392
+ if (!hasPackageFlag) {
393
+ const packageReplacement = renameSourcePackageToken(spans[index].text);
394
+ if (packageReplacement != null) replacements.set(index, packageReplacement);
395
+ break;
396
+ }
397
+ if (hasTailorPackageFlag) {
398
+ const binaryReplacement = renameSourceBinaryToken(spans[index].text);
399
+ if (binaryReplacement != null) replacements.set(index, binaryReplacement);
400
+ }
401
+ break;
402
+ }
403
+ return replacements.size === 0 ? value : replaceSourceSpans(value, replacements, spans);
404
+ }
405
+ function hasPositionalPackageBeforeSourcePackageFlag(tokens, start) {
406
+ for (let index = start; index < tokens.length; index += 1) {
407
+ const token = tokens[index];
408
+ if (SOURCE_PACKAGE_FLAG_RE.test(token)) {
409
+ if (!token.includes("=")) index += 1;
410
+ continue;
411
+ }
412
+ if (token.startsWith("-")) {
413
+ if (skipsRunnerOptionValue(token, tokens[0])) index += 1;
414
+ continue;
415
+ }
416
+ return true;
417
+ }
418
+ return false;
419
+ }
420
+ function firstRunnerPackageToken(tokens) {
421
+ const start = packageRunnerPackageStartTokenIndex(tokens);
422
+ if (start == null) return null;
423
+ for (let index = start; index < tokens.length; index += 1) {
424
+ const token = tokens[index];
425
+ if (SOURCE_PACKAGE_FLAG_RE.test(token)) return token.includes("=") ? sourceTokenValue(token.slice(token.indexOf("=") + 1)) : tokens[index + 1] ?? null;
426
+ if (token.startsWith("-")) {
427
+ if (skipsRunnerOptionValue(token, tokens[0])) index += 1;
428
+ continue;
429
+ }
430
+ return token;
431
+ }
432
+ return null;
433
+ }
434
+ function isPackageRunnerOptionValue(source, offset, flag) {
435
+ if (!RUNNER_OPTION_VALUE_FLAGS.has(flag)) return false;
436
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
437
+ const executable = sourceTokens(source.slice(segmentStart + 1, offset).trim())?.[0];
438
+ return executable === "npx" || executable === "bunx" || executable === "pnpm" || executable === "yarn";
439
+ }
440
+ function isAfterPackageRunnerPrefix(source, offset) {
441
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
442
+ const tokens = sourceTokens(source.slice(segmentStart + 1, offset).trim());
443
+ const executable = tokens?.[0];
444
+ if (executable === "npx" || executable === "bunx") return true;
445
+ return (executable === "pnpm" || executable === "yarn") && tokens?.includes("dlx") === true;
446
+ }
447
+ function isPackageFlagValueInPackageRunner(source, offset) {
448
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
449
+ const tokens = sourceTokens(source.slice(segmentStart + 1, offset).trim());
450
+ if (tokens == null) return false;
451
+ const start = packageRunnerPackageStartTokenIndex(tokens);
452
+ return start != null && !hasPositionalPackageBeforeSourcePackageFlag(tokens, start);
453
+ }
454
+ function sourcePackageFlagsAllowBinaryRewrite(source) {
455
+ const tokens = sourceTokens(source.trim());
456
+ if (tokens == null) return false;
457
+ const start = packageRunnerPackageStartTokenIndex(tokens);
458
+ if (start == null) return false;
459
+ let hasPackageFlag = false;
460
+ let hasTailorPackageFlag = false;
461
+ for (let index = start; index < tokens.length; index += 1) {
462
+ const token = tokens[index];
463
+ if (SOURCE_PACKAGE_FLAG_RE.test(token)) {
464
+ hasPackageFlag = true;
465
+ const rawValue = token.includes("=") ? token.slice(token.indexOf("=") + 1) : tokens[index + 1];
466
+ const value = rawValue == null ? null : sourceTokenValue(rawValue);
467
+ if (value != null && isTailorPackageValue(value)) hasTailorPackageFlag = true;
468
+ if (!token.includes("=")) index += 1;
469
+ continue;
470
+ }
471
+ if (token.startsWith("-")) {
472
+ if (skipsRunnerOptionValue(token, tokens[0])) index += 1;
473
+ continue;
474
+ }
475
+ return false;
476
+ }
477
+ return hasPackageFlag && hasTailorPackageFlag;
478
+ }
479
+ function isAfterOtherPackageRunner(source, offset) {
480
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
481
+ const tokens = sourceTokens(source.slice(segmentStart + 1, offset).trim());
482
+ if (tokens == null) {
483
+ const quoteStart = activeQuoteStart(source, segmentStart + 1, offset);
484
+ if (quoteStart == null) return false;
485
+ const prefixTokens = sourceTokens(source.slice(segmentStart + 1, quoteStart).trim());
486
+ const packageToken = prefixTokens == null ? null : firstRunnerPackageToken(prefixTokens);
487
+ return packageToken != null && !TAILOR_SDK_TOKEN_RE.test(packageToken);
488
+ }
489
+ const packageToken = firstRunnerPackageToken(tokens);
490
+ return packageToken != null && !TAILOR_SDK_TOKEN_RE.test(packageToken);
491
+ }
492
+ function isAfterTailorCliToken(source, offset) {
493
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
494
+ const tokens = sourceTokens(source.slice(segmentStart + 1, offset).trim());
495
+ return tokens != null && tokens.some((token) => isTailorCliTokenValue(token));
496
+ }
497
+ function isAfterTemplatePlaceholder(source, offset) {
498
+ const segmentStart = Math.max(source.lastIndexOf(";", offset - 1), source.lastIndexOf("&", offset - 1), source.lastIndexOf("|", offset - 1));
499
+ const tokens = sourceTokens(source.slice(segmentStart + 1, offset).trim());
500
+ return tokens != null && tokens.some((token) => SOURCE_TEMPLATE_EXPR_PLACEHOLDER_RE.test(token));
501
+ }
502
+ function isTemplateSubstitutionCliValue(text, offset) {
503
+ const tokens = sourceTokens(text.slice(0, offset).trimEnd());
504
+ if (tokens == null || tokens.length === 0) return false;
505
+ if (!isTailorCliValueFlag(tokens.at(-1))) return false;
506
+ return tokens.slice(0, -1).some((token) => isTailorCliTokenValue(token));
507
+ }
508
+ function needsCliRenameMigration(value) {
509
+ if (!value.includes("tailor-sdk")) return false;
510
+ const tokens = sourceTokens(value);
511
+ if (tokens == null) return CLI_RENAME_LEGACY_RE.test(value);
512
+ for (let index = 0; index < tokens.length; index += 1) {
513
+ const token = tokens[index];
514
+ if (isTailorCliValueToken(tokens, index)) continue;
515
+ if (token !== value && token.includes("tailor-sdk") && needsCliRenameMigration(token)) return true;
516
+ if (!isTailorSdkBinaryTokenValue(token)) continue;
517
+ if (tokensAfterCliBinaryNeedRename(tokens, index + 1)) return true;
518
+ }
519
+ return false;
520
+ }
521
+ function isTailorCliValueToken(tokens, index) {
522
+ const token = tokens[index];
523
+ const flag = token.split("=", 1)[0];
524
+ if (token.includes("=") && TAILOR_CLI_VALUE_FLAGS.has(flag) && tokens.slice(0, index).some((value) => isTailorCliTokenValue(value))) return true;
525
+ const previous = tokens[index - 1];
526
+ return previous != null && TAILOR_CLI_VALUE_FLAGS.has(previous.split("=", 1)[0]) && !previous.includes("=") && tokens.slice(0, index - 1).some((value) => isTailorCliTokenValue(value));
527
+ }
528
+ function tokensAfterCliBinaryNeedRename(tokens, start) {
529
+ let commandSeen = false;
530
+ for (let index = start; index < tokens.length; index += 1) {
531
+ const token = tokens[index];
532
+ if (token === "--machineuser" || token.startsWith("--machineuser=")) return true;
533
+ if (token.startsWith("-")) {
534
+ if (TAILOR_CLI_VALUE_FLAGS.has(token.split("=", 1)[0]) && !token.includes("=")) index += 1;
535
+ continue;
536
+ }
537
+ if (!commandSeen) {
538
+ if (token === "apply" || token === "crash-report") return true;
539
+ commandSeen = true;
540
+ }
541
+ }
542
+ return false;
543
+ }
544
+ function renameSourceCommandText(value) {
545
+ if (needsCliRenameMigration(value)) return value;
546
+ const protectedValue = protectSourceCliValueReferences(value);
547
+ return restoreSourceCliValueReferences(renameSourcePackageRunnerTokens(protectedValue.source.replace(SOURCE_PACKAGE_FLAG_EQUALS_QUOTED_VALUE_RE, (match, prefix, openQuote, version, closeQuote, offset, source) => {
548
+ if (!isPackageFlagValueInPackageRunner(source, offset)) return match;
549
+ return `${prefix}${openQuote}${version ? `@tailor-platform/sdk${version}` : "@tailor-platform/sdk"}${closeQuote}`;
550
+ })).replace(SOURCE_PACKAGE_FLAG_VALUE_RE, (match, prefix, version, offset, source) => {
551
+ if (!isPackageFlagValueInPackageRunner(source, offset)) return match;
552
+ return version ? `${prefix}@tailor-platform/sdk${version}` : `${prefix}@tailor-platform/sdk`;
553
+ }).replace(SOURCE_PKG_RUNNER_RE, (match, runner, version) => {
554
+ if (/\s(?:-p|--package)(?:=|\s|$)/.test(runner)) return match;
555
+ return version ? `${runner} @tailor-platform/sdk${version}` : `${runner} @tailor-platform/sdk`;
556
+ }).replace(SOURCE_PACKAGE_FLAG_BINARY_RE, (match, prefix, version) => {
557
+ if (!/\s(?:-p|--package)(?:=|\s)/.test(prefix)) return match;
558
+ if (/(?:^|\s)(?:-p|--package)\s+$/.test(prefix)) return match;
559
+ if (!sourcePackageFlagsAllowBinaryRewrite(prefix)) return match;
560
+ return version ? `${prefix}@tailor-platform/sdk${version}` : `${prefix}tailor`;
561
+ }).replace(SOURCE_TAILOR_SDK_RE, (match, shim, version, offset, source) => {
562
+ if (isAfterOtherPackageRunner(source, offset)) return match;
563
+ if (isAfterTemplatePlaceholder(source, offset)) return match;
564
+ if (shim != null) return `tailor${shim}`;
565
+ return version ? `@tailor-platform/sdk${version}` : "tailor";
566
+ }).replace(SOURCE_DYNAMIC_TAILOR_SDK_RE, (_match, prefix, shim, version) => shim != null ? `${prefix}tailor${shim}` : version ? `${prefix}@tailor-platform/sdk${version}` : `${prefix}tailor`).replace(SOURCE_DYNAMIC_OPTION_TAILOR_SDK_RE, (_match, prefix, shim, version) => shim != null ? `${prefix}tailor${shim}` : version ? `${prefix}@tailor-platform/sdk${version}` : `${prefix}tailor`), protectedValue.protectedValues);
567
+ }
568
+ function sourceLang(filePath) {
569
+ const ext = path.extname(filePath).toLowerCase();
570
+ return ext === ".tsx" || ext === ".jsx" || ext === ".js" ? Lang.Tsx : Lang.TypeScript;
571
+ }
572
+ function pushSourceTextEdit(edits, source, start, end) {
573
+ const text = source.slice(start, end);
574
+ const replacement = renameSourceCommandText(text);
575
+ if (replacement !== text) edits.push([
576
+ start,
577
+ end,
578
+ replacement
579
+ ]);
580
+ }
581
+ function nodeRangeKey(node) {
582
+ const range = node.range();
583
+ return `${range.start.index}:${range.end.index}`;
584
+ }
585
+ function sourceStringContent(node, source) {
586
+ const kind = node.kind();
587
+ if (kind !== "string" && kind !== "template_string") return null;
588
+ if (kind === "template_string" && node.children().some((child) => child.kind() === "template_substitution")) return null;
589
+ const range = node.range();
590
+ return source.slice(range.start.index + 1, range.end.index - 1);
591
+ }
592
+ function sourceStringRawContent(node, source) {
593
+ const kind = node.kind();
594
+ if (kind !== "string" && kind !== "template_string") return null;
595
+ const range = node.range();
596
+ return source.slice(range.start.index + 1, range.end.index - 1);
597
+ }
598
+ function sourceConstInitializerContent(node, source) {
599
+ const directValue = sourceStringContent(node, source);
600
+ if (directValue != null) return directValue;
601
+ if (node.kind() !== "as_expression" && node.kind() !== "satisfies_expression" && node.kind() !== "parenthesized_expression") return null;
602
+ for (const child of node.children()) {
603
+ const childValue = sourceConstInitializerContent(child, source);
604
+ if (childValue != null) return childValue;
605
+ }
606
+ return null;
607
+ }
608
+ function isConstVariableDeclarator(node) {
609
+ return node.parent()?.children().some((child) => child.kind() === "const") ?? false;
610
+ }
611
+ function sourceStaticStringContent(node, source) {
612
+ return sourceStringContent(node, source) ?? (node.kind() === "identifier" ? sourceScopedStringVariableContent(node, source) : null);
613
+ }
614
+ function sourceScopedStringVariableContent(identifier, source) {
615
+ const name = identifier.text();
616
+ const before = identifier.range().start.index;
617
+ let current = identifier.parent();
618
+ while (current != null) {
619
+ if (isSourceScopeNode(current)) {
620
+ const value = findSourceStringVariableInScope(current, name, before, source);
621
+ if (value != null) return value;
622
+ }
623
+ current = current.parent();
624
+ }
625
+ return null;
626
+ }
627
+ function findSourceStringVariableInScope(scope, name, before, source) {
628
+ let value = null;
629
+ const visit = (node) => {
630
+ if (node !== scope && isSourceScopeNode(node)) return;
631
+ if (node.kind() === "variable_declarator" && node.range().end.index < before) {
632
+ const declarationValue = sourceStringVariableDeclarationValue(node, name, source);
633
+ if (declarationValue != null) value = declarationValue;
634
+ return;
635
+ }
636
+ for (const child of node.children()) visit(child);
637
+ };
638
+ visit(scope);
639
+ return value;
640
+ }
641
+ function sourceStringVariableDeclarationValue(node, name, source) {
642
+ if (!isConstVariableDeclarator(node)) return null;
643
+ const children = node.children();
644
+ if (children.find((child) => child.kind() === "identifier")?.text() !== name) return null;
645
+ const initializer = children.findLast((child) => sourceConstInitializerContent(child, source) != null);
646
+ return initializer == null ? null : sourceConstInitializerContent(initializer, source);
647
+ }
648
+ function isSourceScopeNode(node) {
649
+ const kind = node.kind();
650
+ return kind === "program" || kind === "statement_block" || kind === "function_declaration" || kind === "arrow_function" || kind === "method_definition";
651
+ }
652
+ function isSyntaxOnlyNode(node) {
653
+ const kind = node.kind();
654
+ return kind === "[" || kind === "]" || kind === "(" || kind === ")" || kind === "," || kind === "comment";
655
+ }
656
+ function sourceArrayElements(node) {
657
+ return node.children().filter((child) => !isSyntaxOnlyNode(child));
658
+ }
659
+ function nodeIndex(nodes, node) {
660
+ return nodes.findIndex((child) => nodeRangeKey(child) === nodeRangeKey(node));
661
+ }
662
+ function callExpressionCalleeText(argumentsNode) {
663
+ const parentRange = nodeRangeKey(argumentsNode);
664
+ const call = argumentsNode.parent();
665
+ if (call?.kind() !== "call_expression") return null;
666
+ return call.children().find((child) => nodeRangeKey(child) !== parentRange)?.text() ?? null;
667
+ }
668
+ function firstNonOptionIndex(elements, start, source, executable) {
669
+ for (let index = start; index < elements.length; index += 1) {
670
+ const value = sourceStringContent(elements[index], source);
671
+ if (value == null) return null;
672
+ if (!value.startsWith("-")) return index;
673
+ if (skipsRunnerOptionValue(value, executable)) index += 1;
674
+ }
675
+ return null;
676
+ }
677
+ function isTailorCliValueFlag(value) {
678
+ return TAILOR_CLI_VALUE_FLAGS.has(value.split("=", 1)[0]) || isPotentialValueFlag(value);
679
+ }
680
+ function packageRunnerPackageStartIndex(executable, elements, source) {
681
+ if (SOURCE_PACKAGE_RUNNERS.has(executable)) return 0;
682
+ if (SOURCE_NPM_EXEC_PACKAGE_RUNNERS.has(executable)) {
683
+ const execIndex = firstNonOptionIndex(elements, 0, source, executable);
684
+ if (execIndex == null || sourceStringContent(elements[execIndex], source) !== "exec") return null;
685
+ return execIndex + 1;
686
+ }
687
+ if (!SOURCE_DLX_PACKAGE_RUNNERS.has(executable)) return null;
688
+ const dlxIndex = firstNonOptionIndex(elements, 0, source, executable);
689
+ if (dlxIndex == null || sourceStringContent(elements[dlxIndex], source) !== "dlx") return null;
690
+ return dlxIndex + 1;
691
+ }
692
+ function hasPackageFlagBeforeArrayPackage(elements, index, source, start, executable) {
693
+ for (let currentIndex = start; currentIndex < index; currentIndex += 1) {
694
+ const value = sourceStringContent(elements[currentIndex], source);
695
+ if (value == null) return false;
696
+ if (SOURCE_PACKAGE_FLAG_RE.test(value)) return true;
697
+ if (value.startsWith("-")) {
698
+ if (skipsRunnerOptionValue(value, executable)) currentIndex += 1;
699
+ continue;
700
+ }
701
+ return false;
702
+ }
703
+ return false;
704
+ }
705
+ function isKnownTailorPackageValue(value) {
706
+ return value != null && (TAILOR_SDK_TOKEN_RE.test(value) || TAILOR_PLATFORM_SDK_TOKEN_RE.test(value));
707
+ }
708
+ function hasTailorPackageFlagBeforeArrayCommand(elements, index, source, start, executable) {
709
+ for (let currentIndex = start; currentIndex < index; currentIndex += 1) {
710
+ const value = sourceStringContent(elements[currentIndex], source);
711
+ if (value == null) return false;
712
+ if (SOURCE_PACKAGE_FLAG_RE.test(value)) {
713
+ if (value.includes("=")) {
714
+ if (isKnownTailorPackageValue(value.slice(value.indexOf("=") + 1))) return true;
715
+ } else {
716
+ if (isKnownTailorPackageValue(sourceStringContent(elements[currentIndex + 1], source))) return true;
717
+ currentIndex += 1;
718
+ }
719
+ continue;
720
+ }
721
+ if (value.startsWith("-")) {
722
+ if (skipsRunnerOptionValue(value, executable)) currentIndex += 1;
723
+ continue;
724
+ }
725
+ return false;
726
+ }
727
+ return false;
728
+ }
729
+ function isSplitPackageFlagValue(elements, index, source, start, executable) {
730
+ const previous = elements[index - 1];
731
+ if (previous == null) return false;
732
+ const previousValue = sourceStringContent(previous, source);
733
+ return (previousValue === "-p" || previousValue === "--package") && hasPackageFlagBeforeArrayPackage(elements, index, source, start, executable);
734
+ }
735
+ function sourcePackageFlagReplacement(node, source) {
736
+ const text = sourceStringContent(node, source);
737
+ if (text == null) return null;
738
+ const match = /^(?<prefix>-p=|--package=)tailor-sdk(?<version>@[^\s'"`;|&)]+)?$/.exec(text);
739
+ if (match?.groups == null) return null;
740
+ const parent = node.parent();
741
+ if (parent?.kind() !== "array" || !isPackageRunnerArrayArgument(node, source)) return null;
742
+ const argumentsNode = parent.parent();
743
+ if (argumentsNode?.kind() !== "arguments") return null;
744
+ const executableNode = sourceArrayElements(argumentsNode)[0];
745
+ if (executableNode == null) return null;
746
+ const executable = sourceStringContent(executableNode, source);
747
+ if (executable == null) return null;
748
+ const elements = sourceArrayElements(parent);
749
+ const index = nodeIndex(elements, node);
750
+ const start = packageRunnerPackageStartIndex(executable, elements, source);
751
+ if (index < 0 || start == null) return null;
752
+ if (!hasPackageFlagBeforeArrayPackage(elements, index + 1, source, start, executable)) return null;
753
+ return {
754
+ text,
755
+ replacement: `${match.groups.prefix}${renamePackageName(`tailor-sdk${match.groups.version ?? ""}`)}`
756
+ };
757
+ }
758
+ function firstTailorPackageIndex(elements, start, source, executable) {
759
+ for (let index = start; index < elements.length; index += 1) {
760
+ const value = sourceStringContent(elements[index], source);
761
+ if (value == null) return null;
762
+ if (value.startsWith("-")) {
763
+ if (skipsRunnerOptionValue(value, executable)) index += 1;
764
+ continue;
765
+ }
766
+ if (TAILOR_SDK_TOKEN_RE.test(value)) return index;
767
+ return null;
768
+ }
769
+ return null;
770
+ }
771
+ function isPackageRunnerArrayArgument(node, source) {
772
+ const parent = node.parent();
773
+ if (parent?.kind() !== "array") return false;
774
+ const argumentsNode = parent.parent();
775
+ if (argumentsNode?.kind() !== "arguments") return false;
776
+ const callee = callExpressionCalleeText(argumentsNode);
777
+ if (!CLI_ARGUMENT_CALLEE_RE.test(callee ?? "")) return false;
778
+ const executableNode = sourceArrayElements(argumentsNode)[0];
779
+ if (executableNode == null) return false;
780
+ const executable = sourceStringContent(executableNode, source);
781
+ if (executable == null) return false;
782
+ if (executable === "bunx" || executable === "npx") return true;
783
+ if (executable === "npm") {
784
+ const elements = sourceArrayElements(parent);
785
+ const execIndex = firstNonOptionIndex(elements, 0, source, executable);
786
+ return execIndex != null && sourceStringContent(elements[execIndex], source) === "exec";
787
+ }
788
+ if (executable !== "pnpm" && executable !== "yarn") return false;
789
+ const elements = sourceArrayElements(parent);
790
+ const dlxIndex = firstNonOptionIndex(elements, 0, source, executable);
791
+ return dlxIndex != null && sourceStringContent(elements[dlxIndex], source) === "dlx";
792
+ }
793
+ function isPackageRunnerPackageArgument(node, source) {
794
+ const parent = node.parent();
795
+ if (parent?.kind() !== "array" || !isPackageRunnerArrayArgument(node, source)) return false;
796
+ const argumentsNode = parent.parent();
797
+ if (argumentsNode?.kind() !== "arguments") return false;
798
+ const executableNode = sourceArrayElements(argumentsNode)[0];
799
+ if (executableNode == null) return false;
800
+ const executable = sourceStringContent(executableNode, source);
801
+ if (executable == null) return false;
802
+ const elements = sourceArrayElements(parent);
803
+ const index = nodeIndex(elements, node);
804
+ if (index < 0) return false;
805
+ const start = packageRunnerPackageStartIndex(executable, elements, source);
806
+ if (start == null) return false;
807
+ if (hasPackageFlagBeforeArrayPackage(elements, index, source, start, executable)) return isSplitPackageFlagValue(elements, index, source, start, executable);
808
+ return firstTailorPackageIndex(elements, start, source, executable) === index && !arrayHasCliRenameLegacyArgs(elements, index + 1, source);
809
+ }
810
+ function isPackageRunnerCommandBinaryArgument(node, source) {
811
+ const parent = node.parent();
812
+ if (parent?.kind() !== "array" || !isPackageRunnerArrayArgument(node, source)) return false;
813
+ const elements = sourceArrayElements(parent);
814
+ const index = nodeIndex(elements, node);
815
+ if (index < 0) return false;
816
+ const argumentsNode = parent.parent();
817
+ if (argumentsNode?.kind() !== "arguments") return false;
818
+ const executableNode = sourceArrayElements(argumentsNode)[0];
819
+ if (executableNode == null) return false;
820
+ const executable = sourceStringContent(executableNode, source);
821
+ if (executable == null) return false;
822
+ const start = packageRunnerPackageStartIndex(executable, elements, source);
823
+ if (start == null || !hasTailorPackageFlagBeforeArrayCommand(elements, index, source, start, executable)) return false;
824
+ if (arrayHasCliRenameLegacyArgs(elements, index + 1, source)) return false;
825
+ if (packageRunnerCommandIndex(elements, start, source, executable) !== index) return false;
826
+ const text = sourceStringContent(node, source);
827
+ return text != null && TAILOR_SDK_TOKEN_RE.test(text) && !isSplitPackageFlagValue(elements, index, source, start, executable);
828
+ }
829
+ function packageRunnerCommandIndex(elements, start, source, executable) {
830
+ for (let index = start; index < elements.length; index += 1) {
831
+ const value = sourceStringContent(elements[index], source);
832
+ if (value == null) return null;
833
+ if (SOURCE_PACKAGE_FLAG_RE.test(value)) {
834
+ if (!value.includes("=")) index += 1;
835
+ continue;
836
+ }
837
+ if (value.startsWith("-")) {
838
+ if (skipsRunnerOptionValue(value, executable)) index += 1;
839
+ continue;
840
+ }
841
+ return index;
842
+ }
843
+ return null;
844
+ }
845
+ function isPackageManagerExecBinaryArgument(node, source) {
846
+ const parent = node.parent();
847
+ if (parent?.kind() !== "array") return false;
848
+ const argumentsNode = parent.parent();
849
+ if (argumentsNode?.kind() !== "arguments") return false;
850
+ const callee = callExpressionCalleeText(argumentsNode);
851
+ if (!CLI_ARGUMENT_CALLEE_RE.test(callee ?? "")) return false;
852
+ const executableNode = sourceArrayElements(argumentsNode)[0];
853
+ if (executableNode == null) return false;
854
+ const executable = sourceStringContent(executableNode, source);
855
+ if (executable == null || !SOURCE_EXEC_PACKAGE_MANAGERS.has(executable)) return false;
856
+ const elements = sourceArrayElements(parent);
857
+ const index = nodeIndex(elements, node);
858
+ if (index < 0) return false;
859
+ const execIndex = firstNonOptionIndex(elements, 0, source, executable);
860
+ if (execIndex == null || sourceStringContent(elements[execIndex], source) !== "exec") return false;
861
+ return firstNonOptionIndex(elements, execIndex + 1, source, executable) === index && !arrayHasCliRenameLegacyArgs(elements, index + 1, source);
862
+ }
863
+ function isCliBinaryArgument(node, source) {
864
+ const parent = node.parent();
865
+ if (parent?.kind() === "array") {
866
+ if (isPackageRunnerCommandBinaryArgument(node, source)) return true;
867
+ if (isPackageRunnerArrayArgument(node, source)) return false;
868
+ return isPackageManagerExecBinaryArgument(node, source);
869
+ }
870
+ if (parent?.kind() !== "arguments") return false;
871
+ if (!CLI_ARGUMENT_CALLEE_RE.test(callExpressionCalleeText(parent) ?? "")) return false;
872
+ const args = sourceArrayElements(parent);
873
+ if (args[0] == null || nodeRangeKey(args[0]) !== nodeRangeKey(node)) return false;
874
+ const argv = args[1];
875
+ if (argv == null) return true;
876
+ if (argv.kind() === "object") return true;
877
+ return argv.kind() === "array" && !arrayHasCliRenameLegacyArgs(sourceArrayElements(argv), 0, source);
878
+ }
879
+ function arrayHasCliRenameLegacyArgs(elements, start, source) {
880
+ let commandSeen = false;
881
+ for (let index = start; index < elements.length; index += 1) {
882
+ const value = sourceStaticStringContent(elements[index], source) ?? sourceStringRawContent(elements[index], source);
883
+ if (value == null) continue;
884
+ if (value === "--machineuser" || value.startsWith("--machineuser=")) return true;
885
+ if (TAILOR_CLI_VALUE_FLAGS.has(value.split("=", 1)[0]) || commandSeen && TAILOR_CLI_COMMAND_VALUE_FLAGS.has(value.split("=", 1)[0])) {
886
+ if (!value.includes("=")) index += 1;
887
+ continue;
888
+ }
889
+ if (value.startsWith("-")) continue;
890
+ if (!commandSeen) {
891
+ if (CLI_RENAME_LEGACY_RE.test(value)) return true;
892
+ if (value === "apply" || value === "crash-report") return true;
893
+ commandSeen = true;
894
+ }
895
+ }
896
+ return false;
897
+ }
898
+ function isTailorCliArgumentArray(arrayNode, index, source) {
899
+ const argumentsNode = arrayNode.parent();
900
+ if (argumentsNode?.kind() === "arguments") {
901
+ const callArgs = sourceArrayElements(argumentsNode);
902
+ const executable = callArgs[0] == null ? null : sourceStaticStringContent(callArgs[0], source);
903
+ if (executable != null && isTailorCliTokenValue(executable)) return true;
904
+ }
905
+ return sourceArrayElements(arrayNode).slice(0, index).some((element) => {
906
+ const value = sourceStaticStringContent(element, source);
907
+ return value != null && isTailorCliTokenValue(value);
908
+ });
909
+ }
910
+ function isCliValueArgument(node, source) {
911
+ const parent = node.parent();
912
+ if (parent?.kind() !== "array") return false;
913
+ const elements = sourceArrayElements(parent);
914
+ const index = nodeIndex(elements, node);
915
+ if (index < 0) return false;
916
+ if (!isTailorCliArgumentArray(parent, index, source)) return false;
917
+ const text = sourceStringContent(node, source) ?? sourceStringRawContent(node, source);
918
+ if (text != null && text.includes("tailor-sdk") && isTailorCliValueFlag(text) && text.includes("=")) return true;
919
+ if (index === 0) return false;
920
+ const previousValue = sourceStaticStringContent(elements[index - 1], source);
921
+ return text != null && text.includes("tailor-sdk") && previousValue != null && isTailorCliValueFlag(previousValue) && !previousValue.includes("=");
922
+ }
923
+ function pushSourceStringEdit(edits, source, node) {
924
+ const range = node.range();
925
+ const start = range.start.index + 1;
926
+ const end = range.end.index - 1;
927
+ const text = source.slice(start, end);
928
+ const packageFlagReplacement = sourcePackageFlagReplacement(node, source);
929
+ const replacement = packageFlagReplacement != null ? packageFlagReplacement.replacement : TAILOR_SDK_TOKEN_RE.test(text) && isPackageRunnerPackageArgument(node, source) ? renamePackageName(text) : (TAILOR_SDK_TOKEN_RE.test(text) || TAILOR_SDK_PATH_RE.test(text)) && isCliBinaryArgument(node, source) ? renameBinary(text) : isCliValueArgument(node, source) ? text : renameSourceCommandText(text);
930
+ if (replacement !== text) edits.push([
931
+ start,
932
+ end,
933
+ replacement
934
+ ]);
935
+ }
936
+ function templateSubstitutionPlaceholder(index, text, usedPlaceholders) {
937
+ let attempt = 0;
938
+ while (true) {
939
+ const placeholder = `__TAILOR_SDK_TEMPLATE_EXPR_${index}_${attempt}__`;
940
+ if (!text.includes(placeholder) && !usedPlaceholders.has(placeholder)) {
941
+ usedPlaceholders.add(placeholder);
942
+ return placeholder;
943
+ }
944
+ attempt += 1;
945
+ }
946
+ }
947
+ function restoreTemplateMigrationText(text, substitutions) {
948
+ let restored = text;
949
+ for (const substitution of substitutions) restored = restored.replaceAll(substitution.placeholder, () => substitution.migrationText);
950
+ return restored;
951
+ }
952
+ function hasSourceTailorSdkReference(value) {
953
+ TAILOR_SDK_RE.lastIndex = 0;
954
+ const matches = TAILOR_SDK_RE.test(value);
955
+ TAILOR_SDK_RE.lastIndex = 0;
956
+ return matches;
957
+ }
958
+ function hasOnlyProtectedSourceCliValueLegacyToken(value) {
959
+ const protectedValue = protectSourceCliValueReferences(value);
960
+ return protectedValue.protectedValues.length > 0 && !hasSourceTailorSdkReference(protectedValue.source);
961
+ }
962
+ function templateSubstitutionMigrationText(node, source, value) {
963
+ const identifier = node.children().find((child) => child.kind() === "identifier");
964
+ if (identifier != null && node.text() === `\${${identifier.text()}}`) {
965
+ const identifierValue = sourceStaticStringContent(identifier, source);
966
+ if (identifierValue != null) return identifierValue;
967
+ }
968
+ if (!value.startsWith("${") || !value.endsWith("}")) return value;
969
+ return value.slice(2, -1).trim();
970
+ }
971
+ function pushTemplateStringEdit(edits, source, node) {
972
+ const range = node.range();
973
+ const start = range.start.index + 1;
974
+ const end = range.end.index - 1;
975
+ let text = source.slice(start, end);
976
+ const substitutions = [];
977
+ const usedPlaceholders = /* @__PURE__ */ new Set();
978
+ const substitutionNodes = node.children().filter((child) => child.kind() === "template_substitution");
979
+ for (const child of substitutionNodes.toReversed()) {
980
+ const childRange = child.range();
981
+ const childStart = childRange.start.index - start;
982
+ const childEnd = childRange.end.index - start;
983
+ const placeholder = templateSubstitutionPlaceholder(substitutions.length, text, usedPlaceholders);
984
+ const substitutionText = isTemplateSubstitutionCliValue(text, childStart) ? source.slice(childRange.start.index, childRange.end.index) : transformTemplateSubstitutionText(source.slice(childRange.start.index, childRange.end.index));
985
+ substitutions.push({
986
+ placeholder,
987
+ text: substitutionText,
988
+ migrationText: templateSubstitutionMigrationText(child, source, substitutionText)
989
+ });
990
+ text = `${text.slice(0, childStart)}${placeholder}${text.slice(childEnd)}`;
991
+ }
992
+ const migrationText = restoreTemplateMigrationText(text, substitutions);
993
+ if (hasOnlyProtectedSourceCliValueLegacyToken(migrationText)) return;
994
+ let replacement = needsCliRenameMigration(migrationText) ? text : renameSourceCommandText(text);
995
+ for (const substitution of substitutions) replacement = replacement.replaceAll(substitution.placeholder, () => substitution.text);
996
+ if (replacement !== source.slice(start, end)) edits.push([
997
+ start,
998
+ end,
999
+ replacement
1000
+ ]);
1001
+ }
1002
+ function transformTemplateSubstitutionText(value) {
1003
+ if (!value.includes("tailor-sdk") || !value.startsWith("${") || !value.endsWith("}")) return value;
1004
+ const transformed = transformSourceFile(value.slice(2, -1), "template-expression.ts");
1005
+ return transformed == null ? value : `\${${transformed}}`;
1006
+ }
1007
+ function transformSourceFile(source, filePath) {
1008
+ let root;
1009
+ try {
1010
+ root = parse(sourceLang(filePath), source).root();
1011
+ } catch {
1012
+ return null;
1013
+ }
1014
+ const edits = [];
1015
+ const visit = (node) => {
1016
+ const kind = node.kind();
1017
+ const range = node.range();
1018
+ if (kind === "comment" || kind === "jsx_text" || kind === "string_fragment") {
1019
+ pushSourceTextEdit(edits, source, range.start.index, range.end.index);
1020
+ return;
1021
+ }
1022
+ if (kind === "string") {
1023
+ pushSourceStringEdit(edits, source, node);
1024
+ return;
1025
+ }
1026
+ if (kind === "template_string") {
1027
+ if (!node.children().some((child) => child.kind() === "template_substitution")) {
1028
+ pushSourceStringEdit(edits, source, node);
1029
+ return;
1030
+ }
1031
+ if (isCliValueArgument(node, source)) return;
1032
+ pushTemplateStringEdit(edits, source, node);
1033
+ return;
1034
+ }
1035
+ for (const child of node.children()) visit(child);
1036
+ };
1037
+ visit(root);
1038
+ if (edits.length === 0) return null;
1039
+ let updated = source;
1040
+ for (const [start, end, replacement] of edits.toSorted(([a], [b]) => b - a)) updated = `${updated.slice(0, start)}${replacement}${updated.slice(end)}`;
1041
+ return updated === source ? null : updated;
1042
+ }
1043
+ function transformPackageJson(source) {
1044
+ let parsed;
1045
+ try {
1046
+ parsed = JSON.parse(source);
1047
+ } catch {
1048
+ return null;
1049
+ }
1050
+ let modified = false;
1051
+ const scripts = parsed.scripts;
1052
+ if (typeof scripts === "object" && scripts != null && !Array.isArray(scripts)) for (const [name, value] of Object.entries(scripts)) {
1053
+ if (typeof value !== "string") continue;
1054
+ if (!value.includes("tailor-sdk")) continue;
1055
+ const updated = renameBinary(value);
1056
+ if (updated !== value) {
1057
+ scripts[name] = updated;
1058
+ modified = true;
1059
+ }
1060
+ }
1061
+ if (!modified) return null;
1062
+ const trailing = source.endsWith("\n") ? "\n" : "";
1063
+ return JSON.stringify(parsed, null, 2) + trailing;
1064
+ }
1065
+ /**
1066
+ * Rename `tailor-sdk` binary references to `tailor`.
1067
+ *
1068
+ * Handles optional `@version` pins:
1069
+ * - `npx tailor-sdk@latest` → `npx @tailor-platform/sdk@latest` (package-runner form)
1070
+ * - `npx -y tailor-sdk login` → `npx -y @tailor-platform/sdk login` (runner flags preserved)
1071
+ * - `pnpm dlx tailor-sdk@latest` → `pnpm dlx @tailor-platform/sdk@latest` (package-runner form)
1072
+ * - `tailor-sdk@latest` elsewhere → `@tailor-platform/sdk@latest`
1073
+ * Does not rewrite `.tailor-sdk` directory paths or `create-tailor-sdk`.
1074
+ * @param source - File contents
1075
+ * @param filePath - Absolute path to the file (used to dispatch package.json vs text)
1076
+ * @returns Transformed source or null when nothing matched.
1077
+ */
1078
+ function transform(source, filePath) {
1079
+ if (!source.includes("tailor-sdk")) return null;
1080
+ const ext = path.extname(filePath).toLowerCase();
1081
+ if (ext === ".json") return transformPackageJson(source);
1082
+ if (SOURCE_EXTENSIONS.has(ext)) return transformSourceFile(source, filePath);
1083
+ const updated = renameBinary(source);
1084
+ return updated === source ? null : updated;
1085
+ }
1086
+ //#endregion
1087
+ export { transform as default };