Skip to content

Commit

Permalink
feat:CEXT-2038 Resolved module related errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Simran committed Sep 27, 2023
1 parent e4580c2 commit 8af30d0
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 62 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@oclif/config": "^1.15.1",
"@oclif/core": "^1.14.1",
"@oclif/errors": "^1.1.2",
"@testmeshbuilder/mesh-builder": "^0.0.2",
"@testmeshbuilder/mesh-builder": "^0.0.3",
"axios": "^1.2.0",
"chalk": "^4.1.0",
"dotenv": "^16.0.3",
Expand All @@ -29,7 +29,10 @@
"pupa": "^3.1.0",
"source-registry-storage-adapter": "github:devx-services/source-registry-storage-adapter#main",
"uuid": "^8.3.2",
"eslint-plugin-security": "^1.5.0"
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-security": "^1.5.0",
"eslint-plugin-sonarjs": "^0.16.0",
"eslint-plugin-standard": "^5.0.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.15.8",
Expand All @@ -47,10 +50,6 @@
"eslint-plugin-no-loops": "^0.3.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-security": "^1.5.0",
"eslint-plugin-sonarjs": "^0.16.0",
"eslint-plugin-standard": "^5.0.0",
"execa": "4.1.0",
"husky": "7.0.4",
"jest": "^29.2.2",
Expand Down
92 changes: 49 additions & 43 deletions src/commands/api-mesh/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ governing permissions and limitations under the License.

const { Command } = require('@oclif/core');
const { portNoFlag, debugFlag, readFileContents } = require('../../utils');
const meshBuilder = require('@testmeshbuilder/mesh-builder');
//const meshBuilder = require('@testmeshbuilder/mesh-builder');
const meshBuilder = require('@adobe/mesh-builder');
const fs = require('fs');
const UUID = require('../../uuid');
const path = require('path');
const { initRequestId, startGraphqlServer } = require('../../helpers');
const logger = require('../../classes/logger');
require('dotenv').config();


const { validateMesh, buildMesh, compileMesh } = meshBuilder.default;

class RunCommand extends Command {
Expand All @@ -42,59 +44,63 @@ class RunCommand extends Command {
static examples = [];

async run() {
await initRequestId();
try {
await initRequestId();

logger.info(`RequestId: ${global.requestId}`);
logger.info(`RequestId: ${global.requestId}`);

const { args, flags } = await this.parse(RunCommand);
const { args, flags } = await this.parse(RunCommand);

if (!args.file) {
this.error('Missing file path. Run aio api-mesh run --help for more info.');
}
if (!args.file) {
this.error('Missing file path. Run aio api-mesh run --help for more info.');
}

let portNo;
let portNo;

//The environment variables are optional and need default values
if (process.env.PORT !== undefined) {
if (isNaN(process.env.PORT) || !Number.isInteger(parseInt(process.env.PORT))) {
this.error('PORT value in the .env file is not a valid integer');
}
//The environment variables are optional and need default values
if (process.env.PORT !== undefined) {
if (isNaN(process.env.PORT) || !Number.isInteger(parseInt(process.env.PORT))) {
this.error('PORT value in the .env file is not a valid integer');
}

portNo = process.env.PORT;
}
portNo = process.env.PORT;
}

//To set the port number as the provided value in the command
if (flags.port !== undefined) {
portNo = flags.port;
}
//To set the port number as the provided value in the command
if (flags.port !== undefined) {
portNo = flags.port;
}

if (!portNo) {
portNo = 5000;
}
if (!portNo) {
portNo = 5000;
}

try {
//Ensure that current directory includes package.json
if (fs.existsSync(path.join(process.cwd(), 'package.json'))) {
//Read the mesh input file
let inputMeshData = await readFileContents(args.file, this, 'mesh');
let data = JSON.parse(inputMeshData);

//Generating unique mesh id
let meshId = UUID.newUuid().toString();

this.log(`Beginning steps to start server on port : ${portNo}`);

await validateMesh(data.meshConfig);
await buildMesh(meshId, data.meshConfig);
await compileMesh(meshId);
await startGraphqlServer(meshId, portNo, flags.debug);
} else {
this.error(
'`aio api-mesh run` cannot be executed as there is no package.json file in current directory. Use `aio api-mesh init` to setup a package.',
);
try {
//Ensure that current directory includes package.json
if (fs.existsSync(path.join(process.cwd(), 'package.json'))) {
//Read the mesh input file
let inputMeshData = await readFileContents(args.file, this, 'mesh');
let data = JSON.parse(inputMeshData);

//Generating unique mesh id
let meshId = UUID.newUuid().toString();

this.log(`Beginning steps to start server on port : ${portNo}`);

await validateMesh(data.meshConfig);
await buildMesh(meshId, data.meshConfig);
await compileMesh(meshId);
await startGraphqlServer(meshId, portNo, flags.debug);
} else {
this.error(
'`aio api-mesh run` cannot be executed as there is no package.json file in current directory. Use `aio api-mesh init` to setup a package.',
);
}
} catch (error) {
this.log('ERROR: ' + error.message);
}
} catch (error) {
this.log('ERROR: ' + error.message);
this.log('Error from run')
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const fastify = require('fastify');
const { createYoga } = require('graphql-yoga');
// Resolve the path to 'fastify' and 'graphql-yoga' within the local 'node_modules'
const fastifyPath = require.resolve('fastify', { paths: [process.cwd()] });
const yogaPath = require.resolve('graphql-yoga', { paths: [process.cwd()] });

// Load 'fastify' and 'graphql-yoga' using the resolved paths
const fastify = require(fastifyPath);
const { createYoga } = require(yogaPath);

let yogaServer = null;

Expand Down Expand Up @@ -72,8 +77,6 @@ app.route({
method: ['GET', 'POST'],
url: '/graphql',
handler: async (req, res) => {
const yogaServer = await getYogaServer();

console.log('Request received: ', req.body);

const response = await yogaServer.handleNodeRequest(req, {
Expand All @@ -98,11 +101,12 @@ app.listen(
//set the port no of the server based on the input value
port: portNo
},
err => {
async (err) => {
if (err) {
console.error(err);
process.exit(1);
}
yogaServer = await getYogaServer();

console.log(`Server is running on http://localhost:${portNo}/graphql`);
},
Expand Down
4 changes: 3 additions & 1 deletion src/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"eslint-plugin-no-loops": "^0.3.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-security": "^1.5.0",
"eslint-plugin-sonarjs": "^0.16.0"
"eslint-plugin-sonarjs": "^0.16.0",
"fastify": "^4.10.2",
"graphql-yoga": "3.1.1"
},
"engines": {
"node": ">=18.0.0"
Expand Down
Loading

0 comments on commit 8af30d0

Please sign in to comment.