dsh-plugin-container-windows 0.0.0-stage → 0.5.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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +109 -2
  3. package/README.zh-CN.md +110 -0
  4. package/cordis.patch.yml +3 -0
  5. package/docs/ai-chat.png +0 -0
  6. package/docs/ai-new-session.png +0 -0
  7. package/docs/apple-container.png +0 -0
  8. package/docs/attach.png +0 -0
  9. package/docs/docker-console.png +0 -0
  10. package/docs/docker-overview.png +0 -0
  11. package/docs/docker-volumes.png +0 -0
  12. package/docs/image-architecture.png +0 -0
  13. package/docs/package-install.png +0 -0
  14. package/docs/provider-switch.png +0 -0
  15. package/docs/run-container.png +0 -0
  16. package/index.js +141 -0
  17. package/lib/client.js +4092 -0
  18. package/package.json +72 -4
  19. package/src/client/api.js +98 -0
  20. package/src/client/attach.jsx +134 -0
  21. package/src/client/build-image.jsx +224 -0
  22. package/src/client/components.jsx +325 -0
  23. package/src/client/hooks.js +161 -0
  24. package/src/client/i18n.js +602 -0
  25. package/src/client/icons.jsx +239 -0
  26. package/src/client/index.jsx +90 -0
  27. package/src/client/page.jsx +854 -0
  28. package/src/client/platforms.js +2 -0
  29. package/src/client/push-image.jsx +113 -0
  30. package/src/client/run-container.jsx +187 -0
  31. package/src/client/runs.js +182 -0
  32. package/src/client/store.js +91 -0
  33. package/src/client/styles.js +383 -0
  34. package/src/client/tabs/compose.jsx +94 -0
  35. package/src/client/tabs/console.jsx +211 -0
  36. package/src/client/tabs/containers.jsx +148 -0
  37. package/src/client/tabs/images.jsx +154 -0
  38. package/src/client/tabs/networks.jsx +91 -0
  39. package/src/client/tabs/overview.jsx +266 -0
  40. package/src/client/tabs/volumes.jsx +76 -0
  41. package/src/host/apple-cli.js +319 -0
  42. package/src/host/apple.js +495 -0
  43. package/src/host/attach.js +246 -0
  44. package/src/host/commands.js +241 -0
  45. package/src/host/docker-cli.js +468 -0
  46. package/src/host/images.js +143 -0
  47. package/src/host/jobs.js +245 -0
  48. package/src/host/parsers.js +484 -0
  49. package/src/host/runtime.js +379 -0
  50. package/src/host/service.js +892 -0
  51. package/src/host/tools.js +198 -0
  52. package/src/host/util.js +97 -0
@@ -0,0 +1,379 @@
1
+ /**
2
+ * macOS Docker runtime discovery and lifecycle actions.
3
+ *
4
+ * The runtime is inferred from the selected Docker context endpoint plus the
5
+ * local application/CLI inventory. Nothing here mutates Docker configuration:
6
+ * commands use `--context`, and colima/Docker Desktop actions are explicit
7
+ * user choices from the panel.
8
+ */
9
+ import { existsSync } from 'node:fs'
10
+ import { homedir } from 'node:os'
11
+ import { join } from 'node:path'
12
+
13
+ import { classifyEndpoint, parseColimaList, parseColimaStatus } from './parsers.js'
14
+ import { messageOf } from './util.js'
15
+
16
+ const DOCKER_APP_CANDIDATES = ['/Applications/Docker.app', join(homedir(), 'Applications/Docker.app')]
17
+ const ORB_APP_CANDIDATES = ['/Applications/OrbStack.app', join(homedir(), 'Applications/OrbStack.app')]
18
+
19
+ /**
20
+ * @param {NodeJS.ProcessEnv} env
21
+ * @param {string} name
22
+ */
23
+ function envValue(env, name) {
24
+ const exact = env[name]
25
+ if (exact !== undefined) return exact
26
+ const upper = name.toUpperCase()
27
+ return Object.entries(env).find(([key]) => key.toUpperCase() === upper)?.[1]
28
+ }
29
+
30
+ /** @param {NodeJS.ProcessEnv} env */
31
+ function windowsSystemRoot(env) {
32
+ const value = envValue(env, 'SystemRoot') ?? envValue(env, 'WINDIR')
33
+ return typeof value === 'string' && value !== '' ? value : 'C:\\Windows'
34
+ }
35
+
36
+ /**
37
+ * @param {NodeJS.ProcessEnv} env
38
+ * @param {string} home
39
+ */
40
+ function windowsDockerApps(env, home) {
41
+ const roots = [
42
+ envValue(env, 'ProgramFiles'),
43
+ envValue(env, 'ProgramFiles(X86)'),
44
+ envValue(env, 'LOCALAPPDATA'),
45
+ join(home, 'AppData', 'Local'),
46
+ ].filter((value) => typeof value === 'string' && value !== '')
47
+ return roots.map((root) => join(root, 'Docker', 'Docker', 'Docker Desktop.exe'))
48
+ }
49
+
50
+ /** @param {string[]} paths @param {(path: string) => boolean} exists */
51
+ function firstExisting(paths, exists) {
52
+ return paths.find((path) => exists(path)) ?? null
53
+ }
54
+
55
+ /**
56
+ * @param {{
57
+ * cli: import('./docker-cli.js').DockerCli,
58
+ * platform?: NodeJS.Platform,
59
+ * home?: string,
60
+ * env?: NodeJS.ProcessEnv,
61
+ * fs?: { existsSync: (path: string) => boolean },
62
+ * config?: Record<string, any>,
63
+ * }} options
64
+ */
65
+ export function createRuntimeResolver(options) {
66
+ const cli = options.cli
67
+ const platform = options.platform ?? process.platform
68
+ const env = options.env ?? process.env
69
+ const fs = options.fs ?? { existsSync }
70
+ const config = options.config ?? {}
71
+ const controlEnabled = (platform === 'darwin' || platform === 'win32') && config.enableRuntimeControl !== false
72
+ let desktopCliSupport = false
73
+ let desktopCliCheckedAt = 0
74
+
75
+ /**
76
+ * Docker Desktop 4.37+ ships `docker desktop start|stop|restart`. Probe it
77
+ * once per minute; older installs fall back to explorer.exe / taskkill.exe.
78
+ * @param {AbortSignal | undefined} signal
79
+ */
80
+ async function probeDesktopCli(signal) {
81
+ if (platform !== 'win32' || typeof cli.docker !== 'function') return false
82
+ const now = Date.now()
83
+ if (desktopCliCheckedAt !== 0 && now - desktopCliCheckedAt < 60_000) return desktopCliSupport
84
+ desktopCliCheckedAt = now
85
+ try {
86
+ await cli.docker(['desktop', 'status'], { signal, timeoutMs: 5_000, maxBytes: 64 * 1024 })
87
+ desktopCliSupport = true
88
+ } catch {
89
+ desktopCliSupport = false
90
+ }
91
+ return desktopCliSupport
92
+ }
93
+
94
+ /** @param {AbortSignal | undefined} signal */
95
+ async function inventory(signal) {
96
+ const dockerDesktop = platform === 'win32'
97
+ ? firstExisting(windowsDockerApps(env, options.home ?? homedir()), fs.existsSync)
98
+ : firstExisting(DOCKER_APP_CANDIDATES, fs.existsSync)
99
+ const orbStack = platform === 'darwin' ? firstExisting(ORB_APP_CANDIDATES, fs.existsSync) : null
100
+ const colimaPath = platform === 'darwin' ? await cli.ensureColima({ signal }) : null
101
+ let colimaInstances = []
102
+ let colimaError
103
+ if (colimaPath !== null) {
104
+ try {
105
+ const result = await cli.program(colimaPath, ['list', '--json'], { signal, timeoutMs: 15_000, maxBytes: 128 * 1024 })
106
+ colimaInstances = parseColimaList(result.stdout)
107
+ } catch (error) {
108
+ colimaError = messageOf(error)
109
+ }
110
+ }
111
+ const desktopCli = await probeDesktopCli(signal)
112
+ return { dockerDesktop, desktopCli, orbStack, colimaPath, colimaInstances, colimaError }
113
+ }
114
+
115
+ /**
116
+ * @param {Array<{name:string,description:string,endpoint:string,current:boolean,error:string}>} contexts
117
+ * @param {string | null | undefined} requested
118
+ */
119
+ function selectContext(contexts, requested) {
120
+ const current = contexts.find((context) => context.current) ?? null
121
+ const selected = (requested ? contexts.find((context) => context.name === requested) : undefined) ?? current ?? contexts[0] ?? null
122
+ return { current, selected, requested: requested ?? null }
123
+ }
124
+
125
+ /**
126
+ * @param {Awaited<ReturnType<typeof inventory>>} found
127
+ * @param {{name:string, endpoint:string} | null} context
128
+ * @param {{running?: boolean, error?: string}} daemon
129
+ */
130
+ function describe(found, context, daemon = {}) {
131
+ const endpointClass = classifyEndpoint(context?.endpoint)
132
+ const daemonRunning = daemon.running === true
133
+ if (endpointClass.kind === 'colima') {
134
+ const profile = endpointClass.profile ?? 'default'
135
+ const instance =
136
+ found.colimaInstances.find((item) => item.name === profile) ??
137
+ found.colimaInstances.find((item) => (context?.name ?? '').endsWith(profile)) ??
138
+ null
139
+ const vmRunning = instance !== null ? /running/iu.test(instance.status) : daemonRunning
140
+ return {
141
+ kind: 'colima',
142
+ label: 'colima',
143
+ profile,
144
+ installed: found.colimaPath !== null,
145
+ running: daemonRunning,
146
+ vmRunning,
147
+ canStart: controlEnabled && found.colimaPath !== null && !daemonRunning,
148
+ canStop: controlEnabled && found.colimaPath !== null && (daemonRunning || vmRunning),
149
+ canRestart: controlEnabled && found.colimaPath !== null && (daemonRunning || vmRunning) && vmRunning,
150
+ endpoint: context?.endpoint ?? '',
151
+ details: instance,
152
+ error: found.colimaError ?? daemon.error ?? null,
153
+ }
154
+ }
155
+ if (endpointClass.kind === 'orbstack') {
156
+ return {
157
+ kind: 'orbstack',
158
+ label: 'OrbStack',
159
+ profile: null,
160
+ installed: found.orbStack !== null,
161
+ running: daemonRunning,
162
+ vmRunning: daemonRunning,
163
+ canStart: controlEnabled && found.orbStack !== null && !daemonRunning,
164
+ canStop: controlEnabled && found.orbStack !== null && daemonRunning,
165
+ canRestart: false,
166
+ endpoint: context?.endpoint ?? '',
167
+ details: found.orbStack !== null ? { appPath: found.orbStack } : null,
168
+ error: daemon.error ?? null,
169
+ }
170
+ }
171
+ if (endpointClass.kind === 'local-socket') {
172
+ const windowsDesktop = platform === 'win32' && (/npipe:/iu.test(context?.endpoint ?? '') || found.dockerDesktop !== null)
173
+ if (found.dockerDesktop !== null || windowsDesktop) {
174
+ return {
175
+ kind: 'docker-desktop',
176
+ label: 'Docker Desktop',
177
+ profile: null,
178
+ installed: found.dockerDesktop !== null || windowsDesktop,
179
+ running: daemonRunning,
180
+ vmRunning: daemonRunning,
181
+ canStart: controlEnabled && !daemonRunning,
182
+ canStop: controlEnabled && daemonRunning,
183
+ canRestart: controlEnabled && daemonRunning && platform !== 'darwin' && found.desktopCli === true,
184
+ endpoint: context?.endpoint ?? '',
185
+ details: {
186
+ appPath: found.dockerDesktop,
187
+ desktopCli: found.desktopCli === true,
188
+ windows: platform === 'win32',
189
+ },
190
+ error: daemon.error ?? null,
191
+ }
192
+ }
193
+ return {
194
+ kind: 'unknown',
195
+ label: 'Docker',
196
+ profile: null,
197
+ installed: null,
198
+ running: daemonRunning,
199
+ vmRunning: daemonRunning,
200
+ canStart: false,
201
+ canStop: false,
202
+ canRestart: false,
203
+ endpoint: context?.endpoint ?? '',
204
+ details: null,
205
+ error: daemon.error ?? null,
206
+ }
207
+ }
208
+ if (endpointClass.kind === 'remote') {
209
+ return {
210
+ kind: 'remote',
211
+ label: '远程 Docker',
212
+ profile: null,
213
+ installed: null,
214
+ running: daemonRunning,
215
+ vmRunning: daemonRunning,
216
+ canStart: false,
217
+ canStop: false,
218
+ canRestart: false,
219
+ endpoint: context?.endpoint ?? '',
220
+ details: null,
221
+ error: daemon.error ?? null,
222
+ }
223
+ }
224
+ return {
225
+ kind: 'unknown',
226
+ label: 'Docker',
227
+ profile: null,
228
+ installed: null,
229
+ running: daemonRunning,
230
+ vmRunning: daemonRunning,
231
+ canStart: false,
232
+ canStop: false,
233
+ canRestart: false,
234
+ endpoint: context?.endpoint ?? '',
235
+ details: null,
236
+ error: daemon.error ?? null,
237
+ }
238
+ }
239
+
240
+ /**
241
+ * @param {Array<{name:string,description:string,endpoint:string,current:boolean,error:string}>} contexts
242
+ * @param {string | null | undefined} requested
243
+ * @param {{running?: boolean, error?: string}} daemon
244
+ * @param {AbortSignal | undefined} signal
245
+ */
246
+ async function resolve(contexts, requested, daemon, signal) {
247
+ const found = await inventory(signal)
248
+ const selected = selectContext(contexts, requested)
249
+ const runtime = describe(found, selected.selected, daemon)
250
+ if (runtime.kind === 'colima' && runtime.profile !== null && runtime.installed) {
251
+ try {
252
+ const result = await cli.program(found.colimaPath, ['status', '--json', '--profile', runtime.profile], {
253
+ signal,
254
+ timeoutMs: 15_000,
255
+ maxBytes: 256 * 1024,
256
+ })
257
+ const status = parseColimaStatus(result.stdout)
258
+ if (status !== null) {
259
+ runtime.details = { ...(runtime.details ?? {}), ...status }
260
+ runtime.vmRunning = true
261
+ runtime.canStop = controlEnabled
262
+ runtime.canRestart = controlEnabled
263
+ runtime.cpus = status.cpus
264
+ runtime.memory = status.memory
265
+ runtime.disk = status.disk
266
+ runtime.driver = status.driver
267
+ runtime.arch = status.arch
268
+ runtime.mountType = status.mountType
269
+ runtime.dockerSocket = status.dockerSocket
270
+ runtime.kubernetes = status.kubernetes
271
+ }
272
+ } catch (error) {
273
+ // Stopped colima instances cannot report status; the list already
274
+ // carries the state, so this is not an error worth surfacing.
275
+ if (runtime.vmRunning) runtime.error = runtime.error ?? messageOf(error)
276
+ }
277
+ }
278
+ return { ...selected, runtime }
279
+ }
280
+
281
+ /**
282
+ * Build one explicit runtime action. The caller decides confirmation and
283
+ * whether to run it as a job.
284
+ * @param {string} action
285
+ * @param {ReturnType<typeof describe>} runtime
286
+ */
287
+ function actionSpec(action, runtime) {
288
+ if (!controlEnabled) throw Object.assign(new Error('当前平台或配置不允许控制 Docker 运行时'), { code: 'docker/unsupported' })
289
+ if (action !== 'start' && action !== 'stop' && action !== 'restart') {
290
+ throw Object.assign(new Error(`不支持的运行时操作: ${action}`), { code: 'docker/unsupported' })
291
+ }
292
+ if (runtime.kind === 'colima') {
293
+ if (!runtime.installed) throw Object.assign(new Error('未找到 colima CLI'), { code: 'docker/cli-missing' })
294
+ if (action === 'restart' && !runtime.canRestart) {
295
+ throw Object.assign(new Error('当前 colima 未运行,无需重启'), { code: 'docker/unsupported' })
296
+ }
297
+ const colimaPath = cli.colimaPath
298
+ if (colimaPath === undefined || colimaPath === null) throw Object.assign(new Error('未找到 colima CLI'), { code: 'docker/cli-missing' })
299
+ const profile = runtime.profile ?? 'default'
300
+ return {
301
+ argv: [colimaPath, action, '--profile', profile],
302
+ title: `colima ${action}`,
303
+ displayCommand: `colima ${action} --profile ${profile}`,
304
+ timeoutMs: 10 * 60_000,
305
+ }
306
+ }
307
+ if (runtime.kind === 'docker-desktop') {
308
+ if (platform === 'win32') {
309
+ const dockerPath = cli.dockerPath
310
+ if (runtime.details?.desktopCli === true && typeof dockerPath === 'string' && dockerPath !== '') {
311
+ return {
312
+ argv: [dockerPath, 'desktop', action],
313
+ title: `Docker Desktop ${action}`,
314
+ displayCommand: `docker desktop ${action}`,
315
+ timeoutMs: action === 'start' ? 180_000 : 120_000,
316
+ }
317
+ }
318
+ const system32 = join(windowsSystemRoot(env), 'System32')
319
+ if (action === 'start') {
320
+ const app = runtime.details?.appPath
321
+ if (typeof app !== 'string' || app === '') {
322
+ throw Object.assign(new Error('未找到 Docker Desktop 安装路径,不能启动运行时'), { code: 'docker/cli-missing' })
323
+ }
324
+ return {
325
+ argv: [join(system32, 'explorer.exe'), app],
326
+ title: '启动 Docker Desktop',
327
+ displayCommand: `explorer.exe "${app}"`,
328
+ timeoutMs: 30_000,
329
+ }
330
+ }
331
+ if (action === 'stop') {
332
+ return {
333
+ argv: [join(system32, 'taskkill.exe'), '/IM', 'Docker Desktop.exe', '/F', '/T'],
334
+ title: '停止 Docker Desktop',
335
+ displayCommand: 'taskkill.exe /IM "Docker Desktop.exe" /F /T',
336
+ timeoutMs: 120_000,
337
+ }
338
+ }
339
+ throw Object.assign(new Error('当前 Docker Desktop 版本不支持一键重启,请停止后再启动'), { code: 'docker/unsupported' })
340
+ }
341
+ if (action === 'restart') throw Object.assign(new Error('Docker Desktop 暂不支持一键重启,请先停止再启动'), { code: 'docker/unsupported' })
342
+ if (action === 'start') {
343
+ const app = runtime.details?.appPath ?? 'Docker'
344
+ return {
345
+ argv: ['/usr/bin/open', '-a', app],
346
+ title: '启动 Docker Desktop',
347
+ displayCommand: `open -a ${app}`,
348
+ timeoutMs: 30_000,
349
+ }
350
+ }
351
+ return {
352
+ argv: ['/usr/bin/osascript', '-e', 'tell application id "com.docker.docker" to quit'],
353
+ title: '停止 Docker Desktop',
354
+ displayCommand: 'osascript -e \'tell application id "com.docker.docker" to quit\'',
355
+ timeoutMs: 120_000,
356
+ }
357
+ }
358
+ if (runtime.kind === 'orbstack') {
359
+ if (action === 'restart') throw Object.assign(new Error('OrbStack 暂不支持一键重启'), { code: 'docker/unsupported' })
360
+ const app = runtime.details?.appPath ?? 'OrbStack'
361
+ return action === 'start'
362
+ ? {
363
+ argv: ['/usr/bin/open', '-a', app],
364
+ title: '启动 OrbStack',
365
+ displayCommand: `open -a ${app}`,
366
+ timeoutMs: 30_000,
367
+ }
368
+ : {
369
+ argv: ['/usr/bin/osascript', '-e', 'tell application "OrbStack" to quit'],
370
+ title: '停止 OrbStack',
371
+ displayCommand: 'osascript -e \'tell application "OrbStack" to quit\'',
372
+ timeoutMs: 120_000,
373
+ }
374
+ }
375
+ throw Object.assign(new Error('当前 Docker 上下文没有可本机控制的运行时'), { code: 'docker/unsupported' })
376
+ }
377
+
378
+ return { inventory, selectContext, resolve, actionSpec, controlEnabled, platform, env }
379
+ }