Skip to content

Commit 3ac8315

Browse files
committed
Finished up Codify parser and resolver changes
1 parent b9d99f5 commit 3ac8315

File tree

5 files changed

+27
-219
lines changed

5 files changed

+27
-219
lines changed

src/codify-files/parser/index.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,9 @@ import { JsonParser } from './json/json-parser.js';
1010
import { Json5Parser } from './json5/json-parser.js';
1111
import { JsoncParser } from './jsonc/json-parser.js';
1212
import { RemoteParser } from './remote/remote-parser.js';
13-
import { ResolverType } from './resolvers.js';
1413
import { SourceMapCache } from './source-maps.js';
1514
import { YamlParser } from './yaml/yaml-parser.js';
1615

17-
18-
export interface ParserArgs {
19-
allowEmptyProject?: boolean;
20-
allowTemplates?: boolean;
21-
path?: string;
22-
transformProject?: (project: Project) => Project | Promise<Project>;
23-
rawConfigs?: Config[]; // Raw configs are provided directly
24-
resolverType?: ResolverType;
25-
}
26-
2716
interface ParseResult {
2817
configs: ParsedConfig[];
2918
file: InMemoryFile;
@@ -53,10 +42,10 @@ class Parser {
5342
* Error out and tell the user that the following file could not be found
5443
*
5544
*
56-
* @param location
45+
* @param file
5746
* @param args
5847
*/
59-
async parse(file: InMemoryFile, args?: ParserArgs): Promise<Project> {
48+
async parse(file: InMemoryFile): Promise<Project> {
6049
const sourceMaps = new SourceMapCache()
6150

6251
const { configs } = await Promise.resolve(this.parseContents(file, sourceMaps))
@@ -65,15 +54,15 @@ class Parser {
6554
return Project.create(configs, file.path, sourceMaps);
6655
}
6756

68-
async parseJson(configs: Config[]): Promise<Project> {
57+
async parseJson(configs: Config[], path?: string): Promise<Project> {
6958
const sourceMaps = new SourceMapCache()
7059

71-
const configBlocks = this.createConfigBlocks(configs
72-
.map((c) => ({ contents: c, sourceMapKey: '' })),
73-
sourceMaps
74-
)
60+
const configBlocks = this.createConfigBlocks({
61+
configs: configs.map((c) => ({ contents: c, sourceMapKey: '' })),
62+
file: { contents: JSON.stringify(configs), path: path ?? '', fileType: FileType.JSON },
63+
}, sourceMaps)
7564

76-
return Project.create(configBlocks.configs, undefined, sourceMaps);
65+
return Project.create(configBlocks.configs, path, sourceMaps);
7766
}
7867

7968
private parseContents(file: InMemoryFile, sourceMaps: SourceMapCache): ParseResult {

src/codify-files/parser/resolvers.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/codify-files/resolver/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ interface ResolverArgs {
1717
path?: string;
1818
allowTemplates?: boolean;
1919
reporter?: Reporter;
20-
21-
/**
22-
* Narrow down the result to a single file
23-
*/
24-
narrow?: boolean;
20+
allowEmpty?: boolean;
2521
}
2622

2723
/**
@@ -43,9 +39,9 @@ export class CodifyResolver {
4339
* @param location
4440
* @param args
4541
*/
46-
static async resolveFile(location: string, args?: ResolverArgs): Promise<InMemoryFile | InMemoryFile[]> {
47-
return CodifyResolver.run(location, args)
48-
.then((result) => (args?.narrow) ? this.narrow(result) : result.files);
42+
static async resolveFile(location: string, args?: ResolverArgs): Promise<InMemoryFile | null> {
43+
const resolvedFiles = await CodifyResolver.run(location, args)
44+
return this.narrow(resolvedFiles, args);
4945
}
5046

5147
private static async run(location: string, args?: ResolverArgs): Promise<ResolverResult> {
@@ -56,7 +52,7 @@ export class CodifyResolver {
5652
if (args?.path) {
5753
return CodifyResolverRunner.resolveLocal(args?.path)
5854
}
59-
55+
6056
const isLoggedIn = LoginHelper.get()?.isLoggedIn;
6157

6258
return CodifyResolverRunner.run(location, [
@@ -68,9 +64,13 @@ export class CodifyResolver {
6864
]);
6965
}
7066

71-
private static async narrow(result: ResolverResult, args?: ResolverArgs): Promise<InMemoryFile> {
67+
private static async narrow(result: ResolverResult, args?: ResolverArgs): Promise<InMemoryFile | null> {
7268
if (result.files.length === 0) {
73-
throw new NoCodifyFileError(result);
69+
if (!args?.allowEmpty) {
70+
throw new NoCodifyFileError(result);
71+
}
72+
73+
return null;
7474
}
7575

7676
if (result.files.length > 1) {
@@ -89,5 +89,4 @@ export class CodifyResolver {
8989

9090
return result.files[0];
9191
}
92-
93-
}
92+
}

src/commands/validate.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { CodifyParser } from '../codify-files/parser/index.js';
44
import { BaseCommand } from '../common/base-command.js';
55
import { ValidateOrchestrator } from '../orchestrators/validate.js';
66
import Apply from './apply.js';
7+
import { CodifyResolver } from '../codify-files/resolver/index.js';
8+
import { NoCodifyFileError } from '../codify-files/resolver/errors.js';
79

810
export default class Validate extends BaseCommand {
911
static description =
@@ -39,7 +41,10 @@ For more information, visit: https://docs.codifycli.com/commands/validate
3941
path: flags.path ?? args.pathArgs,
4042
}, this.reporter)
4143

42-
await CodifyParser.parse(flags.path ?? args.pathArgs ?? '.');
44+
const codifyFile = await CodifyResolver.resolveFile(flags.path ?? args.pathArgs ?? '.', {
45+
allowEmpty: false,
46+
});
47+
await CodifyParser.parse(codifyFile!);
4348

4449
process.exit(0);
4550
}

src/common/initialize-plugins.ts

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class PluginInitOrchestrator {
6666

6767
const codifyFile = await CodifyResolver.resolveFile(args.path ?? process.cwd(), {
6868
allowTemplates: args.allowTemplates,
69+
allowEmpty: args.allowEmptyProject,
6970
reporter,
7071
});
7172

@@ -84,68 +85,4 @@ export class PluginInitOrchestrator {
8485

8586
return project;
8687
}
87-
88-
/** Resolve the root codify file to run.
89-
* Order:
90-
* 1. If path is specified, return that.
91-
* 2. If path is a dir with only one *codify.json|*codify.jsonc|*codify.json5|*codify.yaml, return that.
92-
* 3. If path is a UUID, return file from Codify remote.
93-
* 4. If multiple exists in the path (dir), then prompt the user to select one.
94-
* 5. If no path is provided, run steps 2 - 4 for the current dir.
95-
* 6. If none exists, return default file from codify remote.
96-
* 7. If user is not logged in, return an error.
97-
*
98-
* Order:
99-
* 1. If the name ends in .json|.jsonc|.json5|.yaml then search the local folder
100-
* 2. If it is a path (relative or absolute) then search for that directory or file
101-
* 3. If the path is a uuid (try to match it with a UUID) on the user's account (if they are logged in)
102-
* 4. Attempt to search for the name on the user's account (if they are logged in)
103-
* 5. Attempt to resolve to a public template (if allowTemplate is enabled)
104-
* Error out and tell the user that the following file could not be found
105-
*
106-
* @param args
107-
* @private
108-
*/
109-
private static async resolveCodify(args: InitializeArgs, reporter: Reporter): Promise<string | undefined> {
110-
const inputPath = args.path ?? process.cwd();
111-
112-
// Cloud files will be fetched and processed later in the parser.
113-
const isCloud = validate(inputPath);
114-
if (isCloud) {
115-
return inputPath;
116-
}
117-
118-
// Direct files can have its path returned.
119-
const isPathDir = await FileUtils.isDir(inputPath);
120-
if (!isPathDir) {
121-
return inputPath;
122-
}
123-
124-
const filesInDir = await fs.readdir(inputPath);
125-
const codifyFiles = filesInDir.filter((f) => config.fileRegex.test(f))
126-
127-
if (codifyFiles.length === 1) {
128-
return codifyFiles[0];
129-
}
130-
131-
if (codifyFiles.length > 0) {
132-
const answer = await reporter.promptOptions(
133-
'Multiple codify files found in dir. Please select one:',
134-
codifyFiles,
135-
);
136-
137-
return path.join(inputPath, codifyFiles[answer]);
138-
}
139-
140-
if (LoginHelper.get()?.isLoggedIn) {
141-
return (await DashboardApiClient.getDefaultDocumentId()) ?? undefined;
142-
}
143-
144-
if (args.allowEmptyProject) {
145-
return undefined;
146-
}
147-
148-
throw new Error('No codify files found.');
149-
}
150-
15188
}

0 commit comments

Comments
 (0)