Skip to content

Commit

Permalink
Add eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Corb3nik committed Jan 7, 2025
1 parent 047834a commit 50613d5
Show file tree
Hide file tree
Showing 28 changed files with 2,897 additions and 535 deletions.
9 changes: 9 additions & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defaultConfig } from "@caido/eslint-config";

export default [
...defaultConfig({
vue: false,
stylistic: false,
compat: false,
}),
];
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"typecheck": "tsc --noEmit",
"test": "pnpm build && vitest",
"build": "tsup",
"lint": "eslint src --ext .ts"
"lint": "eslint --flag unstable_ts_config --fix ./src ./playgrounds"
},
"keywords": [
"caido",
Expand All @@ -40,15 +40,15 @@
"vite": "6.0.7",
"tsup": "8.3.5"
},

"devDependencies": {
"@caido/eslint-config": "0.0.4",
"@types/express": "5.0.0",
"@types/node": "22.10.2",
"@types/ws": "8.5.13",
"eslint": "9.17.0",
"tsup": "8.3.5",
"tsx": "4.19.2",
"typescript": "5.7.2",
"vitest": "2.1.8",
"vite": "6.0.6"
"vitest": "2.1.8"
}
}
72 changes: 42 additions & 30 deletions playgrounds/build-backend/__tests__/build-backend.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
import { describe, it, expect } from 'vitest';
import path from "path";

import { getZipFileContent } from '../../utils';
import path from 'path';
import { describe, expect, it } from "vitest";

describe('build-backend', () => {
import { getZipFileContent } from "../../utils";

describe("build-backend", () => {
it("should have manifest.json file", async () => {
const zipPath = path.resolve(__dirname, '../dist/plugin_package.zip');

const manifestJsonContent = await getZipFileContent(zipPath, 'manifest.json');

expect(manifestJsonContent).toEqual(JSON.stringify({
"id": "build-backend",
"name": "Backend",
"version": "1.0.0",
"description": "Backend plugin",
"author": {
"name": "John Doe",
"email": "[email protected]",
"url": "https://example.com"
},
"plugins": [
const zipPath = path.resolve(__dirname, "../dist/plugin_package.zip");

const manifestJsonContent = await getZipFileContent(
zipPath,
"manifest.json",
);

expect(manifestJsonContent).toEqual(
JSON.stringify(
{
"id": "backend",
"kind": "backend",
"name": "backend",
"entrypoint": "backend/index.js",
"runtime": "javascript"
}
]
}, undefined, 2));
id: "build-backend",
name: "Backend",
version: "1.0.0",
description: "Backend plugin",
author: {
name: "John Doe",
email: "[email protected]",
url: "https://example.com",
},
plugins: [
{
id: "backend",
kind: "backend",
name: "backend",
entrypoint: "backend/index.js",
runtime: "javascript",
},
],
},
undefined,
2,
),
);
});

it("should have index.js file", async () => {
const zipPath = path.resolve(__dirname, '../dist/plugin_package.zip');
const zipPath = path.resolve(__dirname, "../dist/plugin_package.zip");

const indexJsContent = (await getZipFileContent(zipPath, 'backend/index.js'))?.replace(/\s+/g, '');
const indexJsContent = (
await getZipFileContent(zipPath, "backend/index.js")
)?.replace(/\s+/g, "");

const expectedContent = `
//packages/backend/src/index.ts
Expand All @@ -43,7 +55,7 @@ describe('build-backend', () => {
export {
init
};
`.replace(/\s+/g, '');
`.replace(/\s+/g, "");

expect(indexJsContent).toEqual(expectedContent);
});
Expand Down
4 changes: 2 additions & 2 deletions playgrounds/build-backend/caido.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export default {
kind: "backend",
id: "backend",
root: "./packages/backend",
}
},
],
};
};
83 changes: 49 additions & 34 deletions playgrounds/build-frontend/__tests__/build-frontend.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
import { describe, it, expect } from 'vitest';
import path from "path";

import { getZipFileContent } from '../../../playgrounds/utils';
import path from 'path';
import { describe, expect, it } from "vitest";

describe('build-frontend', () => {
import { getZipFileContent } from "../../../playgrounds/utils";

describe("build-frontend", () => {
it("should have manifest.json file", async () => {
const zipPath = path.resolve(__dirname, '../dist/plugin_package.zip');

const manifestJsonContent = await getZipFileContent(zipPath, 'manifest.json');

expect(manifestJsonContent).toEqual(JSON.stringify({
"id": "build-frontend",
"name": "Frontend",
"version": "1.0.0",
"description": "Frontend plugin",
"author": {
"name": "John Doe",
"email": "[email protected]",
"url": "https://example.com"
},
"plugins": [
const zipPath = path.resolve(__dirname, "../dist/plugin_package.zip");

const manifestJsonContent = await getZipFileContent(
zipPath,
"manifest.json",
);

expect(manifestJsonContent).toEqual(
JSON.stringify(
{
"id": "frontend",
"kind": "frontend",
"name": "frontend",
"entrypoint": "frontend/index.js",
"style": "frontend/index.css",
"backend": null
}
]
}, undefined, 2));
id: "build-frontend",
name: "Frontend",
version: "1.0.0",
description: "Frontend plugin",
author: {
name: "John Doe",
email: "[email protected]",
url: "https://example.com",
},
plugins: [
{
id: "frontend",
kind: "frontend",
name: "frontend",
entrypoint: "frontend/index.js",
style: "frontend/index.css",
backend: null,
},
],
},
undefined,
2,
),
);
});

it("should have index.js file", async () => {
const zipPath = path.resolve(__dirname, '../dist/plugin_package.zip');
const zipPath = path.resolve(__dirname, "../dist/plugin_package.zip");

const indexJsContent = (await getZipFileContent(zipPath, 'frontend/index.js'))?.replace(/\s+/g, '');
const indexJsContent = (
await getZipFileContent(zipPath, "frontend/index.js")
)?.replace(/\s+/g, "");

const expectedContent = `
const o = () => {
Expand All @@ -44,16 +56,19 @@ describe('build-frontend', () => {
export {
o as init
};
`.replace(/\s+/g, '');
`.replace(/\s+/g, "");

expect(indexJsContent).toEqual(expectedContent);
});

it("should have index.css file", async () => {
const zipPath = path.resolve(__dirname, '../dist/plugin_package.zip');
const zipPath = path.resolve(__dirname, "../dist/plugin_package.zip");

const indexCssContent = await getZipFileContent(zipPath, 'frontend/index.css');
const indexCssContent = await getZipFileContent(
zipPath,
"frontend/index.css",
);

expect(indexCssContent).toEqual('body{background-color:red}\n');
expect(indexCssContent).toEqual("body{background-color:red}\n");
});
});
4 changes: 2 additions & 2 deletions playgrounds/build-frontend/caido.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export default {
kind: "frontend",
id: "frontend",
root: "./packages/frontend",
}
},
],
};
};
6 changes: 3 additions & 3 deletions playgrounds/build-frontend/packages/frontend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './index.css';
import "./index.css";

export const init = () => {
console.log('init');
};
console.log("init");
};
15 changes: 8 additions & 7 deletions playgrounds/setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { execSync } from 'child_process';
import path from 'path';
import { afterAll, beforeAll, expect } from 'vitest';
import { execSync } from "child_process";
import path from "path";

beforeAll(async ({ file }) => {
import { afterAll, beforeAll, expect } from "vitest";

beforeAll(({ file }) => {
// Get the test file path from the current test file
const testPath = file.filepath;

Expand All @@ -11,17 +12,17 @@ beforeAll(async ({ file }) => {

// Installing the dependencies
console.log(`Installing dependencies in ${playgroundDir}...`);
execSync('pnpm install', {
execSync("pnpm install", {
cwd: playgroundDir,
});

// Run pnpm build in the playground directory
console.log(`Building playground in ${playgroundDir}...`);
execSync('node ../../dist/cli.js build', {
execSync("node ../../dist/cli.js build", {
cwd: playgroundDir,
});
});

afterAll(() => {
// Clean up if needed
});
});
38 changes: 21 additions & 17 deletions playgrounds/utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import JSZip from 'jszip';
import fs from 'fs/promises';
import fs from "fs/promises";

import JSZip from "jszip";

/**
* Extracts the content of a specific file from a zip archive
* @param zipPath - Path to the zip file
* @param filePath - The internal path of the file to extract
* @returns Promise resolving to the file content as a string or null if not found
*/
export async function getZipFileContent(zipPath: string, filePath: string): Promise<string | null> {
try {
const zipBuffer = await fs.readFile(zipPath);
const zip = new JSZip();
const loadedZip = await zip.loadAsync(zipBuffer);
const file = loadedZip.file(filePath);

if (!file) {
return null;
}

return await file.async('string');
} catch (error) {
console.error('Error extracting file from zip:', error);
return null;
export async function getZipFileContent(
zipPath: string,
filePath: string,
): Promise<string | undefined> {
try {
const zipBuffer = await fs.readFile(zipPath);
const zip = new JSZip();
const loadedZip = await zip.loadAsync(zipBuffer);
const file = loadedZip.file(filePath);

if (!file) {
return undefined;
}

return await file.async("string");
} catch (error) {
console.error("Error extracting file from zip:", error);
return undefined;
}
}
Loading

0 comments on commit 50613d5

Please sign in to comment.