@mastra/deployer 0.2.5 → 0.2.6-alpha.10

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.
Files changed (40) hide show
  1. package/dist/_tsup-dts-rollup.d.cts +95 -33
  2. package/dist/_tsup-dts-rollup.d.ts +95 -33
  3. package/dist/build/analyze.cjs +2 -2
  4. package/dist/build/analyze.js +1 -1
  5. package/dist/build/bundler.cjs +3 -3
  6. package/dist/build/bundler.js +1 -1
  7. package/dist/build/index.cjs +22 -24
  8. package/dist/build/index.d.cts +3 -4
  9. package/dist/build/index.d.ts +3 -4
  10. package/dist/build/index.js +6 -4
  11. package/dist/bundler/index.cjs +2 -2
  12. package/dist/bundler/index.js +1 -1
  13. package/dist/chunk-2ZPQX6BX.cjs +254 -0
  14. package/dist/chunk-4AYFLP6G.js +227 -0
  15. package/dist/chunk-5UBGPRKT.js +185 -0
  16. package/dist/{chunk-KLVSED7T.js → chunk-74KZVNKH.js} +8 -4
  17. package/dist/{chunk-OT6UKL2S.cjs → chunk-AE4CVAPK.cjs} +11 -0
  18. package/dist/{chunk-UTZ3434D.js → chunk-BDTZS3JM.js} +3 -0
  19. package/dist/chunk-DYQ225MJ.js +115 -0
  20. package/dist/chunk-I3UVE6EH.cjs +161 -0
  21. package/dist/{chunk-XEFBJH3T.js → chunk-IKPL4RGG.js} +11 -0
  22. package/dist/chunk-NCROGJGB.cjs +142 -0
  23. package/dist/{chunk-NWERLYTR.cjs → chunk-P5SATU7G.cjs} +3 -0
  24. package/dist/chunk-VFZVVUQE.cjs +198 -0
  25. package/dist/chunk-WB3T6NKI.js +133 -0
  26. package/dist/{chunk-NXBTVZHO.cjs → chunk-WFC3CUZ3.cjs} +14 -10
  27. package/dist/index.cjs +12 -11
  28. package/dist/index.js +7 -6
  29. package/dist/server/index.cjs +617 -2682
  30. package/dist/server/index.js +512 -2577
  31. package/dist/services/index.cjs +19 -0
  32. package/dist/services/index.d.cts +3 -0
  33. package/dist/services/index.d.ts +3 -0
  34. package/dist/services/index.js +2 -0
  35. package/dist/templates/instrumentation-template.js +25 -30
  36. package/package.json +15 -4
  37. package/dist/chunk-7GYBZLVN.cjs +0 -286
  38. package/dist/chunk-KFOGAPGX.cjs +0 -433
  39. package/dist/chunk-PUX2FDGX.js +0 -252
  40. package/dist/chunk-ZAXXMFXX.js +0 -399
@@ -1,399 +0,0 @@
1
- import { spawn } from 'child_process';
2
- import { Transform } from 'stream';
3
- import { MastraBase } from '@mastra/core/base';
4
- import fs from 'fs';
5
- import path, { dirname } from 'path';
6
- import { fileURLToPath } from 'url';
7
- import fsExtra from 'fs-extra/esm';
8
- import fsPromises from 'fs/promises';
9
- import * as babel from '@babel/core';
10
- import babel__default from '@babel/core';
11
- import { rollup } from 'rollup';
12
- import esbuild from 'rollup-plugin-esbuild';
13
- import commonjs from '@rollup/plugin-commonjs';
14
-
15
- // src/deploy/log.ts
16
- var createPinoStream = (logger) => {
17
- return new Transform({
18
- transform(chunk, _encoding, callback) {
19
- const line = chunk.toString().trim();
20
- if (line) {
21
- console.log(line);
22
- logger.info(line);
23
- }
24
- callback(null, chunk);
25
- }
26
- });
27
- };
28
- function createChildProcessLogger({ logger, root }) {
29
- const pinoStream = createPinoStream(logger);
30
- return async ({ cmd, args, env }) => {
31
- try {
32
- const subprocess = spawn(cmd, args, {
33
- cwd: root,
34
- shell: true,
35
- env
36
- });
37
- subprocess.stdout?.pipe(pinoStream);
38
- subprocess.stderr?.pipe(pinoStream);
39
- return new Promise((resolve, reject) => {
40
- subprocess.on("close", (code) => {
41
- pinoStream.end();
42
- if (code === 0) {
43
- resolve({ success: true });
44
- } else {
45
- reject(new Error(`Process exited with code ${code}`));
46
- }
47
- });
48
- subprocess.on("error", (error) => {
49
- pinoStream.end();
50
- logger.error("Process failed", { error });
51
- reject(error);
52
- });
53
- });
54
- } catch (error) {
55
- console.log(error);
56
- logger.error("Process failed", { error });
57
- pinoStream.end();
58
- return { success: false, error };
59
- }
60
- };
61
- }
62
- var Deps = class extends MastraBase {
63
- packageManager;
64
- rootDir;
65
- constructor(rootDir = process.cwd()) {
66
- super({ component: "DEPLOYER", name: "DEPS" });
67
- this.rootDir = rootDir;
68
- this.packageManager = this.getPackageManager();
69
- }
70
- findLockFile(dir) {
71
- const lockFiles = ["pnpm-lock.yaml", "package-lock.json", "yarn.lock", "bun.lock"];
72
- for (const file of lockFiles) {
73
- if (fs.existsSync(path.join(dir, file))) {
74
- return file;
75
- }
76
- }
77
- const parentDir = path.resolve(dir, "..");
78
- if (parentDir !== dir) {
79
- return this.findLockFile(parentDir);
80
- }
81
- return null;
82
- }
83
- getPackageManager() {
84
- const lockFile = this.findLockFile(this.rootDir);
85
- switch (lockFile) {
86
- case "pnpm-lock.yaml":
87
- return "pnpm";
88
- case "package-lock.json":
89
- return "npm";
90
- case "yarn.lock":
91
- return "yarn";
92
- case "bun.lock":
93
- return "bun";
94
- default:
95
- return "npm";
96
- }
97
- }
98
- async install({ dir = this.rootDir, packages = [] }) {
99
- let runCommand = this.packageManager;
100
- switch (this.packageManager) {
101
- case "npm":
102
- runCommand = `${this.packageManager} i`;
103
- break;
104
- case "pnpm":
105
- runCommand = `${this.packageManager} --ignore-workspace install`;
106
- break;
107
- default:
108
- runCommand = `${this.packageManager} ${packages?.length > 0 ? `add` : `install`}`;
109
- }
110
- const cpLogger = createChildProcessLogger({
111
- logger: this.logger,
112
- root: dir
113
- });
114
- return cpLogger({
115
- cmd: runCommand,
116
- args: packages,
117
- env: {
118
- PATH: process.env.PATH
119
- }
120
- });
121
- }
122
- async installPackages(packages) {
123
- let runCommand = this.packageManager;
124
- if (this.packageManager === "npm") {
125
- runCommand = `${this.packageManager} i`;
126
- } else {
127
- runCommand = `${this.packageManager} add`;
128
- }
129
- const cpLogger = createChildProcessLogger({
130
- logger: this.logger,
131
- root: ""
132
- });
133
- return cpLogger({
134
- cmd: `${runCommand}`,
135
- args: packages,
136
- env: {
137
- PATH: process.env.PATH
138
- }
139
- });
140
- }
141
- async checkDependencies(dependencies) {
142
- try {
143
- const packageJsonPath = path.join(this.rootDir, "package.json");
144
- try {
145
- await fsPromises.access(packageJsonPath);
146
- } catch {
147
- return "No package.json file found in the current directory";
148
- }
149
- const packageJson = JSON.parse(await fsPromises.readFile(packageJsonPath, "utf-8"));
150
- for (const dependency of dependencies) {
151
- if (!packageJson.dependencies || !packageJson.dependencies[dependency]) {
152
- return `Please install ${dependency} before running this command (${this.packageManager} install ${dependency})`;
153
- }
154
- }
155
- return "ok";
156
- } catch (err) {
157
- console.error(err);
158
- return "Could not check dependencies";
159
- }
160
- }
161
- async getProjectName() {
162
- try {
163
- const packageJsonPath = path.join(this.rootDir, "package.json");
164
- const packageJson = await fsPromises.readFile(packageJsonPath, "utf-8");
165
- const pkg = JSON.parse(packageJson);
166
- return pkg.name;
167
- } catch (err) {
168
- throw err;
169
- }
170
- }
171
- async getPackageVersion() {
172
- const __filename = fileURLToPath(import.meta.url);
173
- const __dirname = dirname(__filename);
174
- const pkgJsonPath = path.join(__dirname, "..", "..", "package.json");
175
- const content = await fsExtra.readJSON(pkgJsonPath);
176
- return content.version;
177
- }
178
- async addScriptsToPackageJson(scripts) {
179
- const packageJson = JSON.parse(await fsPromises.readFile("package.json", "utf-8"));
180
- packageJson.scripts = {
181
- ...packageJson.scripts,
182
- ...scripts
183
- };
184
- await fsPromises.writeFile("package.json", JSON.stringify(packageJson, null, 2));
185
- }
186
- };
187
- function removeAllExceptTelemetryConfig(result) {
188
- const t = babel__default.types;
189
- return {
190
- name: "remove-all-except-telemetry-config",
191
- visitor: {
192
- ExportNamedDeclaration: {
193
- // remove all exports
194
- exit(path2) {
195
- path2.remove();
196
- }
197
- },
198
- NewExpression(path2, state) {
199
- const varDeclaratorPath = path2.findParent((path3) => t.isVariableDeclarator(path3.node));
200
- if (!varDeclaratorPath) {
201
- return;
202
- }
203
- const parentNode = path2.parentPath.node;
204
- if (!t.isVariableDeclarator(parentNode) || !t.isIdentifier(parentNode.id) || parentNode.id.name !== "mastra") {
205
- return;
206
- }
207
- let mastraArgs = t.objectExpression([]);
208
- if (t.isObjectExpression(path2.node.arguments[0])) {
209
- mastraArgs = path2.node.arguments[0];
210
- }
211
- let telemetry = mastraArgs.properties.find(
212
- // @ts-ignore
213
- (prop) => prop.key.name === "telemetry"
214
- );
215
- let telemetryValue = t.objectExpression([]);
216
- const programPath = path2.scope.getProgramParent().path;
217
- if (!programPath) {
218
- return;
219
- }
220
- if (telemetry && t.isObjectProperty(telemetry) && t.isExpression(telemetry.value)) {
221
- result.hasCustomConfig = true;
222
- telemetryValue = telemetry.value;
223
- if (t.isIdentifier(telemetry.value) && telemetry.value.name === "telemetry") {
224
- const telemetryBinding = state.file.scope.getBinding("telemetry");
225
- if (telemetryBinding && t.isVariableDeclarator(telemetryBinding.path.node)) {
226
- const id = path2.scope.generateUidIdentifier("telemetry");
227
- telemetryBinding.path.replaceWith(t.variableDeclarator(id, telemetryBinding.path.node.init));
228
- telemetryValue = id;
229
- }
230
- }
231
- }
232
- const exportDeclaration = t.exportNamedDeclaration(
233
- t.variableDeclaration("const", [t.variableDeclarator(t.identifier("telemetry"), telemetryValue)]),
234
- []
235
- );
236
- programPath.node.body.push(exportDeclaration);
237
- }
238
- }
239
- };
240
- }
241
- function removeNonReferencedNodes() {
242
- const t = babel__default.types;
243
- return {
244
- name: "remove-non-referenced-nodes",
245
- visitor: {
246
- Program(path2) {
247
- const scope = path2.scope;
248
- const currentBody = path2.get("body");
249
- const filteredBody = currentBody.filter((childPath) => {
250
- if (childPath.isExportDeclaration()) {
251
- return true;
252
- }
253
- if (childPath.isVariableDeclaration()) {
254
- return childPath.node.declarations.some((decl) => {
255
- if (!t.isIdentifier(decl.id)) {
256
- return false;
257
- }
258
- const name = decl.id.name;
259
- const binding = scope.getBinding(name);
260
- return binding && (binding.referenced || binding.referencePaths.length > 0);
261
- });
262
- }
263
- if (childPath.isFunctionDeclaration() || childPath.isClassDeclaration()) {
264
- if (!t.isIdentifier(childPath.node.id)) {
265
- return false;
266
- }
267
- const name = childPath.node.id.name;
268
- const binding = scope.getBinding(name);
269
- return binding && (binding.referenced || binding.referencePaths.length > 0);
270
- }
271
- if (childPath.isImportDeclaration()) {
272
- return childPath.node.specifiers.some((specifier) => {
273
- const importedName = specifier.local.name;
274
- const binding = scope.getBinding(importedName);
275
- return binding && (binding.referenced || binding.referencePaths.length > 0);
276
- });
277
- }
278
- return false;
279
- });
280
- path2.set(
281
- "body",
282
- filteredBody.map((p) => p.node)
283
- );
284
- }
285
- }
286
- };
287
- }
288
- function recursiveRemoveNonReferencedNodes(code) {
289
- return new Promise(async (resolve, reject) => {
290
- babel.transform(
291
- code,
292
- {
293
- babelrc: false,
294
- configFile: false,
295
- plugins: [removeNonReferencedNodes()]
296
- },
297
- (err, result) => {
298
- if (err) {
299
- return reject(err);
300
- }
301
- if (result && result.code !== code) {
302
- return recursiveRemoveNonReferencedNodes(result.code).then(resolve, reject);
303
- }
304
- resolve({
305
- code: result.code,
306
- map: result.map
307
- });
308
- }
309
- );
310
- });
311
- }
312
-
313
- // src/build/telemetry.ts
314
- function getTelemetryBundler(entryFile, result) {
315
- return rollup({
316
- logLevel: "silent",
317
- input: {
318
- "telemetry-config": entryFile
319
- },
320
- treeshake: "smallest",
321
- plugins: [
322
- // transpile typescript to something we understand
323
- esbuild({
324
- target: "node20",
325
- platform: "node",
326
- minify: false
327
- }),
328
- commonjs({
329
- extensions: [".js", ".ts"],
330
- strictRequires: "strict",
331
- transformMixedEsModules: true,
332
- ignoreTryCatch: false
333
- }),
334
- {
335
- name: "get-telemetry-config",
336
- transform(code, id) {
337
- if (id !== entryFile) {
338
- return;
339
- }
340
- return new Promise((resolve, reject) => {
341
- babel.transform(
342
- code,
343
- {
344
- babelrc: false,
345
- configFile: false,
346
- filename: id,
347
- plugins: [removeAllExceptTelemetryConfig(result)]
348
- },
349
- (err, result2) => {
350
- if (err) {
351
- return reject(err);
352
- }
353
- resolve({
354
- code: result2.code,
355
- map: result2.map
356
- });
357
- }
358
- );
359
- });
360
- }
361
- },
362
- // let esbuild remove all unused imports
363
- esbuild({
364
- target: "node20",
365
- platform: "node",
366
- minify: false
367
- }),
368
- {
369
- name: "cleanup",
370
- transform(code, id) {
371
- if (id !== entryFile) {
372
- return;
373
- }
374
- return recursiveRemoveNonReferencedNodes(code);
375
- }
376
- },
377
- // let esbuild remove all unused imports
378
- esbuild({
379
- target: "node20",
380
- platform: "node",
381
- minify: false
382
- })
383
- ]
384
- });
385
- }
386
- async function writeTelemetryConfig(entryFile, outputDir) {
387
- const result = {
388
- hasCustomConfig: false
389
- };
390
- const bundle = await getTelemetryBundler(entryFile, result);
391
- await bundle.write({
392
- dir: outputDir,
393
- format: "es",
394
- entryFileNames: "[name].mjs"
395
- });
396
- return result;
397
- }
398
-
399
- export { Deps, createChildProcessLogger, createPinoStream, recursiveRemoveNonReferencedNodes, writeTelemetryConfig };