Skip to content

Commit 307e13a

Browse files
committed
WIP
1 parent 50c2d76 commit 307e13a

27 files changed

+222
-62
lines changed

src/common/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('AjvValidationError tests', () => {
4545
const sourceMaps = new SourceMapCache()
4646
const result = new JsonParser().parse({
4747
fileType: FileType.JSON,
48-
filePath: '/test/path/to/test.json',
48+
path: '/test/path/to/test.json',
4949
contents
5050
}, sourceMaps);
5151

src/common/initialize-plugins.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface InitializeArgs {
2121
forceEmptyProject?: boolean;
2222
codifyConfigs?: Config[];
2323
noProgress?: boolean;
24+
allowTemplates?: boolean;
2425
}
2526

2627
export interface InitializationResult {
@@ -59,7 +60,7 @@ export class PluginInitOrchestrator {
5960
return CodifyParser.parseJson(args.codifyConfigs);
6061
}
6162

62-
const codifyPath = await PluginInitOrchestrator.resolveCodifyRootPath(args, reporter);
63+
const codifyPath = await PluginInitOrchestrator.resolveCodify(args, reporter);
6364
ctx.subprocessStarted(SubProcessName.PARSE);
6465

6566
const project = codifyPath
@@ -85,10 +86,18 @@ export class PluginInitOrchestrator {
8586
* 6. If none exists, return default file from codify cloud.
8687
* 7. If user is not logged in, return an error.
8788
*
89+
* Order:
90+
* 1. If the name ends in .json|.jsonc|.json5|.yaml then search the local folder
91+
* 2. If it is a path (relative or absolute) then search for that directory or file
92+
* 3. If the path is a uuid (try to match it with a UUID) on the user's account (if they are logged in)
93+
* 4. Attempt to search for the name on the user's account (if they are logged in)
94+
* 5. Attempt to resolve to a public template (if allowTemplate is enabled)
95+
* Error out and tell the user that the following file could not be found
96+
*
8897
* @param args
8998
* @private
9099
*/
91-
private static async resolveCodifyRootPath(args: InitializeArgs, reporter: Reporter): Promise<string | undefined> {
100+
private static async resolveCodify(args: InitializeArgs, reporter: Reporter): Promise<string | undefined> {
92101
const inputPath = args.path ?? process.cwd();
93102

94103
// Cloud files will be fetched and processed later in the parser.

src/entities/project.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { OS, PlanRequestData, ResourceOperation, ValidateResponseData } from 'codify-schemas';
2-
import * as os from 'os'
2+
import * as os from 'node:os'
33
import { validate } from 'uuid'
44

55
import {
@@ -14,30 +14,29 @@ import { SourceMapCache } from '../parser/source-maps.js';
1414
import { ResourceDefinitionMap } from '../plugins/plugin-manager.js';
1515
import { DependencyGraphResolver } from '../utils/dependency-graph-resolver.js';
1616
import { groupBy } from '../utils/index.js';
17+
import { ShellUtils } from '../utils/shell.js';
1718
import { ConfigBlock, ConfigType } from './config.js';
1819
import { type Plan } from './plan.js';
1920
import { ProjectConfig } from './project-config.js';
2021
import { ResourceConfig } from './resource-config.js';
21-
import { ShellUtils } from '../utils/shell.js';
2222

2323
export class Project {
2424
projectConfig: ProjectConfig | null;
2525
resourceConfigs: ResourceConfig[];
2626
stateConfigs: ResourceConfig[] | null = null;
2727
evaluationOrder: null | string[] = null;
2828

29-
codifyFiles: string[];
30-
29+
path?: string;
3130
sourceMaps?: SourceMapCache;
3231
planRequestsCache?: Map<string, PlanRequestData>
3332

3433
isDestroyProject = false;
3534

3635
static empty(): Project {
37-
return Project.create([], []);
36+
return Project.create([]);
3837
}
3938

40-
static create(configs: ConfigBlock[], codifyFiles: string[], sourceMaps?: SourceMapCache): Project {
39+
static create(configs: ConfigBlock[], path?: string, sourceMaps?: SourceMapCache): Project {
4140
const projectConfigs = configs.filter((u) => u.configClass === ConfigType.PROJECT);
4241
if (projectConfigs.length > 1) {
4342
throw new Error(`Only one project config can be specified. Found ${projectConfigs.length}. \n\n
@@ -47,16 +46,16 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
4746
return new Project(
4847
(projectConfigs[0] as ProjectConfig) ?? null,
4948
configs.filter((u) => u.configClass !== ConfigType.PROJECT) as ResourceConfig[],
50-
codifyFiles,
49+
path,
5150
sourceMaps,
5251
);
5352
}
5453

55-
constructor(projectConfig: ProjectConfig | null, resourceConfigs: ResourceConfig[], codifyFiles: string[], sourceMaps?: SourceMapCache) {
54+
constructor(projectConfig: ProjectConfig | null, resourceConfigs: ResourceConfig[], path?: string, sourceMaps?: SourceMapCache) {
5655
this.projectConfig = projectConfig;
5756
this.resourceConfigs = resourceConfigs;
5857
this.sourceMaps = sourceMaps;
59-
this.codifyFiles = codifyFiles;
58+
this.path = path;
6059

6160
this.addUniqueNamesForDuplicateResources()
6261
}
@@ -66,7 +65,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
6665
}
6766

6867
exists(): boolean {
69-
return this.codifyFiles.length > 0;
68+
return Boolean(this.path);
7069
}
7170

7271
isStateful(): boolean {
@@ -75,7 +74,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
7574

7675
// TODO: Update to a more robust method in the future
7776
isCloud(): boolean {
78-
return validate(this.codifyFiles[0])
77+
return Boolean(this.path && validate(this.path));
7978
}
8079

8180
filterInPlace(ids: string[]): Project {
@@ -130,7 +129,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
130129
const uninstallProject = new Project(
131130
this.projectConfig,
132131
this.resourceConfigs,
133-
this.codifyFiles,
132+
this.path,
134133
this.sourceMaps,
135134
)
136135

src/generators/file-modification-calculator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class FileModificationCalculator {
1818
private readonly indentString: string;
1919

2020
constructor(existing: Project) {
21-
const { file, sourceMap } = existing.sourceMaps!.getSourceMap(existing.codifyFiles[0])!;
21+
const { file, sourceMap } = existing.sourceMaps!.getSourceMap(existing.path!)!;
2222
this.existingFile = file;
2323
this.sourceMap = sourceMap;
2424
this.existingConfigs = [...existing.resourceConfigs];
@@ -97,8 +97,8 @@ export class FileModificationCalculator {
9797
return;
9898
}
9999

100-
if (this.existingFile?.fileType !== FileType.JSON && this.existingFile?.fileType !== FileType.JSON5 && this.existingFile?.fileType !== FileType.JSONC && this.existingFile?.fileType !== FileType.CLOUD) {
101-
throw new Error(`Only updating .json, .json5, and .jsonc files are currently supported. Found ${this.existingFile?.filePath}`);
100+
if (this.existingFile?.fileType !== FileType.JSON && this.existingFile?.fileType !== FileType.JSON5 && this.existingFile?.fileType !== FileType.JSONC && this.existingFile?.fileType !== FileType.REMOTE) {
101+
throw new Error(`Only updating .json, .json5, and .jsonc files are currently supported. Found ${this.existingFile?.path}`);
102102
}
103103

104104
if (this.existingConfigs.some((r) => !r.resourceInfo)) {
@@ -239,7 +239,7 @@ export class FileModificationCalculator {
239239
return 'cjson'
240240
}
241241

242-
if (fileType === FileType.CLOUD) {
242+
if (fileType === FileType.REMOTE) {
243243
return 'cjson'
244244
}
245245

src/orchestrators/destroy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Open a new terminal or source '.zshrc' for the new changes to be reflected`);
8080
const destroyProject = new Project(
8181
null,
8282
resourcesToDestroy,
83-
project.codifyFiles
83+
project.path
8484
).toDestroyProject();
8585

8686
destroyProject.resolveDependenciesAndCalculateEvalOrder(resourceDefinitions);

src/orchestrators/import.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ export class ImportOrchestrator {
174174
// Special handling for remote-file resources. Offer to save them remotely if any changes are detected on import.
175175
await ImportOrchestrator.handleCodifyRemoteFiles(reporter, importResult);
176176

177-
const multipleCodifyFiles = project.codifyFiles.length > 1;
177+
const multipleCodifyFiles = project.path.length > 1;
178178
const saveType = await ImportOrchestrator.getSaveType(reporter, project, args);
179179

180180
// Update an existing file
181181
if (saveType === SaveType.EXISTING) {
182182
const file = multipleCodifyFiles
183-
? project.codifyFiles[await reporter.promptOptions('\nIf new resources are added, where to write them?', project.codifyFiles)]
184-
: project.codifyFiles[0];
183+
? project.path[await reporter.promptOptions('\nIf new resources are added, where to write them?', project.path)]
184+
: project.path;
185185
await ImportOrchestrator.updateExistingFiles(reporter, project, importResult, resourceInfoList, file, pluginManager);
186186
return;
187187
}
@@ -411,7 +411,7 @@ ${JSON.stringify(unsupportedTypeIds)}`);
411411
args: ImportArgs,
412412
): Promise<SaveType> {
413413
const projectExists = project.exists();
414-
const multipleCodifyFiles = project.codifyFiles.length > 1;
414+
const multipleCodifyFiles = project.path.length > 1;
415415

416416
if (args.updateExisting && projectExists) {
417417
return SaveType.EXISTING;
@@ -421,7 +421,7 @@ ${JSON.stringify(unsupportedTypeIds)}`);
421421
'\nDo you want to save the results?',
422422
[
423423
projectExists ?
424-
multipleCodifyFiles ? 'Update existing files' : `Update existing file (${project.codifyFiles})`
424+
multipleCodifyFiles ? 'Update existing files' : `Update existing file (${project.path})`
425425
: undefined,
426426
'In a new file',
427427
'No'

src/orchestrators/refresh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class RefreshOrchestrator {
5050
project,
5151
importResult,
5252
resourceInfoList,
53-
project.codifyFiles[0],
53+
project.path!,
5454
pluginManager,
5555
);
5656
}

src/orchestrators/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const TestOrchestrator = {
5858
// Install codify on the VM
5959
// await spawn(`tart exec ${vmName} /bin/bash -c "$(curl -fsSL https://releases.codifycli.com/install.sh)"`, { interactive: true });
6060
const { data: ip } = await spawnSafe(`tart ip ${vmName}`, { interactive: true });
61-
await spawn(`sshpass -p "admin" scp -o PubkeyAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${initializationResult.project.codifyFiles[0]} admin@${ip}:~/codify.jsonc`, { interactive: true });
61+
await spawn(`sshpass -p "admin" scp -o PubkeyAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${initializationResult.project.path} admin@${ip}:~/codify.jsonc`, { interactive: true });
6262

6363
if (args.vmOs === OS.Darwin) {
6464
await spawn(`tart exec ${vmName} osascript -e "tell application \\"Terminal\\" to do script \\"cd ~/ && codify apply\\""`, { interactive: true });

src/parser/cloud/cloud-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class CloudParser implements LanguageSpecificParser {
1616
const { type, ...config } = content;
1717
return {
1818
contents: { type, ...config },
19-
sourceMapKey: SourceMapCache.constructKey(file.filePath, `/${idx}`),
19+
sourceMapKey: SourceMapCache.constructKey(file.path, `/${idx}`),
2020
}
2121
})
2222
}

src/parser/entities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SourceMapCache } from './source-maps.js';
33

44
export interface InMemoryFile {
55
contents: string;
6-
filePath: string;
6+
path: string;
77
fileType: FileType;
88
}
99

@@ -21,5 +21,5 @@ export enum FileType {
2121
YAML = 'yaml',
2222
JSON5 = 'json5',
2323
JSONC = 'jsonc',
24-
CLOUD = 'cloud',
24+
REMOTE = 'remote',
2525
}

0 commit comments

Comments
 (0)