Skip to content

Commit 2c285d1

Browse files
committed
split class and test-utils
1 parent cc1fa14 commit 2c285d1

File tree

15 files changed

+1560
-37
lines changed

15 files changed

+1560
-37
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@types/rimraf": "^4.0.5"
4343
},
4444
"dependencies": {
45+
"@launchql/logger": "^1.0.1",
4546
"@launchql/migrate": "^2.2.1",
4647
"@launchql/server-utils": "^2.1.15",
4748
"@launchql/templatizer": "^2.1.6",
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
import { Logger } from '@launchql/logger';
2+
import {
3+
ConfigManager,
4+
ModuleManager,
5+
ExtensionManager,
6+
PackageManager,
7+
PlanGenerator
8+
} from '../managers';
9+
10+
export enum ProjectContext {
11+
Workspace = 'workspace',
12+
Module = 'module',
13+
Unknown = 'unknown'
14+
}
15+
16+
/**
17+
* Refactored LaunchQL class using manager pattern
18+
* This class serves as a facade to the various managers
19+
*/
20+
export class LaunchQL {
21+
private logger = new Logger('launchql');
22+
private configManager: ConfigManager;
23+
private moduleManager: ModuleManager;
24+
private extensionManager: ExtensionManager;
25+
private packageManager: PackageManager;
26+
private planGenerator: PlanGenerator;
27+
28+
constructor(cwd: string = process.cwd()) {
29+
// Initialize managers
30+
this.configManager = new ConfigManager(cwd);
31+
this.moduleManager = new ModuleManager(this.configManager);
32+
this.extensionManager = new ExtensionManager(this.configManager, this.moduleManager);
33+
this.packageManager = new PackageManager(
34+
this.configManager,
35+
this.moduleManager,
36+
this.extensionManager
37+
);
38+
this.planGenerator = new PlanGenerator(
39+
this.configManager,
40+
this.moduleManager,
41+
this.extensionManager
42+
);
43+
}
44+
45+
// Delegate to ConfigManager
46+
resetCwd(cwd: string): void {
47+
this.configManager.resetCwd(cwd);
48+
}
49+
50+
isInsideAllowedDirs(cwd: string): boolean {
51+
return this.configManager.isInsideAllowedDirs(cwd);
52+
}
53+
54+
ensureModule(): void {
55+
this.configManager.ensureModule();
56+
}
57+
58+
ensureWorkspace(): void {
59+
this.configManager.ensureWorkspace();
60+
}
61+
62+
getContext(): ProjectContext {
63+
if (this.configManager.isInModule() && this.configManager.isInWorkspace()) {
64+
return ProjectContext.Module;
65+
}
66+
if (this.configManager.isInWorkspace()) return ProjectContext.Workspace;
67+
return ProjectContext.Unknown;
68+
}
69+
70+
isInWorkspace(): boolean {
71+
return this.configManager.isInWorkspace();
72+
}
73+
74+
isInModule(): boolean {
75+
return this.configManager.isInModule();
76+
}
77+
78+
getWorkspacePath(): string | undefined {
79+
return this.configManager.getWorkspacePath();
80+
}
81+
82+
getModulePath(): string | undefined {
83+
return this.configManager.getModulePath();
84+
}
85+
86+
get cwd(): string {
87+
return this.configManager.getCwd();
88+
}
89+
90+
get workspacePath(): string | undefined {
91+
return this.configManager.getWorkspacePath();
92+
}
93+
94+
get modulePath(): string | undefined {
95+
return this.configManager.getModulePath();
96+
}
97+
98+
get config(): any {
99+
return this.configManager.getConfig();
100+
}
101+
102+
get allowedDirs(): string[] {
103+
return this.configManager.getAllowedDirs();
104+
}
105+
106+
// Delegate to ModuleManager
107+
clearCache(): void {
108+
this.moduleManager.clearCache();
109+
this.extensionManager.clearCache();
110+
}
111+
112+
getAvailableModules(): LaunchQL[] {
113+
return this.moduleManager.getAvailableModules();
114+
}
115+
116+
getModuleMap(): any {
117+
return this.moduleManager.getModuleMap();
118+
}
119+
120+
getModuleName(): string {
121+
return this.moduleManager.getModuleName();
122+
}
123+
124+
getModuleDependencies(): string[] {
125+
return this.moduleManager.getModuleDependencies();
126+
}
127+
128+
getModuleDependencyChanges(): string[] {
129+
return this.moduleManager.getModuleDependencyChanges();
130+
}
131+
132+
getLatestChange(): string | undefined {
133+
return this.moduleManager.getLatestChange();
134+
}
135+
136+
getLatestChangeAndVersion(): any {
137+
return this.moduleManager.getLatestChangeAndVersion();
138+
}
139+
140+
getModulePlan(): string {
141+
return this.moduleManager.getModulePlan();
142+
}
143+
144+
getModuleSQL(): string[] {
145+
return this.moduleManager.getModuleSQL();
146+
}
147+
148+
getModuleControlFile(): any {
149+
return this.moduleManager.getModuleControlFile();
150+
}
151+
152+
getModuleMakefile(): string | null {
153+
return this.moduleManager.getModuleMakefile();
154+
}
155+
156+
normalizeChangeName(name: string): string {
157+
return this.moduleManager.normalizeChangeName(name);
158+
}
159+
160+
async initModule(name: string): Promise<string> {
161+
return this.moduleManager.initModule(name);
162+
}
163+
164+
// Delegate to ExtensionManager
165+
getModuleInfo(): any {
166+
return this.extensionManager.getModuleInfo();
167+
}
168+
169+
getModuleExtensions(): string[] {
170+
return this.extensionManager.getModuleExtensions();
171+
}
172+
173+
getAvailableExtensions(): string[] {
174+
return this.extensionManager.getAvailableExtensions();
175+
}
176+
177+
getInstalledExtensions(): string[] {
178+
return this.extensionManager.getInstalledExtensions();
179+
}
180+
181+
async installExtension(packageSpec: string): Promise<void> {
182+
return this.extensionManager.installExtension(packageSpec);
183+
}
184+
185+
async writeExtensions(): Promise<void> {
186+
return this.extensionManager.writeExtensions();
187+
}
188+
189+
// Delegate to PackageManager
190+
async publishToDist(): Promise<void> {
191+
return this.packageManager.publishToDist();
192+
}
193+
194+
setModuleDependencies(dependencies: string[]): void {
195+
this.packageManager.setModuleDependencies(dependencies);
196+
}
197+
198+
getRequiredModules(): string[] {
199+
return this.packageManager.getRequiredModules();
200+
}
201+
202+
// Delegate to PlanGenerator
203+
generatePlanFromFiles(): string {
204+
return this.planGenerator.generatePlanFromFiles();
205+
}
206+
207+
writeModulePlan(): void {
208+
this.planGenerator.writeModulePlan();
209+
}
210+
211+
addChangeToProject(name: string, dependencies?: string[]): void {
212+
this.planGenerator.addChangeToProject(name, dependencies);
213+
}
214+
215+
validatePlanConsistency(): { valid: boolean; errors: string[] } {
216+
return this.planGenerator.validatePlanConsistency();
217+
}
218+
219+
// Convenience methods that combine multiple managers
220+
async install(packages: string[]): Promise<void> {
221+
for (const pkg of packages) {
222+
await this.installExtension(pkg);
223+
}
224+
}
225+
226+
async deploy(): Promise<void> {
227+
// Validate plan first
228+
const validation = this.validatePlanConsistency();
229+
if (!validation.valid) {
230+
throw new Error(`Invalid plan: ${validation.errors.join(', ')}`);
231+
}
232+
233+
// Deploy logic would go here
234+
this.logger.info('Deploy functionality to be implemented');
235+
}
236+
237+
async revert(target?: string): Promise<void> {
238+
// Revert logic would go here
239+
this.logger.info('Revert functionality to be implemented');
240+
}
241+
242+
async verify(): Promise<void> {
243+
// Verify logic would go here
244+
this.logger.info('Verify functionality to be implemented');
245+
}
246+
247+
async status(): Promise<any> {
248+
// Status logic would go here
249+
return {
250+
workspace: this.getWorkspacePath(),
251+
module: this.getModulePath(),
252+
context: this.getContext(),
253+
modules: this.getAvailableModules().map(m => m.getModuleName()),
254+
extensions: this.getInstalledExtensions()
255+
};
256+
}
257+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { walkUp } from '../utils';
4+
import { Logger } from '@launchql/logger';
5+
6+
export interface LaunchQLConfig {
7+
version?: string;
8+
directories?: string[];
9+
[key: string]: any;
10+
}
11+
12+
export class ConfigManager {
13+
private logger = new Logger('launchql:config');
14+
private config?: LaunchQLConfig;
15+
private workspacePath?: string;
16+
private modulePath?: string;
17+
private allowedDirs: string[] = [];
18+
private cwd: string;
19+
20+
constructor(cwd: string = process.cwd()) {
21+
this.cwd = cwd;
22+
this.initialize();
23+
}
24+
25+
private initialize(): void {
26+
this.workspacePath = this.resolveLaunchqlPath();
27+
this.modulePath = this.resolveSqitchPath();
28+
29+
if (this.workspacePath) {
30+
this.config = this.loadConfig();
31+
this.allowedDirs = this.loadAllowedDirs();
32+
}
33+
}
34+
35+
resetCwd(cwd: string): void {
36+
this.cwd = cwd;
37+
this.initialize();
38+
}
39+
40+
private resolveLaunchqlPath(): string | undefined {
41+
try {
42+
return walkUp(this.cwd, 'launchql.json');
43+
} catch {
44+
return undefined;
45+
}
46+
}
47+
48+
private resolveSqitchPath(): string | undefined {
49+
try {
50+
return walkUp(this.cwd, 'sqitch.conf');
51+
} catch {
52+
return undefined;
53+
}
54+
}
55+
56+
private loadConfig(): LaunchQLConfig {
57+
if (!this.workspacePath) {
58+
throw new Error('Workspace path not found');
59+
}
60+
const configPath = path.join(this.workspacePath, 'launchql.json');
61+
const content = fs.readFileSync(configPath, 'utf8');
62+
return JSON.parse(content);
63+
}
64+
65+
private loadAllowedDirs(): string[] {
66+
if (!this.config?.directories) return [];
67+
68+
return this.config.directories.map(dir =>
69+
path.resolve(this.workspacePath!, dir)
70+
);
71+
}
72+
73+
isInsideAllowedDirs(cwd: string): boolean {
74+
return this.allowedDirs.some(dir => cwd.startsWith(dir));
75+
}
76+
77+
getWorkspacePath(): string | undefined {
78+
return this.workspacePath;
79+
}
80+
81+
getModulePath(): string | undefined {
82+
return this.modulePath;
83+
}
84+
85+
getConfig(): LaunchQLConfig | undefined {
86+
return this.config;
87+
}
88+
89+
get<T = any>(key: string): T | undefined {
90+
return this.config?.[key] as T;
91+
}
92+
93+
getAllowedDirs(): string[] {
94+
return [...this.allowedDirs];
95+
}
96+
97+
getCwd(): string {
98+
return this.cwd;
99+
}
100+
101+
isInWorkspace(): boolean {
102+
return !!this.workspacePath;
103+
}
104+
105+
isInModule(): boolean {
106+
return (
107+
!!this.modulePath &&
108+
!!this.workspacePath &&
109+
this.modulePath.startsWith(this.workspacePath)
110+
);
111+
}
112+
113+
ensureWorkspace(): void {
114+
if (!this.workspacePath) {
115+
throw new Error('Not inside a workspace');
116+
}
117+
}
118+
119+
ensureModule(): void {
120+
if (!this.modulePath) {
121+
throw new Error('Not inside a module');
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)