Skip to content

Commit

Permalink
feat(generator): fix the problem that doesn't works replace map in ro…
Browse files Browse the repository at this point in the history
…ute path

- fix the problem that doesn't works replace map in route path
- enhance error, warn display, now ordered display using type
  • Loading branch information
imjuni committed Feb 5, 2024
1 parent 82eff8d commit e0d3122
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 13 deletions.
8 changes: 4 additions & 4 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
"inlineSourceMap": true /* Emit a single file with source maps instead of having a separate file. */,
"inlineSourceMap": true /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
"include": ["handlers/**/*.ts", "interface/**/*.ts", "server.ts"],
"exclude": ["example/**", "dist/**", "**/erdia_eg", "**/.configs/**", "**/docs/**/*"]
}
},
"include": ["handlers/**/*.ts", "interface/**/*.ts", "server.ts"],
"exclude": ["example/**", "dist/**", "**/erdia_eg", "**/.configs/**", "**/docs/**/*"]
}
2 changes: 1 addition & 1 deletion src/cli/commands/routeCommandSyncHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function routeCommandSyncHandler(options: TRouteOption) {
const table = createTable(routings.routes);

if (routings.reasons.length > 0) {
ReasonContainer.it.add(...routings.reasons);
ReasonContainer.it.add(...ReasonContainer.aggregate(routings.reasons));
show('log', ReasonContainer.it.show());
}

Expand Down
4 changes: 3 additions & 1 deletion src/compilers/routes/getRouteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { validateTypeReferences } from '#/compilers/validators/validateTypeRefer
import type { IBaseOption } from '#/configs/interfaces/IBaseOption';
import { getImportConfigurationFromResolutions } from '#/generators/getImportConfigurationFromResolutions';
import { getExtraMethod } from '#/routes/extractors/getExtraMethod';
import { getRouteMap } from '#/routes/extractors/getRouteMap';
import type { IRouteConfiguration } from '#/routes/interfaces/IRouteConfiguration';
import { getRoutePath } from '#/routes/paths/getRoutePath';
import { appendPostfixHash } from '#/tools/appendPostfixHash';
Expand Down Expand Up @@ -41,7 +42,8 @@ export async function getRouteHandler(
const typeReferenceNodes = parameter == null ? [] : getTypeReferences(parameter);
const isValidTypeReference = validateTypeReferences(sourceFile, typeReferenceNodes);
const extraMethods = await getExtraMethod(sourceFile.getFilePath().toString());
const routePathConfiguration = await getRoutePath(relativeFilePath);
const routePathMap = await getRouteMap(sourceFile.getFilePath().toString());
const routePathConfiguration = await getRoutePath(relativeFilePath, routePathMap);
const routeOptions = getRouteOptions(sourceFile);
const importedModules = [
...getResolvedImportedModules({
Expand Down
14 changes: 14 additions & 0 deletions src/modules/ReasonContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ export class ReasonContainer {
ReasonContainer.#isBootstrap = true;
}

static aggregate(reasons: IReason[]): IReason[] {
const aggregated = reasons.reduce<Record<IReason['type'], IReason[]>>(
(aggregation, reason) => {
return { ...aggregation, [reason.type]: [...aggregation[reason.type], reason] };
},
{
error: [],
warn: [],
},
);

return [...aggregated.warn, ...aggregated.error];
}

#reasons: IReason[];

constructor() {
Expand Down
4 changes: 3 additions & 1 deletion src/modules/files/getIncludePatterns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IBaseOption } from '#/configs/interfaces/IBaseOption';
import { getFileScope } from '#/modules/files/getFileScope';
import { escape } from 'glob';
import { isDescendant } from 'my-node-fp';
import type * as tsm from 'ts-morph';

Expand All @@ -19,6 +20,7 @@ export function getIncludePatterns(
}

const filePaths = tsconfig.fileNames.filter((filePath) => isDescendant(projectDirPath, filePath));
const escaped = filePaths.map((filePath) => escape(filePath));

return filePaths;
return escaped;
}
4 changes: 2 additions & 2 deletions src/routes/extractors/__tests__/route.map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const emptyMap = new Map<string, string>();

describe('getRouteMap', () => {
it('successfully load map', async () => {
const dirPath = path.join(process.cwd(), 'examples', 'handlers', 'avengers', 'heroes', '[id]', '$time');
const dirPath = path.join(process.cwd(), 'examples', 'handlers', 'avengers', 'heroes', '[id]', '[$time]');
const filePath = path.join(dirPath, 'post.ts');
const r01 = await getRouteMap(filePath);
expect(r01).toMatchObject(map);
});

it('exception', async () => {
const dirPath = path.join(process.cwd(), 'examples', 'handlers', 'avengers', 'heroes', '[id]', '$time');
const dirPath = path.join(process.cwd(), 'examples', 'handlers', 'avengers', 'heroes', '[id]', '[$time]');
const filePath = path.join(dirPath, 'post333.ts333');
const r01 = await getRouteMap(filePath);
expect(r01).toMatchObject(emptyMap);
Expand Down
20 changes: 20 additions & 0 deletions src/routes/paths/__tests__/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ describe('evaluateRoutePath', () => {
]);
});

it('replace variable', () => {
const inp = '[$time]';
const r01 = getRouteVariables(inp);

expect(r01).toMatchObject([
{ matched: '[$time]', kind: CE_ROUTE_PATH_KIND.REPLACE, nullable: false, variable: 'time' },
]);
});

it('replace variables', () => {
const inp = '[$time]-[$name]';
const r01 = getRouteVariables(inp);

expect(r01).toMatchObject([
{ matched: '[$time]', kind: 4, nullable: false, variable: 'time' },
{ matched: '-', kind: 5, nullable: false, variable: '-' },
{ matched: '[$name]', kind: 4, nullable: false, variable: 'name' },
]);
});

it('mixed multiple variable', () => {
const inp = '[[kind]]-[[wildcard]]-[id]';
const r01 = getRouteVariables(inp);
Expand Down
10 changes: 6 additions & 4 deletions src/routes/paths/getRoutePath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CE_ROUTE_INFO_KIND } from '#/routes/const-enum/CE_ROUTE_INFO_KIND';
import { getRouteMap } from '#/routes/extractors/getRouteMap';
import type { IRoutePathSummary } from '#/routes/interfaces/IRoutePathSummary';
import { evaluteRouteVariable } from '#/routes/paths/evaluteRouteVariable';
import { getRouteVariables } from '#/routes/paths/getRouteVariables';
Expand All @@ -8,16 +7,19 @@ import { basenames, replaceSepToPosix, startSepAppend } from 'my-node-fp';
import path from 'node:path';
import urljoin from 'url-join';

export async function getRoutePath(filePath: string): Promise<IRoutePathSummary> {
export async function getRoutePath(filePath: string, map: Map<string, string>): Promise<IRoutePathSummary> {
const urlPath = replaceSepToPosix(filePath);
const filename = basenames(urlPath, ['.ts', '.mts', '.cts']);
const dirname = path.dirname(urlPath);

const map = await getRouteMap(filePath);
const evaluates = dirname
.split(path.posix.sep)
.filter((endpoint) => endpoint !== '')
.map((endpoint) => getRouteVariables(endpoint).map((variable) => evaluteRouteVariable(variable, map)));
.map((endpoint) => {
const variables = getRouteVariables(endpoint);
const evaluateds = variables.map((variable) => evaluteRouteVariable(variable, map));
return evaluateds;
});

const joined = urljoin(evaluates.map((evaluate) => evaluate.join('')));

Expand Down

0 comments on commit e0d3122

Please sign in to comment.