| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 1 | import { NodeJSAnalyzer } from "./nodejs.js"; |
| 2 | import { FileSystem, RealFileSystem } from "./fs.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 3 | import { Volume, IFs, createFsFromVolume } from "memfs"; |
| 4 | import { test, expect } from "@jest/globals"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 5 | import { expandValue } from "./env.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 6 | import shell from "shelljs"; |
| 7 | |
| 8 | class InMemoryFileSystem implements FileSystem { |
| 9 | constructor(private readonly fs: IFs) {} |
| 10 | |
| 11 | exists(path: string): boolean { |
| 12 | return this.fs.existsSync(path); |
| 13 | } |
| 14 | |
| 15 | // TODO(gio): add encoding |
| 16 | async readFile(path: string, encoding?: BufferEncoding): Promise<string> { |
| 17 | const contents = await this.fs.promises.readFile(path); |
| 18 | return contents.toString(encoding); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | test("canvas", async () => { |
| 23 | const fs: FileSystem = new RealFileSystem("/home/gio/code/apps/canvas/back"); |
| 24 | const analyzer = new NodeJSAnalyzer(); |
| 25 | expect(analyzer.detect(fs, "/")).toBe(true); |
| 26 | const info = await analyzer.analyze(fs, "/"); |
| 27 | console.log(info); |
| 28 | }); |
| 29 | |
| 30 | test("nodejs", async () => { |
| 31 | return; |
| 32 | const root = "/"; |
| 33 | const vol = Volume.fromNestedJSON( |
| 34 | { |
| 35 | "package.json": JSON.stringify({ |
| 36 | name: "test", |
| 37 | version: "1.0.0", |
| 38 | dependencies: { |
| 39 | prisma: "6.6.0", |
| 40 | }, |
| 41 | }), |
| 42 | "package-lock.json": "fake", |
| 43 | "prisma/schema.prisma": ` |
| 44 | datasource db { |
| 45 | provider = "sqlite" |
| 46 | url = env("DB_URL") |
| 47 | } |
| 48 | `, |
| 49 | }, |
| 50 | root, |
| 51 | ); |
| 52 | const fs: FileSystem = new InMemoryFileSystem(createFsFromVolume(vol)); |
| 53 | const analyzer = new NodeJSAnalyzer(); |
| 54 | expect(analyzer.detect(fs, root)).toBe(true); |
| 55 | const info = await analyzer.analyze(fs, root); |
| 56 | console.log(info); |
| 57 | }); |
| 58 | |
| 59 | test("env", () => { |
| 60 | console.log(expandValue("${PORT} ${DODO_VOLUME_DB}")); |
| 61 | console.log(expandValue("$PORT $DODO_VOLUME_DB")); |
| 62 | console.log(expandValue("${UNDEFINED:-${MACHINE}${UNDEFINED:-default}}")); |
| 63 | }); |
| 64 | |
| 65 | test("clone", async () => { |
| 66 | expect(shell.which("ssh-agent")).toBeTruthy(); |
| 67 | expect(shell.which("ssh-add")).toBeTruthy(); |
| 68 | expect(shell.which("git")).toBeTruthy(); |
| 69 | expect( |
| 70 | shell.exec( |
| 71 | "GIT_SSH_COMMAND='ssh -i /home/gio/.ssh/key -o IdentitiesOnly=yes' git clone git@github.com:giolekva/dodo-blog.git /tmp/dodo-blog", |
| 72 | ).code, |
| 73 | ).toBe(0); |
| 74 | const fs: FileSystem = new RealFileSystem("/tmp/dodo-blog"); |
| 75 | const analyzer = new NodeJSAnalyzer(); |
| 76 | expect(analyzer.detect(fs, "/")).toBe(true); |
| 77 | const info = await analyzer.analyze(fs, "/"); |
| 78 | console.log(info); |
| 79 | }); |
| 80 | |
| 81 | test("keygen", () => { |
| 82 | expect(shell.exec(`ssh-keygen -y -t ed25519 -f /tmp/key`).code).toBe(0); |
| 83 | }); |