imcp 0.1.4 → 0.1.5

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 (228) hide show
  1. package/README.md +21 -4
  2. package/dist/cli/commands/install.js.map +1 -0
  3. package/dist/cli/commands/list.js.map +1 -0
  4. package/dist/cli/commands/pull.js.map +1 -0
  5. package/dist/cli/commands/serve.js.map +1 -0
  6. package/dist/cli/commands/start.d.ts +2 -0
  7. package/dist/cli/commands/start.js +32 -0
  8. package/dist/cli/commands/start.js.map +1 -0
  9. package/dist/cli/commands/sync.d.ts +2 -0
  10. package/dist/cli/commands/sync.js +17 -0
  11. package/dist/cli/commands/sync.js.map +1 -0
  12. package/dist/cli/commands/uninstall.js.map +1 -0
  13. package/dist/cli/index.js +0 -0
  14. package/dist/cli/index.js.map +1 -0
  15. package/dist/core/ConfigurationLoader.d.ts +32 -0
  16. package/{src/core/loaders/ConfigurationLoader.ts → dist/core/ConfigurationLoader.js} +236 -298
  17. package/dist/core/ConfigurationLoader.js.map +1 -0
  18. package/dist/core/ConfigurationProvider.d.ts +35 -0
  19. package/{src/core/loaders/ConfigurationProvider.ts → dist/core/ConfigurationProvider.js} +375 -462
  20. package/dist/core/ConfigurationProvider.js.map +1 -0
  21. package/dist/core/InstallationService.d.ts +50 -0
  22. package/dist/core/InstallationService.js +350 -0
  23. package/dist/core/InstallationService.js.map +1 -0
  24. package/dist/core/MCPManager.d.ts +28 -0
  25. package/dist/core/MCPManager.js +188 -0
  26. package/dist/core/MCPManager.js.map +1 -0
  27. package/dist/core/RequirementService.d.ts +40 -0
  28. package/dist/core/RequirementService.js +110 -0
  29. package/dist/core/RequirementService.js.map +1 -0
  30. package/dist/core/ServerSchemaLoader.d.ts +11 -0
  31. package/dist/core/ServerSchemaLoader.js +43 -0
  32. package/dist/core/ServerSchemaLoader.js.map +1 -0
  33. package/dist/core/ServerSchemaProvider.d.ts +17 -0
  34. package/dist/core/ServerSchemaProvider.js +120 -0
  35. package/dist/core/ServerSchemaProvider.js.map +1 -0
  36. package/dist/core/constants.d.ts +47 -0
  37. package/dist/core/constants.js +94 -0
  38. package/dist/core/constants.js.map +1 -0
  39. package/dist/core/installers/BaseInstaller.d.ts +74 -0
  40. package/dist/core/installers/BaseInstaller.js +253 -0
  41. package/dist/core/installers/BaseInstaller.js.map +1 -0
  42. package/dist/core/installers/ClientInstaller.d.ts +23 -0
  43. package/dist/core/installers/ClientInstaller.js +564 -0
  44. package/dist/core/installers/ClientInstaller.js.map +1 -0
  45. package/dist/core/installers/CommandInstaller.d.ts +37 -0
  46. package/{src/core/installers/requirements/CommandInstaller.ts → dist/core/installers/CommandInstaller.js} +173 -231
  47. package/dist/core/installers/CommandInstaller.js.map +1 -0
  48. package/dist/core/installers/GeneralInstaller.d.ts +33 -0
  49. package/dist/core/installers/GeneralInstaller.js +85 -0
  50. package/dist/core/installers/GeneralInstaller.js.map +1 -0
  51. package/dist/core/installers/InstallerFactory.d.ts +54 -0
  52. package/{src/core/installers/requirements/InstallerFactory.ts → dist/core/installers/InstallerFactory.js} +97 -114
  53. package/dist/core/installers/InstallerFactory.js.map +1 -0
  54. package/dist/core/installers/NpmInstaller.d.ts +26 -0
  55. package/dist/core/installers/NpmInstaller.js +127 -0
  56. package/dist/core/installers/NpmInstaller.js.map +1 -0
  57. package/dist/core/installers/PipInstaller.d.ts +28 -0
  58. package/dist/core/installers/PipInstaller.js +127 -0
  59. package/dist/core/installers/PipInstaller.js.map +1 -0
  60. package/{src/core/installers/requirements/RequirementInstaller.ts → dist/core/installers/RequirementInstaller.d.ts} +33 -42
  61. package/dist/core/installers/RequirementInstaller.js +3 -0
  62. package/dist/core/installers/RequirementInstaller.js.map +1 -0
  63. package/dist/core/installers/clients/BaseClientInstaller.js.map +1 -0
  64. package/dist/core/installers/clients/ClientInstaller.js.map +1 -0
  65. package/dist/core/installers/clients/ClientInstallerFactory.js.map +1 -0
  66. package/dist/core/installers/clients/ClineInstaller.js.map +1 -0
  67. package/dist/core/installers/clients/ExtensionInstaller.js.map +1 -0
  68. package/dist/core/installers/clients/GithubCopilotInstaller.js.map +1 -0
  69. package/dist/core/installers/clients/MSRooCodeInstaller.js.map +1 -0
  70. package/dist/core/installers/index.js.map +1 -0
  71. package/dist/core/installers/requirements/BaseInstaller.js.map +1 -0
  72. package/dist/core/installers/requirements/CommandInstaller.js.map +1 -0
  73. package/dist/core/installers/requirements/GeneralInstaller.js.map +1 -0
  74. package/dist/core/installers/requirements/InstallerFactory.js.map +1 -0
  75. package/dist/core/installers/requirements/NpmInstaller.js.map +1 -0
  76. package/dist/core/installers/requirements/NugetInstaller.js.map +1 -0
  77. package/dist/core/installers/requirements/PipInstaller.js.map +1 -0
  78. package/dist/core/installers/requirements/RequirementInstaller.js.map +1 -0
  79. package/dist/core/loaders/ConfigurationLoader.js.map +1 -0
  80. package/dist/core/loaders/ConfigurationProvider.js.map +1 -0
  81. package/dist/core/loaders/InstallOperationManager.js.map +1 -0
  82. package/dist/core/loaders/ServerSchemaLoader.js.map +1 -0
  83. package/dist/core/loaders/ServerSchemaProvider.js.map +1 -0
  84. package/dist/core/loaders/SystemSettingsManager.js.map +1 -0
  85. package/dist/core/metadatas/constants.js.map +1 -0
  86. package/dist/core/metadatas/recordingConstants.js.map +1 -0
  87. package/dist/core/metadatas/types.js.map +1 -0
  88. package/dist/core/onboard/FeedOnboardService.js.map +1 -0
  89. package/dist/core/onboard/OnboardProcessor.js.map +1 -0
  90. package/dist/core/onboard/OnboardStatus.js.map +1 -0
  91. package/dist/core/onboard/OnboardStatusManager.js.map +1 -0
  92. package/dist/core/types.d.ts +166 -0
  93. package/dist/core/types.js +16 -0
  94. package/dist/core/types.js.map +1 -0
  95. package/dist/core/validators/FeedValidator.js.map +1 -0
  96. package/dist/core/validators/IServerValidator.js.map +1 -0
  97. package/dist/core/validators/SSEServerValidator.js.map +1 -0
  98. package/dist/core/validators/ServerValidatorFactory.js.map +1 -0
  99. package/dist/core/validators/StdioServerValidator.js.map +1 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/services/InstallRequestValidator.d.ts +21 -0
  102. package/dist/services/InstallRequestValidator.js +99 -0
  103. package/dist/services/InstallRequestValidator.js.map +1 -0
  104. package/dist/services/InstallationService.js.map +1 -0
  105. package/dist/services/MCPManager.js.map +1 -0
  106. package/dist/services/RequirementService.js.map +1 -0
  107. package/dist/services/ServerService.js.map +1 -0
  108. package/dist/services/TelemetryService.js.map +1 -0
  109. package/dist/utils/UpdateCheckTracker.js.map +1 -0
  110. package/dist/utils/adoUtils.js.map +1 -0
  111. package/dist/utils/clientUtils.js.map +1 -0
  112. package/dist/utils/feedUtils.js.map +1 -0
  113. package/dist/utils/githubAuth.js.map +1 -0
  114. package/dist/utils/githubUtils.js.map +1 -0
  115. package/dist/utils/logger.js.map +1 -0
  116. package/dist/utils/macroExpressionUtils.js.map +1 -0
  117. package/dist/utils/osUtils.js.map +1 -0
  118. package/dist/utils/versionUtils.js.map +1 -0
  119. package/dist/web/contract/serverContract.js.map +1 -0
  120. package/dist/web/public/index.html +1 -1
  121. package/dist/web/public/js/modal/installHandler.js +227 -0
  122. package/dist/web/public/js/modal/loadingUI.js +74 -0
  123. package/dist/web/public/js/modal/messageQueue.js +101 -45
  124. package/dist/web/public/js/modal/modalUI.js +214 -0
  125. package/{src/web/public/js/modal/versionUtils.js → dist/web/public/js/modal/version.js} +1 -1
  126. package/dist/web/public/onboard.html +4 -4
  127. package/dist/web/server.js.map +1 -0
  128. package/package.json +5 -1
  129. package/.github/ISSUE_TEMPLATE/JitAccess.yml +0 -28
  130. package/.github/acl/access.yml +0 -20
  131. package/.github/compliance/inventory.yml +0 -5
  132. package/.github/policies/jit.yml +0 -19
  133. package/.roo/rules-code/rules.md +0 -88
  134. package/dist/core/onboard/InstallOperationManager.d.ts +0 -23
  135. package/dist/core/onboard/InstallOperationManager.js +0 -144
  136. package/docs/ONBOARDING_PAGE_DESIGN.md +0 -260
  137. package/docs/Telemetry.md +0 -136
  138. package/memory-bank/activeContext.md +0 -26
  139. package/memory-bank/decisionLog.md +0 -91
  140. package/memory-bank/productContext.md +0 -41
  141. package/memory-bank/progress.md +0 -35
  142. package/memory-bank/systemPatterns.md +0 -10
  143. package/src/cli/commands/install.ts +0 -139
  144. package/src/cli/commands/list.ts +0 -113
  145. package/src/cli/commands/pull.ts +0 -16
  146. package/src/cli/commands/serve.ts +0 -39
  147. package/src/cli/commands/uninstall.ts +0 -64
  148. package/src/cli/index.ts +0 -82
  149. package/src/core/installers/clients/BaseClientInstaller.ts +0 -341
  150. package/src/core/installers/clients/ClientInstaller.ts +0 -222
  151. package/src/core/installers/clients/ClientInstallerFactory.ts +0 -43
  152. package/src/core/installers/clients/ClineInstaller.ts +0 -35
  153. package/src/core/installers/clients/ExtensionInstaller.ts +0 -165
  154. package/src/core/installers/clients/GithubCopilotInstaller.ts +0 -79
  155. package/src/core/installers/clients/MSRooCodeInstaller.ts +0 -32
  156. package/src/core/installers/index.ts +0 -11
  157. package/src/core/installers/requirements/BaseInstaller.ts +0 -85
  158. package/src/core/installers/requirements/GeneralInstaller.ts +0 -133
  159. package/src/core/installers/requirements/NpmInstaller.ts +0 -271
  160. package/src/core/installers/requirements/NugetInstaller.ts +0 -203
  161. package/src/core/installers/requirements/PipInstaller.ts +0 -207
  162. package/src/core/loaders/InstallOperationManager.ts +0 -367
  163. package/src/core/loaders/ServerSchemaLoader.ts +0 -117
  164. package/src/core/loaders/ServerSchemaProvider.ts +0 -99
  165. package/src/core/loaders/SystemSettingsManager.ts +0 -278
  166. package/src/core/metadatas/constants.ts +0 -122
  167. package/src/core/metadatas/recordingConstants.ts +0 -65
  168. package/src/core/metadatas/types.ts +0 -202
  169. package/src/core/onboard/FeedOnboardService.ts +0 -501
  170. package/src/core/onboard/OnboardProcessor.ts +0 -356
  171. package/src/core/onboard/OnboardStatus.ts +0 -60
  172. package/src/core/onboard/OnboardStatusManager.ts +0 -416
  173. package/src/core/validators/FeedValidator.ts +0 -135
  174. package/src/core/validators/IServerValidator.ts +0 -21
  175. package/src/core/validators/SSEServerValidator.ts +0 -43
  176. package/src/core/validators/ServerValidatorFactory.ts +0 -51
  177. package/src/core/validators/StdioServerValidator.ts +0 -313
  178. package/src/index.ts +0 -44
  179. package/src/services/InstallationService.ts +0 -102
  180. package/src/services/MCPManager.ts +0 -249
  181. package/src/services/RequirementService.ts +0 -627
  182. package/src/services/ServerService.ts +0 -161
  183. package/src/services/TelemetryService.ts +0 -59
  184. package/src/utils/UpdateCheckTracker.ts +0 -86
  185. package/src/utils/adoUtils.ts +0 -293
  186. package/src/utils/clientUtils.ts +0 -72
  187. package/src/utils/feedUtils.ts +0 -31
  188. package/src/utils/githubAuth.ts +0 -212
  189. package/src/utils/githubUtils.ts +0 -164
  190. package/src/utils/logger.ts +0 -195
  191. package/src/utils/macroExpressionUtils.ts +0 -104
  192. package/src/utils/osUtils.ts +0 -700
  193. package/src/utils/versionUtils.ts +0 -114
  194. package/src/web/contract/serverContract.ts +0 -74
  195. package/src/web/public/css/detailsWidget.css +0 -235
  196. package/src/web/public/css/modal.css +0 -757
  197. package/src/web/public/css/notifications.css +0 -101
  198. package/src/web/public/css/onboard.css +0 -107
  199. package/src/web/public/css/serverCategoryList.css +0 -120
  200. package/src/web/public/css/serverDetails.css +0 -139
  201. package/src/web/public/index.html +0 -359
  202. package/src/web/public/js/api.js +0 -132
  203. package/src/web/public/js/detailsWidget.js +0 -264
  204. package/src/web/public/js/flights/flights.js +0 -127
  205. package/src/web/public/js/modal/index.js +0 -52
  206. package/src/web/public/js/modal/installModal.js +0 -162
  207. package/src/web/public/js/modal/installation.js +0 -266
  208. package/src/web/public/js/modal/loadingModal.js +0 -182
  209. package/src/web/public/js/modal/modalSetup.js +0 -595
  210. package/src/web/public/js/modal/modalUtils.js +0 -37
  211. package/src/web/public/js/modal.js +0 -42
  212. package/src/web/public/js/notifications.js +0 -137
  213. package/src/web/public/js/onboard/formProcessor.js +0 -1037
  214. package/src/web/public/js/onboard/index.js +0 -374
  215. package/src/web/public/js/onboard/publishHandler.js +0 -172
  216. package/src/web/public/js/onboard/state.js +0 -76
  217. package/src/web/public/js/onboard/templates.js +0 -342
  218. package/src/web/public/js/onboard/uiHandlers.js +0 -1076
  219. package/src/web/public/js/onboard/validationHandlers.js +0 -493
  220. package/src/web/public/js/serverCategoryDetails.js +0 -364
  221. package/src/web/public/js/serverCategoryList.js +0 -241
  222. package/src/web/public/js/settings.js +0 -314
  223. package/src/web/public/modal.html +0 -84
  224. package/src/web/public/onboard.html +0 -296
  225. package/src/web/public/settings.html +0 -135
  226. package/src/web/public/styles.css +0 -277
  227. package/src/web/server.ts +0 -478
  228. package/tsconfig.json +0 -18
@@ -1,207 +0,0 @@
1
- import { RequirementConfig, RequirementStatus, ServerInstallOptions } from '../../metadatas/types.js';
2
- import { BaseInstaller } from './BaseInstaller.js';
3
- import { handleGitHubRelease, getGitHubLatestVersion } from '../../../utils/githubUtils.js';
4
- import { handleArtifact as handleAdoArtifact, getArtifactLatestVersion } from '../../../utils/adoUtils.js';
5
- import { compareVersions } from '../../../utils/versionUtils.js';
6
- import { Logger } from '../../../utils/logger.js';
7
- import { InstallOperationManager } from '../../loaders/InstallOperationManager.js';
8
- import * as RecordingConstants from '../../metadatas/recordingConstants.js';
9
- /**
10
- * Installer implementation for Python packages using pip
11
- */
12
- export class PipInstaller extends BaseInstaller {
13
- private getPythonCommand(options?: ServerInstallOptions): string {
14
- return options?.settings?.pythonEnv as string || 'python';
15
- }
16
-
17
- private getPipCommand(options?: ServerInstallOptions): string {
18
- const pythonCmd = this.getPythonCommand(options);
19
- return `${pythonCmd} -m pip`;
20
- }
21
-
22
- /**
23
- * Check if this installer can handle the given requirement type
24
- * @param requirement The requirement to check
25
- * @returns True if this installer can handle the requirement
26
- */
27
- canHandle(requirement: RequirementConfig): boolean {
28
- return requirement.type === 'pip';
29
- }
30
-
31
- supportCheckUpdates(): boolean {
32
- return true;
33
- }
34
-
35
- /**
36
- * Get the latest version available for the pip package.
37
- * @param requirement The requirement to check.
38
- * @param options Optional server install options.
39
- * @returns The latest version string, or undefined if not found or not applicable.
40
- */
41
- async getLatestVersion(requirement: RequirementConfig, options?: ServerInstallOptions): Promise<string | undefined> {
42
- if (requirement.registry) {
43
- if (requirement.registry.githubRelease) {
44
- return getGitHubLatestVersion(this.execPromise, requirement.registry.githubRelease.repository);
45
- } else if (requirement.registry.artifacts) {
46
- // Assuming getArtifactLatestVersion exists and has a compatible signature
47
- return getArtifactLatestVersion(requirement, requirement.registry.artifacts, options);
48
- }
49
- }
50
- // Default: get common latest version from pip index
51
- const pipCmd = this.getPipCommand(options);
52
- const { stdout } = await this.execPromise(`${pipCmd} index versions ${requirement.name} --pre=0`);
53
- // Parse output to find the latest version. Example output:
54
- // mypackage (1.0.0)
55
- // Available versions: 1.0.0, 0.9.0
56
- // LATEST: 1.0.0
57
- // Or for some packages:
58
- // mypackage
59
- // VERSIONS: 1.0.0, 0.9.0
60
- // Latest: 1.0.0
61
- const latestMatch = stdout.match(/(?:LATEST|Latest):\s*([^\s]+)/);
62
- if (latestMatch && latestMatch[1]) {
63
- return latestMatch[1];
64
- }
65
- // Fallback if LATEST line is not found, try to get the first version from "Available versions" or "VERSIONS"
66
- const versionsMatch = stdout.match(/(?:Available versions|VERSIONS):\s*([^\n]+)/);
67
- if (versionsMatch && versionsMatch[1]) {
68
- const versions = versionsMatch[1].split(',').map(v => v.trim());
69
- if (versions.length > 0) {
70
- // Assuming versions are listed in a somewhat reasonable order,
71
- // or we might need more sophisticated version sorting here.
72
- return versions[0];
73
- }
74
- }
75
- return undefined; // Or throw an error if version cannot be determined
76
- }
77
-
78
- /**
79
- * Check if the Python package is already installed
80
- * @param requirement The requirement to check
81
- * @param options Optional server install options
82
- * @returns The status of the requirement
83
- */
84
- async checkInstallation(requirement: RequirementConfig, options?: ServerInstallOptions): Promise<RequirementStatus> {
85
- try {
86
- const pipCmd = this.getPipCommand(options);
87
- const { stdout, stderr } = await this.execPromise(`${pipCmd} show ${requirement.name}`);
88
-
89
- const installed = stdout.includes(requirement.name.toLowerCase());
90
- const versionMatch = stdout.match(/Version: (.+)/);
91
- const installedVersion = versionMatch ? versionMatch[1] : undefined;
92
-
93
- return {
94
- name: requirement.name,
95
- type: 'pip',
96
- installed: installed,
97
- version: installedVersion,
98
- inProgress: false,
99
- pythonEnv: this.getPythonCommand(options)
100
- };
101
- } catch (error) {
102
- return {
103
- name: requirement.name,
104
- type: 'pip',
105
- installed: false,
106
- error: error instanceof Error ? error.message : String(error),
107
- inProgress: false,
108
- pythonEnv: this.getPythonCommand(options)
109
- };
110
- }
111
- }
112
-
113
- /**
114
- * Install the Python package
115
- * @param requirement The requirement to install
116
- * @param recorder Optional InstallOperationManager for recording steps
117
- * @param options Optional server install options
118
- * @returns The status of the installation
119
- */
120
- async install(requirement: RequirementConfig, recorder: InstallOperationManager, options?: ServerInstallOptions): Promise<RequirementStatus> {
121
-
122
- return await recorder.recording(
123
- async (): Promise<RequirementStatus> => {
124
- const status = await this.checkInstallation(requirement, options);
125
- if (status.installed && status.version && compareVersions(status.version, requirement.version) === 0 && !requirement.version.toLowerCase().includes('latest')) {
126
- Logger.log(`${requirement.name}==${status.version} already installed for ${this.getPythonCommand}.`);
127
- return status;
128
- }
129
-
130
- const pipCmd = this.getPipCommand(options);
131
- let command: string
132
-
133
- if (!requirement.registry) {
134
- if (requirement.version && !requirement.version.includes('latest')) {
135
- command = `${pipCmd} install ${requirement.name}==${requirement.version}`;
136
- } else {
137
- command = `${pipCmd} install --upgrade ${requirement.name}`;
138
- }
139
- } else {
140
- let packageSource: string;
141
- if (requirement.registry.githubRelease) {
142
- const result = await handleGitHubRelease(requirement, requirement.registry.githubRelease);
143
- packageSource = result.resolvedPath;
144
- command = `${pipCmd} install "${packageSource}"`
145
- } else if (requirement.registry.artifacts) {
146
- const pythonCmd = this.getPythonCommand(options);
147
- const adoArtifactResult = await handleAdoArtifact(requirement, requirement.registry.artifacts, pythonCmd);
148
-
149
- command = `${pipCmd} install ${adoArtifactResult.package} --extra-index-url ${adoArtifactResult.registryUrl}`;
150
- } else {
151
- await recorder.recordStep('PipInstaller:RegistryConfig', 'failed', 'Invalid registry configuration');
152
- throw new Error('Invalid registry configuration');
153
- }
154
- }
155
- return await recorder.recording(
156
- async () => {
157
- const { stderr } = await this.execPromise(command);
158
- if (stderr && stderr.toLowerCase().includes('error')) {
159
- Logger.debug(`Pip installation error: ${stderr}`);
160
-
161
- // wait for 5 seconds as python pip would be little delayed
162
- await new Promise(resolve => setTimeout(resolve, 5000));
163
- const checkStatus = await this.checkInstallation(requirement, options);
164
- if (!checkStatus.installed) {
165
- Logger.error(`Package not found after the command, ${stderr}`);
166
- throw new Error(`Pip installation failed with: ${stderr}`);
167
- }
168
- }
169
-
170
- return {
171
- name: requirement.name,
172
- type: 'pip',
173
- installed: true,
174
- version: requirement.version, // This might need to be updated to actual installed version
175
- inProgress: false,
176
- pythonEnv: this.getPythonCommand(options)
177
- };
178
- },
179
- {
180
- stepName: `${RecordingConstants.STEP_INSTALL_COMMAND_PREFIX}: ${requirement.name} : ${requirement.version}`,
181
- inProgressMessage: `Running: ${command}`,
182
- endMessage: (result) => result.installed ? `Succeeded: ${command}` : `Failed: ${command}`,
183
- }
184
- );
185
- },
186
- {
187
- stepName: RecordingConstants.STEP_PIP_INSTALLER_INSTALL,
188
- inProgressMessage: `Installing pip package: ${requirement.name}`,
189
- endMessage: (result) => result.installed
190
- ? `Install completed for ${requirement.name} with version ${result.version}`
191
- : `Install failed for ${requirement.name}`,
192
- onError: (error) => {
193
- return {
194
- result: {
195
- name: requirement.name,
196
- type: 'pip',
197
- installed: false,
198
- error: error instanceof Error ? error.message : String(error),
199
- inProgress: false,
200
- pythonEnv: this.getPythonCommand(options)
201
- },
202
- message: error instanceof Error ? error.message : String(error),
203
- };
204
- },
205
- });
206
- }
207
- }
@@ -1,367 +0,0 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { SETTINGS_DIR } from '../metadatas/constants.js';
4
- import { InstallOperationDetails, InstallOperationStep } from '../metadatas/types.js';
5
- import { Logger } from '../../utils/logger.js';
6
-
7
- const INSTALL_STATUS_DIR = path.join(SETTINGS_DIR, 'InstallOperationStatus');
8
-
9
- export class InstallOperationManager {
10
- private installOperationStatus: Record<string, InstallOperationDetails> = {};
11
- private statusLock: Promise<void> = Promise.resolve();
12
- private readonly categoryName: string;
13
- private readonly serverName: string;
14
- private readonly statusFilePath: string;
15
-
16
- /**
17
- * Creates an InstallOperationManager instance for a specific category and server.
18
- * @param categoryName The name of the category.
19
- * @param serverName The name of the server.
20
- */
21
- constructor(categoryName: string, serverName: string) {
22
- this.categoryName = categoryName;
23
- this.serverName = serverName;
24
- this.statusFilePath = path.join(
25
- INSTALL_STATUS_DIR,
26
- this.categoryName,
27
- `${this.serverName}.json`
28
- );
29
- this.loadStatuses().catch(error => Logger.error('Failed to initialize InstallOperationManager:', error));
30
- }
31
-
32
- /**
33
- * Returns an InstallOperationManager instance for the given category and server.
34
- * @param categoryName The name of the category.
35
- * @param serverName The name of the server.
36
- */
37
- private static instanceMap: Map<string, InstallOperationManager> = new Map();
38
-
39
- /**
40
- * Returns a cached InstallOperationManager instance for the given category and server.
41
- * If an instance does not exist, it will be created and cached.
42
- * @param categoryName The name of the category.
43
- * @param serverName The name of the server.
44
- */
45
- public static getInstance(categoryName: string, serverName: string): InstallOperationManager {
46
- const key = `${categoryName}::${serverName}`;
47
- if (!this.instanceMap.has(key)) {
48
- this.instanceMap.set(key, new InstallOperationManager(categoryName, serverName));
49
- }
50
- return this.instanceMap.get(key)!;
51
- }
52
-
53
- private async withLock<T>(operation: () => Promise<T>): Promise<T> {
54
- const current = this.statusLock;
55
- let resolve: () => void;
56
- this.statusLock = new Promise<void>(r => resolve = r);
57
- try {
58
- await current;
59
- return await operation();
60
- } finally {
61
- resolve!();
62
- }
63
- }
64
-
65
- private async loadStatuses(): Promise<void> {
66
- await this.withLock(async () => {
67
- try {
68
- await fs.mkdir(path.dirname(this.statusFilePath), { recursive: true });
69
- const data = await fs.readFile(this.statusFilePath, 'utf-8');
70
- this.installOperationStatus = JSON.parse(data);
71
- } catch (error) {
72
- if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
73
- this.installOperationStatus = {};
74
- await this.saveStatuses(); // Create the file if it doesn't exist
75
- } else {
76
- Logger.error('Failed to load install operation statuses:', error);
77
- this.installOperationStatus = {}; // Initialize with empty statuses in case of other errors
78
- }
79
- }
80
- });
81
- }
82
-
83
- private async saveStatuses(): Promise<void> {
84
- await fs.mkdir(path.dirname(this.statusFilePath), { recursive: true });
85
- await fs.writeFile(this.statusFilePath, JSON.stringify(this.installOperationStatus, null, 2));
86
- }
87
-
88
- private get operationKey(): string {
89
- return `${this.categoryName}-${this.serverName}`;
90
- }
91
-
92
- /**
93
- * Record a step for this category/server instance.
94
- */
95
- public async recordStep(
96
- stepName: string,
97
- status: 'pending' | 'in-progress' | 'completed' | 'failed',
98
- message?: string
99
- ): Promise<InstallOperationDetails> {
100
- return await this.withLock(async () => {
101
- let operationDetails = this.installOperationStatus[this.operationKey];
102
-
103
- const newStep: InstallOperationStep = {
104
- name: stepName,
105
- status,
106
- message,
107
- timestamp: new Date().toISOString(),
108
- };
109
-
110
- if (!operationDetails) {
111
- operationDetails = {
112
- currentStep: stepName,
113
- steps: [newStep],
114
- lastUpdated: new Date().toISOString(),
115
- error: status === 'failed' ? message : undefined,
116
- overallStatus: status === 'failed' ? 'failed' : 'in-progress',
117
- };
118
- } else {
119
- // Check for an existing unfinished step with the same name
120
- const existingStepIndex = operationDetails.steps.findIndex(
121
- s => s.name === stepName && s.status !== 'completed' && s.status !== 'failed'
122
- );
123
- if (existingStepIndex !== -1) {
124
- // Update the existing unfinished step
125
- operationDetails.steps[existingStepIndex] = newStep;
126
- } else {
127
- operationDetails.steps.push(newStep);
128
- }
129
- operationDetails.currentStep = stepName;
130
- operationDetails.lastUpdated = new Date().toISOString();
131
-
132
- if (status === 'failed') {
133
- operationDetails.overallStatus = 'failed';
134
- if (!operationDetails.error) { // Store the first error
135
- operationDetails.error = message;
136
- }
137
- // Mark all in-progress steps as cancelled
138
- operationDetails.steps = operationDetails.steps.map(s =>
139
- s.status === 'in-progress'
140
- ? {
141
- ...s,
142
- status: 'canceled',
143
- message: (s.message ? s.message + ' ' : '') + '[Canceled due to failure]',
144
- timestamp: new Date().toISOString(),
145
- }
146
- : s
147
- );
148
- } else if (status === 'completed' && stepName.toLowerCase().includes('completed')) {
149
- // Check if all steps are completed or if there are any failures
150
- const allStepsCompleted = operationDetails.steps.every(s =>
151
- s.status === 'completed' || (s.name === stepName && status === 'completed')
152
- );
153
- operationDetails.overallStatus = allStepsCompleted ? 'completed' : 'in-progress';
154
- if (allStepsCompleted) {
155
- operationDetails.error = undefined; // Clear error on completion
156
- }
157
- } else {
158
- if (operationDetails.overallStatus !== 'failed') {
159
- operationDetails.overallStatus = 'in-progress';
160
- }
161
- }
162
- }
163
-
164
- // Special handling for the "InstallCompleted" step or similar final step names
165
- if (stepName === 'InstallCompleted') {
166
- operationDetails.overallStatus = status === 'completed' ? 'completed' : 'failed';
167
- if (status === 'completed') operationDetails.error = undefined;
168
- }
169
-
170
- this.installOperationStatus[this.operationKey] = operationDetails;
171
- await this.saveStatuses();
172
- return operationDetails;
173
- });
174
- }
175
-
176
- /**
177
- * Resets (deletes) the operation status for this category/server.
178
- * This is useful to call before starting a new installation attempt.
179
- */
180
- public async resetOperation(): Promise<InstallOperationManager> {
181
- await this.withLock(async () => {
182
- if (this.installOperationStatus[this.operationKey]) {
183
- delete this.installOperationStatus[this.operationKey];
184
- Logger.info(`Reset installation operation status for ${this.operationKey}`);
185
- await this.saveStatuses();
186
- }
187
- });
188
- return this;
189
- }
190
-
191
- /**
192
- * Gets the details for this category/server.
193
- */
194
- public async getDetails(): Promise<InstallOperationDetails | undefined> {
195
- await this.loadStatuses(); // Ensure latest statuses are loaded
196
- return this.installOperationStatus[this.operationKey];
197
- }
198
-
199
- /**
200
- * Gets all operation details in this file (for this category/server).
201
- */
202
- public async getAllDetails(): Promise<Record<string, InstallOperationDetails>> {
203
- await this.loadStatuses();
204
- return this.installOperationStatus;
205
- }
206
-
207
- /**
208
- * Explicitly mark the overall status of an operation as 'completed' or 'failed'.
209
- * This can be used to force the final state regardless of step details.
210
- * @param status 'completed' or 'failed'
211
- * @param error Optional error message if failed
212
- */
213
- public async markOverallStatus(status: 'completed' | 'failed', error?: string): Promise<InstallOperationDetails | undefined> {
214
- return await this.withLock(async () => {
215
- const operationDetails = this.installOperationStatus[this.operationKey];
216
- if (!operationDetails) return undefined;
217
- operationDetails.overallStatus = status;
218
- operationDetails.lastUpdated = new Date().toISOString();
219
- if (status === 'failed') {
220
- if (error) {
221
- operationDetails.error = error;
222
- }
223
- // Mark all in-progress steps as cancelled
224
- operationDetails.steps = operationDetails.steps.map(s =>
225
- s.status === 'in-progress'
226
- ? {
227
- ...s,
228
- status: 'canceled',
229
- message: (s.message ? s.message + ' ' : '') + '[Canceled due to failure]',
230
- timestamp: new Date().toISOString(),
231
- }
232
- : s
233
- );
234
- }
235
- if (status === 'completed') {
236
- operationDetails.error = undefined;
237
- }
238
- await this.saveStatuses();
239
- return operationDetails;
240
- });
241
- }
242
-
243
- /**
244
- * Executes a function and records its step status as 'in-progress', 'completed', or 'failed'.
245
- *
246
- * Useful for wrapping tasks with consistent status tracking and flexible error handling.
247
- *
248
- * @template T The return type of the function being executed.
249
- *
250
- * @param fn The function to execute. Can be synchronous or asynchronous.
251
- * @param options Optional configuration:
252
- * - stepName: Custom name for the step. Defaults to the function name or 'unnamedStep'.
253
- * - inProgressMessage: Optional message to log when the step starts.
254
- * - endMessage: Optional static string or function to generate a final message from the result or error.
255
- * - onResult: A function `(result: T) => boolean` that determines whether the step was successful. Defaults to always `true`.
256
- * - onError: Optional function `(error) => { result: T; message: string } | never` that handles an error and may return a fallback result and message, or throw.
257
- *
258
- * @returns A promise that resolves with the result of the function if successful, or rethrows on failure.
259
- *
260
- * @throws The original error or any error thrown from `onError`.
261
- */
262
- public async recording<T>(
263
- fn: () => T | Promise<T>,
264
- options?: {
265
- stepName?: string;
266
- inProgressMessage?: string;
267
- endMessage?: string | ((data: T) => string | undefined);
268
- onResult?: (result: T) => boolean;
269
- onError?: (error: unknown) => { result: T; message: string } | Promise<{ result: T; message: string }> | never;
270
- }
271
- ): Promise<T> {
272
- const { stepName, inProgressMessage = 'Step in progress', endMessage, onResult = () => true, onError } = options || {};
273
-
274
- const resolvedStepName = stepName ?? (fn.name || 'unnamedStep');
275
- const getEndMessage = (data: T, fallback: string) =>
276
- typeof endMessage === 'function' ? endMessage(data) || 'Step completed' : endMessage ?? fallback;
277
-
278
- await this.recordStep(resolvedStepName, 'in-progress', inProgressMessage);
279
-
280
- try {
281
- const result = await Promise.resolve(fn());
282
- const isSuccess = onResult(result);
283
- await this.recordStep(resolvedStepName, isSuccess ? 'completed' : 'failed', getEndMessage(result, isSuccess ? 'Step completed successfully' : 'Step failed'));
284
- return result;
285
- } catch (err) {
286
- if (!onError) {
287
- await this.recordStep(resolvedStepName, 'failed', (err as Error)?.message || String(err));
288
- throw err;
289
- }
290
-
291
- try {
292
- const { result, message } = await onError(err);
293
- await this.recordStep(resolvedStepName, 'failed', message);
294
- return result;
295
- } catch (onErrorThrown) {
296
- await this.recordStep(resolvedStepName, 'failed', (onErrorThrown as Error)?.message || String(onErrorThrown));
297
- throw onErrorThrown;
298
- }
299
- }
300
- }
301
-
302
-
303
- /**
304
- * Executes an asynchronous "fire-and-forget" task and records its step status as 'in-progress', 'completed', or 'failed'.
305
- *
306
- * Unlike `recording`, this does not await the result — it runs the task in the background.
307
- * Useful for side-effecting operations like updates or notifications where progress should be logged but not block flow.
308
- *
309
- * @template T The resolved type of the asynchronous task.
310
- *
311
- * @param fn The async function to execute.
312
- * @param options Optional configuration:
313
- * - stepName: Custom name for the step. Defaults to the function name or 'unnamedStep'.
314
- * - inProgressMessage: Message to log when the step starts.
315
- * - endMessage: Static string or function to generate the final message from result or error.
316
- * - onResult: A boolean or function `(result: T) => boolean` to determine if the step is successful.
317
- * - onError: A function `(error) => string` or `Promise<string>` to handle errors and return a message.
318
- * If it throws, the error is rethrown and logged.
319
- * - onComplete: Optional callback to run after completion, regardless of success or failure.
320
- *
321
- * @returns void
322
- */
323
- public recordingAsync<T>(fn: () => Promise<T>, options?: {
324
- stepName?: string;
325
- inProgressMessage?: string;
326
- endMessage?: string | ((result: T) => string);
327
- onResult?: (result: T) => boolean;
328
- onError?: (error: unknown) => string | Promise<string> | never;
329
- onComplete?: () => void;
330
- }
331
- ): void {
332
- const {
333
- stepName,
334
- inProgressMessage = 'Step in progress',
335
- endMessage,
336
- onResult = () => true,
337
- onError,
338
- onComplete,
339
- } = options || {};
340
-
341
- const resolvedStepName = stepName ?? (fn.name || 'unnamedStep');
342
- const getMessage = (data: T, fallback: string) => typeof endMessage === 'function' ? endMessage(data) : endMessage ?? fallback;
343
-
344
- this.recordStep(resolvedStepName, 'in-progress', inProgressMessage).then(() => {
345
- fn()
346
- .then(result => {
347
- const isSuccess = onResult(result);
348
- const message = getMessage(result, isSuccess ? 'Step completed successfully' : 'Step failed');
349
- this.recordStep(resolvedStepName, isSuccess ? 'completed' : 'failed', message).then(onComplete).catch();
350
- })
351
- .catch(async (err) => {
352
- if (onError) {
353
- try {
354
- const errorMessage = await onError(err);
355
- await this.recordStep(resolvedStepName, 'failed', errorMessage);
356
- } catch (onErrorThrown) {
357
- await this.recordStep(resolvedStepName, 'failed', (onErrorThrown as Error)?.message || String(onErrorThrown));
358
- throw onErrorThrown;
359
- }
360
- } else {
361
- const fallback = getMessage(err, (err as Error)?.message || String(err));
362
- await this.recordStep(resolvedStepName, 'failed', fallback);
363
- }
364
- });
365
- });
366
- }
367
- }