imcp 0.0.1

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 (124) hide show
  1. package/.github/ISSUE_TEMPLATE/JitAccess.yml +28 -0
  2. package/.github/acl/access.yml +20 -0
  3. package/.github/compliance/inventory.yml +5 -0
  4. package/.github/policies/jit.yml +19 -0
  5. package/README.md +137 -0
  6. package/dist/cli/commands/install.d.ts +2 -0
  7. package/dist/cli/commands/install.js +105 -0
  8. package/dist/cli/commands/list.d.ts +2 -0
  9. package/dist/cli/commands/list.js +90 -0
  10. package/dist/cli/commands/pull.d.ts +2 -0
  11. package/dist/cli/commands/pull.js +17 -0
  12. package/dist/cli/commands/serve.d.ts +2 -0
  13. package/dist/cli/commands/serve.js +32 -0
  14. package/dist/cli/commands/start.d.ts +2 -0
  15. package/dist/cli/commands/start.js +32 -0
  16. package/dist/cli/commands/sync.d.ts +2 -0
  17. package/dist/cli/commands/sync.js +17 -0
  18. package/dist/cli/commands/uninstall.d.ts +2 -0
  19. package/dist/cli/commands/uninstall.js +39 -0
  20. package/dist/cli/index.d.ts +2 -0
  21. package/dist/cli/index.js +114 -0
  22. package/dist/core/ConfigurationProvider.d.ts +31 -0
  23. package/dist/core/ConfigurationProvider.js +416 -0
  24. package/dist/core/InstallationService.d.ts +17 -0
  25. package/dist/core/InstallationService.js +144 -0
  26. package/dist/core/MCPManager.d.ts +17 -0
  27. package/dist/core/MCPManager.js +98 -0
  28. package/dist/core/RequirementService.d.ts +45 -0
  29. package/dist/core/RequirementService.js +123 -0
  30. package/dist/core/constants.d.ts +29 -0
  31. package/dist/core/constants.js +55 -0
  32. package/dist/core/installers/BaseInstaller.d.ts +73 -0
  33. package/dist/core/installers/BaseInstaller.js +247 -0
  34. package/dist/core/installers/ClientInstaller.d.ts +17 -0
  35. package/dist/core/installers/ClientInstaller.js +307 -0
  36. package/dist/core/installers/CommandInstaller.d.ts +36 -0
  37. package/dist/core/installers/CommandInstaller.js +170 -0
  38. package/dist/core/installers/GeneralInstaller.d.ts +32 -0
  39. package/dist/core/installers/GeneralInstaller.js +87 -0
  40. package/dist/core/installers/InstallerFactory.d.ts +52 -0
  41. package/dist/core/installers/InstallerFactory.js +95 -0
  42. package/dist/core/installers/NpmInstaller.d.ts +25 -0
  43. package/dist/core/installers/NpmInstaller.js +123 -0
  44. package/dist/core/installers/PipInstaller.d.ts +25 -0
  45. package/dist/core/installers/PipInstaller.js +114 -0
  46. package/dist/core/installers/RequirementInstaller.d.ts +32 -0
  47. package/dist/core/installers/RequirementInstaller.js +3 -0
  48. package/dist/core/installers/index.d.ts +6 -0
  49. package/dist/core/installers/index.js +7 -0
  50. package/dist/core/types.d.ts +152 -0
  51. package/dist/core/types.js +16 -0
  52. package/dist/index.d.ts +11 -0
  53. package/dist/index.js +19 -0
  54. package/dist/services/InstallRequestValidator.d.ts +21 -0
  55. package/dist/services/InstallRequestValidator.js +99 -0
  56. package/dist/services/ServerService.d.ts +47 -0
  57. package/dist/services/ServerService.js +145 -0
  58. package/dist/utils/UpdateCheckTracker.d.ts +39 -0
  59. package/dist/utils/UpdateCheckTracker.js +80 -0
  60. package/dist/utils/clientUtils.d.ts +29 -0
  61. package/dist/utils/clientUtils.js +105 -0
  62. package/dist/utils/feedUtils.d.ts +5 -0
  63. package/dist/utils/feedUtils.js +29 -0
  64. package/dist/utils/githubAuth.d.ts +1 -0
  65. package/dist/utils/githubAuth.js +123 -0
  66. package/dist/utils/logger.d.ts +14 -0
  67. package/dist/utils/logger.js +90 -0
  68. package/dist/utils/osUtils.d.ts +16 -0
  69. package/dist/utils/osUtils.js +235 -0
  70. package/dist/web/public/css/modal.css +250 -0
  71. package/dist/web/public/css/notifications.css +70 -0
  72. package/dist/web/public/index.html +157 -0
  73. package/dist/web/public/js/api.js +213 -0
  74. package/dist/web/public/js/modal.js +572 -0
  75. package/dist/web/public/js/notifications.js +99 -0
  76. package/dist/web/public/js/serverCategoryDetails.js +210 -0
  77. package/dist/web/public/js/serverCategoryList.js +82 -0
  78. package/dist/web/public/modal.html +61 -0
  79. package/dist/web/public/styles.css +155 -0
  80. package/dist/web/server.d.ts +5 -0
  81. package/dist/web/server.js +150 -0
  82. package/package.json +53 -0
  83. package/src/cli/commands/install.ts +140 -0
  84. package/src/cli/commands/list.ts +112 -0
  85. package/src/cli/commands/pull.ts +16 -0
  86. package/src/cli/commands/serve.ts +37 -0
  87. package/src/cli/commands/uninstall.ts +54 -0
  88. package/src/cli/index.ts +127 -0
  89. package/src/core/ConfigurationProvider.ts +489 -0
  90. package/src/core/InstallationService.ts +173 -0
  91. package/src/core/MCPManager.ts +134 -0
  92. package/src/core/RequirementService.ts +147 -0
  93. package/src/core/constants.ts +61 -0
  94. package/src/core/installers/BaseInstaller.ts +292 -0
  95. package/src/core/installers/ClientInstaller.ts +423 -0
  96. package/src/core/installers/CommandInstaller.ts +185 -0
  97. package/src/core/installers/GeneralInstaller.ts +89 -0
  98. package/src/core/installers/InstallerFactory.ts +109 -0
  99. package/src/core/installers/NpmInstaller.ts +128 -0
  100. package/src/core/installers/PipInstaller.ts +121 -0
  101. package/src/core/installers/RequirementInstaller.ts +38 -0
  102. package/src/core/installers/index.ts +9 -0
  103. package/src/core/types.ts +163 -0
  104. package/src/index.ts +44 -0
  105. package/src/services/InstallRequestValidator.ts +112 -0
  106. package/src/services/ServerService.ts +181 -0
  107. package/src/utils/UpdateCheckTracker.ts +86 -0
  108. package/src/utils/clientUtils.ts +112 -0
  109. package/src/utils/feedUtils.ts +31 -0
  110. package/src/utils/githubAuth.ts +142 -0
  111. package/src/utils/logger.ts +101 -0
  112. package/src/utils/osUtils.ts +250 -0
  113. package/src/web/public/css/modal.css +250 -0
  114. package/src/web/public/css/notifications.css +70 -0
  115. package/src/web/public/index.html +157 -0
  116. package/src/web/public/js/api.js +213 -0
  117. package/src/web/public/js/modal.js +572 -0
  118. package/src/web/public/js/notifications.js +99 -0
  119. package/src/web/public/js/serverCategoryDetails.js +210 -0
  120. package/src/web/public/js/serverCategoryList.js +82 -0
  121. package/src/web/public/modal.html +61 -0
  122. package/src/web/public/styles.css +155 -0
  123. package/src/web/server.ts +195 -0
  124. package/tsconfig.json +18 -0
@@ -0,0 +1,134 @@
1
+ import { EventEmitter } from 'events';
2
+ import { ConfigurationProvider } from './ConfigurationProvider.js';
3
+ import { InstallationService } from './InstallationService.js';
4
+ import {
5
+ MCPEvent,
6
+ MCPEventData,
7
+ MCPServerCategory,
8
+ ServerInstallOptions,
9
+ ServerCategoryListOptions,
10
+ ServerOperationResult,
11
+ ServerUninstallOptions,
12
+ InstallationStatus
13
+ } from './types.js';
14
+ import path from 'path';
15
+
16
+ export class MCPManager extends EventEmitter {
17
+ private installationService: InstallationService;
18
+ private configProvider: ConfigurationProvider;
19
+
20
+ constructor() {
21
+ super();
22
+ this.configProvider = ConfigurationProvider.getInstance();
23
+ this.installationService = new InstallationService();
24
+ }
25
+
26
+ async syncFeeds(): Promise<void> {
27
+ await this.configProvider.syncFeeds();
28
+ }
29
+
30
+ async initialize(): Promise<void> {
31
+ try {
32
+ await this.configProvider.initialize();
33
+ } catch (error) {
34
+ console.error("Error during MCPManager initialization:", error);
35
+ throw error;
36
+ }
37
+ }
38
+
39
+ async listServerCategories(options: ServerCategoryListOptions = {}): Promise<MCPServerCategory[]> {
40
+ const { local = true } = options;
41
+ if (local) {
42
+ return await this.configProvider.getServerCategories();
43
+ }
44
+ return [];
45
+ }
46
+
47
+ async getFeedConfiguration(categoryName: string) {
48
+ return this.configProvider.getFeedConfiguration(categoryName);
49
+ }
50
+
51
+ async installServer(
52
+ categoryName: string,
53
+ serverName: string,
54
+ requestOptions: ServerInstallOptions = {}
55
+ ): Promise<ServerOperationResult> {
56
+ try {
57
+ const server = await this.configProvider.getServerCategory(categoryName);
58
+ if (!server) {
59
+ return {
60
+ success: false,
61
+ message: `Server category ${categoryName} is not onboarded`,
62
+ };
63
+ }
64
+ const installResult = await this.installationService.install(categoryName, serverName, requestOptions);
65
+
66
+ if (!installResult.success) {
67
+ return installResult;
68
+ }
69
+
70
+ this.emit(MCPEvent.SERVER_INSTALLED, { server });
71
+ return installResult;
72
+
73
+ } catch (error) {
74
+ console.error(`Unexpected error during installServer for ${serverName}:`, error);
75
+ return {
76
+ success: false,
77
+ message: `Failed to install ${serverName}: ${error instanceof Error ? error.message : String(error)}`,
78
+ error: error instanceof Error ? error : new Error(String(error)),
79
+ };
80
+ }
81
+ }
82
+
83
+ async uninstallServer(
84
+ categoryName: string,
85
+ serverName: string,
86
+ options: ServerUninstallOptions = {}
87
+ ): Promise<ServerOperationResult> {
88
+ try {
89
+ const serverCategory = await this.configProvider.getServerCategory(categoryName);
90
+ if (!serverCategory) {
91
+ return {
92
+ success: false,
93
+ message: `Server category ${categoryName} is not onboarded`,
94
+ };
95
+ }
96
+
97
+ // Clear installation status
98
+ await this.configProvider.updateInstallationStatus(
99
+ categoryName,
100
+ {},
101
+ {}
102
+ );
103
+
104
+ this.emit(MCPEvent.SERVER_UNINSTALLED, { serverName });
105
+
106
+ return {
107
+ success: true,
108
+ message: `Successfully uninstalled ${serverName}`,
109
+ };
110
+ } catch (error) {
111
+ return {
112
+ success: false,
113
+ message: `Failed to uninstall ${serverName}`,
114
+ error: error as Error,
115
+ };
116
+ }
117
+ }
118
+
119
+ // Type-safe event emitter methods
120
+ emit<E extends MCPEvent>(event: E, data: MCPEventData[E]): boolean {
121
+ return super.emit(event, data);
122
+ }
123
+
124
+ on<E extends MCPEvent>(event: E, listener: (data: MCPEventData[E]) => void): this {
125
+ return super.on(event, listener);
126
+ }
127
+
128
+ off<E extends MCPEvent>(event: E, listener: (data: MCPEventData[E]) => void): this {
129
+ return super.off(event, listener);
130
+ }
131
+ }
132
+
133
+ // Export a singleton instance
134
+ export const mcpManager = new MCPManager();
@@ -0,0 +1,147 @@
1
+ import { RequirementConfig, RequirementStatus } from './types.js';
2
+ import { createInstallerFactory } from './installers/index.js';
3
+ import { exec } from 'child_process';
4
+ import util from 'util';
5
+
6
+ /**
7
+ * Service responsible for managing requirements installation and status
8
+ */
9
+ export class RequirementService {
10
+ private static instance: RequirementService;
11
+ private installerFactory = createInstallerFactory(util.promisify(exec));
12
+
13
+ private constructor() { }
14
+
15
+ /**
16
+ * Get the singleton instance of RequirementService
17
+ * @returns The RequirementService instance
18
+ */
19
+ public static getInstance(): RequirementService {
20
+ if (!RequirementService.instance) {
21
+ RequirementService.instance = new RequirementService();
22
+ }
23
+ return RequirementService.instance;
24
+ }
25
+
26
+ /**
27
+ * Install a requirement
28
+ * @param requirement The requirement to install
29
+ * @returns The installation status
30
+ */
31
+ public async installRequirement(requirement: RequirementConfig): Promise<RequirementStatus> {
32
+ // Validate requirement
33
+ this.validateRequirement(requirement);
34
+
35
+ // Install the requirement
36
+ return await this.installerFactory.install(requirement);
37
+ }
38
+
39
+ /**
40
+ * Check the installation status of a requirement
41
+ * @param requirement The requirement to check
42
+ * @returns The installation status
43
+ */
44
+ public async checkRequirementStatus(requirement: RequirementConfig): Promise<RequirementStatus> {
45
+ // Validate requirement
46
+ this.validateRequirement(requirement);
47
+
48
+ // Check the installation status
49
+ return await this.installerFactory.checkInstallation(requirement);
50
+ }
51
+
52
+ /**
53
+ * Check if updates are available for a requirement
54
+ * @param requirement The requirement to check for updates
55
+ * @returns Updated status with available updates information
56
+ */
57
+ public async checkRequirementForUpdates(requirement: RequirementConfig): Promise<RequirementStatus> {
58
+ // Validate requirement
59
+ this.validateRequirement(requirement);
60
+
61
+ // Get current status
62
+ const status = await this.checkRequirementStatus(requirement);
63
+
64
+ // Check for updates using the appropriate installer
65
+ const installer = this.installerFactory.getInstaller(requirement);
66
+ if (!installer) {
67
+ return status;
68
+ }
69
+
70
+ // If the installer supports update checking, use it
71
+ if ('checkForUpdates' in installer) {
72
+ return await (installer as any).checkForUpdates(requirement, status);
73
+ }
74
+
75
+ return status;
76
+ }
77
+
78
+ /**
79
+ * Install multiple requirements
80
+ * @param requirements The requirements to install
81
+ * @returns A map of requirement names to their installation status
82
+ */
83
+ public async installRequirements(requirements: RequirementConfig[]): Promise<Record<string, RequirementStatus>> {
84
+ const results: Record<string, RequirementStatus> = {};
85
+
86
+ // Process each requirement sequentially to avoid conflicts
87
+ for (const requirement of requirements) {
88
+ results[requirement.name] = await this.installRequirement(requirement);
89
+ }
90
+
91
+ return results;
92
+ }
93
+
94
+ /**
95
+ * Validate a requirement configuration
96
+ * @param requirement The requirement to validate
97
+ * @throws Error if the requirement is invalid
98
+ */
99
+ private validateRequirement(requirement: RequirementConfig): void {
100
+ // Ensure requirement has required fields
101
+ if (!requirement.name) {
102
+ throw new Error('Requirement name is required');
103
+ }
104
+
105
+ if (!requirement.type) {
106
+ throw new Error('Requirement type is required');
107
+ }
108
+
109
+ // For type 'other', registry must be specified
110
+ if (requirement.type === 'other' && !requirement.registry) {
111
+ throw new Error('Registry must be specified for requirement type "other"');
112
+ }
113
+
114
+ // Validate registry configuration if provided
115
+ if (requirement.registry) {
116
+ const { githubRelease, artifacts, local } = requirement.registry;
117
+
118
+ // Validate GitHub release configuration
119
+ if (githubRelease) {
120
+ if (!githubRelease.repository) {
121
+ throw new Error('Repository is required for GitHub release registry');
122
+ }
123
+
124
+ if (!githubRelease.assetName) {
125
+ throw new Error('Asset name is required for GitHub release registry');
126
+ }
127
+ }
128
+
129
+ // Validate artifacts registry configuration
130
+ if (artifacts) {
131
+ if (!artifacts.registryUrl) {
132
+ throw new Error('Registry URL is required for artifacts registry');
133
+ }
134
+ }
135
+
136
+ // Validate local registry configuration
137
+ if (local) {
138
+ if (!local.localPath) {
139
+ throw new Error('Local path is required for local registry');
140
+ }
141
+ }
142
+ }
143
+ }
144
+ }
145
+
146
+ // Export a singleton instance
147
+ export const requirementService = RequirementService.getInstance();
@@ -0,0 +1,61 @@
1
+ import os from 'os';
2
+ import path from 'path';
3
+
4
+ /**
5
+ * Defines constants used across the application.
6
+ */
7
+
8
+ /**
9
+ * GitHub repository configuration for feeds
10
+ */
11
+ export const GITHUB_REPO = {
12
+ url: 'https://github.com/ai-microsoft/imcp-feed.git',
13
+ feedsPath: 'feeds'
14
+ };
15
+
16
+ /**
17
+ * Local settings directory path based on OS
18
+ */
19
+ export const SETTINGS_DIR = (() => {
20
+ switch (process.platform) {
21
+ case 'win32':
22
+ return path.join(os.homedir(), '.imcp');
23
+ default: // linux, darwin (macOS), etc.
24
+ return path.join(os.homedir(), '.imcp');
25
+ }
26
+ })();
27
+
28
+ /**
29
+ * Local feeds directory path
30
+ */
31
+ export const LOCAL_FEEDS_DIR = path.join(SETTINGS_DIR, 'feeds');
32
+
33
+ const CODE_STRORAGE_DIR = path.join(os.homedir(), 'AppData', 'Roaming', 'Code', 'User')
34
+ const CODE_INSIDER_STRORAGE_DIR = path.join(os.homedir(), 'AppData', 'Roaming', 'Code - Insiders', 'User')
35
+
36
+ /**
37
+ * Supported client configurations.
38
+ * Key: Client name (e.g., 'vscode')
39
+ * Value: Client-specific settings or configuration details.
40
+ * TODO: Define actual client settings structure.
41
+ */
42
+ export const SUPPORTED_CLIENTS: Record<string, any> = {
43
+ 'Cline': { /* VS Code specific settings */
44
+ codeSettingPath: path.join(CODE_STRORAGE_DIR, 'globalStorage', 'saoudrizwan.claude-dev', 'settings', 'cline_mcp_settings.json'),
45
+ codeInsiderSettingPath: path.join(CODE_INSIDER_STRORAGE_DIR, 'globalStorage', 'saoudrizwan.claude-dev', 'settings', 'cline_mcp_settings.json'),
46
+ },
47
+ 'MSRooCode': { /* MSROO specific settings */
48
+ codeSettingPath: path.join(CODE_STRORAGE_DIR, 'globalStorage', 'microsoftai.ms-roo-cline', 'settings', 'cline_mcp_settings.json'),
49
+ codeInsiderSettingPath: path.join(CODE_INSIDER_STRORAGE_DIR, 'globalStorage', 'microsoftai.ms-roo-clinev', 'settings', 'cline_mcp_settings.json'),
50
+ },
51
+ 'GithubCopilot': { /* GitHub Copilot specific settings */
52
+ codeSettingPath: path.join(CODE_STRORAGE_DIR, 'settings.json'),
53
+ codeInsiderSettingPath: path.join(CODE_INSIDER_STRORAGE_DIR, 'settings.json'),
54
+ },
55
+ // Add other supported clients here
56
+ };
57
+
58
+ /**
59
+ * List of supported client names.
60
+ */
61
+ export const SUPPORTED_CLIENT_NAMES = Object.keys(SUPPORTED_CLIENTS);
@@ -0,0 +1,292 @@
1
+ import { RequirementConfig, RequirementStatus, RegistryConfig } from '../types.js';
2
+ import path from 'path';
3
+ import fs from 'fs/promises';
4
+ import { SETTINGS_DIR } from '../constants.js';
5
+ import { extractZipFile } from '../../utils/clientUtils.js';
6
+ import { RequirementInstaller } from './RequirementInstaller.js';
7
+
8
+ /**
9
+ * Abstract base class with common functionality for all requirement installers
10
+ */
11
+ export abstract class BaseInstaller implements RequirementInstaller {
12
+ protected execPromise: (command: string) => Promise<{ stdout: string; stderr: string }>;
13
+ protected downloadsDir: string;
14
+
15
+ constructor(execPromise: (command: string) => Promise<{ stdout: string; stderr: string }>) {
16
+ this.execPromise = execPromise;
17
+ this.downloadsDir = path.join(SETTINGS_DIR, 'downloads');
18
+ }
19
+
20
+ abstract canHandle(requirement: RequirementConfig): boolean;
21
+ abstract install(requirement: RequirementConfig): Promise<RequirementStatus>;
22
+ abstract checkInstallation(requirement: RequirementConfig): Promise<RequirementStatus>;
23
+
24
+ /**
25
+ * Check if updates are available for the requirement
26
+ * @param requirement The requirement to check
27
+ * @param currentStatus The current status of the requirement
28
+ * @returns The status of the requirement with update information
29
+ */
30
+ async checkForUpdates(requirement: RequirementConfig, currentStatus: RequirementStatus): Promise<RequirementStatus> {
31
+ try {
32
+ // If requirement is not installed, no need to check for updates
33
+ if (!currentStatus.installed) {
34
+ return currentStatus;
35
+ }
36
+
37
+ // If the version doesn't contain "latest", no update check needed
38
+ if (!requirement.version.includes('latest')) {
39
+ return currentStatus;
40
+ }
41
+
42
+ let latestVersion: string | undefined;
43
+
44
+ // Check based on registry type
45
+ if (requirement.registry?.githubRelease) {
46
+ latestVersion = await this.getGitHubLatestVersion(requirement.registry.githubRelease.repository);
47
+ } else {
48
+ // Use common practice based on requirement type
49
+ switch (requirement.type) {
50
+ case 'npm':
51
+ latestVersion = await this.getNpmLatestVersion(requirement.name);
52
+ break;
53
+ case 'pip':
54
+ latestVersion = await this.getPipLatestVersion(requirement.name);
55
+ break;
56
+ // Add other types as needed
57
+ }
58
+ }
59
+
60
+ // If we found a latest version and it's different from current
61
+ if (latestVersion && latestVersion !== currentStatus.version) {
62
+ return {
63
+ ...currentStatus,
64
+ availableUpdate: {
65
+ version: latestVersion,
66
+ message: `Update available: ${currentStatus.version} → ${latestVersion}`,
67
+ checkTime: new Date().toISOString()
68
+ }
69
+ };
70
+ }
71
+
72
+ return currentStatus;
73
+ } catch (error) {
74
+ // Don't update status on error, just log it
75
+ console.warn(`Error checking for updates for ${requirement.name}: ${error instanceof Error ? error.message : String(error)}`);
76
+ return currentStatus;
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Helper method to ensure downloads directory exists
82
+ */
83
+ protected async ensureDownloadsDir(): Promise<void> {
84
+ await fs.mkdir(this.downloadsDir, { recursive: true });
85
+ }
86
+
87
+ /**
88
+ * Helper to handle GitHub release downloads
89
+ * @param requirement The requirement configuration
90
+ * @param registry The GitHub release registry configuration
91
+ * @returns The path to the downloaded file
92
+ */
93
+ protected async handleGitHubRelease(
94
+ requirement: RequirementConfig,
95
+ registry: RegistryConfig['githubRelease']
96
+ ): Promise<{ resolvedVersion: string; resolvedPath: string }> {
97
+ if (!registry) {
98
+ throw new Error('GitHub release registry configuration is required');
99
+ }
100
+
101
+ await this.ensureDownloadsDir();
102
+ const { repository, assetsName, assetName } = registry;
103
+
104
+ if (!repository) {
105
+ throw new Error('GitHub repository is required for GitHub release downloads');
106
+ }
107
+
108
+ let version = requirement.version;
109
+ let resolvedAssetName = assetName || '';
110
+ let resolvedAssetsName = assetsName || '';
111
+
112
+ // Handle latest version detection
113
+ if (version.includes('${latest}') || version === 'latest') {
114
+ const { stdout } = await this.execPromise(`gh release view --repo ${repository} --json tagName --jq .tagName`);
115
+ const latestTag = stdout.trim();
116
+ let latestVersion = latestTag
117
+ if (latestVersion.startsWith('v') && version.startsWith('v')) {
118
+ latestVersion = latestVersion.substring(1); // Remove 'v' prefix if present
119
+ // Replace ${latest} in version and asset names
120
+ version = version.replace('${latest}', latestVersion);
121
+ if (assetsName) {
122
+ resolvedAssetsName = assetsName.replace('${latest}', latestVersion);
123
+ }
124
+ if (assetName) {
125
+ resolvedAssetName = assetName.replace('${latest}', latestVersion);
126
+ }
127
+ }
128
+ }
129
+ const pattern = resolvedAssetsName ? resolvedAssetsName : resolvedAssetName;
130
+
131
+ if (!pattern) {
132
+ throw new Error('Either assetsName or assetName must be specified for GitHub release downloads');
133
+ }
134
+
135
+ // Download the release asset
136
+ const downloadPath = path.join(this.downloadsDir, path.basename(pattern));
137
+ if (!await this.fileExists(downloadPath)) {
138
+ await this.execPromise(`gh release download ${version} --repo ${repository} --pattern "${pattern}" -O "${downloadPath}"`);
139
+ }
140
+
141
+ // Handle zip file extraction if the downloaded file is a zip
142
+ if (downloadPath.endsWith('.zip')) {
143
+ const extractDir = path.join(this.downloadsDir, path.basename(pattern, '.zip'));
144
+ await fs.mkdir(extractDir, { recursive: true });
145
+
146
+ // Extract the zip file
147
+ await extractZipFile(downloadPath, { dir: extractDir });
148
+ let assetPath = '';
149
+ // If resolvedAssetName is specified, look for it in the extracted directory
150
+ if (resolvedAssetName) {
151
+ assetPath = path.join(extractDir, resolvedAssetName);
152
+ try {
153
+ await fs.access(assetPath);
154
+ return { resolvedVersion: version, resolvedPath: assetPath };
155
+ } catch (error) {
156
+ throw new Error(`Asset ${resolvedAssetName} not found in extracted directory ${extractDir}`);
157
+ }
158
+ }
159
+ else {
160
+ assetPath = path.join(extractDir, path.basename(pattern, '.zip') + '.tgz');
161
+ }
162
+
163
+ // If no specific asset is required, return the extraction directory
164
+ return { resolvedVersion: version, resolvedPath: extractDir };
165
+ }
166
+
167
+ return { resolvedVersion: version, resolvedPath: downloadPath };
168
+ }
169
+
170
+ /**
171
+ * Helper to handle artifact registry downloads
172
+ * @param requirement The requirement configuration
173
+ * @param registry The artifacts registry configuration
174
+ * @returns The registry URL
175
+ */
176
+ protected async handleArtifactsRegistry(
177
+ requirement: RequirementConfig,
178
+ registry: RegistryConfig['artifacts']
179
+ ): Promise<string> {
180
+ if (!registry) {
181
+ throw new Error('Artifacts registry configuration is required');
182
+ }
183
+
184
+ const { registryUrl, assetName } = registry;
185
+
186
+ if (!registryUrl) {
187
+ throw new Error('Registry URL is required for artifacts downloads');
188
+ }
189
+
190
+ return registryUrl;
191
+ }
192
+
193
+ /**
194
+ * Helper to handle local path registry
195
+ * @param requirement The requirement configuration
196
+ * @param registry The local registry configuration
197
+ * @returns The local path or extracted asset path
198
+ */
199
+ protected async handleLocalRegistry(
200
+ requirement: RequirementConfig,
201
+ registry: RegistryConfig['local']
202
+ ): Promise<string> {
203
+ if (!registry) {
204
+ throw new Error('Local registry configuration is required');
205
+ }
206
+
207
+ const { localPath, assetName } = registry;
208
+
209
+ if (!localPath) {
210
+ throw new Error('Local path is required for local registry');
211
+ }
212
+
213
+ // Verify the local path exists
214
+ try {
215
+ await fs.access(localPath);
216
+ } catch (error) {
217
+ throw new Error(`Local path ${localPath} does not exist or is not accessible`);
218
+ }
219
+
220
+ // If the path is a zip file and assetName is specified, extract it
221
+ if (localPath.endsWith('.zip') && assetName) {
222
+ const extractDir = path.join(this.downloadsDir, path.basename(localPath, '.zip'));
223
+ await fs.mkdir(extractDir, { recursive: true });
224
+
225
+ // Extract the zip file
226
+ await extractZipFile(localPath, { dir: extractDir });
227
+
228
+ // Find the asset in the extracted directory
229
+ const assetPath = path.join(extractDir, assetName);
230
+ try {
231
+ await fs.access(assetPath);
232
+ return assetPath;
233
+ } catch (error) {
234
+ throw new Error(`Asset ${assetName} not found in extracted directory ${extractDir}`);
235
+ }
236
+ }
237
+
238
+ return localPath;
239
+ }
240
+
241
+ private async fileExists(filePath: string): Promise<boolean> {
242
+ try {
243
+ await fs.access(filePath)
244
+ return true
245
+ } catch {
246
+ return false
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Get the latest version available for a GitHub repository
252
+ * @param repository The GitHub repository in format 'owner/repo'
253
+ * @returns The latest version or tag
254
+ */
255
+ protected async getGitHubLatestVersion(repository: string): Promise<string> {
256
+ try {
257
+ // Use GitHub CLI to get the latest release
258
+ const { stdout } = await this.execPromise(`gh release view --repo ${repository} --json tagName --jq .tagName`);
259
+ const latestTag = stdout.trim();
260
+
261
+ // Remove 'v' prefix if present
262
+ return latestTag.startsWith('v') ? latestTag.substring(1) : latestTag;
263
+ } catch (error) {
264
+ // If gh command fails, try to get the latest tag
265
+ const { stdout } = await this.execPromise(`git ls-remote --tags --refs https://github.com/${repository}.git | sort -t '/' -k 3 -V | tail -n 1 | awk -F/ '{print $3}'`);
266
+ let latestTag = stdout.trim();
267
+
268
+ // Remove 'v' prefix if present
269
+ return latestTag.startsWith('v') ? latestTag.substring(1) : latestTag;
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Get the latest version available for an NPM package
275
+ * @param packageName The name of the NPM package
276
+ * @returns The latest version
277
+ */
278
+ protected async getNpmLatestVersion(packageName: string): Promise<string> {
279
+ const { stdout } = await this.execPromise(`npm view ${packageName} version`);
280
+ return stdout.trim();
281
+ }
282
+
283
+ /**
284
+ * Get the latest version available for a pip package
285
+ * @param packageName The name of the pip package
286
+ * @returns The latest version
287
+ */
288
+ protected async getPipLatestVersion(packageName: string): Promise<string> {
289
+ const { stdout } = await this.execPromise(`pip index versions ${packageName} --pre=0 | grep -oP "(?<=Latest:\\s)[^\\s]+" | head -1`);
290
+ return stdout.trim();
291
+ }
292
+ }