Skip to content

Commit 4a6570d

Browse files
committed
WIP for virtual applies
1 parent 7cb3972 commit 4a6570d

File tree

6 files changed

+68
-35
lines changed

6 files changed

+68
-35
lines changed

src/common/initialize-plugins.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ export interface InitializationResult {
2222
project: Project,
2323
}
2424

25+
export interface ParseInput {
26+
fileOrDir: string | undefined,
27+
json: string,
28+
}
29+
2530
export class PluginInitOrchestrator {
2631
static async run(
2732
args: InitializeArgs,
2833
reporter: Reporter,
2934
): Promise<InitializationResult> {
35+
36+
3037
let project = await PluginInitOrchestrator.parse(
3138
args.path,
3239
args.allowEmptyProject ?? false,
@@ -45,7 +52,7 @@ export class PluginInitOrchestrator {
4552
}
4653

4754
private static async parse(
48-
fileOrDir: string | undefined,
55+
input: ParseInput,
4956
allowEmptyProject: boolean,
5057
reporter: Reporter
5158
): Promise<Project> {
@@ -55,24 +62,24 @@ export class PluginInitOrchestrator {
5562
? await PluginInitOrchestrator.findCodifyJson()
5663
: fileOrDir
5764

58-
if (!pathToParse && !allowEmptyProject) {
59-
ctx.subprocessFinished(SubProcessName.PARSE);
60-
ctx.subprocessStarted(SubProcessName.CREATE_ROOT_FILE)
61-
const createRootCodifyFile = await reporter.promptConfirmation('\nNo codify file found. Do you want to create a root file at ~/codify.jsonc?');
62-
63-
if (createRootCodifyFile) {
64-
await fs.writeFile(
65-
path.resolve(os.homedir(), 'codify.jsonc'),
66-
'[]',
67-
{ encoding: 'utf8', flag: 'wx' }
68-
); // flag: 'wx' prevents overwrites if the file exists
69-
}
70-
71-
ctx.subprocessFinished(SubProcessName.CREATE_ROOT_FILE)
72-
73-
console.log('Created ~/codify.jsonc file')
74-
process.exit(0);
75-
}
65+
// if (!pathToParse && !allowEmptyProject) {
66+
// ctx.subprocessFinished(SubProcessName.PARSE);
67+
// ctx.subprocessStarted(SubProcessName.CREATE_ROOT_FILE)
68+
// const createRootCodifyFile = await reporter.promptConfirmation('\nNo codify file found. Do you want to create a root file at ~/codify.jsonc?');
69+
//
70+
// if (createRootCodifyFile) {
71+
// await fs.writeFile(
72+
// path.resolve(os.homedir(), 'codify.jsonc'),
73+
// '[]',
74+
// { encoding: 'utf8', flag: 'wx' }
75+
// ); // flag: 'wx' prevents overwrites if the file exists
76+
// }
77+
//
78+
// ctx.subprocessFinished(SubProcessName.CREATE_ROOT_FILE)
79+
//
80+
// console.log('Created ~/codify.jsonc file')
81+
// process.exit(0);
82+
// }
7683

7784
const project = pathToParse
7885
? await CodifyParser.parse(pathToParse)

src/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ export const config = {
66
'https://codify-dashboard.com',
77
'https://https://codify-dashboard.kevinwang5658.workers.dev',
88
'http://localhost:3000'
9-
]
9+
],
10+
11+
codifyFileRegex: /^(.*)?codify(.json|.yaml|.json5|.jsonc)$/
1012
}

src/connect/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IncomingMessage, Server, createServer } from 'node:http';
22
import { Duplex } from 'node:stream';
3-
import { v4 as uuid } from 'uuid';
43
import { WebSocket, WebSocketServer } from 'ws';
4+
55
import { config } from '../config.js';
66

77
let instance: WsServerManager | undefined;

src/entities/project.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ import { type Plan } from './plan.js';
1111
import { ProjectConfig } from './project-config.js';
1212
import { ResourceConfig } from './resource-config.js';
1313

14+
interface CodifyFile {
15+
type: 'local' | 'remote';
16+
pathOrId: string;
17+
}
18+
19+
export enum ProjectType {
20+
LOCAL = 'local',
21+
REMOTE = 'remote',
22+
}
23+
1424
export class Project {
1525
projectConfig: ProjectConfig | null;
1626
resourceConfigs: ResourceConfig[];
1727
stateConfigs: ResourceConfig[] | null = null;
1828
evaluationOrder: null | string[] = null;
1929

20-
codifyFiles: string[];
30+
codifyFiles: CodifyFile[];
2131

2232
sourceMaps?: SourceMapCache;
2333
planRequestsCache?: Map<string, PlanRequestData>
@@ -28,7 +38,7 @@ export class Project {
2838
return Project.create([], []);
2939
}
3040

31-
static create(configs: ConfigBlock[], codifyFiles: string[], sourceMaps?: SourceMapCache): Project {
41+
static create(configs: ConfigBlock[], codifyFiles: CodifyFile[], sourceMaps?: SourceMapCache): Project {
3242
const projectConfigs = configs.filter((u) => u.configClass === ConfigType.PROJECT);
3343
if (projectConfigs.length > 1) {
3444
throw new Error(`Only one project config can be specified. Found ${projectConfigs.length}. \n\n
@@ -43,7 +53,7 @@ ${JSON.stringify(projectConfigs, null, 2)}`);
4353
);
4454
}
4555

46-
constructor(projectConfig: ProjectConfig | null, resourceConfigs: ResourceConfig[], codifyFiles: string[], sourceMaps?: SourceMapCache) {
56+
constructor(projectConfig: ProjectConfig | null, resourceConfigs: ResourceConfig[], codifyFiles: CodifyFile[], sourceMaps?: SourceMapCache) {
4757
this.projectConfig = projectConfig;
4858
this.resourceConfigs = resourceConfigs;
4959
this.sourceMaps = sourceMaps;

src/parser/config-factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Factory {
2222
sourceMaps: SourceMapCache
2323
): ConfigBlock {
2424
const rawConfig = parsedConfig.contents;
25-
const type = parsedConfig.contents.type;
25+
const { type } = parsedConfig.contents;
2626

2727
switch (type) {
2828
case ConfigType.PROJECT: {

src/parser/index.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import { JsoncParser } from './jsonc/json-parser.js';
1212
import { FileReader } from './reader.js';
1313
import { SourceMapCache } from './source-maps.js';
1414
import { YamlParser } from './yaml/yaml-parser.js';
15+
import { config } from '../config.js';
1516

16-
export const CODIFY_FILE_REGEX = /^(.*)?codify(.json|.yaml|.json5|.jsonc)$/;
17+
export interface ParseInput {
18+
fileOrDir: string | undefined,
19+
json: string,
20+
}
1721

1822
class Parser {
1923
private readonly languageSpecificParsers= {
@@ -23,16 +27,26 @@ class Parser {
2327
[FileType.JSONC]: new JsoncParser()
2428
}
2529

26-
async parse(dirOrFile: string): Promise<Project> {
27-
const absolutePath = path.resolve(dirOrFile);
30+
async parse(input: ParseInput): Promise<Project> {
2831
const sourceMaps = new SourceMapCache()
29-
const codifyFiles = await this.getFilePaths(absolutePath)
32+
33+
let files: InMemoryFile[] = [];
34+
if (input.fileOrDir) {
35+
const absolutePath = path.resolve(input.fileOrDir);
36+
const codifyFiles = await this.getFilePaths(absolutePath)
37+
files = await this.readFiles(codifyFiles);
38+
} else if (input.json) {
39+
files = [{
40+
filePath: 'stdin',
41+
fileType: FileType.JSON,
42+
contents: input.json
43+
}]
44+
}
3045

31-
const configs = await this.readFiles(codifyFiles)
32-
.then((files) => this.parseContents(files, sourceMaps))
33-
.then((config) => this.createConfigBlocks(config, sourceMaps))
46+
const contents = this.parseContents(files, sourceMaps)
47+
const configs = this.createConfigBlocks(contents, sourceMaps)
3448

35-
return Project.create(configs, codifyFiles, sourceMaps);
49+
return Project.create(configs, files, sourceMaps);
3650
}
3751

3852
private async getFilePaths(dirOrFile: string): Promise<string[]> {
@@ -41,7 +55,7 @@ class Parser {
4155
// A single file was passed in. We need to test if the file satisfies the codify file regex
4256
if (!isDirectory) {
4357
const fileName = path.basename(dirOrFile);
44-
if (!CODIFY_FILE_REGEX.test(fileName)) {
58+
if (!config.codifyFileRegex.test(fileName)) {
4559
throw new Error(`Invalid file path provided ${dirOrFile} ${fileName}. Expected the file to be *.codify.jsonc, *.codify.json5, *.codify.json, or *.codify.yaml `)
4660
}
4761

@@ -51,7 +65,7 @@ class Parser {
5165
const filesInDir = await fs.readdir(dirOrFile);
5266

5367
return filesInDir
54-
.filter((name) => CODIFY_FILE_REGEX.test(name))
68+
.filter((name) => config.codifyFileRegex.test(name))
5569
.map((name) => path.join(dirOrFile, name))
5670
}
5771

0 commit comments

Comments
 (0)