chore:lint

This commit is contained in:
Bereket Engida
2024-10-26 16:55:52 +03:00
parent d9a8407295
commit f3352560e0
2 changed files with 135 additions and 79 deletions

View File

@@ -49,10 +49,13 @@ function getPathAliases(cwd: string): Record<string, string> | null {
for (const [alias, aliasPaths] of obj) {
for (const aliasedPath of aliasPaths) {
const resolvedBaseUrl = path.join(cwd, baseUrl);
const finalAlias = alias.slice(-1) === "*" ? alias.slice(0,-1) : alias;
const finalAliasedPath = aliasedPath.slice(-1) === "*" ? aliasedPath.slice(0,-1) : aliasedPath;
const finalAlias = alias.slice(-1) === "*" ? alias.slice(0, -1) : alias;
const finalAliasedPath =
aliasedPath.slice(-1) === "*"
? aliasedPath.slice(0, -1)
: aliasedPath;
result[finalAlias || ""] = path.join(resolvedBaseUrl,finalAliasedPath);
result[finalAlias || ""] = path.join(resolvedBaseUrl, finalAliasedPath);
}
}
addSvelteKitEnvModules(result);

View File

@@ -1,17 +1,17 @@
import {afterEach, beforeEach, describe, expect, it} from "vitest";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {test} from "vitest";
import { test } from "vitest";
import os from "node:os";
import fs from "node:fs/promises";
import path from "node:path";
import {getConfig} from "../src/utils/get-config";
import { getConfig } from "../src/utils/get-config";
interface TmpDirFixture {
tmpdir: string;
}
async function createTempDir() {
const tmpdir = path.join(process.cwd(), 'test', "getConfig_test-");
const tmpdir = path.join(process.cwd(), "test", "getConfig_test-");
return await fs.mkdtemp(tmpdir);
}
@@ -21,30 +21,31 @@ export const tmpdirTest = test.extend<TmpDirFixture>({
await use(directory);
await fs.rm(directory, {recursive: true});
await fs.rm(directory, { recursive: true });
},
});
let tmpDir = '.';
let tmpDir = ".";
describe("getConfig", async () => {
beforeEach(async () => {
const tmp = path.join(process.cwd(), "getConfig_test-");
tmpDir = await fs.mkdtemp(tmp);
})
});
afterEach(async () => {
await fs.rm(tmpDir, {recursive: true});
})
await fs.rm(tmpDir, { recursive: true });
});
it("should resolve resolver type alias", async () => {
const authPath = path.join(tmpDir, 'server', 'auth');
const dbPath = path.join(tmpDir, 'server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "server", "auth");
const dbPath = path.join(tmpDir, "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -53,10 +54,12 @@ describe("getConfig", async () => {
"@server/*": ["./server/*"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "@server/db/db";
@@ -68,29 +71,36 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
const config = await getConfig({cwd: tmpDir, configPath: 'server/auth/auth.ts'})
const config = await getConfig({
cwd: tmpDir,
configPath: "server/auth/auth.ts",
});
expect(config).not.toBe(null);
});
it("should resolve direct alias", async () => {
const authPath = path.join(tmpDir, 'server', 'auth');
const dbPath = path.join(tmpDir, 'server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "server", "auth");
const dbPath = path.join(tmpDir, "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -99,10 +109,12 @@ describe("getConfig", async () => {
"prismaDbClient": ["./server/db/db"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "prismaDbClient";
@@ -114,29 +126,36 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
const config = await getConfig({cwd: tmpDir, configPath: 'server/auth/auth.ts'})
const config = await getConfig({
cwd: tmpDir,
configPath: "server/auth/auth.ts",
});
expect(config).not.toBe(null);
});
it("should resolve resolver type alias with relative path", async () => {
const authPath = path.join(tmpDir, 'test','server', 'auth');
const dbPath = path.join(tmpDir, 'test','server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "test", "server", "auth");
const dbPath = path.join(tmpDir, "test", "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -145,10 +164,12 @@ describe("getConfig", async () => {
"@server/*": ["./server/*"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "@server/db/db";
@@ -160,29 +181,36 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
const config = await getConfig({cwd: tmpDir, configPath: 'test/server/auth/auth.ts'})
const config = await getConfig({
cwd: tmpDir,
configPath: "test/server/auth/auth.ts",
});
expect(config).not.toBe(null);
});
it("should resolve direct alias with relative path", async () => {
const authPath = path.join(tmpDir, 'test','server', 'auth');
const dbPath = path.join(tmpDir, 'test','server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "test", "server", "auth");
const dbPath = path.join(tmpDir, "test", "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -191,10 +219,12 @@ describe("getConfig", async () => {
"prismaDbClient": ["./server/db/db"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "prismaDbClient";
@@ -206,29 +236,36 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
const config = await getConfig({cwd: tmpDir, configPath: 'test/server/auth/auth.ts'})
const config = await getConfig({
cwd: tmpDir,
configPath: "test/server/auth/auth.ts",
});
expect(config).not.toBe(null);
});
it("should resolve with relative import", async () => {
const authPath = path.join(tmpDir, 'test','server', 'auth');
const dbPath = path.join(tmpDir, 'test','server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "test", "server", "auth");
const dbPath = path.join(tmpDir, "test", "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -237,10 +274,12 @@ describe("getConfig", async () => {
"prismaDbClient": ["./server/db/db"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "../db/db";
@@ -252,29 +291,36 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
const config = await getConfig({cwd: tmpDir, configPath: 'test/server/auth/auth.ts'})
const config = await getConfig({
cwd: tmpDir,
configPath: "test/server/auth/auth.ts",
});
expect(config).not.toBe(null);
});
it("should error with invalid alias", async () => {
const authPath = path.join(tmpDir, 'server', 'auth');
const dbPath = path.join(tmpDir, 'server', 'db');
await fs.mkdir(authPath, {recursive: true});
await fs.mkdir(dbPath, {recursive: true});
const authPath = path.join(tmpDir, "server", "auth");
const dbPath = path.join(tmpDir, "server", "db");
await fs.mkdir(authPath, { recursive: true });
await fs.mkdir(dbPath, { recursive: true });
//create dummy tsconfig.json
await fs.writeFile(path.join(tmpDir, "tsconfig.json"),
await fs.writeFile(
path.join(tmpDir, "tsconfig.json"),
`{
"compilerOptions": {
/* Path Aliases */
@@ -283,10 +329,12 @@ describe("getConfig", async () => {
"@server/*": ["./PathIsInvalid/*"]
}
}
}`);
}`,
);
//create dummy auth.ts
await fs.writeFile(path.join(authPath, "auth.ts"),
await fs.writeFile(
path.join(authPath, "auth.ts"),
`import {betterAuth} from "better-auth";
import {prismaAdapter} from "better-auth/adapters/prisma";
import {db} from "@server/db/db";
@@ -298,16 +346,21 @@ describe("getConfig", async () => {
emailAndPassword: {
enabled: true,
}
})`);
})`,
);
//create dummy db.ts
await fs.writeFile(path.join(dbPath, "db.ts"),
await fs.writeFile(
path.join(dbPath, "db.ts"),
`class PrismaClient {
constructor() {}
}
export const db = new PrismaClient()`);
export const db = new PrismaClient()`,
);
await expect(() => getConfig({cwd: tmpDir, configPath: 'server/auth/auth.ts'})).rejects.toThrowError();
await expect(() =>
getConfig({ cwd: tmpDir, configPath: "server/auth/auth.ts" }),
).rejects.toThrowError();
});
})
});