Skip to content

Commit 398924b

Browse files
author
Alessandro Romanelli
committed
Added endpoint pattern normalization and removed local requirement of expresso-api
1 parent 54d4a2d commit 398924b

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/lib/proxy/model/Endpoint.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export class Endpoint {
3535
}
3636

3737
analyzeHandler(handler: string): [OpenAPIV3.ResponsesObject, OpenAPIV3.ParameterObject[]] {
38-
console.log(handler, this.getResponses(handler));
3938
return [this.getResponses(handler), this.getParameters(handler)];
4039
}
4140

src/lib/proxy/model/Handler.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from 'express-original';
22
import { Endpoint, EndpointJSON } from './Endpoint';
33
import { Method } from './Method';
4+
import path from 'path'
45

56
export interface HandlerJSON {
67
_instance: ExpressHandler;
@@ -41,13 +42,13 @@ export class Handler {
4142
// TODO: Add sorting depending on operation order
4243
getEndpoints(basePath = ''): { [k1: string]: { [k2 in Method]: Endpoint } } {
4344
const endpoints: { [k1: string]: { [k2 in Method]: Endpoint } } = {};
44-
for (const path of Object.keys(this._endpoints)) {
45-
const fullPath = `${basePath}${path}`;
46-
endpoints[fullPath] = this._endpoints[path];
45+
for (const p of Object.keys(this._endpoints)) {
46+
const fullPath = path.normalize(`${basePath}${p}`);
47+
endpoints[fullPath] = this._endpoints[p];
4748
}
48-
for (const path of Object.keys(this._routers)) {
49-
const fullPath = `${basePath}${path}`;
50-
Object.assign(endpoints, this._routers[path].getEndpoints(fullPath));
49+
for (const p of Object.keys(this._routers)) {
50+
const fullPath = path.normalize(`${basePath}${p}`);
51+
Object.assign(endpoints, this._routers[p].getEndpoints(fullPath));
5152
}
5253

5354
return endpoints;

src/lib/replacer/index.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import path from 'path';
22
import { stat, move, copy, remove, readFile } from 'fs-extra';
33
import logger from 'jet-logger';
44
import filesize from 'filesize';
5+
import { exec as syncExec } from 'child_process';
6+
import util from "util";
7+
8+
const exec = util.promisify(syncExec);
9+
510

611
/**
712
* Function to create a working copy of the project at basePath substituting express
@@ -32,20 +37,6 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
3237
logger.err(e);
3338
}
3439

35-
// Ensure that expresso-api is installed locally
36-
try {
37-
const pkg = JSON.parse(await readFile(path.resolve(basePath, 'package.json'), 'utf-8'));
38-
if (!Object.keys(pkg.dependencies).includes('expresso-api')) {
39-
logger.err(
40-
"'expresso-api' is not present in the package.json, please install the package locally with:\n'npm install expresso-api'",
41-
);
42-
return false;
43-
}
44-
} catch (e) {
45-
logger.err(`Could not find package.json file within '${basePath}'`);
46-
return false;
47-
}
48-
4940
try {
5041
const { size } = await stat(basePath);
5142
logger.info(`Creating work copy for ${basePath} (${filesize(size)})`);
@@ -59,22 +50,32 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
5950
// Move it within the original folder
6051
await move(path.resolve(basePath, '../.expresso-runtime'), path.resolve(basePath, '.expresso-runtime'));
6152
logger.info(`Created folder '.expresso-runtime' work copy`);
62-
// Install the 'expresso-api' as 'express' within the work copy
53+
54+
const { stdout } = await exec("npm list -g | head -1")
55+
const npmLibFolder = stdout.trim()
56+
57+
// Install the global 'expresso-api' as local 'express' within the work copy
6358
await copy(
64-
path.resolve(basePath, 'node_modules/expresso-api'),
59+
path.resolve(npmLibFolder, 'node_modules/expresso-api'),
6560
path.resolve(basePath, '.expresso-runtime/node_modules/express'),
6661
{
67-
recursive: true,
68-
},
69-
);
70-
// Install the 'express' types within as types for 'expresso-api'
71-
await copy(
72-
path.resolve(basePath, 'node_modules/@types/express/index.d.ts'),
73-
path.resolve(basePath, '.expresso-runtime/node_modules/express/dist/lib/index.d.ts'),
74-
{
75-
overwrite: true,
76-
},
77-
);
62+
recursive: true
63+
}
64+
)
65+
66+
try {
67+
// Install the 'express' types within as types for 'expresso-api'
68+
await copy(
69+
path.resolve(basePath, 'node_modules/@types/express/index.d.ts'),
70+
path.resolve(basePath, '.expresso-runtime/node_modules/express/dist/lib/index.d.ts'),
71+
{
72+
overwrite: true,
73+
},
74+
);
75+
} catch (e) {
76+
logger.warn("Could not find any 'express' types installed")
77+
}
78+
7879
// Install the real 'express' within the work copy with a different name to avoid conflicts
7980
await copy(
8081
path.resolve(basePath, 'node_modules/express'),
@@ -84,6 +85,7 @@ export const replaceExpress = async (basePath: string): Promise<boolean> => {
8485
},
8586
);
8687
logger.info(`Created 'express' proxy within work copy`);
88+
8789
} catch (e) {
8890
logger.err(e);
8991
logger.err(`Failed to replace 'express' in folder '${path.resolve(basePath, '.expresso-runtime')}'`);

0 commit comments

Comments
 (0)