Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix: pr-33 warnings and scripts loading error #42

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import AppError from './utils/appError';
import {
calculatePackages,
catchAsync,
deleteRepoFolderIfExist,
dirName,
ensureFolderExists,
execPromise,
exists,
getCorrectNameWithVersion,
installDependencies,
isIAllApps
isIAllApps,
listDirectoriesWithPrefix
} from './utils/utils';

import { appsDirectory } from './utils/config';
Expand Down Expand Up @@ -124,34 +125,26 @@ export const fetchFiles = (
export const fetchFilesFromRepo = catchAsync(
async (
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
res: Response,
next: NextFunction
res: Response
) => {
const { branch, url } = req.body;

await ensureFolderExists(appsDir);

try {
deleteRepoFolderIfExist(appsDir, url);
} catch (err) {
next(
new AppError(
'error occurred in deleting repository directory',
500
)
);
}
const repoName = dirName(url);

const appsWithSameName = listDirectoriesWithPrefix(repoName, appsDir);

const appName = getCorrectNameWithVersion(repoName, appsWithSameName);

await execPromise(
`cd ${appsDir}; git clone --single-branch --depth=1 --branch ${branch} ${url} `
`cd ${appsDir}; git clone --single-branch --depth=1 --branch ${branch} ${url} ${appName}`
);

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

currentFile['id'] = id;
currentFile.path = `${appsDir}/${id}`;
currentFile['id'] = appName;
currentFile.path = `${appsDir}/${appName}`;

res.status(201).send({ id });
res.status(201).send({ appName });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, the response must contain id

It should be like

id:appName

}
);

Expand Down Expand Up @@ -181,26 +174,20 @@ export const fetchBranchList = catchAsync(
export const fetchFileList = catchAsync(
async (
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
res: Response,
next: NextFunction
res: Response
) => {
await ensureFolderExists(appsDir);
const repoName = dirName(req.body.url);

const appsWithSameName = listDirectoriesWithPrefix(repoName, appsDir);

const appName = getCorrectNameWithVersion(repoName, appsWithSameName);

try {
deleteRepoFolderIfExist(appsDir, req.body.url);
} catch (err) {
next(
new AppError(
'error occurred in deleting repository directory',
500
)
);
}
await execPromise(
`cd ${appsDir} ; git clone ${req.body.url} --depth=1 --no-checkout`
`cd ${appsDir} ; git clone ${req.body.url} ${appName} --depth=1 --no-checkout`
);

const dirPath = `${appsDir}/${dirName(req.body.url)}`;
const dirPath = `${appsDir}/${appName}`;

const { stdout } = await execPromise(
`cd ${dirPath} ; git ls-tree -r ${req.body.branch} --name-only; cd .. ; rm -r ${dirPath}`
Expand Down
2 changes: 1 addition & 1 deletion src/controller/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { deleteStatusMessage } from '../utils/resposeTexts';
export default (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response
) => {
): Response => {
// Extract the suffix (application name) of the application from the request body
const { suffix: app } = req.body;

Expand Down
15 changes: 15 additions & 0 deletions src/controller/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { currentFile, namearg } from '../constants';
import { MetaCallJSON } from '@metacall/protocol/deployment';
import AppError from '../utils/appError';
import { appsDirectory } from '../utils/config';
import {
getCorrectNameWithVersion,
listDirectoriesWithPrefix
} from '../utils/utils';

const appsDir = appsDirectory();

Expand Down Expand Up @@ -79,6 +83,17 @@ export default (req: Request, res: Response, next: NextFunction): void => {

bb.on('finish', () => {
handleError(() => {
const appsWithSameName = listDirectoriesWithPrefix(
currentFile.id,
appsDir
);

const appName = getCorrectNameWithVersion(
currentFile.id,
appsWithSameName
);
currentFile['id'] = appName;

const appLocation = path.join(appsDir, `${currentFile.id}`);

fs.createReadStream(currentFile.path).pipe(
Expand Down
41 changes: 40 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ export const diff = (object1: any, object2: any): any => {
return object1; // eslint-disable-line
};

export function isIAllApps(data: any): data is IAllApps {
export function isIAllApps(data: unknown): data is IAllApps {
return typeof data === 'object' && data !== null;
}

export const listDirectoriesWithPrefix = (
searchString: string,
appsDir: string
): string[] => {
try {
const filteredDirectories = fs
.readdirSync(appsDir)
.filter(
file =>
fs.statSync(join(appsDir, file)).isDirectory() &&
file.startsWith(searchString)
);
return filteredDirectories;
} catch (err) {
console.error('Error reading directory:', err);
return [];
}
};

export const isArrEmpty = <T>(arr: T[]): boolean => arr.length === 0;

export const getCorrectNameWithVersion = (
appName: string,
appsWithSameName: string[]
): string => {
if (isArrEmpty(appsWithSameName)) {
return appName + '-v1';
}
const lastApp = appsWithSameName[appsWithSameName.length - 1];
const regex = /-v(\d+)$/;
const match = regex.exec(lastApp);
if (match) {
const versionNumber = parseInt(match[1]) + 1;
return currentFile.id + '-v' + versionNumber.toString();
}

return '';
};
Loading