Skip to content

Commit 47baf27

Browse files
committed
feat: Added login method
1 parent 6c5290d commit 47baf27

File tree

4 files changed

+212
-8
lines changed

4 files changed

+212
-8
lines changed

package-lock.json

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

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@codifycli/ink-form": "0.0.12",
88
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
99
"@inkjs/ui": "^2",
10+
"@mischnic/json-sourcemap": "^0.1.1",
1011
"@oclif/core": "^4.0.8",
1112
"@oclif/plugin-autocomplete": "^3.2.24",
1213
"@oclif/plugin-help": "^6.2.4",
@@ -22,19 +23,19 @@
2223
"ink-big-text": "^2.0.0",
2324
"ink-gradient": "^3.0.0",
2425
"ink-select-input": "^6.0.0",
26+
"jju": "^1.4.0",
2527
"jotai": "^2.11.1",
2628
"js-yaml": "^4.1.0",
2729
"js-yaml-source-map": "^0.2.2",
2830
"json-source-map": "^0.6.1",
31+
"json5": "^2.2.3",
2932
"latest-semver": "^4.0.0",
3033
"nanoid": "^5.0.9",
34+
"open": "^10.1.2",
3135
"parse-json": "^8.1.0",
3236
"react": "^18.3.1",
3337
"semver": "^7.5.4",
34-
"supports-color": "^9.4.0",
35-
"json5": "^2.2.3",
36-
"@mischnic/json-sourcemap": "^0.1.1",
37-
"jju": "^1.4.0"
38+
"supports-color": "^9.4.0"
3839
},
3940
"description": "Codify allows users to configure settings, install new packages, and automate their systems using code instead of the GUI. Get set up on a new laptop in one click, maintain a Codify file within your project so anyone can get started and never lose your cool apps or favourite settings again.",
4041
"devDependencies": {
@@ -43,15 +44,15 @@
4344
"@types/chalk": "^2.2.0",
4445
"@types/debug": "^4.1.12",
4546
"@types/diff": "^7.0.1",
47+
"@types/jju": "^1.4.5",
4648
"@types/js-yaml": "^4.0.9",
49+
"@types/json5": "^2.2.0",
4750
"@types/mocha": "^10.0.10",
4851
"@types/node": "^20",
4952
"@types/react": "^18.3.1",
5053
"@types/semver": "^7.5.4",
5154
"@types/strip-ansi": "^5.2.1",
52-
"@types/jju": "^1.4.5",
5355
"@typescript-eslint/eslint-plugin": "^8.16.0",
54-
"@types/json5": "^2.2.0",
5556
"codify-plugin-lib": "^1.0.151",
5657
"esbuild": "^0.24.0",
5758
"esbuild-plugin-copy": "^2.1.1",

src/commands/login.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { BaseCommand } from '../common/base-command.js';
2+
import { LoginOrchestrator } from '../orchestrators/login.js';
3+
4+
export default class Login extends BaseCommand {
5+
static description =
6+
`Validate a codify.jsonc/codify.json/codify.yaml file.
7+
8+
For more information, visit: https://docs.codifycli.com/commands/validate
9+
`
10+
11+
static flags = {}
12+
13+
static examples = [
14+
'<%= config.bin %> <%= command.id %>',
15+
'<%= config.bin %> <%= command.id %> --path=../../import.codify.jsonc',
16+
]
17+
18+
public async run(): Promise<void> {
19+
const { flags } = await this.parse(Login)
20+
21+
await LoginOrchestrator.run();
22+
}
23+
}

src/orchestrators/login.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as fs from 'node:fs/promises';
2+
import { type IncomingMessage, ServerResponse, createServer } from 'node:http';
3+
import * as os from 'node:os';
4+
import path from 'node:path';
5+
import open from 'open';
6+
7+
export class LoginOrchestrator {
8+
static async run(){
9+
const server = createServer((req, res) => {
10+
LoginOrchestrator.handleRequests(req, res);
11+
});
12+
13+
process.stdout.on('data', (data) => {
14+
console.log(data);
15+
})
16+
17+
server.listen(51_039, 'localhost', () => {
18+
console.log('Opening CLI auth page...')
19+
open('http://localhost:3000/auth/cli');
20+
})
21+
}
22+
23+
private static async handleRequests(req: IncomingMessage, res: ServerResponse<IncomingMessage>) {
24+
try {
25+
if (req.method !== 'POST') {
26+
res.writeHead(400, { 'Content-Type': 'application/json' });
27+
return;
28+
}
29+
30+
const json = await new Promise((resolve) => {
31+
const buf = new Array<Uint8Array>()
32+
req.on('data', (chunk) => {
33+
buf.push(chunk);
34+
}).on('end', () => {
35+
const body = Buffer.concat(buf).toString();
36+
const json = JSON.parse(body);
37+
resolve(json);
38+
}).on('error', (err) => {
39+
console.error(err);
40+
})
41+
});
42+
43+
const credentialsPath = path.join(os.homedir(), '.codify', 'credentials.json');
44+
console.log(`Saving credentials to ${credentialsPath}`);
45+
await fs.writeFile(credentialsPath, JSON.stringify(json));
46+
47+
res.writeHead(200, { 'Content-Type': 'application/json' });
48+
process.exit(0);
49+
} catch (error) {
50+
console.error(error);
51+
res.writeHead(500, { 'Content-Type': 'application/json' });
52+
process.exit(1);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)