@heroku/mcp-server 0.0.1-alpha.0

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 (42) hide show
  1. package/LICENSE.txt +206 -0
  2. package/README.md +279 -0
  3. package/dist/index.js +86 -0
  4. package/dist/repl/heroku-cli-repl.js +210 -0
  5. package/dist/repl/heroku-cli-repl.spec.js +106 -0
  6. package/dist/services/app-service.js +42 -0
  7. package/dist/services/app-setup-service.js +72 -0
  8. package/dist/services/build-service.js +149 -0
  9. package/dist/services/source-service.js +42 -0
  10. package/dist/tools/addons.js +241 -0
  11. package/dist/tools/addons.spec.js +335 -0
  12. package/dist/tools/apps.js +222 -0
  13. package/dist/tools/apps.spec.js +332 -0
  14. package/dist/tools/data.js +345 -0
  15. package/dist/tools/data.spec.js +300 -0
  16. package/dist/tools/deploy-to-heroku.js +449 -0
  17. package/dist/tools/deploy-to-heroku.spec.js +166 -0
  18. package/dist/tools/logs.js +61 -0
  19. package/dist/tools/logs.spec.js +112 -0
  20. package/dist/tools/maintenance.js +55 -0
  21. package/dist/tools/maintenance.spec.js +115 -0
  22. package/dist/tools/pipelines.js +146 -0
  23. package/dist/tools/pipelines.spec.js +183 -0
  24. package/dist/tools/ps.js +99 -0
  25. package/dist/tools/ps.spec.js +127 -0
  26. package/dist/tools/spaces.js +34 -0
  27. package/dist/tools/spaces.spec.js +71 -0
  28. package/dist/tools/teams.js +37 -0
  29. package/dist/tools/teams.spec.js +69 -0
  30. package/dist/utils/app-json.schema.json +232 -0
  31. package/dist/utils/command-builder.js +61 -0
  32. package/dist/utils/command-builder.spec.js +95 -0
  33. package/dist/utils/generate-request-init.js +18 -0
  34. package/dist/utils/handle-cli-output.js +26 -0
  35. package/dist/utils/handle-cli-output.spec.js +54 -0
  36. package/dist/utils/mcp-tool-response.js +1 -0
  37. package/dist/utils/read-app-json.js +39 -0
  38. package/dist/utils/read-app-json.spec.js +106 -0
  39. package/dist/utils/tarball.js +114 -0
  40. package/dist/utils/tarball.spec.js +98 -0
  41. package/dist/utils/tool-commands.js +47 -0
  42. package/package.json +85 -0
@@ -0,0 +1,61 @@
1
+ /**
2
+ * A builder class for constructing Heroku CLI commands with flags and positional arguments.
3
+ * This class provides a fluent interface for building command-line arguments in a structured way.
4
+ */
5
+ export class CommandBuilder {
6
+ baseCommand;
7
+ flags = [];
8
+ args = [];
9
+ /**
10
+ * Creates a new CommandBuilder instance.
11
+ *
12
+ * @param baseCommand - The base Heroku CLI command to execute (e.g., 'apps', 'apps:create')
13
+ */
14
+ constructor(baseCommand) {
15
+ this.baseCommand = baseCommand;
16
+ }
17
+ /**
18
+ * Adds command-line flags to the command.
19
+ *
20
+ * @param flags - An object containing flag names and their values. Boolean flags are added without values,
21
+ * while string flags are added with their values.
22
+ * @returns The builder instance for method chaining
23
+ */
24
+ addFlags(flags) {
25
+ for (const [flag, value] of Object.entries(flags)) {
26
+ if (value) {
27
+ if (typeof value === 'boolean')
28
+ this.flags.push(`--${flag}`);
29
+ else
30
+ this.flags.push(`--${flag}=${value}`);
31
+ }
32
+ }
33
+ return this;
34
+ }
35
+ /**
36
+ * Adds positional arguments to the command.
37
+ *
38
+ * @param args - An object containing argument names and their values. Only values are used in the final command.
39
+ * @returns The builder instance for method chaining
40
+ */
41
+ addPositionalArguments(args) {
42
+ for (const [, value] of Object.entries(args)) {
43
+ if (value)
44
+ this.args.push(value);
45
+ }
46
+ return this;
47
+ }
48
+ /**
49
+ * Builds and returns the complete command string.
50
+ *
51
+ * @returns A string representing the command with all flags and arguments
52
+ */
53
+ build() {
54
+ let command = this.baseCommand;
55
+ if (this.flags.length > 0)
56
+ command += ` ${this.flags.join(' ')}`;
57
+ if (this.args.length > 0)
58
+ command += ` -- ${this.args.join(' ')}`;
59
+ return command;
60
+ }
61
+ }
@@ -0,0 +1,95 @@
1
+ import { expect } from 'chai';
2
+ import { CommandBuilder } from './command-builder.js';
3
+ import { TOOL_COMMAND_MAP } from './tool-commands.js';
4
+ /**
5
+ * Unit tests for the CommandBuilder class.
6
+ * Some of these tests do not produce valid commands, but they are designed to test the CommandBuilder class.
7
+ */
8
+ describe('CommandBuilder', () => {
9
+ describe('constructor', () => {
10
+ it('creates a new instance with the base command', () => {
11
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
12
+ expect(builder.build()).to.equal('apps');
13
+ });
14
+ });
15
+ describe('addFlags', () => {
16
+ it('adds boolean flags correctly', () => {
17
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
18
+ builder.addFlags({ all: true, json: true });
19
+ expect(builder.build()).to.equal('apps --all --json');
20
+ });
21
+ it('adds string flags with values correctly', () => {
22
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
23
+ builder.addFlags({ team: 'my-team', space: 'my-space' });
24
+ expect(builder.build()).to.equal('apps --team=my-team --space=my-space');
25
+ });
26
+ it('handles mixed boolean and string flags', () => {
27
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
28
+ builder.addFlags({ all: true, team: 'my-team', json: true });
29
+ expect(builder.build()).to.equal('apps --all --team=my-team --json');
30
+ });
31
+ it('ignores undefined flag values', () => {
32
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
33
+ builder.addFlags({ all: true, team: undefined, json: true });
34
+ expect(builder.build()).to.equal('apps --all --json');
35
+ });
36
+ it('ignores false boolean flags', () => {
37
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
38
+ builder.addFlags({ all: false, json: true });
39
+ expect(builder.build()).to.equal('apps --json');
40
+ });
41
+ it('supports method chaining', () => {
42
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
43
+ const result = builder.addFlags({ all: true });
44
+ expect(result).to.equal(builder);
45
+ });
46
+ });
47
+ describe('addPositionalArguments', () => {
48
+ it('adds positional arguments correctly', () => {
49
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
50
+ builder.addPositionalArguments({ new_name: 'new-app-name' });
51
+ expect(builder.build()).to.equal('apps:rename -- new-app-name');
52
+ });
53
+ it('adds multiple positional arguments in order', () => {
54
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.TRANSFER_APP);
55
+ builder.addPositionalArguments({ app: 'my-app', recipient: 'user@example.com' });
56
+ expect(builder.build()).to.equal('apps:transfer -- my-app user@example.com');
57
+ });
58
+ it('ignores undefined argument values', () => {
59
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
60
+ builder.addPositionalArguments({ app: undefined, new_name: 'new-app-name' });
61
+ expect(builder.build()).to.equal('apps:rename -- new-app-name');
62
+ });
63
+ it('supports method chaining', () => {
64
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
65
+ const result = builder.addPositionalArguments({ new_name: 'new-app-name' });
66
+ expect(result).to.equal(builder);
67
+ });
68
+ });
69
+ describe('build', () => {
70
+ it('combines flags and positional arguments correctly', () => {
71
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP);
72
+ builder.addFlags({ region: 'eu', team: 'my-team' }).addPositionalArguments({ app: 'my-new-app' });
73
+ expect(builder.build()).to.equal('apps:create --region=eu --team=my-team -- my-new-app');
74
+ });
75
+ it('handles commands with no flags or arguments', () => {
76
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
77
+ expect(builder.build()).to.equal('apps');
78
+ });
79
+ it('handles commands with only flags', () => {
80
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.LIST_APPS);
81
+ builder.addFlags({ all: true, json: true });
82
+ expect(builder.build()).to.equal('apps --all --json');
83
+ });
84
+ it('handles commands with only positional arguments', () => {
85
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.RENAME_APP);
86
+ builder.addPositionalArguments({ new_name: 'new-app-name' });
87
+ expect(builder.build()).to.equal('apps:rename -- new-app-name');
88
+ });
89
+ it('maintains flag and argument order across multiple calls', () => {
90
+ const builder = new CommandBuilder(TOOL_COMMAND_MAP.CREATE_APP);
91
+ builder.addFlags({ region: 'eu' }).addPositionalArguments({ app: 'my-app' }).addFlags({ team: 'my-team' });
92
+ expect(builder.build()).to.equal('apps:create --region=eu --team=my-team -- my-app');
93
+ });
94
+ });
95
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Generates a request init object for making API requests to the Heroku API.
3
+ *
4
+ * @param signal An optional abort signal to pass to the request init object.
5
+ * @returns A promise that resolves to a request init object.
6
+ */
7
+ export async function generateRequestInit(signal) {
8
+ const pkg = await import('../../package.json', { with: { type: 'json' } });
9
+ const { version } = pkg.default;
10
+ return {
11
+ signal,
12
+ headers: {
13
+ Authorization: `Bearer ${process.env.HEROKU_API_KEY?.trim() ?? ''}`,
14
+ Referer: `heroku-mcp/${version}`,
15
+ 'User-Agent': `Heroku-MCP-Server/${version} (${process.platform}; ${process.arch}; node/${process.version})`
16
+ }
17
+ };
18
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Handles Heroku REPL command output and formats it into a standardized MCP tool response.
3
+ * This function processes both successful command output and error messages.
4
+ *
5
+ * When the output contains error markers (<<<ERROR>>>...<<<END ERROR>>>), it formats
6
+ * the error message with a standardized prefix and returns an error response.
7
+ * Otherwise, it returns the command output as a successful response.
8
+ *
9
+ * @param output - The output string from the Heroku REPL command execution
10
+ * @returns An McpToolResponse object containing either the formatted output or error message
11
+ */
12
+ export function handleCliOutput(output) {
13
+ const errorPattern = /<<<ERROR>>>(.|\n)*?<<<END ERROR>>>/;
14
+ const errorMatch = output?.match(errorPattern);
15
+ if (errorMatch || !output) {
16
+ const baseMessage = '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command ' +
17
+ 'suggestions that may be provided in the command output or error details. ';
18
+ return {
19
+ isError: true,
20
+ content: [{ type: 'text', text: `${baseMessage}Details:\n${output || 'No response from command'}` }]
21
+ };
22
+ }
23
+ return {
24
+ content: [{ type: 'text', text: output }]
25
+ };
26
+ }
@@ -0,0 +1,54 @@
1
+ import { expect } from 'chai';
2
+ import { handleCliOutput } from './handle-cli-output.js';
3
+ describe('handleCliOutput', () => {
4
+ const baseMessage = '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command ' +
5
+ 'suggestions that may be provided in the command output or error details. ';
6
+ describe('error handling', () => {
7
+ it('handles output with error markers', () => {
8
+ const output = 'Some output\n<<<ERROR>>>Not found<<<END ERROR>>>\nMore output';
9
+ const result = handleCliOutput(output);
10
+ expect(result).to.deep.equal({
11
+ isError: true,
12
+ content: [{ type: 'text', text: `${baseMessage}Details:\n${output}` }]
13
+ });
14
+ });
15
+ it('handles output with multiline error message', () => {
16
+ const output = 'Some output\n<<<ERROR>>>First line\nSecond line\nThird line<<<END ERROR>>>\nMore output';
17
+ const result = handleCliOutput(output);
18
+ expect(result).to.deep.equal({
19
+ isError: true,
20
+ content: [{ type: 'text', text: `${baseMessage}Details:\n${output}` }]
21
+ });
22
+ });
23
+ it('handles output with empty error message', () => {
24
+ const output = 'Some output\n<<<ERROR>>><<<END ERROR>>>\nMore output';
25
+ const result = handleCliOutput(output);
26
+ expect(result).to.deep.equal({
27
+ isError: true,
28
+ content: [{ type: 'text', text: `${baseMessage}Details:\n${output}` }]
29
+ });
30
+ });
31
+ });
32
+ describe('success handling', () => {
33
+ it('handles normal output without error markers', () => {
34
+ const output = 'Command executed successfully\nWith multiple lines\nof output';
35
+ const result = handleCliOutput(output);
36
+ expect(result).to.deep.equal({
37
+ content: [{ type: 'text', text: output }]
38
+ });
39
+ });
40
+ it('handles empty output', () => {
41
+ const output = '';
42
+ const result = handleCliOutput(output);
43
+ expect(result).to.deep.equal({
44
+ content: [
45
+ {
46
+ type: 'text',
47
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\nNo response from command'
48
+ }
49
+ ],
50
+ isError: true
51
+ });
52
+ });
53
+ });
54
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,39 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs/promises';
3
+ import { Validator } from 'jsonschema';
4
+ /**
5
+ * Retrieves the app.json. This file must be in the
6
+ * root of the workspace and must be valid.
7
+ *
8
+ * @param workspaceRootUri The workspace uri to retrieve the file from
9
+ * @param appJsonFile The app.json file to read
10
+ * @returns The typed app.json as an object
11
+ * @throws {Error} If the app.json cannot be read or is invalid
12
+ */
13
+ export async function readAppJson(workspaceRootUri, appJsonFile) {
14
+ const appJsonUri = path.join(workspaceRootUri, 'app.json');
15
+ let appJsonBytes = appJsonFile;
16
+ if (!appJsonFile) {
17
+ try {
18
+ await fs.stat(appJsonUri);
19
+ }
20
+ catch {
21
+ throw new Error(`Cannot find app.json file at ${appJsonUri}`);
22
+ }
23
+ appJsonBytes = await fs.readFile(appJsonUri);
24
+ }
25
+ let appJson;
26
+ try {
27
+ appJson = JSON.parse(Buffer.from(appJsonBytes).toString());
28
+ }
29
+ catch (e) {
30
+ throw new Error(`Cannot parse the app.json file: ${e.message}`);
31
+ }
32
+ const validator = new Validator();
33
+ const schema = await import('./app-json.schema.json', { with: { type: 'json' } });
34
+ const result = validator.validate(appJson, schema.default);
35
+ if (!result.valid) {
36
+ throw new Error(`Invalid app.json file: ${result.errors.map((e) => e.message).join(', ')}`);
37
+ }
38
+ return appJson;
39
+ }
@@ -0,0 +1,106 @@
1
+ import { expect } from 'chai';
2
+ import path from 'node:path';
3
+ import fs from 'node:fs/promises';
4
+ import os from 'node:os';
5
+ import { readAppJson } from './read-app-json.js';
6
+ describe('readAppJson', () => {
7
+ let tempDir;
8
+ beforeEach(async () => {
9
+ // Create a temporary directory for each test
10
+ tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'app-json-test-'));
11
+ });
12
+ afterEach(async () => {
13
+ // Clean up temporary directory after each test
14
+ await fs.rm(tempDir, { recursive: true, force: true });
15
+ });
16
+ it('should read and validate a valid app.json file', async () => {
17
+ const validAppJson = {
18
+ name: 'test-app',
19
+ description: 'Test application',
20
+ keywords: ['test', 'app'],
21
+ env: {
22
+ API_KEY: {
23
+ description: 'API key for service',
24
+ required: true
25
+ }
26
+ },
27
+ formation: {
28
+ web: {
29
+ quantity: 1,
30
+ size: 'standard-1x'
31
+ }
32
+ },
33
+ stack: 'heroku-22'
34
+ };
35
+ const appJsonPath = path.join(tempDir, 'app.json');
36
+ await fs.writeFile(appJsonPath, JSON.stringify(validAppJson, null, 2));
37
+ const result = await readAppJson(tempDir);
38
+ expect(result).to.deep.equal(validAppJson);
39
+ });
40
+ it('should throw error when app.json file does not exist', async () => {
41
+ try {
42
+ await readAppJson(tempDir);
43
+ expect.fail('Should have thrown an error');
44
+ }
45
+ catch (error) {
46
+ expect(error).to.be.instanceOf(Error);
47
+ expect(error.message).to.include('Cannot find app.json file');
48
+ }
49
+ });
50
+ it('should throw error for invalid JSON syntax', async () => {
51
+ const invalidJson = '{ "name": "test-app", invalid json }';
52
+ const appJsonPath = path.join(tempDir, 'app.json');
53
+ await fs.writeFile(appJsonPath, invalidJson);
54
+ try {
55
+ await readAppJson(tempDir);
56
+ expect.fail('Should have thrown an error');
57
+ }
58
+ catch (error) {
59
+ expect(error).to.be.instanceOf(Error);
60
+ expect(error.message).to.include('Cannot parse the app.json file');
61
+ }
62
+ });
63
+ it('should throw error for schema validation failures', async () => {
64
+ const invalidAppJson = {
65
+ name: 'test-app',
66
+ // Invalid stack value - must be one of the allowed values
67
+ stack: 'invalid-stack',
68
+ // Invalid website URL format
69
+ website: 'not-a-url',
70
+ // Invalid buildpack format - missing required url property
71
+ buildpacks: [{}]
72
+ };
73
+ const appJsonPath = path.join(tempDir, 'app.json');
74
+ await fs.writeFile(appJsonPath, JSON.stringify(invalidAppJson));
75
+ try {
76
+ await readAppJson(tempDir);
77
+ expect.fail('Should have thrown an error');
78
+ }
79
+ catch (error) {
80
+ console.log('Actual error message:', error.message);
81
+ expect(error).to.be.instanceOf(Error);
82
+ expect(error.message).to.include('Invalid app.json file');
83
+ }
84
+ });
85
+ it('should accept and validate app.json content as Uint8Array', async () => {
86
+ const validAppJson = {
87
+ name: 'test-app',
88
+ stack: 'heroku-22'
89
+ };
90
+ const appJsonContent = new TextEncoder().encode(JSON.stringify(validAppJson));
91
+ const result = await readAppJson(tempDir, appJsonContent);
92
+ expect(result).to.deep.equal(validAppJson);
93
+ });
94
+ it('should throw error for invalid app.json content as Uint8Array', async () => {
95
+ const invalidJson = '{ invalid json }';
96
+ const appJsonContent = new TextEncoder().encode(invalidJson);
97
+ try {
98
+ await readAppJson(tempDir, appJsonContent);
99
+ expect.fail('Should have thrown an error');
100
+ }
101
+ catch (error) {
102
+ expect(error).to.be.instanceOf(Error);
103
+ expect(error.message).to.include('Cannot parse the app.json file');
104
+ }
105
+ });
106
+ });
@@ -0,0 +1,114 @@
1
+ import zlib from 'node:zlib';
2
+ import { readFile, stat, readdir } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { execSync } from 'node:child_process';
5
+ import * as tar from 'tar-stream';
6
+ /**
7
+ * Creates a compressed tarball (tar.gz) from the contents of a workspace folder.
8
+ * The function respects git ignore rules and excludes node_modules directory.
9
+ *
10
+ * @param root - The workspace folder to create a tarball from
11
+ * @param additionalContents - Additional contents to include in the tarball
12
+ * @returns A Promise that resolves to a Uint8Array containing the compressed tarball data
13
+ *
14
+ * @example
15
+ * const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
16
+ * if (workspaceFolder) {
17
+ * const tarballData = await packSources(workspaceFolder);
18
+ * }
19
+ *
20
+ * - Files in node_modules are automatically excluded
21
+ * - Files matched by .gitignore rules are excluded
22
+ * - File paths in the archive are relative to the workspace root
23
+ * - The function uses streaming to handle large directories efficiently
24
+ *
25
+ * @throws {Error} If unable to read workspace files or create the tarball
26
+ */
27
+ export async function packSources(root, additionalContents = []) {
28
+ const files = await getSourceFilePaths(root);
29
+ const pack = tar.pack();
30
+ const gzip = zlib.createGzip();
31
+ const chunks = [];
32
+ gzip.on('data', (chunk) => chunks.push(chunk));
33
+ pack.pipe(gzip);
34
+ for (const file of files) {
35
+ try {
36
+ const content = await readFile(file);
37
+ const relativePath = path.relative(root, file);
38
+ pack.entry({ name: relativePath }, Buffer.from(content));
39
+ }
40
+ catch {
41
+ // Unreadable file or directory
42
+ }
43
+ }
44
+ for (const additionalContent of additionalContents) {
45
+ const { contents, relativePath } = additionalContent;
46
+ pack.entry({ name: relativePath }, Buffer.from(contents));
47
+ }
48
+ pack.finalize();
49
+ await new Promise((resolve) => gzip.on('end', resolve));
50
+ return Buffer.concat(chunks);
51
+ }
52
+ /**
53
+ * Gets all file paths from the root directory of the workspace
54
+ *
55
+ * @param root The root directory of the workspace
56
+ * @returns A promise that resolves to an array of file paths
57
+ */
58
+ export async function getSourceFilePaths(root) {
59
+ let result;
60
+ try {
61
+ // We expect most users to have a git repo in their workspace
62
+ result = execSync('git ls-files -c -o --exclude-standard', { cwd: root }).toString();
63
+ }
64
+ catch {
65
+ // If no git repo is found, we'll just return the root directory
66
+ result = './';
67
+ }
68
+ const paths = result.split('\n').filter((file) => Boolean(file));
69
+ const allFiles = [];
70
+ for (const p of paths) {
71
+ const fullPath = path.join(root, p);
72
+ try {
73
+ const stats = await stat(fullPath);
74
+ if (stats.isDirectory()) {
75
+ // Recursively get all files in directory
76
+ const files = await walkDirectory(fullPath);
77
+ allFiles.push(...files);
78
+ }
79
+ else {
80
+ allFiles.push(fullPath);
81
+ }
82
+ }
83
+ catch {
84
+ // Ignore
85
+ }
86
+ }
87
+ return allFiles;
88
+ }
89
+ /**
90
+ * Walks a directory and returns all file paths
91
+ *
92
+ * @param dir The directory to walk
93
+ * @returns A promise that resolves to an array of file paths
94
+ */
95
+ async function walkDirectory(dir) {
96
+ const files = [];
97
+ try {
98
+ const entries = await readdir(dir, { withFileTypes: true });
99
+ for (const entry of entries) {
100
+ const fullPath = path.join(dir, entry.name);
101
+ if (entry.isDirectory()) {
102
+ const subFiles = await walkDirectory(fullPath);
103
+ files.push(...subFiles);
104
+ }
105
+ else {
106
+ files.push(fullPath);
107
+ }
108
+ }
109
+ }
110
+ catch {
111
+ // Ignore
112
+ }
113
+ return files;
114
+ }
@@ -0,0 +1,98 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import os from 'node:os';
4
+ import { execSync } from 'node:child_process';
5
+ import { expect } from 'chai';
6
+ import { packSources, getSourceFilePaths } from './tarball.js';
7
+ describe('tarball utils', () => {
8
+ let tempDir;
9
+ beforeEach(async () => {
10
+ // Create a temporary directory for each test
11
+ tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'tarball-test-'));
12
+ });
13
+ afterEach(async () => {
14
+ // Clean up temporary directory after each test
15
+ await fs.rm(tempDir, { recursive: true, force: true });
16
+ });
17
+ async function createTempFile(relativePath, content) {
18
+ const filePath = path.join(tempDir, relativePath);
19
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
20
+ await fs.writeFile(filePath, content);
21
+ return filePath;
22
+ }
23
+ describe('packSources', () => {
24
+ it('creates a tarball from workspace files', async () => {
25
+ // Create some test files
26
+ await createTempFile('file1.txt', 'content 1');
27
+ await createTempFile('file2.ts', 'content 2');
28
+ await createTempFile('nested/file3.js', 'content 3');
29
+ const tarballData = await packSources(tempDir);
30
+ expect(tarballData).to.be.instanceOf(Uint8Array);
31
+ expect(tarballData.length).to.be.greaterThan(0);
32
+ // Create a new temp dir to extract and verify contents
33
+ const extractDir = await fs.mkdtemp(path.join(os.tmpdir(), 'extract-'));
34
+ try {
35
+ // Extract the tarball
36
+ await fs.writeFile(path.join(extractDir, 'archive.tar.gz'), tarballData);
37
+ execSync('tar xzf archive.tar.gz', { cwd: extractDir });
38
+ // Verify contents
39
+ const file1Content = await fs.readFile(path.join(extractDir, 'file1.txt'), 'utf8');
40
+ const file2Content = await fs.readFile(path.join(extractDir, 'file2.ts'), 'utf8');
41
+ const file3Content = await fs.readFile(path.join(extractDir, 'nested/file3.js'), 'utf8');
42
+ expect(file1Content).to.equal('content 1');
43
+ expect(file2Content).to.equal('content 2');
44
+ expect(file3Content).to.equal('content 3');
45
+ }
46
+ finally {
47
+ await fs.rm(extractDir, { recursive: true, force: true });
48
+ }
49
+ });
50
+ it('includes additional contents in the tarball', async () => {
51
+ await createTempFile('existing.txt', 'existing content');
52
+ const additionalContents = [
53
+ {
54
+ relativePath: 'extra/generated.txt',
55
+ contents: Buffer.from('generated content')
56
+ }
57
+ ];
58
+ const tarballData = await packSources(tempDir, additionalContents);
59
+ expect(tarballData).to.be.instanceOf(Uint8Array);
60
+ expect(tarballData.length).to.be.greaterThan(0);
61
+ // Extract and verify
62
+ const extractDir = await fs.mkdtemp(path.join(os.tmpdir(), 'extract-'));
63
+ try {
64
+ await fs.writeFile(path.join(extractDir, 'archive.tar.gz'), tarballData);
65
+ execSync('tar xzf archive.tar.gz', { cwd: extractDir });
66
+ const existingContent = await fs.readFile(path.join(extractDir, 'existing.txt'), 'utf8');
67
+ const generatedContent = await fs.readFile(path.join(extractDir, 'extra/generated.txt'), 'utf8');
68
+ expect(existingContent).to.equal('existing content');
69
+ expect(generatedContent).to.equal('generated content');
70
+ }
71
+ finally {
72
+ await fs.rm(extractDir, { recursive: true, force: true });
73
+ }
74
+ });
75
+ });
76
+ describe('getSourceFilePaths', () => {
77
+ it('gets all files from the directory', async () => {
78
+ await createTempFile('file1.txt', 'content');
79
+ await createTempFile('file2.ts', 'content');
80
+ await createTempFile('nested/file3.js', 'content');
81
+ const result = await getSourceFilePaths(tempDir);
82
+ expect(result).to.have.lengthOf(3);
83
+ expect(result).to.include(path.join(tempDir, 'file1.txt'));
84
+ expect(result).to.include(path.join(tempDir, 'file2.ts'));
85
+ expect(result).to.include(path.join(tempDir, 'nested/file3.js'));
86
+ });
87
+ it('handles nested directories', async () => {
88
+ await createTempFile('dir1/file1.txt', 'content');
89
+ await createTempFile('dir1/subdir/file2.txt', 'content');
90
+ await createTempFile('file3.txt', 'content');
91
+ const result = await getSourceFilePaths(tempDir);
92
+ expect(result).to.have.lengthOf(3);
93
+ expect(result).to.include(path.join(tempDir, 'dir1/file1.txt'));
94
+ expect(result).to.include(path.join(tempDir, 'dir1/subdir/file2.txt'));
95
+ expect(result).to.include(path.join(tempDir, 'file3.txt'));
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Maps tool names to their corresponding Heroku CLI commands.
3
+ * This mapping ensures consistent command usage across the application.
4
+ */
5
+ export const TOOL_COMMAND_MAP = {
6
+ // App-related commands
7
+ LIST_APPS: 'apps',
8
+ CREATE_APP: 'apps:create',
9
+ RENAME_APP: 'apps:rename',
10
+ TRANSFER_APP: 'apps:transfer',
11
+ GET_APP_INFO: 'apps:info',
12
+ // Maintenance mode commands
13
+ MAINTENANCE_ON: 'maintenance:on',
14
+ MAINTENANCE_OFF: 'maintenance:off',
15
+ // Space-related commands
16
+ LIST_PRIVATE_SPACES: 'spaces',
17
+ // Team-related commands
18
+ LIST_TEAMS: 'teams',
19
+ // Addons commands
20
+ LIST_ADDONS: 'addons',
21
+ GET_ADDON_INFO: 'addons:info',
22
+ CREATE_ADDON: 'addons:create',
23
+ LIST_ADDON_SERVICES: 'addons:services',
24
+ LIST_ADDON_PLANS: 'addons:plans',
25
+ // PostgreSQL commands
26
+ PG_PSQL: 'pg:psql',
27
+ PG_INFO: 'pg:info',
28
+ PG_PS: 'pg:ps',
29
+ PG_LOCKS: 'pg:locks',
30
+ PG_OUTLIERS: 'pg:outliers',
31
+ PG_CREDENTIALS: 'pg:credentials',
32
+ PG_KILL: 'pg:kill',
33
+ PG_MAINTENANCE: 'pg:maintenance',
34
+ PG_BACKUPS: 'pg:backups',
35
+ PG_UPGRADE: 'pg:upgrade',
36
+ // Process commands
37
+ PS: 'ps',
38
+ PS_SCALE: 'ps:scale',
39
+ PS_RESTART: 'ps:restart',
40
+ // Pipelines commands
41
+ PIPELINES: 'pipelines',
42
+ PIPELINES_CREATE: 'pipelines:create',
43
+ PIPELINES_PROMOTE: 'pipelines:promote',
44
+ PIPELINES_INFO: 'pipelines:info',
45
+ // Logs commands
46
+ LOGS: 'logs'
47
+ };