Skip to content

Commit

Permalink
Merge #6
Browse files Browse the repository at this point in the history
chore: slim down npm package; test: introduce tests to package
  • Loading branch information
thisjt authored Oct 13, 2024
2 parents 1b49301 + f10a8c6 commit 48c6acc
Show file tree
Hide file tree
Showing 8 changed files with 580 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

node_modules/
dist/
test/
coverage/
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@

node_modules/
.github/
src/
coverage/
.prettierignore
README.md
eslint.config.js
prettier.config.js
vitest.config.js
tsconfig.json
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hono-file-router",
"version": "0.0.4",
"version": "0.0.5",
"description": "File-based router for easier route management for hono.",
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand All @@ -19,7 +19,8 @@
"scripts": {
"dev": "pkgroll --watch",
"build": "pkgroll",
"test": "echo \"no test for now\""
"test": "vitest run",
"test:watch": "vitest"
},
"keywords": [
"hono",
Expand All @@ -34,6 +35,7 @@
},
"devDependencies": {
"@types/node": "^22.7.5",
"@vitest/coverage-istanbul": "^2.1.2",
"eslint": "^9.12.0",
"eslint-config-prettier": "^9.1.0",
"globals": "^15.11.0",
Expand Down
497 changes: 497 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/filerouterscanner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expectTypeOf, expect } from 'vitest';

import { getCwd, cleanupPath, directoryScanner, fileExists } from './filerouterscanner';

describe('file router scanner', () => {
it('test cwd', () => {
const directory = getCwd();

expectTypeOf(directory).toMatchTypeOf('pathstring');
});

it('test cleanup path', () => {
expect(cleanupPath('/./')).toBe('/');
expect(cleanupPath('\\')).toBe('/');
});

it('scans directories', () => {
const listOfFiles = directoryScanner('./src');
const filtered = listOfFiles.filter((file) => file.name === 'filerouterscanner.ts');
expect(filtered[0].name).toBe('filerouterscanner.ts');
});

it('checks for existing file', () => {
expect(fileExists('./src/filerouterscanner.ts')).toBe(true);
expect(fileExists('./src/thisfiledoesnotexistandwillneverbe')).toBe(false);
});

it.todo('test filerouterscanner');
});
25 changes: 16 additions & 9 deletions src/filerouterscanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ type httpMethodTypes = { [K in (typeof httpMethods)[number]]?: RouteInfo };
export type RouteInfo = { file: string; route: string; method: (typeof httpMethods)[number] };

export function fileRouterScanner(cwd: string, initpath: string) {
const folders = fs.readdirSync(`${cwd}/${initpath}`, { recursive: true, withFileTypes: true });

const normalFolders = folders.map((folders) => ({ name: folders.name, path: cleanupPath(folders.parentPath), directory: folders.isDirectory() }));
const normalFolders = directoryScanner(`${cwd}/${initpath}`);

const resolvedPath = cleanupPath(path.resolve(initpath));
normalFolders.push({ name: '.', path: resolvedPath, directory: true });
Expand All @@ -25,8 +23,8 @@ export function fileRouterScanner(cwd: string, initpath: string) {

httpMethods.forEach((methodOrIndex) => {
const pathToFile = `${folder.path}/${folder.name}/${methodOrIndex}`;
const jsExists = fs.existsSync(`${pathToFile}.js`);
const tsExists = fs.existsSync(`${pathToFile}.ts`);
const jsExists = fileExists(`${pathToFile}.js`);
const tsExists = fileExists(`${pathToFile}.ts`);

if (jsExists && tsExists) errors.push({ message: 'Both js and ts files exist for that method. Only one must exist.', data: pathToFile });

Expand All @@ -46,10 +44,6 @@ export function fileRouterScanner(cwd: string, initpath: string) {
routes.push(methodAssignment);
});

function cleanupPath(path: string) {
return path.replaceAll('/./', '/').replaceAll('\\', '/');
}

function replaceParams(path: string) {
return path.replace(/\[([^\]]+)\]/g, ':$1');
}
Expand All @@ -59,6 +53,19 @@ export function fileRouterScanner(cwd: string, initpath: string) {
return routes;
}

export function fileExists(file: string) {
return fs.existsSync(file);
}

export function directoryScanner(path: string) {
const folders = fs.readdirSync(path, { recursive: true, withFileTypes: true });
return folders.map((folders) => ({ name: folders.name, path: cleanupPath(folders.parentPath), directory: folders.isDirectory() }));
}

export function cleanupPath(path: string) {
return path.replaceAll('/./', '/').replaceAll('\\', '/');
}

export function getCwd() {
return process.cwd();
}
12 changes: 12 additions & 0 deletions src/routegenerator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest';

import { createHandler } from './routegenerator';

describe('route generator', () => {
it('test route generator', () => {
const PROMISE = createHandler((context) => {
return context.json({ hello: 'world' });
});
expect(Array.isArray(PROMISE)).toBe(true);
});
});
11 changes: 11 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
coverage: {
provider: 'istanbul',
include: ['./src/**/*'],
enabled: true,
},
},
});

0 comments on commit 48c6acc

Please sign in to comment.