-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.service.ts
More file actions
170 lines (153 loc) · 4.78 KB
/
project.service.ts
File metadata and controls
170 lines (153 loc) · 4.78 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
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import dotenv from 'dotenv';
import { findUpSync } from 'find-up';
import { ZodError } from 'zod';
import {
aiLlmBootstrapEnvSchema,
aiLlmSmokeEnvSchema,
bootstrapEnvSchema,
formatZodError,
labEnvSchema,
smokeEnvSchema
} from '../config/lab-env.schema.js';
import { PROJECT_MARKERS, REPOSITORY_PATHS, resolveRepositoryLayout } from '../config/repository-layout.js';
import { ensureDevelopmentFileLogging } from './runtime-log.service.js';
import type { GlobalCliOptions } from '../types/cli.types.js';
import type {
AiLlmBootstrapEnv,
AiLlmSmokeEnv,
BootstrapEnv,
LabEnv,
ProjectContext,
SmokeEnv
} from '../types/project.types.js';
import { printInfo } from '../ui/logger.js';
/**
* Creates the runtime context used by commands that operate on a checkout.
*/
export function createProjectContext(options: GlobalCliOptions): ProjectContext {
const workingDirectory = process.cwd();
const resolution = resolveProjectRoot(options.projectDir, workingDirectory);
const projectRoot = resolution.projectRoot;
const layout = resolveRepositoryLayout(projectRoot);
const fileLogSession = ensureDevelopmentFileLogging(projectRoot);
if (fileLogSession.initializedNow && fileLogSession.filePath) {
printInfo(`Development file log: ${fileLogSession.filePath}`, 'runtime');
}
return {
projectRoot,
runtimeSource: resolution.runtimeSource,
workingDirectory,
layout,
env: loadLabEnv(layout.envFile)
};
}
/**
* Validates and narrows the env for bootstrap workflows.
*/
export function parseBootstrapEnv(env: LabEnv): BootstrapEnv {
return parseWithSchema(() => bootstrapEnvSchema.parse(env));
}
/**
* Validates and narrows the env for AI LLM bootstrap workflows.
*/
export function parseAiLlmBootstrapEnv(env: LabEnv): AiLlmBootstrapEnv {
return parseWithSchema(() => aiLlmBootstrapEnvSchema.parse(env));
}
/**
* Validates and narrows the env for smoke-check workflows.
*/
export function parseSmokeEnv(env: LabEnv): SmokeEnv {
return parseWithSchema(() => smokeEnvSchema.parse(env));
}
/**
* Validates and narrows the env for AI LLM smoke-check workflows.
*/
export function parseAiLlmSmokeEnv(env: LabEnv): AiLlmSmokeEnv {
return parseWithSchema(() => aiLlmSmokeEnvSchema.parse(env));
}
/**
* Resolves the lab asset root from an explicit path, the current checkout, or the installed package.
*/
export function resolveProjectRoot(
explicitProjectDir?: string,
cwd = process.cwd(),
packagedProjectRoot = resolvePackagedProjectRoot()
): {
projectRoot: string;
runtimeSource: ProjectContext['runtimeSource'];
} {
if (explicitProjectDir) {
const projectRoot = resolve(explicitProjectDir);
validateProjectRoot(projectRoot);
return {
projectRoot,
runtimeSource: 'explicit-path'
};
}
const projectRoot = findUpSync(
(directory) => (isProjectRoot(directory) ? directory : undefined),
{
cwd,
type: 'directory'
}
);
if (!projectRoot) {
if (isProjectRoot(packagedProjectRoot)) {
return {
projectRoot: packagedProjectRoot,
runtimeSource: 'packaged-install'
};
}
throw new Error('Could not locate the Atlas Lab assets from the current directory or the installed package.');
}
return {
projectRoot,
runtimeSource: 'checkout'
};
}
/**
* Loads the lab env file using the same parsing rules as runtime tooling.
*/
function loadLabEnv(envFile: string): LabEnv {
return parseWithSchema(() => labEnvSchema.parse(dotenv.parse(readFileSync(envFile))));
}
/**
* Centralizes Zod parsing so services get readable validation errors.
*/
function parseWithSchema<TValue>(parse: () => TValue): TValue {
try {
return parse();
} catch (error) {
if (error instanceof ZodError) {
throw new Error(`Invalid ${REPOSITORY_PATHS.envFile}: ${formatZodError(error)}`);
}
throw error;
}
}
/**
* Validates an explicit project path passed by the user.
*/
function validateProjectRoot(projectRoot: string): void {
if (!isProjectRoot(projectRoot)) {
throw new Error(
`Invalid project directory: ${projectRoot}. Expected ${REPOSITORY_PATHS.composeFile} and ${REPOSITORY_PATHS.envFile} in that path.`
);
}
}
/**
* Checks whether a folder looks like a valid lab checkout.
*/
function isProjectRoot(directory: string): boolean {
return PROJECT_MARKERS.every((marker) => existsSync(join(directory, marker)));
}
/**
* Resolves the package root that contains the bundled CLI and the embedded lab assets.
*/
export function resolvePackagedProjectRoot(moduleUrl = import.meta.url): string {
const moduleFile = fileURLToPath(moduleUrl);
return resolve(dirname(moduleFile), '..', '..');
}