Skip to content

Commit

Permalink
Trying to improve more issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Apr 9, 2024
1 parent d51e339 commit 72802cb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
23 changes: 15 additions & 8 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
WorkerMessageUnknown,
allApplications,
childProcesses,
currentFile,
deleteBody,
deployBody,
fetchBranchListBody,
Expand All @@ -23,7 +22,6 @@ import {

import AppError from './utils/appError';
import {
calculatePackages,
catchAsync,
deleteRepoFolderIfExist,
dirName,
Expand Down Expand Up @@ -159,8 +157,9 @@ export const fetchFilesFromRepo = catchAsync(

const id = dirName(req.body.url);

currentFile['id'] = id;
currentFile.path = `${appsDir}/${id}`;
// TODO: This method is wrong
// currentFile['id'] = id;
// currentFile.path = `${appsDir}/${id}`;

return res.status(201).send({ id });
}
Expand Down Expand Up @@ -230,12 +229,20 @@ export const deploy = catchAsync(
next: NextFunction
) => {
try {
req.body.resourceType == 'Repository' &&
(await calculatePackages(next));

// TODO Currently Deploy function will only work for workdir, we will add the addRepo
// req.body.resourceType == 'Repository' &&
// (await calculatePackages(next));

console.log(req.body);

const currentFile: CurrentUploadedFile = {
id: '',
type: '',
path: '',
jsons: []
};

await installDependencies();
await installDependencies(currentFile);

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

Expand Down
8 changes: 0 additions & 8 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ export interface CurrentUploadedFile {
path: string;
}

export const currentFile: CurrentUploadedFile = {
id: '',
type: '',
jsons: [],
runners: [],
path: ''
};

export const createInstallDependenciesScript = (
runner: string,
path: string
Expand Down
10 changes: 8 additions & 2 deletions src/controller/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import busboy from 'busboy';
import { NextFunction, Request, Response } from 'express';
import { Extract } from 'unzipper';

import { currentFile, namearg } from '../constants';
import { CurrentUploadedFile, namearg } from '../constants';

import { MetaCallJSON } from '@metacall/protocol/deployment';
import AppError from '../utils/appError';
Expand All @@ -29,6 +29,12 @@ const getUploadError = (on: keyof busboy.BusboyEvents): AppError => {

export default (req: Request, res: Response, next: NextFunction): void => {
const bb = busboy({ headers: req.headers });
const currentFile: CurrentUploadedFile = {
id: '',
type: '',
path: '',
jsons: []
};

const handleError = (fn: () => void, on: keyof busboy.BusboyEvents) => {
try {
Expand Down Expand Up @@ -69,7 +75,7 @@ export default (req: Request, res: Response, next: NextFunction): void => {

bb.on('finish', () => {
handleError(() => {
const appLocation = path.join(appsDir, `${currentFile.id}`);
const appLocation = path.join(appsDir, currentFile.id);

fs.createReadStream(currentFile.path).pipe(
Extract({ path: appLocation })
Expand Down
12 changes: 7 additions & 5 deletions src/utils/autoDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
WorkerMessage,
WorkerMessageUnknown,
allApplications,
childProcesses,
currentFile
childProcesses
} from '../constants';
import { isIAllApps, logProcessOutput } from './utils';

Expand All @@ -26,9 +25,12 @@ export const findJsonFilesRecursively = async (
const desiredPath = path.join(__dirname, '../worker/index.js');
const id = path.basename(appsDir);

currentFile.id = id;
(currentFile.type = 'application/x-zip-compressed'),
(currentFile.path = appsDir);
const currentFile: CurrentUploadedFile = {
id,
type: 'application/x-zip-compressed',
path: appsDir,
jsons: []
};

const proc = spawn('metacall', [desiredPath, filePath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
Expand Down
13 changes: 9 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import { PackageError, generatePackage } from '@metacall/protocol/package';
import { NextFunction, Request, RequestHandler, Response } from 'express';

import {
CurrentUploadedFile,
IAllApps,
InspectObject,
PIDToColorCodeMap,
allApplications,
asniCode,
assignedColorCodes,
createInstallDependenciesScript,
currentFile
createInstallDependenciesScript
} from '../constants';
import { logger } from './logger';

export const dirName = (gitUrl: string): string =>
String(gitUrl.split('/')[gitUrl.split('/').length - 1]).replace('.git', '');

// Create a proper hashmap that contains all the installation commands mapped to their runner name and shorten this function
export const installDependencies = async (): Promise<void> => {
export const installDependencies = async (
currentFile: CurrentUploadedFile
): Promise<void> => {
if (!currentFile.runners) return;

for (const runner of currentFile.runners) {
Expand All @@ -37,7 +39,10 @@ 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 (next: NextFunction): Promise<void> => {
export const calculatePackages = async (
currentFile: CurrentUploadedFile,
next: NextFunction
): Promise<void> => {
const data = await generatePackage(currentFile.path);

if (data.error == PackageError.Empty) {
Expand Down

0 comments on commit 72802cb

Please sign in to comment.