create-mastra 1.0.0-beta.1 → 1.0.0-beta.11

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 CHANGED
@@ -4,26 +4,26 @@ import { randomUUID } from 'node:crypto';
4
4
  import * as fs3__default from 'node:fs';
5
5
  import fs3__default__default, { existsSync, readFileSync, writeFileSync } from 'node:fs';
6
6
  import os from 'node:os';
7
- import path, { dirname } from 'node:path';
7
+ import path3, { dirname } from 'node:path';
8
8
  import { fileURLToPath } from 'node:url';
9
9
  import { PostHog } from 'posthog-node';
10
+ import fs4 from 'node:fs/promises';
10
11
  import util, { stripVTControlCharacters } from 'node:util';
11
12
  import y$1, { stdout, stdin } from 'node:process';
12
13
  import * as g from 'node:readline';
13
14
  import g__default from 'node:readline';
14
15
  import { Writable } from 'node:stream';
15
- import fs4 from 'node:fs/promises';
16
16
  import child_process from 'node:child_process';
17
17
  import tty from 'node:tty';
18
18
  import fsExtra, { readJSON, ensureFile, writeJSON } from 'fs-extra/esm';
19
19
  import prettier from 'prettier';
20
20
  import { execa } from 'execa';
21
+ import fsExtra$1 from 'fs-extra';
21
22
  import pino from 'pino';
22
23
  import pretty from 'pino-pretty';
23
- import fsExtra$1 from 'fs-extra';
24
24
 
25
25
  var __filename = fileURLToPath(import.meta.url);
26
- var __dirname = path.dirname(__filename);
26
+ var __dirname = path3.dirname(__filename);
27
27
  var analyticsInstance = null;
28
28
  function getAnalytics() {
29
29
  return analyticsInstance;
@@ -39,7 +39,7 @@ var PosthogAnalytics = class {
39
39
  host = "https://app.posthog.com"
40
40
  }) {
41
41
  this.version = version;
42
- const cliConfigPath = path.join(__dirname, "mastra-cli.json");
42
+ const cliConfigPath = path3.join(__dirname, "mastra-cli.json");
43
43
  if (existsSync(cliConfigPath)) {
44
44
  try {
45
45
  const { distinctId, sessionId } = JSON.parse(readFileSync(cliConfigPath, "utf-8"));
@@ -67,7 +67,7 @@ var PosthogAnalytics = class {
67
67
  }
68
68
  writeCliConfig({ distinctId, sessionId }) {
69
69
  try {
70
- writeFileSync(path.join(__dirname, "mastra-cli.json"), JSON.stringify({ distinctId, sessionId }));
70
+ writeFileSync(path3.join(__dirname, "mastra-cli.json"), JSON.stringify({ distinctId, sessionId }));
71
71
  } catch {
72
72
  }
73
73
  }
@@ -382,7 +382,7 @@ function DD({onlyFirst:e=false}={}){const t=["[\\u001B\\u009B][[\\]()#;?]*(?:(?:
382
382
  `).length-1;this.output.write(srcExports.cursor.move(-999,u*-1));}render(){const u=Y$1(this._render(this)??"",process.stdout.columns,{hard:true});if(u!==this._prevFrame){if(this.state==="initial")this.output.write(srcExports.cursor.hide);else {const t=BD(this._prevFrame,u);if(this.restoreCursor(),t&&t?.length===1){const F=t[0];this.output.write(srcExports.cursor.move(0,F)),this.output.write(srcExports.erase.lines(1));const s=u.split(`
383
383
  `);this.output.write(s[F]),this._prevFrame=u,this.output.write(srcExports.cursor.move(0,s.length-F-1));return}if(t&&t?.length>1){const F=t[0];this.output.write(srcExports.cursor.move(0,F)),this.output.write(srcExports.erase.down());const s=u.split(`
384
384
  `).slice(F);this.output.write(s.join(`
385
- `)),this._prevFrame=u;return}this.output.write(srcExports.erase.down());}this.output.write(u),this.state==="initial"&&(this.state="active"),this._prevFrame=u;}}}var OD=Object.defineProperty,PD=(e,u,t)=>u in e?OD(e,u,{enumerable:true,configurable:true,writable:true,value:t}):e[u]=t,J=(e,u,t)=>(PD(e,typeof u!="symbol"?u+"":u,t),t);class LD extends x{constructor(u){super(u,false),J(this,"options"),J(this,"cursor",0),this.options=u.options,this.cursor=this.options.findIndex(({value:t})=>t===u.initialValue),this.cursor===-1&&(this.cursor=0),this.changeValue(),this.on("cursor",t=>{switch(t){case "left":case "up":this.cursor=this.cursor===0?this.options.length-1:this.cursor-1;break;case "down":case "right":this.cursor=this.cursor===this.options.length-1?0:this.cursor+1;break}this.changeValue();});}get _value(){return this.options[this.cursor]}changeValue(){this.value=this._value.value;}}class RD extends x{get valueWithCursor(){if(this.state==="submit")return this.value;if(this.cursor>=this.value.length)return `${this.value}\u2588`;const u=this.value.slice(0,this.cursor),[t,...F]=this.value.slice(this.cursor);return `${u}${color2.inverse(t)}${F.join("")}`}get cursor(){return this._cursor}constructor(u){super(u),this.on("finalize",()=>{this.value||(this.value=u.defaultValue);});}}
385
+ `)),this._prevFrame=u;return}this.output.write(srcExports.erase.down());}this.output.write(u),this.state==="initial"&&(this.state="active"),this._prevFrame=u;}}}class dD extends x{get cursor(){return this.value?0:1}get _value(){return this.cursor===0}constructor(u){super(u,false),this.value=!!u.initialValue,this.on("value",()=>{this.value=this._value;}),this.on("confirm",t=>{this.output.write(srcExports.cursor.move(0,-1)),this.value=t,this.state="submit",this.close();}),this.on("cursor",()=>{this.value=!this.value;});}}var OD=Object.defineProperty,PD=(e,u,t)=>u in e?OD(e,u,{enumerable:true,configurable:true,writable:true,value:t}):e[u]=t,J=(e,u,t)=>(PD(e,typeof u!="symbol"?u+"":u,t),t);class LD extends x{constructor(u){super(u,false),J(this,"options"),J(this,"cursor",0),this.options=u.options,this.cursor=this.options.findIndex(({value:t})=>t===u.initialValue),this.cursor===-1&&(this.cursor=0),this.changeValue(),this.on("cursor",t=>{switch(t){case "left":case "up":this.cursor=this.cursor===0?this.options.length-1:this.cursor-1;break;case "down":case "right":this.cursor=this.cursor===this.options.length-1?0:this.cursor+1;break}this.changeValue();});}get _value(){return this.options[this.cursor]}changeValue(){this.value=this._value.value;}}class RD extends x{get valueWithCursor(){if(this.state==="submit")return this.value;if(this.cursor>=this.value.length)return `${this.value}\u2588`;const u=this.value.slice(0,this.cursor),[t,...F]=this.value.slice(this.cursor);return `${u}${color2.inverse(t)}${F.join("")}`}get cursor(){return this._cursor}constructor(u){super(u),this.on("finalize",()=>{this.value||(this.value=u.defaultValue);});}}
386
386
 
387
387
  function ce(){return y$1.platform!=="win32"?y$1.env.TERM!=="linux":!!y$1.env.CI||!!y$1.env.WT_SESSION||!!y$1.env.TERMINUS_SUBLIME||y$1.env.ConEmuTask==="{cmd::Cmder}"||y$1.env.TERM_PROGRAM==="Terminus-Sublime"||y$1.env.TERM_PROGRAM==="vscode"||y$1.env.TERM==="xterm-256color"||y$1.env.TERM==="alacritty"||y$1.env.TERMINAL_EMULATOR==="JetBrains-JediTerm"}const V=ce(),u=(t,n)=>V?t:n,le=u("\u25C6","*"),L=u("\u25A0","x"),W=u("\u25B2","x"),C=u("\u25C7","o"),ue=u("\u250C","T"),o=u("\u2502","|"),d=u("\u2514","\u2014"),k=u("\u25CF",">"),P=u("\u25CB"," "),_=u("\u2500","-"),me=u("\u256E","+"),de=u("\u251C","+"),pe=u("\u256F","+"),q=u("\u25CF","\u2022"),D=u("\u25C6","*"),U=u("\u25B2","!"),K=u("\u25A0","x"),b=t=>{switch(t){case "initial":case "active":return color2.cyan(le);case "cancel":return color2.red(L);case "error":return color2.yellow(W);case "submit":return color2.green(C)}},G=t=>{const{cursor:n,options:r,style:i}=t,s=t.maxItems??Number.POSITIVE_INFINITY,c=Math.max(process.stdout.rows-4,0),a=Math.min(c,Math.max(s,5));let l=0;n>=l+a-3?l=Math.max(Math.min(n-a+3,r.length-a),0):n<l+2&&(l=Math.max(n-2,0));const $=a<r.length&&l>0,g=a<r.length&&l+a<r.length;return r.slice(l,l+a).map((p,v,f)=>{const j=v===0&&$,E=v===f.length-1&&g;return j||E?color2.dim("..."):i(p,v+l===n)})},he=t=>new RD({validate:t.validate,placeholder:t.placeholder,defaultValue:t.defaultValue,initialValue:t.initialValue,render(){const n=`${color2.gray(o)}
388
388
  ${b(this.state)} ${t.message}
@@ -392,7 +392,12 @@ ${color2.yellow(d)} ${color2.yellow(this.error)}
392
392
  `;case "submit":return `${n}${color2.gray(o)} ${color2.dim(this.value||t.placeholder)}`;case "cancel":return `${n}${color2.gray(o)} ${color2.strikethrough(color2.dim(this.value??""))}${this.value?.trim()?`
393
393
  ${color2.gray(o)}`:""}`;default:return `${n}${color2.cyan(o)} ${i}
394
394
  ${color2.cyan(d)}
395
- `}}}).prompt(),ve=t=>{const n=(r,i)=>{const s=r.label??String(r.value);switch(i){case "selected":return `${color2.dim(s)}`;case "active":return `${color2.green(k)} ${s} ${r.hint?color2.dim(`(${r.hint})`):""}`;case "cancelled":return `${color2.strikethrough(color2.dim(s))}`;default:return `${color2.dim(P)} ${color2.dim(s)}`}};return new LD({options:t.options,initialValue:t.initialValue,render(){const r=`${color2.gray(o)}
395
+ `}}}).prompt(),ye=t=>{const n=t.active??"Yes",r=t.inactive??"No";return new dD({active:n,inactive:r,initialValue:t.initialValue??true,render(){const i=`${color2.gray(o)}
396
+ ${b(this.state)} ${t.message}
397
+ `,s=this.value?n:r;switch(this.state){case "submit":return `${i}${color2.gray(o)} ${color2.dim(s)}`;case "cancel":return `${i}${color2.gray(o)} ${color2.strikethrough(color2.dim(s))}
398
+ ${color2.gray(o)}`;default:return `${i}${color2.cyan(o)} ${this.value?`${color2.green(k)} ${n}`:`${color2.dim(P)} ${color2.dim(n)}`} ${color2.dim("/")} ${this.value?`${color2.dim(P)} ${color2.dim(r)}`:`${color2.green(k)} ${r}`}
399
+ ${color2.cyan(d)}
400
+ `}}}).prompt()},ve=t=>{const n=(r,i)=>{const s=r.label??String(r.value);switch(i){case "selected":return `${color2.dim(s)}`;case "active":return `${color2.green(k)} ${s} ${r.hint?color2.dim(`(${r.hint})`):""}`;case "cancelled":return `${color2.strikethrough(color2.dim(s))}`;default:return `${color2.dim(P)} ${color2.dim(s)}`}};return new LD({options:t.options,initialValue:t.initialValue,render(){const r=`${color2.gray(o)}
396
401
  ${b(this.state)} ${t.message}
397
402
  `;switch(this.state){case "submit":return `${r}${color2.gray(o)} ${n(this.options[this.cursor],"selected")}`;case "cancel":return `${r}${color2.gray(o)} ${n(this.options[this.cursor],"cancelled")}
398
403
  ${color2.gray(o)}`;default:return `${r}${color2.cyan(o)} ${G({cursor:this.cursor,options:this.options,maxItems:t.maxItems,style:(i,s)=>n(i,s?"active":"inactive")}).join(`
@@ -887,6 +892,7 @@ class YoctoSpinner {
887
892
  #exitHandlerBound;
888
893
  #isInteractive;
889
894
  #lastSpinnerFrameTime = 0;
895
+ #isSpinning = false;
890
896
 
891
897
  constructor(options = {}) {
892
898
  const spinner = options.spinner ?? defaultSpinner;
@@ -908,13 +914,17 @@ class YoctoSpinner {
908
914
  return this;
909
915
  }
910
916
 
917
+ this.#isSpinning = true;
911
918
  this.#hideCursor();
912
919
  this.#render();
913
920
  this.#subscribeToProcessEvents();
914
921
 
915
- this.#timer = setInterval(() => {
916
- this.#render();
917
- }, this.#interval);
922
+ // Only start the timer in interactive mode
923
+ if (this.#isInteractive) {
924
+ this.#timer = setInterval(() => {
925
+ this.#render();
926
+ }, this.#interval);
927
+ }
918
928
 
919
929
  return this;
920
930
  }
@@ -924,8 +934,12 @@ class YoctoSpinner {
924
934
  return this;
925
935
  }
926
936
 
927
- clearInterval(this.#timer);
928
- this.#timer = undefined;
937
+ this.#isSpinning = false;
938
+ if (this.#timer) {
939
+ clearInterval(this.#timer);
940
+ this.#timer = undefined;
941
+ }
942
+
929
943
  this.#showCursor();
930
944
  this.clear();
931
945
  this.#unsubscribeFromProcessEvents();
@@ -958,7 +972,7 @@ class YoctoSpinner {
958
972
  }
959
973
 
960
974
  get isSpinning() {
961
- return this.#timer !== undefined;
975
+ return this.#isSpinning;
962
976
  }
963
977
 
964
978
  get text() {
@@ -1130,10 +1144,15 @@ var MastraLogger = class {
1130
1144
  }
1131
1145
  };
1132
1146
 
1133
- var PinoLogger = class extends MastraLogger {
1147
+ var PinoLogger = class _PinoLogger extends MastraLogger {
1134
1148
  logger;
1135
1149
  constructor(options = {}) {
1136
1150
  super(options);
1151
+ const internalOptions = options;
1152
+ if (internalOptions._logger) {
1153
+ this.logger = internalOptions._logger;
1154
+ return;
1155
+ }
1137
1156
  let prettyStream = void 0;
1138
1157
  if (!options.overrideDefaultTransports) {
1139
1158
  prettyStream = pretty({
@@ -1150,7 +1169,8 @@ var PinoLogger = class extends MastraLogger {
1150
1169
  {
1151
1170
  name: options.name || "app",
1152
1171
  level: options.level || LogLevel.INFO,
1153
- formatters: options.formatters
1172
+ formatters: options.formatters,
1173
+ redact: options.redact
1154
1174
  },
1155
1175
  options.overrideDefaultTransports ? options?.transports?.default : transportsAry.length === 0 ? prettyStream : pino.multistream([
1156
1176
  ...transportsAry.map(([, transport]) => ({
@@ -1164,6 +1184,38 @@ var PinoLogger = class extends MastraLogger {
1164
1184
  ])
1165
1185
  );
1166
1186
  }
1187
+ /**
1188
+ * Creates a child logger with additional bound context.
1189
+ * All logs from the child logger will include the bound context.
1190
+ *
1191
+ * @param bindings - Key-value pairs to include in all logs from this child logger
1192
+ * @returns A new PinoLogger instance with the bound context
1193
+ *
1194
+ * @example
1195
+ * ```typescript
1196
+ * const baseLogger = new PinoLogger({ name: 'MyApp' });
1197
+ *
1198
+ * // Create module-scoped logger
1199
+ * const serviceLogger = baseLogger.child({ module: 'UserService' });
1200
+ * serviceLogger.info('User created', { userId: '123' });
1201
+ * // Output includes: { module: 'UserService', userId: '123', msg: 'User created' }
1202
+ *
1203
+ * // Create request-scoped logger
1204
+ * const requestLogger = baseLogger.child({ requestId: req.id });
1205
+ * requestLogger.error('Request failed', { err: error });
1206
+ * // Output includes: { requestId: 'abc', msg: 'Request failed', err: {...} }
1207
+ * ```
1208
+ */
1209
+ child(bindings) {
1210
+ const childPino = this.logger.child(bindings);
1211
+ const childOptions = {
1212
+ name: this.name,
1213
+ level: this.level,
1214
+ transports: Object.fromEntries(this.transports),
1215
+ _logger: childPino
1216
+ };
1217
+ return new _PinoLogger(childOptions);
1218
+ }
1167
1219
  debug(message, args = {}) {
1168
1220
  this.logger.debug(args, message);
1169
1221
  }
@@ -1178,118 +1230,8 @@ var PinoLogger = class extends MastraLogger {
1178
1230
  }
1179
1231
  };
1180
1232
 
1181
- var args = ["-y", "@mastra/mcp-docs-server"];
1182
- var createMcpConfig = (editor) => {
1183
- if (editor === "vscode") {
1184
- return {
1185
- servers: {
1186
- mastra: process.platform === `win32` ? {
1187
- command: "cmd",
1188
- args: ["/c", "npx", ...args],
1189
- type: "stdio"
1190
- } : {
1191
- command: "npx",
1192
- args,
1193
- type: "stdio"
1194
- }
1195
- }
1196
- };
1197
- }
1198
- return {
1199
- mcpServers: {
1200
- mastra: {
1201
- command: "npx",
1202
- args
1203
- }
1204
- }
1205
- };
1206
- };
1207
- function makeConfig(original, editor) {
1208
- if (editor === "vscode") {
1209
- return {
1210
- ...original,
1211
- servers: {
1212
- ...original?.servers || {},
1213
- ...createMcpConfig(editor).servers
1214
- }
1215
- };
1216
- }
1217
- return {
1218
- ...original,
1219
- mcpServers: {
1220
- ...original?.mcpServers || {},
1221
- ...createMcpConfig(editor).mcpServers
1222
- }
1223
- };
1224
- }
1225
- async function writeMergedConfig(configPath, editor) {
1226
- const configExists = existsSync(configPath);
1227
- const config = makeConfig(configExists ? await readJSON(configPath) : {}, editor);
1228
- await ensureFile(configPath);
1229
- await writeJSON(configPath, config, {
1230
- spaces: 2
1231
- });
1232
- }
1233
- var windsurfGlobalMCPConfigPath = path.join(os.homedir(), ".codeium", "windsurf", "mcp_config.json");
1234
- var cursorGlobalMCPConfigPath = path.join(os.homedir(), ".cursor", "mcp.json");
1235
- path.join(process.cwd(), ".vscode", "mcp.json");
1236
- var vscodeGlobalMCPConfigPath = path.join(
1237
- os.homedir(),
1238
- process.platform === "win32" ? path.join("AppData", "Roaming", "Code", "User", "settings.json") : process.platform === "darwin" ? path.join("Library", "Application Support", "Code", "User", "settings.json") : path.join(".config", "Code", "User", "settings.json")
1239
- );
1240
- async function installMastraDocsMCPServer({ editor, directory }) {
1241
- if (editor === `cursor`) {
1242
- await writeMergedConfig(path.join(directory, ".cursor", "mcp.json"), "cursor");
1243
- }
1244
- if (editor === `vscode`) {
1245
- await writeMergedConfig(path.join(directory, ".vscode", "mcp.json"), "vscode");
1246
- }
1247
- if (editor === `cursor-global`) {
1248
- const alreadyInstalled = await globalMCPIsAlreadyInstalled(editor);
1249
- if (alreadyInstalled) {
1250
- return;
1251
- }
1252
- await writeMergedConfig(cursorGlobalMCPConfigPath, "cursor-global");
1253
- }
1254
- if (editor === `windsurf`) {
1255
- const alreadyInstalled = await globalMCPIsAlreadyInstalled(editor);
1256
- if (alreadyInstalled) {
1257
- return;
1258
- }
1259
- await writeMergedConfig(windsurfGlobalMCPConfigPath, editor);
1260
- }
1261
- }
1262
- async function globalMCPIsAlreadyInstalled(editor) {
1263
- let configPath = ``;
1264
- if (editor === "windsurf") {
1265
- configPath = windsurfGlobalMCPConfigPath;
1266
- } else if (editor === "cursor-global") {
1267
- configPath = cursorGlobalMCPConfigPath;
1268
- } else if (editor === "vscode") {
1269
- configPath = vscodeGlobalMCPConfigPath;
1270
- }
1271
- if (!configPath || !existsSync(configPath)) {
1272
- return false;
1273
- }
1274
- try {
1275
- const configContents = await readJSON(configPath);
1276
- if (!configContents) return false;
1277
- if (editor === "vscode") {
1278
- if (!configContents.servers) return false;
1279
- const hasMastraMCP2 = Object.values(configContents.servers).some(
1280
- (server) => server?.args?.find((arg) => arg?.includes(`@mastra/mcp-docs-server`))
1281
- );
1282
- return hasMastraMCP2;
1283
- }
1284
- if (!configContents?.mcpServers) return false;
1285
- const hasMastraMCP = Object.values(configContents.mcpServers).some(
1286
- (server) => server?.args?.find((arg) => arg?.includes(`@mastra/mcp-docs-server`))
1287
- );
1288
- return hasMastraMCP;
1289
- } catch {
1290
- return false;
1291
- }
1292
- }
1233
+ var package_default = {
1234
+ version: "1.0.0-beta.11"};
1293
1235
  function getPackageManagerAddCommand(pm) {
1294
1236
  switch (pm) {
1295
1237
  case "npm":
@@ -1312,11 +1254,11 @@ var DepsService = class {
1312
1254
  findLockFile(dir) {
1313
1255
  const lockFiles = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
1314
1256
  for (const file of lockFiles) {
1315
- if (fs3__default__default.existsSync(path.join(dir, file))) {
1257
+ if (fs3__default__default.existsSync(path3.join(dir, file))) {
1316
1258
  return file;
1317
1259
  }
1318
1260
  }
1319
- const parentDir = path.resolve(dir, "..");
1261
+ const parentDir = path3.resolve(dir, "..");
1320
1262
  if (parentDir !== dir) {
1321
1263
  return this.findLockFile(parentDir);
1322
1264
  }
@@ -1349,7 +1291,7 @@ var DepsService = class {
1349
1291
  }
1350
1292
  async checkDependencies(dependencies) {
1351
1293
  try {
1352
- const packageJsonPath = path.join(process.cwd(), "package.json");
1294
+ const packageJsonPath = path3.join(process.cwd(), "package.json");
1353
1295
  try {
1354
1296
  await fs4.access(packageJsonPath);
1355
1297
  } catch {
@@ -1369,7 +1311,7 @@ var DepsService = class {
1369
1311
  }
1370
1312
  async getProjectName() {
1371
1313
  try {
1372
- const packageJsonPath = path.join(process.cwd(), "package.json");
1314
+ const packageJsonPath = path3.join(process.cwd(), "package.json");
1373
1315
  const packageJson = await fs4.readFile(packageJsonPath, "utf-8");
1374
1316
  const pkg = JSON.parse(packageJson);
1375
1317
  return pkg.name;
@@ -1457,8 +1399,8 @@ var FileService = class {
1457
1399
  */
1458
1400
  async copyStarterFile(inputFile, outputFilePath, replaceIfExists) {
1459
1401
  const __filename = fileURLToPath(import.meta.url);
1460
- const __dirname = path.dirname(__filename);
1461
- const filePath = path.resolve(__dirname, "starter-files", inputFile);
1402
+ const __dirname = path3.dirname(__filename);
1403
+ const filePath = path3.resolve(__dirname, "starter-files", inputFile);
1462
1404
  const fileString = fs3__default__default.readFileSync(filePath, "utf8");
1463
1405
  if (fs3__default__default.existsSync(outputFilePath) && !replaceIfExists) {
1464
1406
  console.info(`${outputFilePath} already exists`);
@@ -1468,7 +1410,7 @@ var FileService = class {
1468
1410
  return true;
1469
1411
  }
1470
1412
  async setupEnvFile({ dbUrl }) {
1471
- const envPath = path.join(process.cwd(), ".env.development");
1413
+ const envPath = path3.join(process.cwd(), ".env.development");
1472
1414
  await fsExtra.ensureFile(envPath);
1473
1415
  const fileEnvService = new FileEnvService(envPath);
1474
1416
  await fileEnvService.setEnvValue("DB_URL", dbUrl);
@@ -1492,21 +1434,142 @@ var FileService = class {
1492
1434
  fs3__default__default.writeFileSync(filePath, fileContent);
1493
1435
  }
1494
1436
  };
1437
+ var createArgs = (versionTag) => {
1438
+ const packageName = versionTag ? `@mastra/mcp-docs-server@${versionTag}` : "@mastra/mcp-docs-server";
1439
+ return ["-y", packageName];
1440
+ };
1441
+ var createMcpConfig = (editor, versionTag) => {
1442
+ const args = createArgs(versionTag);
1443
+ if (editor === "vscode") {
1444
+ return {
1445
+ servers: {
1446
+ mastra: process.platform === `win32` ? {
1447
+ command: "cmd",
1448
+ args: ["/c", "npx", ...args],
1449
+ type: "stdio"
1450
+ } : {
1451
+ command: "npx",
1452
+ args,
1453
+ type: "stdio"
1454
+ }
1455
+ }
1456
+ };
1457
+ }
1458
+ return {
1459
+ mcpServers: {
1460
+ mastra: {
1461
+ command: "npx",
1462
+ args
1463
+ }
1464
+ }
1465
+ };
1466
+ };
1467
+ function makeConfig(original, editor, versionTag) {
1468
+ if (editor === "vscode") {
1469
+ return {
1470
+ ...original,
1471
+ servers: {
1472
+ ...original?.servers || {},
1473
+ ...createMcpConfig(editor, versionTag).servers
1474
+ }
1475
+ };
1476
+ }
1477
+ return {
1478
+ ...original,
1479
+ mcpServers: {
1480
+ ...original?.mcpServers || {},
1481
+ ...createMcpConfig(editor, versionTag).mcpServers
1482
+ }
1483
+ };
1484
+ }
1485
+ async function writeMergedConfig(configPath, editor, versionTag) {
1486
+ const configExists = existsSync(configPath);
1487
+ const config = makeConfig(configExists ? await readJSON(configPath) : {}, editor, versionTag);
1488
+ await ensureFile(configPath);
1489
+ await writeJSON(configPath, config, {
1490
+ spaces: 2
1491
+ });
1492
+ }
1493
+ var windsurfGlobalMCPConfigPath = path3.join(os.homedir(), ".codeium", "windsurf", "mcp_config.json");
1494
+ var cursorGlobalMCPConfigPath = path3.join(os.homedir(), ".cursor", "mcp.json");
1495
+ path3.join(process.cwd(), ".vscode", "mcp.json");
1496
+ var vscodeGlobalMCPConfigPath = path3.join(
1497
+ os.homedir(),
1498
+ process.platform === "win32" ? path3.join("AppData", "Roaming", "Code", "User", "settings.json") : process.platform === "darwin" ? path3.join("Library", "Application Support", "Code", "User", "settings.json") : path3.join(".config", "Code", "User", "settings.json")
1499
+ );
1500
+ async function installMastraDocsMCPServer({
1501
+ editor,
1502
+ directory,
1503
+ versionTag
1504
+ }) {
1505
+ if (editor === `cursor`) {
1506
+ await writeMergedConfig(path3.join(directory, ".cursor", "mcp.json"), "cursor", versionTag);
1507
+ }
1508
+ if (editor === `vscode`) {
1509
+ await writeMergedConfig(path3.join(directory, ".vscode", "mcp.json"), "vscode", versionTag);
1510
+ }
1511
+ if (editor === `cursor-global`) {
1512
+ const alreadyInstalled = await globalMCPIsAlreadyInstalled(editor, versionTag);
1513
+ if (alreadyInstalled) {
1514
+ return;
1515
+ }
1516
+ await writeMergedConfig(cursorGlobalMCPConfigPath, "cursor-global", versionTag);
1517
+ }
1518
+ if (editor === `windsurf`) {
1519
+ const alreadyInstalled = await globalMCPIsAlreadyInstalled(editor, versionTag);
1520
+ if (alreadyInstalled) {
1521
+ return;
1522
+ }
1523
+ await writeMergedConfig(windsurfGlobalMCPConfigPath, editor, versionTag);
1524
+ }
1525
+ }
1526
+ async function globalMCPIsAlreadyInstalled(editor, versionTag) {
1527
+ let configPath = ``;
1528
+ if (editor === "windsurf") {
1529
+ configPath = windsurfGlobalMCPConfigPath;
1530
+ } else if (editor === "cursor-global") {
1531
+ configPath = cursorGlobalMCPConfigPath;
1532
+ } else if (editor === "vscode") {
1533
+ configPath = vscodeGlobalMCPConfigPath;
1534
+ }
1535
+ if (!configPath || !existsSync(configPath)) {
1536
+ return false;
1537
+ }
1538
+ try {
1539
+ const configContents = await readJSON(configPath);
1540
+ if (!configContents) return false;
1541
+ const expectedPackage = versionTag ? `@mastra/mcp-docs-server@${versionTag}` : "@mastra/mcp-docs-server";
1542
+ if (editor === "vscode") {
1543
+ if (!configContents.servers) return false;
1544
+ const hasMastraMCP2 = Object.values(configContents.servers).some(
1545
+ (server) => server?.args?.find((arg) => arg === expectedPackage)
1546
+ );
1547
+ return hasMastraMCP2;
1548
+ }
1549
+ if (!configContents?.mcpServers) return false;
1550
+ const hasMastraMCP = Object.values(configContents.mcpServers).some(
1551
+ (server) => server?.args?.find((arg) => arg === expectedPackage)
1552
+ );
1553
+ return hasMastraMCP;
1554
+ } catch {
1555
+ return false;
1556
+ }
1557
+ }
1495
1558
  var exec = util.promisify(child_process.exec);
1496
1559
  var getModelIdentifier = (llmProvider) => {
1497
- if (llmProvider === "openai") {
1498
- return `'openai/gpt-4o-mini'`;
1499
- } else if (llmProvider === "anthropic") {
1500
- return `'anthropic/claude-sonnet-4-5-20250929'`;
1560
+ let model = "openai/gpt-4o";
1561
+ if (llmProvider === "anthropic") {
1562
+ model = "anthropic/claude-sonnet-4-5";
1501
1563
  } else if (llmProvider === "groq") {
1502
- return `'groq/llama-3.3-70b-versatile'`;
1564
+ model = "groq/llama-3.3-70b-versatile";
1503
1565
  } else if (llmProvider === "google") {
1504
- return `'google/gemini-2.5-pro'`;
1566
+ model = "google/gemini-2.5-pro";
1505
1567
  } else if (llmProvider === "cerebras") {
1506
- return `'cerebras/llama-3.3-70b'`;
1568
+ model = "cerebras/llama-3.3-70b";
1507
1569
  } else if (llmProvider === "mistral") {
1508
- return `'mistral/mistral-medium-2508'`;
1570
+ model = "mistral/mistral-medium-2508";
1509
1571
  }
1572
+ return model;
1510
1573
  };
1511
1574
  async function writeAgentSample(llmProvider, destPath, addExampleTool, addScorers) {
1512
1575
  const modelString = getModelIdentifier(llmProvider);
@@ -1527,7 +1590,6 @@ async function writeAgentSample(llmProvider, destPath, addExampleTool, addScorer
1527
1590
  const content = `
1528
1591
  import { Agent } from '@mastra/core/agent';
1529
1592
  import { Memory } from '@mastra/memory';
1530
- import { LibSQLStore } from '@mastra/libsql';
1531
1593
  ${addExampleTool ? `import { weatherTool } from '../tools/weather-tool';` : ""}
1532
1594
  ${addScorers ? `import { scorers } from '../scorers/weather-scorer';` : ""}
1533
1595
 
@@ -1535,7 +1597,7 @@ export const weatherAgent = new Agent({
1535
1597
  id: 'weather-agent',
1536
1598
  name: 'Weather Agent',
1537
1599
  instructions: \`${instructions}\`,
1538
- model: ${modelString},
1600
+ model: '${modelString}',
1539
1601
  ${addExampleTool ? "tools: { weatherTool }," : ""}
1540
1602
  ${addScorers ? `scorers: {
1541
1603
  toolCallAppropriateness: {
@@ -1560,12 +1622,7 @@ export const weatherAgent = new Agent({
1560
1622
  },
1561
1623
  },
1562
1624
  },` : ""}
1563
- memory: new Memory({
1564
- storage: new LibSQLStore({
1565
- id: "memory-storage",
1566
- url: "file:../mastra.db", // path is relative to the .mastra/output directory
1567
- })
1568
- })
1625
+ memory: new Memory()
1569
1626
  });
1570
1627
  `;
1571
1628
  const formattedContent = await prettier.format(content, {
@@ -1794,7 +1851,7 @@ export const translationScorer = createScorer({
1794
1851
  description: 'Checks that non-English location names are translated and used correctly',
1795
1852
  type: 'agent',
1796
1853
  judge: {
1797
- model: ${modelString},
1854
+ model: '${modelString}',
1798
1855
  instructions:
1799
1856
  'You are an expert evaluator of translation quality for geographic locations. ' +
1800
1857
  'Determine whether the user text mentions a non-English location and whether the assistant correctly uses an English translation of that location. ' +
@@ -1891,7 +1948,7 @@ var writeIndexFile = async ({
1891
1948
  addScorers
1892
1949
  }) => {
1893
1950
  const indexPath = dirPath + "/index.ts";
1894
- const destPath = path.join(indexPath);
1951
+ const destPath = path3.join(indexPath);
1895
1952
  try {
1896
1953
  await fs4.writeFile(destPath, "");
1897
1954
  const filteredExports = [
@@ -1935,7 +1992,7 @@ export const mastra = new Mastra({
1935
1992
  observability: new Observability({
1936
1993
  // Enables DefaultExporter and CloudExporter for tracing
1937
1994
  default: { enabled: true },
1938
- }),
1995
+ }),
1939
1996
  });
1940
1997
  `
1941
1998
  );
@@ -1974,7 +2031,7 @@ var writeAPIKey = async ({ provider, apiKey }) => {
1974
2031
  };
1975
2032
  var createMastraDir = async (directory) => {
1976
2033
  let dir = directory.trim().split("/").filter((item) => item !== "");
1977
- const dirPath = path.join(process.cwd(), ...dir, "mastra");
2034
+ const dirPath = path3.join(process.cwd(), ...dir, "mastra");
1978
2035
  try {
1979
2036
  await fs4.access(dirPath);
1980
2037
  return { ok: false };
@@ -1999,8 +2056,8 @@ var LLM_PROVIDERS = [
1999
2056
  { value: "cerebras", label: "Cerebras" },
2000
2057
  { value: "mistral", label: "Mistral" }
2001
2058
  ];
2002
- var interactivePrompt = async (args2 = {}) => {
2003
- const { skip = {}, options: { showBanner = true } = {} } = args2;
2059
+ var interactivePrompt = async (args = {}) => {
2060
+ const { skip = {}, options: { showBanner = true } = {} } = args;
2004
2061
  if (showBanner) {
2005
2062
  Ie(color2.inverse(" Mastra Init "));
2006
2063
  }
@@ -2038,46 +2095,29 @@ var interactivePrompt = async (args2 = {}) => {
2038
2095
  return void 0;
2039
2096
  },
2040
2097
  configureEditorWithDocsMCP: async () => {
2041
- const windsurfIsAlreadyInstalled = await globalMCPIsAlreadyInstalled(`windsurf`);
2042
- const cursorIsAlreadyInstalled = await globalMCPIsAlreadyInstalled(`cursor`);
2043
- const vscodeIsAlreadyInstalled = await globalMCPIsAlreadyInstalled(`vscode`);
2044
2098
  const editor = await ve({
2045
2099
  message: `Make your IDE into a Mastra expert? (Installs Mastra's MCP server)`,
2046
2100
  options: [
2047
2101
  { value: "skip", label: "Skip for now", hint: "default" },
2048
2102
  {
2049
2103
  value: "cursor",
2050
- label: "Cursor (project only)",
2051
- hint: cursorIsAlreadyInstalled ? `Already installed globally` : void 0
2104
+ label: "Cursor (project only)"
2052
2105
  },
2053
2106
  {
2054
2107
  value: "cursor-global",
2055
- label: "Cursor (global, all projects)",
2056
- hint: cursorIsAlreadyInstalled ? `Already installed` : void 0
2108
+ label: "Cursor (global, all projects)"
2057
2109
  },
2058
2110
  {
2059
2111
  value: "windsurf",
2060
- label: "Windsurf",
2061
- hint: windsurfIsAlreadyInstalled ? `Already installed` : void 0
2112
+ label: "Windsurf"
2062
2113
  },
2063
2114
  {
2064
2115
  value: "vscode",
2065
- label: "VSCode",
2066
- hint: vscodeIsAlreadyInstalled ? `Already installed` : void 0
2116
+ label: "VSCode"
2067
2117
  }
2068
2118
  ]
2069
2119
  });
2070
2120
  if (editor === `skip`) return void 0;
2071
- if (editor === `windsurf` && windsurfIsAlreadyInstalled) {
2072
- M.message(`
2073
- Windsurf is already installed, skipping.`);
2074
- return void 0;
2075
- }
2076
- if (editor === `vscode` && vscodeIsAlreadyInstalled) {
2077
- M.message(`
2078
- VSCode is already installed, skipping.`);
2079
- return void 0;
2080
- }
2081
2121
  if (editor === `cursor`) {
2082
2122
  M.message(
2083
2123
  `
@@ -2086,19 +2126,19 @@ Note: you will need to go into Cursor Settings -> MCP Settings and manually enab
2086
2126
  );
2087
2127
  }
2088
2128
  if (editor === `cursor-global`) {
2089
- const confirm = await ve({
2129
+ const confirm3 = await ve({
2090
2130
  message: `Global install will add/update ${cursorGlobalMCPConfigPath} and make the Mastra docs MCP server available in all your Cursor projects. Continue?`,
2091
2131
  options: [
2092
2132
  { value: "yes", label: "Yes, I understand" },
2093
2133
  { value: "skip", label: "No, skip for now" }
2094
2134
  ]
2095
2135
  });
2096
- if (confirm !== `yes`) {
2136
+ if (confirm3 !== `yes`) {
2097
2137
  return void 0;
2098
2138
  }
2099
2139
  }
2100
2140
  if (editor === `windsurf`) {
2101
- const confirm = await ve({
2141
+ const confirm3 = await ve({
2102
2142
  message: `Windsurf only supports a global MCP config (at ${windsurfGlobalMCPConfigPath}) is it ok to add/update that global config?
2103
2143
  This means the Mastra docs MCP server will be available in all your Windsurf projects.`,
2104
2144
  options: [
@@ -2106,11 +2146,18 @@ This means the Mastra docs MCP server will be available in all your Windsurf pro
2106
2146
  { value: "skip", label: "No, skip for now" }
2107
2147
  ]
2108
2148
  });
2109
- if (confirm !== `yes`) {
2149
+ if (confirm3 !== `yes`) {
2110
2150
  return void 0;
2111
2151
  }
2112
2152
  }
2113
2153
  return editor;
2154
+ },
2155
+ initGit: async () => {
2156
+ if (skip?.gitInit) return false;
2157
+ return ye({
2158
+ message: "Initialize a new git repository?",
2159
+ initialValue: true
2160
+ });
2114
2161
  }
2115
2162
  },
2116
2163
  {
@@ -2125,6 +2172,9 @@ This means the Mastra docs MCP server will be available in all your Windsurf pro
2125
2172
  function getPackageManager() {
2126
2173
  const userAgent = process.env.npm_config_user_agent || "";
2127
2174
  const execPath = process.env.npm_execpath || "";
2175
+ if (userAgent.includes("bun")) {
2176
+ return "bun";
2177
+ }
2128
2178
  if (userAgent.includes("yarn")) {
2129
2179
  return "yarn";
2130
2180
  }
@@ -2134,6 +2184,9 @@ function getPackageManager() {
2134
2184
  if (userAgent.includes("npm")) {
2135
2185
  return "npm";
2136
2186
  }
2187
+ if (execPath.includes("bun")) {
2188
+ return "bun";
2189
+ }
2137
2190
  if (execPath.includes("yarn")) {
2138
2191
  return "yarn";
2139
2192
  }
@@ -2145,6 +2198,20 @@ function getPackageManager() {
2145
2198
  }
2146
2199
  return "npm";
2147
2200
  }
2201
+ async function gitInit({ cwd }) {
2202
+ await execa("git", ["init"], { cwd, stdio: "ignore" });
2203
+ await execa("git", ["add", "-A"], { cwd, stdio: "ignore" });
2204
+ await execa(
2205
+ "git",
2206
+ [
2207
+ "commit",
2208
+ "-m",
2209
+ '"Initial commit from Mastra"',
2210
+ '--author="dane-ai-mastra[bot] <dane-ai-mastra[bot]@users.noreply.github.com>"'
2211
+ ],
2212
+ { cwd, stdio: "ignore" }
2213
+ );
2214
+ }
2148
2215
  var logger = createLogger(false);
2149
2216
  function createLogger(debug = false) {
2150
2217
  return new PinoLogger({
@@ -2154,19 +2221,23 @@ function createLogger(debug = false) {
2154
2221
  }
2155
2222
  var exec2 = util.promisify(child_process.exec);
2156
2223
  async function cloneTemplate(options) {
2157
- const { template, projectName, targetDir } = options;
2158
- const projectPath = targetDir ? path.resolve(targetDir, projectName) : path.resolve(projectName);
2224
+ const { template, projectName, targetDir, branch, llmProvider } = options;
2225
+ const projectPath = targetDir ? path3.resolve(targetDir, projectName) : path3.resolve(projectName);
2159
2226
  const spinner4 = yoctoSpinner({ text: `Cloning template "${template.title}"...` }).start();
2160
2227
  try {
2161
2228
  if (await directoryExists(projectPath)) {
2162
2229
  spinner4.error(`Directory ${projectName} already exists`);
2163
2230
  throw new Error(`Directory ${projectName} already exists`);
2164
2231
  }
2165
- await cloneRepositoryWithoutGit(template.githubUrl, projectPath);
2232
+ await cloneRepositoryWithoutGit(template.githubUrl, projectPath, branch);
2166
2233
  await updatePackageJson(projectPath, projectName);
2167
- const envExamplePath = path.join(projectPath, ".env.example");
2234
+ const envExamplePath = path3.join(projectPath, ".env.example");
2168
2235
  if (await fileExists(envExamplePath)) {
2169
- await fs4.copyFile(envExamplePath, path.join(projectPath, ".env"));
2236
+ const envPath = path3.join(projectPath, ".env");
2237
+ await fs4.copyFile(envExamplePath, envPath);
2238
+ if (llmProvider) {
2239
+ await updateEnvFile(envPath, llmProvider);
2240
+ }
2170
2241
  }
2171
2242
  spinner4.success(`Template "${template.title}" cloned successfully to ${projectName}`);
2172
2243
  return projectPath;
@@ -2191,21 +2262,27 @@ async function fileExists(filePath) {
2191
2262
  return false;
2192
2263
  }
2193
2264
  }
2194
- async function cloneRepositoryWithoutGit(repoUrl, targetPath) {
2265
+ async function cloneRepositoryWithoutGit(repoUrl, targetPath, branch) {
2195
2266
  await fs4.mkdir(targetPath, { recursive: true });
2196
2267
  try {
2197
2268
  const degitRepo = repoUrl.replace("https://github.com/", "");
2198
- const degitCommand = shellQuote2.quote(["npx", "degit", degitRepo, targetPath]);
2269
+ const degitRepoWithBranch = branch ? `${degitRepo}#${branch}` : degitRepo;
2270
+ const degitCommand = shellQuote2.quote(["npx", "degit", degitRepoWithBranch, targetPath]);
2199
2271
  await exec2(degitCommand, {
2200
2272
  cwd: process.cwd()
2201
2273
  });
2202
2274
  } catch {
2203
2275
  try {
2204
- const gitCommand = shellQuote2.quote(["git", "clone", repoUrl, targetPath]);
2276
+ const gitArgs = ["git", "clone"];
2277
+ if (branch) {
2278
+ gitArgs.push("--branch", branch);
2279
+ }
2280
+ gitArgs.push(repoUrl, targetPath);
2281
+ const gitCommand = shellQuote2.quote(gitArgs);
2205
2282
  await exec2(gitCommand, {
2206
2283
  cwd: process.cwd()
2207
2284
  });
2208
- const gitDir = path.join(targetPath, ".git");
2285
+ const gitDir = path3.join(targetPath, ".git");
2209
2286
  if (await directoryExists(gitDir)) {
2210
2287
  await fs4.rm(gitDir, { recursive: true, force: true });
2211
2288
  }
@@ -2215,7 +2292,7 @@ async function cloneRepositoryWithoutGit(repoUrl, targetPath) {
2215
2292
  }
2216
2293
  }
2217
2294
  async function updatePackageJson(projectPath, projectName) {
2218
- const packageJsonPath = path.join(projectPath, "package.json");
2295
+ const packageJsonPath = path3.join(projectPath, "package.json");
2219
2296
  try {
2220
2297
  const packageJsonContent = await fs4.readFile(packageJsonPath, "utf-8");
2221
2298
  const packageJson = JSON.parse(packageJsonContent);
@@ -2225,6 +2302,22 @@ async function updatePackageJson(projectPath, projectName) {
2225
2302
  logger.warn(`Could not update package.json: ${error instanceof Error ? error.message : "Unknown error"}`);
2226
2303
  }
2227
2304
  }
2305
+ async function updateEnvFile(envPath, llmProvider) {
2306
+ try {
2307
+ const envContent = await fs4.readFile(envPath, "utf-8");
2308
+ const modelString = getModelIdentifier(llmProvider);
2309
+ if (!modelString) {
2310
+ logger.warn(`Could not get model identifier for provider: ${llmProvider}`);
2311
+ return;
2312
+ }
2313
+ const modelValue = modelString.replace(/'/g, "");
2314
+ const updatedContent = envContent.replace(/^MODEL=.*/m, `MODEL=${modelValue}`);
2315
+ await fs4.writeFile(envPath, updatedContent, "utf-8");
2316
+ logger.info(`Updated MODEL in .env to ${modelValue}`);
2317
+ } catch (error) {
2318
+ logger.warn(`Could not update .env file: ${error instanceof Error ? error.message : "Unknown error"}`);
2319
+ }
2320
+ }
2228
2321
  async function installDependencies(projectPath, packageManager) {
2229
2322
  const spinner4 = yoctoSpinner({ text: "Installing dependencies..." }).start();
2230
2323
  try {
@@ -2310,7 +2403,8 @@ var init = async ({
2310
2403
  llmApiKey,
2311
2404
  addExample = false,
2312
2405
  configureEditorWithDocsMCP,
2313
- versionTag
2406
+ versionTag,
2407
+ initGit = false
2314
2408
  }) => {
2315
2409
  s.start("Initializing Mastra");
2316
2410
  const packageVersionTag = versionTag ? `@${versionTag}` : "";
@@ -2364,10 +2458,21 @@ var init = async ({
2364
2458
  if (configureEditorWithDocsMCP) {
2365
2459
  await installMastraDocsMCPServer({
2366
2460
  editor: configureEditorWithDocsMCP,
2367
- directory: process.cwd()
2461
+ directory: process.cwd(),
2462
+ versionTag
2368
2463
  });
2369
2464
  }
2370
2465
  s.stop();
2466
+ if (initGit) {
2467
+ const s2 = Y();
2468
+ try {
2469
+ s2.start("Initializing git repository");
2470
+ await gitInit({ cwd: process.cwd() });
2471
+ s2.stop("Git repository initialized");
2472
+ } catch {
2473
+ s2.stop();
2474
+ }
2475
+ }
2371
2476
  if (!llmApiKey) {
2372
2477
  Me(`
2373
2478
  ${color2.green("Mastra initialized successfully!")}
@@ -2413,6 +2518,32 @@ var execWithTimeout = async (command, timeoutMs) => {
2413
2518
  throw error;
2414
2519
  }
2415
2520
  };
2521
+ async function getInitCommand(pm) {
2522
+ switch (pm) {
2523
+ case "npm":
2524
+ return "npm init -y";
2525
+ case "pnpm":
2526
+ return "pnpm init";
2527
+ case "yarn":
2528
+ return "yarn init -y";
2529
+ case "bun":
2530
+ return "bun init -y";
2531
+ default:
2532
+ return "npm init -y";
2533
+ }
2534
+ }
2535
+ async function initializePackageJson(pm) {
2536
+ const initCommand = await getInitCommand(pm);
2537
+ await exec3(initCommand);
2538
+ const packageJsonPath = path3.join(process.cwd(), "package.json");
2539
+ const packageJson = JSON.parse(await fs4.readFile(packageJsonPath, "utf-8"));
2540
+ packageJson.type = "module";
2541
+ packageJson.engines = {
2542
+ ...packageJson.engines,
2543
+ node: ">=22.13.0"
2544
+ };
2545
+ await fs4.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
2546
+ }
2416
2547
  async function installMastraDependency(pm, dependency, versionTag, isDev, timeout) {
2417
2548
  let installCommand = getPackageManagerAddCommand(pm);
2418
2549
  if (isDev) {
@@ -2459,7 +2590,7 @@ var createMastraProject = async ({
2459
2590
  xe("Operation cancelled");
2460
2591
  process.exit(0);
2461
2592
  }
2462
- let result;
2593
+ let result = void 0;
2463
2594
  if (needsInteractive) {
2464
2595
  result = await interactivePrompt({
2465
2596
  options: { showBanner: false },
@@ -2467,10 +2598,13 @@ var createMastraProject = async ({
2467
2598
  });
2468
2599
  }
2469
2600
  const s2 = Y();
2601
+ const originalCwd = process.cwd();
2602
+ let projectPath = null;
2470
2603
  try {
2471
2604
  s2.start("Creating project");
2472
2605
  try {
2473
2606
  await fs4.mkdir(projectName);
2607
+ projectPath = path3.resolve(originalCwd, projectName);
2474
2608
  } catch (error) {
2475
2609
  if (error instanceof Error && "code" in error && error.code === "EEXIST") {
2476
2610
  s2.stop(`A directory named "${projectName}" already exists. Please choose a different name.`);
@@ -2485,9 +2619,7 @@ var createMastraProject = async ({
2485
2619
  const installCommand = getPackageManagerAddCommand(pm);
2486
2620
  s2.message("Initializing project structure");
2487
2621
  try {
2488
- await exec3(`npm init -y`);
2489
- await exec3(`npm pkg set type="module"`);
2490
- await exec3(`npm pkg set engines.node=">=20.9.0"`);
2622
+ await initializePackageJson(pm);
2491
2623
  const depsService = new DepsService();
2492
2624
  await depsService.addScriptsToPackageJson({
2493
2625
  dev: "mastra dev",
@@ -2566,44 +2698,61 @@ var createMastraProject = async ({
2566
2698
  s2.stop();
2567
2699
  const errorMessage = error instanceof Error ? error.message : "An unexpected error occurred";
2568
2700
  xe(`Project creation failed: ${errorMessage}`);
2701
+ if (projectPath && fs3__default__default.existsSync(projectPath)) {
2702
+ try {
2703
+ process.chdir(originalCwd);
2704
+ await fs4.rm(projectPath, { recursive: true, force: true });
2705
+ } catch (cleanupError) {
2706
+ console.error(
2707
+ `Warning: Failed to clean up project directory: ${cleanupError instanceof Error ? cleanupError.message : "Unknown error"}`
2708
+ );
2709
+ }
2710
+ }
2569
2711
  process.exit(1);
2570
2712
  }
2571
2713
  };
2572
- var create = async (args2) => {
2573
- if (args2.template !== void 0) {
2574
- await createFromTemplate({ ...args2, injectedAnalytics: args2.analytics });
2714
+ var version$1 = package_default.version;
2715
+ var create = async (args) => {
2716
+ if (args.template !== void 0) {
2717
+ await createFromTemplate({
2718
+ projectName: args.projectName,
2719
+ template: args.template,
2720
+ timeout: args.timeout,
2721
+ injectedAnalytics: args.analytics,
2722
+ llmProvider: args.llmProvider
2723
+ });
2575
2724
  return;
2576
2725
  }
2577
- const needsInteractive = args2.components === void 0 || args2.llmProvider === void 0 || args2.addExample === void 0;
2726
+ const needsInteractive = args.components === void 0 || args.llmProvider === void 0 || args.addExample === void 0;
2578
2727
  const { projectName, result } = await createMastraProject({
2579
- projectName: args2?.projectName,
2580
- createVersionTag: args2?.createVersionTag,
2581
- timeout: args2?.timeout,
2582
- llmProvider: args2?.llmProvider,
2583
- llmApiKey: args2?.llmApiKey,
2728
+ projectName: args?.projectName,
2729
+ createVersionTag: args?.createVersionTag,
2730
+ timeout: args?.timeout,
2731
+ llmProvider: args?.llmProvider,
2732
+ llmApiKey: args?.llmApiKey,
2584
2733
  needsInteractive
2585
2734
  });
2586
- const directory = args2.directory || "src/";
2735
+ const directory = args.directory || "src/";
2587
2736
  if (needsInteractive && result) {
2588
2737
  await init({
2589
2738
  ...result,
2590
2739
  llmApiKey: result?.llmApiKey,
2591
2740
  components: ["agents", "tools", "workflows", "scorers"],
2592
2741
  addExample: true,
2593
- versionTag: args2.createVersionTag
2742
+ versionTag: args.createVersionTag
2594
2743
  });
2595
2744
  postCreate({ projectName });
2596
2745
  return;
2597
2746
  }
2598
- const { components = [], llmProvider = "openai", addExample = false, llmApiKey } = args2;
2747
+ const { components = [], llmProvider = "openai", addExample = false, llmApiKey } = args;
2599
2748
  await init({
2600
2749
  directory,
2601
2750
  components,
2602
2751
  llmProvider,
2603
2752
  addExample,
2604
2753
  llmApiKey,
2605
- configureEditorWithDocsMCP: args2.mcpServer,
2606
- versionTag: args2.createVersionTag
2754
+ configureEditorWithDocsMCP: args.mcpServer,
2755
+ versionTag: args.createVersionTag
2607
2756
  });
2608
2757
  postCreate({ projectName });
2609
2758
  };
@@ -2694,9 +2843,9 @@ async function createFromGitHubUrl(url) {
2694
2843
  workflows: []
2695
2844
  };
2696
2845
  }
2697
- async function createFromTemplate(args2) {
2846
+ async function createFromTemplate(args) {
2698
2847
  let selectedTemplate;
2699
- if (args2.template === true) {
2848
+ if (args.template === true) {
2700
2849
  const templates = await loadTemplates();
2701
2850
  const selected = await selectTemplate(templates);
2702
2851
  if (!selected) {
@@ -2704,11 +2853,11 @@ async function createFromTemplate(args2) {
2704
2853
  return;
2705
2854
  }
2706
2855
  selectedTemplate = selected;
2707
- } else if (args2.template && typeof args2.template === "string") {
2708
- if (isGitHubUrl(args2.template)) {
2856
+ } else if (args.template && typeof args.template === "string") {
2857
+ if (isGitHubUrl(args.template)) {
2709
2858
  const spinner4 = Y();
2710
2859
  spinner4.start("Validating GitHub repository...");
2711
- const validation = await validateGitHubProject(args2.template);
2860
+ const validation = await validateGitHubProject(args.template);
2712
2861
  if (!validation.isValid) {
2713
2862
  spinner4.stop("Validation failed");
2714
2863
  M.error("This does not appear to be a valid Mastra project:");
@@ -2716,14 +2865,14 @@ async function createFromTemplate(args2) {
2716
2865
  throw new Error("Invalid Mastra project");
2717
2866
  }
2718
2867
  spinner4.stop("Valid Mastra project \u2713");
2719
- selectedTemplate = await createFromGitHubUrl(args2.template);
2868
+ selectedTemplate = await createFromGitHubUrl(args.template);
2720
2869
  } else {
2721
2870
  const templates = await loadTemplates();
2722
- const found = findTemplateByName(templates, args2.template);
2871
+ const found = findTemplateByName(templates, args.template);
2723
2872
  if (!found) {
2724
- M.error(`Template "${args2.template}" not found. Available templates:`);
2873
+ M.error(`Template "${args.template}" not found. Available templates:`);
2725
2874
  templates.forEach((t) => M.info(` - ${t.title} (use: ${t.slug.replace("template-", "")})`));
2726
- throw new Error(`Template "${args2.template}" not found`);
2875
+ throw new Error(`Template "${args.template}" not found`);
2727
2876
  }
2728
2877
  selectedTemplate = found;
2729
2878
  }
@@ -2731,7 +2880,7 @@ async function createFromTemplate(args2) {
2731
2880
  if (!selectedTemplate) {
2732
2881
  throw new Error("No template selected");
2733
2882
  }
2734
- let projectName = args2.projectName;
2883
+ let projectName = args.projectName;
2735
2884
  if (!projectName) {
2736
2885
  const defaultName = getDefaultProjectName(selectedTemplate);
2737
2886
  const response = await he({
@@ -2745,19 +2894,61 @@ async function createFromTemplate(args2) {
2745
2894
  }
2746
2895
  projectName = response;
2747
2896
  }
2897
+ let llmProvider = args.llmProvider;
2898
+ if (!llmProvider) {
2899
+ const providerResponse = await ve({
2900
+ message: "Select a default provider:",
2901
+ options: LLM_PROVIDERS
2902
+ });
2903
+ if (pD(providerResponse)) {
2904
+ M.info("Project creation cancelled.");
2905
+ return;
2906
+ }
2907
+ llmProvider = providerResponse;
2908
+ }
2909
+ let initGit = false;
2910
+ const gitConfirmResult = await ye({
2911
+ message: "Initialize a new git repository?",
2912
+ initialValue: true
2913
+ });
2914
+ if (!pD(gitConfirmResult)) {
2915
+ initGit = gitConfirmResult;
2916
+ }
2917
+ let projectPath = null;
2748
2918
  try {
2749
- const analytics = args2.injectedAnalytics || getAnalytics();
2919
+ const analytics = args.injectedAnalytics || getAnalytics();
2750
2920
  if (analytics) {
2751
2921
  analytics.trackEvent("cli_template_used", {
2752
2922
  template_slug: selectedTemplate.slug,
2753
2923
  template_title: selectedTemplate.title
2754
2924
  });
2925
+ if (llmProvider) {
2926
+ analytics.trackEvent("cli_model_provider_selected", {
2927
+ provider: llmProvider,
2928
+ selection_method: args.llmProvider ? "cli_args" : "interactive"
2929
+ });
2930
+ }
2755
2931
  }
2756
- const projectPath = await cloneTemplate({
2932
+ const isBeta = version$1?.includes("beta") ?? false;
2933
+ const isMastraTemplate = selectedTemplate.githubUrl.includes("github.com/mastra-ai/");
2934
+ const branch = isBeta && isMastraTemplate ? "beta" : void 0;
2935
+ projectPath = await cloneTemplate({
2757
2936
  template: selectedTemplate,
2758
- projectName
2937
+ projectName,
2938
+ branch,
2939
+ llmProvider
2759
2940
  });
2760
2941
  await installDependencies(projectPath);
2942
+ if (initGit) {
2943
+ const s2 = Y();
2944
+ try {
2945
+ s2.start("Initializing git repository");
2946
+ await gitInit({ cwd: projectPath });
2947
+ s2.stop("Git repository initialized");
2948
+ } catch {
2949
+ s2.stop();
2950
+ }
2951
+ }
2761
2952
  Me(`
2762
2953
  ${color2.green("Mastra template installed!")}
2763
2954
 
@@ -2766,6 +2957,17 @@ async function createFromTemplate(args2) {
2766
2957
  `);
2767
2958
  postCreate({ projectName });
2768
2959
  } catch (error) {
2960
+ if (projectPath) {
2961
+ try {
2962
+ if (fs3__default__default.existsSync(projectPath)) {
2963
+ await fs4.rm(projectPath, { recursive: true, force: true });
2964
+ }
2965
+ } catch (cleanupError) {
2966
+ console.error(
2967
+ `Warning: Failed to clean up project directory: ${cleanupError instanceof Error ? cleanupError.message : "Unknown error"}`
2968
+ );
2969
+ }
2970
+ }
2769
2971
  M.error(`Failed to create project from template: ${error instanceof Error ? error.message : "Unknown error"}`);
2770
2972
  throw error;
2771
2973
  }
@@ -2774,7 +2976,7 @@ async function createFromTemplate(args2) {
2774
2976
  async function getPackageVersion() {
2775
2977
  const __filename = fileURLToPath(import.meta.url);
2776
2978
  const __dirname = dirname(__filename);
2777
- const pkgJsonPath = path.join(__dirname, "..", "package.json");
2979
+ const pkgJsonPath = path3.join(__dirname, "..", "package.json");
2778
2980
  const content = await fsExtra$1.readJSON(pkgJsonPath);
2779
2981
  return content.version;
2780
2982
  }