Skip to content

Commit 4c2759e

Browse files
committed
Added login helper and trying out parser abstraction
1 parent 7cb3972 commit 4c2759e

File tree

5 files changed

+79
-9
lines changed

5 files changed

+79
-9
lines changed

src/common/initialize-plugins.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SubProcessName, ctx } from '../events/context.js';
77
import { CODIFY_FILE_REGEX, CodifyParser } from '../parser/index.js';
88
import { DependencyMap, PluginManager } from '../plugins/plugin-manager.js';
99
import { Reporter } from '../ui/reporters/reporter.js';
10+
import { LoginHelper } from '../connect/login-helper.js';
1011

1112
export interface InitializeArgs {
1213
path?: string;
@@ -55,7 +56,9 @@ export class PluginInitOrchestrator {
5556
? await PluginInitOrchestrator.findCodifyJson()
5657
: fileOrDir
5758

58-
if (!pathToParse && !allowEmptyProject) {
59+
const isLoggedIn = LoginHelper.get()?.isLoggedIn ?? false;
60+
61+
if (!pathToParse && !allowEmptyProject && !isLoggedIn) {
5962
ctx.subprocessFinished(SubProcessName.PARSE);
6063
ctx.subprocessStarted(SubProcessName.CREATE_ROOT_FILE)
6164
const createRootCodifyFile = await reporter.promptConfirmation('\nNo codify file found. Do you want to create a root file at ~/codify.jsonc?');

src/connect/login-helper.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from 'node:fs/promises';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
5+
interface Credentials {
6+
accessToken: string;
7+
email: string;
8+
userId: string;
9+
expiry: string;
10+
}
11+
12+
export class LoginHelper {
13+
private static instance: LoginHelper;
14+
15+
private constructor(
16+
public isLoggedIn: boolean,
17+
public credentials?: Credentials
18+
) {};
19+
20+
static async load(): Promise<LoginHelper> {
21+
if (LoginHelper.instance) {
22+
return LoginHelper.instance;
23+
}
24+
25+
const credentials = await LoginHelper.read();
26+
if (!credentials) {
27+
LoginHelper.instance = new LoginHelper(false);
28+
return LoginHelper.instance;
29+
}
30+
31+
if (new Date(credentials.expiry).getTime() < Date.now()) {
32+
LoginHelper.instance = new LoginHelper(false);
33+
return LoginHelper.instance;
34+
}
35+
36+
LoginHelper.instance = new LoginHelper(true, credentials);
37+
return LoginHelper.instance;
38+
}
39+
40+
static get(): LoginHelper | undefined {
41+
return LoginHelper.instance;
42+
}
43+
44+
static async save(credentials: Credentials) {
45+
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
46+
console.log(`Saving credentials to ${credentialsPath}`);
47+
await fs.writeFile(credentialsPath, JSON.stringify(credentials));
48+
}
49+
50+
private static async read(): Promise<Credentials | undefined> {
51+
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
52+
const credentialsStr = await fs.readFile(credentialsPath, 'utf8');
53+
54+
try {
55+
return JSON.parse(credentialsStr);
56+
} catch {
57+
return undefined;
58+
}
59+
}
60+
}

src/orchestrators/login.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import open from 'open';
77

88
import { config } from '../config.js';
99
import { ajv } from '../utils/ajv.js';
10+
import { LoginHelper } from '../connect/login-helper.js';
1011

1112
const schema = {
1213
type: 'object',
@@ -48,7 +49,7 @@ export class LoginOrchestrator {
4849
return res.status(400).send({ message: ajv.errorsText() })
4950
}
5051

51-
await LoginOrchestrator.saveCredentials(body)
52+
await LoginHelper.save(body);
5253
return res.sendStatus(200);
5354
});
5455

@@ -57,10 +58,4 @@ export class LoginOrchestrator {
5758
open('http://localhost:3000/auth/cli');
5859
})
5960
}
60-
61-
private static async saveCredentials(credentials: Credentials) {
62-
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
63-
console.log(`Saving credentials to ${credentialsPath}`);
64-
await fs.writeFile(credentialsPath, JSON.stringify(credentials));
65-
}
6661
}

src/orchestrators/plan.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PluginManager } from '../plugins/plugin-manager.js';
66
import { Reporter } from '../ui/reporters/reporter.js';
77
import { createStartupShellScriptsIfNotExists } from '../utils/file.js';
88
import { ValidateOrchestrator } from './validate.js';
9+
import { LoginHelper } from '../connect/login-helper.js';
910

1011
export interface PlanArgs {
1112
path?: string;
@@ -21,7 +22,9 @@ export interface PlanOrchestratorResponse {
2122

2223
export class PlanOrchestrator {
2324
static async run(args: PlanArgs, reporter: Reporter): Promise<PlanOrchestratorResponse> {
24-
ctx.processStarted(ProcessName.PLAN)
25+
ctx.processStarted(ProcessName.PLAN);
26+
27+
await LoginHelper.load();
2528

2629
const initializationResult = await PluginInitOrchestrator.run({
2730
...args,

src/parser/cloud/cloud-parser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { InMemoryFile, LanguageSpecificParser, ParsedConfig } from '../entities.js';
2+
import { SourceMapCache } from '../source-maps.js';
3+
4+
export class CloudParser implements LanguageSpecificParser {
5+
parse(file: InMemoryFile, sourceMaps: SourceMapCache): ParsedConfig[] {
6+
throw new Error('Method not implemented.');
7+
}
8+
9+
}

0 commit comments

Comments
 (0)