Skip to content

Commit

Permalink
Handle package empty error in deploy controller (#39)
Browse files Browse the repository at this point in the history
* next middleware  added

* packageEmpty error handling done
  • Loading branch information
HeeManSu authored Mar 13, 2024
1 parent 9405db2 commit 97eaf35
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
75 changes: 42 additions & 33 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isIAllApps
} from './utils/utils';

import { PackageError } from '@metacall/protocol/package';
import { appsDirectory } from './utils/config';

const appsDir = appsDirectory();
Expand Down Expand Up @@ -213,49 +214,57 @@ export const fetchFileList = catchAsync(
export const deploy = catchAsync(
async (
req: Omit<Request, 'body'> & { body: deployBody },
res: Response
res: Response,
next: NextFunction
) => {
req.body.resourceType == 'Repository' && (await calculatePackages());
try {
req.body.resourceType == 'Repository' &&
(await calculatePackages(next));

// TODO Currently Deploy function will only work for workdir, we will add the addRepo
// TODO Currently Deploy function will only work for workdir, we will add the addRepo

await installDependencies();
await installDependencies();

const desiredPath = path.join(__dirname, '/worker/index.js');
const desiredPath = path.join(__dirname, '/worker/index.js');

const proc = spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});
const proc = spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});

proc.send({
type: protocol.l,
currentFile
});
proc.send({
type: protocol.l,
currentFile
});

proc.stdout?.on('data', (data: Buffer) => {
console.log(data.toString().green);
});
proc.stderr?.on('data', (data: Buffer) => {
console.log(data.toString().red);
});
proc.stdout?.on('data', (data: Buffer) => {
console.log(data.toString().green);
});
proc.stderr?.on('data', (data: Buffer) => {
console.log(data.toString().red);
});

proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
if (isIAllApps(data.data)) {
const appName = Object.keys(data.data)[0];
cps[appName] = proc;
allApplications[appName] = data.data[appName];
proc.on('message', (data: childProcessResponse) => {
if (data.type === protocol.g) {
if (isIAllApps(data.data)) {
const appName = Object.keys(data.data)[0];
cps[appName] = proc;
allApplications[appName] = data.data[appName];
}
}
}
});

res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
version: 'v1'
});
});

// Handle err == PackageError.Empty, use next function for error handling
res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
version: 'v1'
});
} catch (err) {
// Check if the error is PackageError.Empty
if (err === PackageError.Empty) {
return next(err);
}
return next(err);
}
}
);

Expand Down
6 changes: 4 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export const installDependencies = async (): Promise<void> => {
};

//check if repo contains metacall-*.json if not create and calculate runners then install dependencies
export const calculatePackages = async (): Promise<void> => {
export const calculatePackages = async (next: NextFunction): Promise<void> => {
const data = await generatePackage(currentFile.path);

if (data.error == PackageError.Empty) throw PackageError.Empty;
if (data.error == PackageError.Empty) {
return next(new Error(PackageError.Empty));
}
// currentFile.jsons = JSON.parse(data.jsons.toString()); FIXME Fix this line
currentFile.runners = data.runners;
};
Expand Down

0 comments on commit 97eaf35

Please sign in to comment.