Skip to content

Commit 46b3259

Browse files
committed
feat: Refactored the initialization process to return a resource definition. We can put info in here in the future if needed. Right now return if a resource has any sensitive parameters
1 parent 5fb7d70 commit 46b3259

File tree

12 files changed

+55
-63
lines changed

12 files changed

+55
-63
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"ajv": "^8.12.0",
1818
"ajv-formats": "^3.0.1",
1919
"chalk": "^5.3.0",
20-
"codify-schemas": "^1.0.77",
20+
"codify-schemas": "^1.0.81",
2121
"cors": "^2.8.5",
2222
"debug": "^4.3.4",
2323
"detect-indent": "^7.0.1",

src/common/initialize-plugins.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import * as fs from 'node:fs/promises'
2-
import * as os from 'node:os'
32
import * as path from 'node:path'
3+
import { validate } from 'uuid';
44

5+
import { DashboardApiClient } from '../api/dashboard/index.js';
6+
import { LoginHelper } from '../connect/login-helper.js';
57
import { Project } from '../entities/project.js';
68
import { SubProcessName, ctx } from '../events/context.js';
79
import { CODIFY_FILE_REGEX, CodifyParser } from '../parser/index.js';
8-
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
10+
import { PluginManager, ResourceDefinitionMap } from '../plugins/plugin-manager.js';
911
import { Reporter } from '../ui/reporters/reporter.js';
10-
import { LoginHelper } from '../connect/login-helper.js';
1112
import { FileUtils } from '../utils/file.js';
12-
import { validate } from 'uuid';
13-
import { DashboardApiClient } from '../api/dashboard/index.js';
1413

1514
export interface InitializeArgs {
1615
path?: string;
@@ -21,7 +20,7 @@ export interface InitializeArgs {
2120
}
2221

2322
export interface InitializationResult {
24-
typeIdsToDependenciesMap: DependencyMap
23+
resourceDefinitions: ResourceDefinitionMap
2524
pluginManager: PluginManager,
2625
project: Project,
2726
}
@@ -42,10 +41,10 @@ export class PluginInitOrchestrator {
4241

4342
ctx.subprocessStarted(SubProcessName.INITIALIZE_PLUGINS)
4443
const pluginManager = new PluginManager();
45-
const typeIdsToDependenciesMap = await pluginManager.initialize(project, args.secure, args.verbosityLevel);
44+
const resourceDefinitions = await pluginManager.initialize(project, args.secure, args.verbosityLevel);
4645
ctx.subprocessFinished(SubProcessName.INITIALIZE_PLUGINS)
4746

48-
return { typeIdsToDependenciesMap, pluginManager, project };
47+
return { resourceDefinitions, pluginManager, project };
4948
}
5049

5150
private static async parse(

src/entities/project.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { validate } from 'uuid'
44
import { PluginValidationError, PluginValidationErrorParams, TypeNotFoundError } from '../common/errors.js';
55
import { ctx } from '../events/context.js';
66
import { SourceMapCache } from '../parser/source-maps.js';
7-
import { DependencyMap } from '../plugins/plugin-manager.js';
7+
import { ResourceDefinitionMap } from '../plugins/plugin-manager.js';
88
import { DependencyGraphResolver } from '../utils/dependency-graph-resolver.js';
99
import { groupBy } from '../utils/index.js';
1010
import { ConfigBlock, ConfigType } from './config.js';
@@ -155,16 +155,16 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
155155
}
156156
}
157157

158-
validateTypeIds(resourceMap: Map<string, string[]>) {
159-
const invalidConfigs = this.resourceConfigs.filter((c) => !resourceMap.get(c.type));
158+
validateTypeIds(resourceDefinitions: ResourceDefinitionMap) {
159+
const invalidConfigs = this.resourceConfigs.filter((c) => !resourceDefinitions.has(c.type));
160160

161161
if (invalidConfigs.length > 0) {
162162
throw new TypeNotFoundError(invalidConfigs, this.sourceMaps);
163163
}
164164
}
165165

166-
resolveDependenciesAndCalculateEvalOrder(dependencyMap?: DependencyMap) {
167-
this.resolveResourceDependencies(dependencyMap);
166+
resolveDependenciesAndCalculateEvalOrder(resourceDefinitions?: ResourceDefinitionMap) {
167+
this.resolveResourceDependencies(resourceDefinitions);
168168
this.calculateEvaluationOrder();
169169
}
170170

@@ -189,7 +189,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
189189
) ?? null;
190190
}
191191

192-
private resolveResourceDependencies(dependencyMap?: DependencyMap) {
192+
private resolveResourceDependencies(resourceDefinitions?: ResourceDefinitionMap) {
193193
const resourceMap = new Map(this.resourceConfigs.map((r) => [r.id, r] as const));
194194

195195
for (const r of this.resourceConfigs) {
@@ -198,7 +198,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
198198
r.addDependenciesBasedOnParameters((id) => resourceMap.has(id));
199199

200200
// Plugin dependencies are soft dependencies. They only activate if the dependent resource is present.
201-
r.addDependencies(dependencyMap?.get(r.type)
201+
r.addDependencies(resourceDefinitions?.get(r.type)?.dependencies
202202
?.filter((type) => [...resourceMap.values()].some((r) => r.type === type))
203203
?.flatMap((type) => [...resourceMap.values()].filter((r) => r.type === type).map((r) => r.id)) ?? []
204204
);

src/orchestrators/destroy.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Project } from '../entities/project.js';
44
import { ResourceConfig } from '../entities/resource-config.js';
55
import { ResourceInfo } from '../entities/resource-info.js';
66
import { ProcessName, SubProcessName, ctx } from '../events/context.js';
7-
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
7+
import { PluginManager, ResourceDefinitionMap } from '../plugins/plugin-manager.js';
88
import { PromptType, Reporter } from '../ui/reporters/reporter.js';
99
import { wildCardMatch } from '../utils/wild-card-match.js';
1010

@@ -68,11 +68,11 @@ Open a new terminal or source '.zshrc' for the new changes to be reflected`);
6868
reporter: Reporter,
6969
initializeResult: InitializationResult
7070
): Promise<{ plan: Plan, destroyProject: Project }> {
71-
const { project, pluginManager, typeIdsToDependenciesMap } = initializeResult;
71+
const { project, pluginManager, resourceDefinitions } = initializeResult;
7272

7373
// TODO: In the future if a user supplies resourceId.name (naming a specific resource) destroy that resource instead of stripping the name out.
74-
const matchedTypes = this.matchTypeIds(typeIds.map((id) => id.split('.').at(0) ?? ''), [...typeIdsToDependenciesMap.keys()])
75-
await DestroyOrchestrator.validateTypeIds(matchedTypes, project, pluginManager, typeIdsToDependenciesMap);
74+
const matchedTypes = this.matchTypeIds(typeIds.map((id) => id.split('.').at(0) ?? ''), [...resourceDefinitions.keys()])
75+
await DestroyOrchestrator.validateTypeIds(matchedTypes, project, pluginManager, resourceDefinitions);
7676

7777
const resourceInfoList = (await pluginManager.getMultipleResourceInfo(matchedTypes));
7878
const resourcesToDestroy = await DestroyOrchestrator.getDestroyParameters(reporter, project, resourceInfoList);
@@ -83,7 +83,7 @@ Open a new terminal or source '.zshrc' for the new changes to be reflected`);
8383
project.codifyFiles
8484
).toDestroyProject();
8585

86-
destroyProject.resolveDependenciesAndCalculateEvalOrder(typeIdsToDependenciesMap);
86+
destroyProject.resolveDependenciesAndCalculateEvalOrder(resourceDefinitions);
8787
const plan = await ctx.subprocess(ProcessName.PLAN, () =>
8888
pluginManager.plan(destroyProject)
8989
)
@@ -96,16 +96,16 @@ Open a new terminal or source '.zshrc' for the new changes to be reflected`);
9696
reporter: Reporter,
9797
initializeResult: InitializationResult
9898
): Promise<{ plan: Plan, destroyProject: Project }> {
99-
const { pluginManager, project, typeIdsToDependenciesMap } = initializeResult;
99+
const { pluginManager, project, resourceDefinitions } = initializeResult;
100100

101101
await ctx.subprocess(SubProcessName.VALIDATE, async () => {
102-
project.validateTypeIds(typeIdsToDependenciesMap);
102+
project.validateTypeIds(resourceDefinitions);
103103
const validationResults = await pluginManager.validate(project);
104104
project.handlePluginResourceValidationResults(validationResults);
105105
})
106106

107107
const destroyProject = project.toDestroyProject();
108-
destroyProject.resolveDependenciesAndCalculateEvalOrder(typeIdsToDependenciesMap);
108+
destroyProject.resolveDependenciesAndCalculateEvalOrder(resourceDefinitions);
109109

110110
const plan = await ctx.subprocess(ProcessName.PLAN, () =>
111111
pluginManager.plan(destroyProject)
@@ -147,10 +147,10 @@ ${JSON.stringify(unsupportedTypeIds)}`);
147147
return result;
148148
}
149149

150-
private static async validateTypeIds(typeIds: string[], project: Project, pluginManager: PluginManager, dependencyMap: DependencyMap): Promise<void> {
151-
project.validateTypeIds(dependencyMap);
150+
private static async validateTypeIds(typeIds: string[], project: Project, pluginManager: PluginManager, resourceDefinitions: ResourceDefinitionMap): Promise<void> {
151+
project.validateTypeIds(resourceDefinitions);
152152

153-
const unsupportedTypeIds = typeIds.filter((type) => !dependencyMap.has(type));
153+
const unsupportedTypeIds = typeIds.filter((type) => !resourceDefinitions.has(type));
154154
if (unsupportedTypeIds.length > 0) {
155155
throw new Error(`The following resources cannot be destroyed. No plugins found that support the following types:
156156
${JSON.stringify(unsupportedTypeIds)}`);

src/orchestrators/import.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { FileModificationCalculator } from '../generators/file-modification-calc
1313
import { ModificationType } from '../generators/index.js';
1414
import { FileUpdater } from '../generators/writer.js';
1515
import { CodifyParser } from '../parser/index.js';
16-
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
16+
import { PluginManager, ResourceDefinitionMap } from '../plugins/plugin-manager.js';
1717
import { prettyFormatFileDiff } from '../ui/file-diff-pretty-printer.js';
1818
import { PromptType, Reporter } from '../ui/reporters/reporter.js';
1919
import { FileUtils } from '../utils/file.js';
@@ -56,10 +56,10 @@ export class ImportOrchestrator {
5656
}
5757

5858
static async autoImportAll(reporter: Reporter, initializeResult: InitializationResult, args: ImportArgs) {
59-
const { project, pluginManager, typeIdsToDependenciesMap } = initializeResult;
59+
const { project, pluginManager, resourceDefinitions } = initializeResult;
6060

6161
ctx.subprocessStarted(SubProcessName.IMPORT_RESOURCE)
62-
const importResults = await Promise.all([...typeIdsToDependenciesMap.keys()].map(async (typeId) => {
62+
const importResults = await Promise.all([...resourceDefinitions.keys()].map(async (typeId) => {
6363
try {
6464
return await pluginManager.importResource({
6565
core: { type: typeId },
@@ -98,10 +98,10 @@ export class ImportOrchestrator {
9898

9999
/** Import new resources. Type ids supplied. This will ask for any required parameters */
100100
static async runNewImport(typeIds: string[], reporter: Reporter, initializeResult: InitializationResult, args: ImportArgs): Promise<void> {
101-
const { project, pluginManager, typeIdsToDependenciesMap } = initializeResult;
101+
const { project, pluginManager, resourceDefinitions } = initializeResult;
102102

103-
const matchedTypes = this.matchTypeIds(typeIds, [...typeIdsToDependenciesMap.keys()])
104-
await ImportOrchestrator.validate(matchedTypes, project, pluginManager, typeIdsToDependenciesMap);
103+
const matchedTypes = this.matchTypeIds(typeIds, [...resourceDefinitions.keys()])
104+
await ImportOrchestrator.validate(matchedTypes, project, pluginManager, resourceDefinitions);
105105

106106
const resourceInfoList = (await pluginManager.getMultipleResourceInfo(matchedTypes))
107107
.filter((info) => info.canImport)
@@ -364,10 +364,10 @@ ${JSON.stringify(unsupportedTypeIds)}`);
364364
return result;
365365
}
366366

367-
private static async validate(typeIds: string[], project: Project, pluginManager: PluginManager, dependencyMap: DependencyMap): Promise<void> {
368-
project.validateTypeIds(dependencyMap);
367+
private static async validate(typeIds: string[], project: Project, pluginManager: PluginManager, resourceDefinitions: ResourceDefinitionMap): Promise<void> {
368+
project.validateTypeIds(resourceDefinitions);
369369

370-
const unsupportedTypeIds = typeIds.filter((type) => !dependencyMap.has(type));
370+
const unsupportedTypeIds = typeIds.filter((type) => !resourceDefinitions.has(type));
371371
if (unsupportedTypeIds.length > 0) {
372372
throw new Error(`The following resources cannot be imported. No plugins found that support the following types:
373373
${JSON.stringify(unsupportedTypeIds)}`);

src/orchestrators/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ export const InitializeOrchestrator = {
2222
await reporter.displayProgress();
2323

2424

25-
const { pluginManager, typeIdsToDependenciesMap } = await PluginInitOrchestrator.run(args, reporter);
25+
const { pluginManager, resourceDefinitions } = await PluginInitOrchestrator.run(args, reporter);
2626

2727
ctx.subprocessStarted(SubProcessName.IMPORT_RESOURCE)
28-
const importResults = await Promise.all([...typeIdsToDependenciesMap.keys()].map(async (typeId) => {
28+
const importResults = await Promise.all([...resourceDefinitions.keys()].map(async (typeId) => {
2929
try {
3030
return await pluginManager.importResource({
3131
core: { type: typeId },

src/orchestrators/plan.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export class PlanOrchestrator {
2727
const initializationResult = await PluginInitOrchestrator.run({
2828
...args,
2929
}, reporter);
30-
const { typeIdsToDependenciesMap, pluginManager, project } = initializationResult;
30+
const { resourceDefinitions, pluginManager, project } = initializationResult;
3131

3232
await createStartupShellScriptsIfNotExists();
3333

3434
await ValidateOrchestrator.run({ existing: initializationResult }, reporter);
35-
project.resolveDependenciesAndCalculateEvalOrder(typeIdsToDependenciesMap);
35+
project.resolveDependenciesAndCalculateEvalOrder(resourceDefinitions);
3636
project.addXCodeToolsConfig(); // We have to add xcode-tools config always since almost every resource depends on it
3737

3838
const plan = await PlanOrchestrator.plan(project, pluginManager);

src/orchestrators/validate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const ValidateOrchestrator = {
1616
): Promise<void> {
1717
const {
1818
project,
19-
typeIdsToDependenciesMap: dependencyMap,
19+
resourceDefinitions,
2020
pluginManager,
2121
} = args.existing ?? await PluginInitOrchestrator.run(args, reporter)
2222

@@ -26,7 +26,7 @@ export const ValidateOrchestrator = {
2626
ctx.processStarted(SubProcessName.VALIDATE)
2727
}
2828

29-
project.validateTypeIds(dependencyMap);
29+
project.validateTypeIds(resourceDefinitions);
3030
const validationResults = await pluginManager.validate(project);
3131
project.handlePluginResourceValidationResults(validationResults);
3232

src/plugins/plugin-manager.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {
2-
ImportResponseData,
1+
import {
2+
ImportResponseData, ResourceDefinition,
33
ResourceJson,
44
ValidateResponseData,
55
} from 'codify-schemas';
@@ -16,7 +16,7 @@ import { PluginResolver } from './resolver.js';
1616

1717
type PluginName = string;
1818
type ResourceTypeId = string;
19-
export type DependencyMap = Map<ResourceTypeId, ResourceTypeId[]>;
19+
export type ResourceDefinitionMap = Map<ResourceTypeId, ResourceDefinition>;
2020

2121
const DEFAULT_PLUGINS = {
2222
'default': 'latest',
@@ -28,17 +28,15 @@ export class PluginManager {
2828
private resourceToPluginMapping = new Map<string, string>()
2929
private pluginToResourceMapping = new Map<string, string[]>()
3030

31-
async initialize(project: Project | null, secureMode = false, verbosityLevel = 0): Promise<DependencyMap> {
31+
async initialize(project: Project | null, secureMode = false, verbosityLevel = 0): Promise<ResourceDefinitionMap> {
3232
const plugins = await this.resolvePlugins(project);
3333

3434
for (const plugin of plugins) {
3535
this.plugins.set(plugin.name, plugin)
3636
}
3737

3838
this.registerKillListeners(plugins)
39-
40-
const dependencyMap = await this.initializePlugins(plugins, secureMode, verbosityLevel);
41-
return dependencyMap;
39+
return this.initializePlugins(plugins, secureMode, verbosityLevel);
4240
}
4341

4442
async validate(project: Project): Promise<ValidateResponseData[]> {
@@ -158,15 +156,15 @@ export class PluginManager {
158156
return PluginResolver.resolveAll(pluginDefinitions);
159157
}
160158

161-
private async initializePlugins(plugins: Plugin[], secureMode: boolean, verbosityLevel: number): Promise<Map<string, string[]>> {
159+
private async initializePlugins(plugins: Plugin[], secureMode: boolean, verbosityLevel: number): Promise<Map<string, ResourceDefinition>> {
162160
const responses = await Promise.all(
163161
plugins.map(async (p) => {
164162
const initializeResult = await p.initialize(secureMode, verbosityLevel);
165163
return [p.name, initializeResult.resourceDefinitions] as const
166164
})
167165
);
168166

169-
const resourceMap = new Map<string, string[]>;
167+
const resourceMap = new Map<string, ResourceDefinition>();
170168

171169
for (const [pluginName, definitions] of responses) {
172170
for (const definition of definitions) {
@@ -189,7 +187,7 @@ export class PluginManager {
189187
throw new Error(`Duplicated types between plugins ${this.resourceToPluginMapping.get(definition.type)} and ${pluginName}`);
190188
}
191189

192-
resourceMap.set(definition.type, definition.dependencies)
190+
resourceMap.set(definition.type, definition)
193191
}
194192
}
195193

0 commit comments

Comments
 (0)