-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcreate-project.ts
More file actions
205 lines (182 loc) · 7.23 KB
/
create-project.ts
File metadata and controls
205 lines (182 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type * as CommandExecutor from "@effect/platform/CommandExecutor"
import type { PlatformError } from "@effect/platform/Error"
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { Effect } from "effect"
import type { CreateCommand } from "../../core/domain.js"
import { deriveRepoPathParts } from "../../core/domain.js"
import { runCommandWithExitCodes } from "../../shell/command-runner.js"
import { ensureDockerDaemonAccess } from "../../shell/docker.js"
import { CommandFailedError } from "../../shell/errors.js"
import type {
CloneFailedError,
DockerAccessError,
DockerCommandError,
FileExistsError,
PortProbeError
} from "../../shell/errors.js"
import { logDockerAccessInfo } from "../access-log.js"
import { renderError } from "../errors.js"
import { applyGithubForkConfig } from "../github-fork.js"
import { defaultProjectsRoot } from "../menu-helpers.js"
import { findSshPrivateKey } from "../path-helpers.js"
import { buildSshCommand } from "../projects-core.js"
import { autoSyncState } from "../state-repo.js"
import { runDockerUpIfNeeded } from "./docker-up.js"
import { buildProjectConfigs, resolveDockerGitRootRelativePath } from "./paths.js"
import { resolveSshPort } from "./ports.js"
import { migrateProjectOrchLayout, prepareProjectFiles } from "./prepare-files.js"
type CreateProjectRuntime = FileSystem.FileSystem | Path.Path | CommandExecutor.CommandExecutor
type CreateProjectError =
| FileExistsError
| CloneFailedError
| DockerAccessError
| DockerCommandError
| PortProbeError
| PlatformError
type CreateContext = {
readonly baseDir: string
readonly resolveRootPath: (value: string) => string
}
const makeCreateContext = (path: Path.Path, baseDir: string): CreateContext => {
const projectsRoot = path.resolve(defaultProjectsRoot(baseDir))
const resolveRootPath = (value: string): string => resolveDockerGitRootRelativePath(path, projectsRoot, value)
return { baseDir, resolveRootPath }
}
const resolveRootedConfig = (command: CreateCommand, ctx: CreateContext): CreateCommand["config"] => ({
...command.config,
dockerGitPath: ctx.resolveRootPath(command.config.dockerGitPath),
authorizedKeysPath: ctx.resolveRootPath(command.config.authorizedKeysPath),
envGlobalPath: ctx.resolveRootPath(command.config.envGlobalPath),
envProjectPath: ctx.resolveRootPath(command.config.envProjectPath),
codexAuthPath: ctx.resolveRootPath(command.config.codexAuthPath),
codexSharedAuthPath: ctx.resolveRootPath(command.config.codexSharedAuthPath)
})
const resolveCreateConfig = (
command: CreateCommand,
ctx: CreateContext,
resolvedOutDir: string
): Effect.Effect<
CreateCommand["config"],
PortProbeError | PlatformError,
FileSystem.FileSystem | Path.Path | CommandExecutor.CommandExecutor
> =>
resolveSshPort(resolveRootedConfig(command, ctx), resolvedOutDir).pipe(
Effect.flatMap((config) => applyGithubForkConfig(config))
)
const logCreatedProject = (resolvedOutDir: string, createdFiles: ReadonlyArray<string>) =>
Effect.gen(function*(_) {
yield* _(Effect.log(`Created docker-git project in ${resolvedOutDir}`))
for (const file of createdFiles) {
yield* _(Effect.log(` - ${file}`))
}
}).pipe(Effect.asVoid)
const formatStateSyncLabel = (repoUrl: string): string => {
const repoPath = deriveRepoPathParts(repoUrl).pathParts.join("/")
return repoPath.length > 0 ? repoPath : repoUrl
}
const isInteractiveTty = (): boolean => process.stdin.isTTY === true && process.stdout.isTTY === true
const buildSshArgs = (
config: CreateCommand["config"],
sshKeyPath: string | null
): ReadonlyArray<string> => {
const args: Array<string> = []
if (sshKeyPath !== null) {
args.push("-i", sshKeyPath)
}
args.push(
"-tt",
"-Y",
"-o",
"LogLevel=ERROR",
"-o",
"StrictHostKeyChecking=no",
"-o",
"UserKnownHostsFile=/dev/null",
"-p",
String(config.sshPort),
`${config.sshUser}@localhost`
)
return args
}
// CHANGE: auto-open SSH after environment is created (best-effort)
// WHY: clone flow should drop the user into the container without manual copy/paste
// QUOTE(ТЗ): "Мне надо что бы он сразу открыл SSH"
// REF: issue-39
// SOURCE: n/a
// FORMAT THEOREM: forall c: openSsh(c) -> ssh_session_started(c) || warning_logged(c)
// PURITY: SHELL
// EFFECT: Effect<void, never, FileSystem | Path | CommandExecutor>
// INVARIANT: SSH failures do not fail the create/clone command
// COMPLEXITY: O(1) + ssh
const openSshBestEffort = (
template: CreateCommand["config"]
): Effect.Effect<void, never, CreateProjectRuntime> =>
Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const sshKey = yield* _(findSshPrivateKey(fs, path, process.cwd()))
const sshCommand = buildSshCommand(template, sshKey)
yield* _(Effect.log(`Opening SSH: ${sshCommand}`))
yield* _(
runCommandWithExitCodes(
{
cwd: process.cwd(),
command: "ssh",
args: buildSshArgs(template, sshKey)
},
[0, 130],
(exitCode) => new CommandFailedError({ command: "ssh", exitCode })
)
)
}).pipe(
Effect.asVoid,
Effect.matchEffect({
onFailure: (error) => Effect.logWarning(`SSH auto-open failed: ${renderError(error)}`),
onSuccess: () => Effect.void
})
)
const runCreateProject = (
path: Path.Path,
command: CreateCommand
): Effect.Effect<void, CreateProjectError, CreateProjectRuntime> =>
Effect.gen(function*(_) {
if (command.runUp) {
yield* _(ensureDockerDaemonAccess(process.cwd()))
}
const ctx = makeCreateContext(path, process.cwd())
const resolvedOutDir = path.resolve(ctx.resolveRootPath(command.outDir))
const resolvedConfig = yield* _(resolveCreateConfig(command, ctx, resolvedOutDir))
const { globalConfig, projectConfig } = buildProjectConfigs(path, ctx.baseDir, resolvedOutDir, resolvedConfig)
yield* _(migrateProjectOrchLayout(ctx.baseDir, globalConfig, ctx.resolveRootPath))
const createdFiles = yield* _(
prepareProjectFiles(resolvedOutDir, ctx.baseDir, globalConfig, projectConfig, {
force: command.force,
forceEnv: command.forceEnv
})
)
yield* _(logCreatedProject(resolvedOutDir, createdFiles))
yield* _(
runDockerUpIfNeeded(resolvedOutDir, projectConfig, {
runUp: command.runUp,
waitForClone: command.waitForClone,
force: command.force,
forceEnv: command.forceEnv
})
)
if (command.runUp) {
yield* _(logDockerAccessInfo(resolvedOutDir, projectConfig))
}
yield* _(autoSyncState(`chore(state): update ${formatStateSyncLabel(projectConfig.repoUrl)}`))
if (command.openSsh) {
if (!command.runUp) {
yield* _(Effect.logWarning("Skipping SSH auto-open: docker compose up disabled (--no-up)."))
} else if (!isInteractiveTty()) {
yield* _(Effect.logWarning("Skipping SSH auto-open: not running in an interactive TTY."))
} else {
yield* _(openSshBestEffort(projectConfig))
}
}
}).pipe(Effect.asVoid)
export const createProject = (command: CreateCommand): Effect.Effect<void, CreateProjectError, CreateProjectRuntime> =>
Path.Path.pipe(Effect.flatMap((path) => runCreateProject(path, command)))