Skip to content

Commit

Permalink
add clone api
Browse files Browse the repository at this point in the history
  • Loading branch information
oz-keisar-d committed Aug 20, 2024
1 parent 941b322 commit 8849d05
Show file tree
Hide file tree
Showing 14 changed files with 469 additions and 214 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM mhart/alpine-node
FROM timbru31/node-alpine-git

COPY docker/* .

EXPOSE 1512
EXPOSE 1512 3000-3050

CMD [ "npm", "start" ]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build:dll": "cross-env NODE_ENV=development TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.ts",
"build:main": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.main.prod.ts",
"build:renderer": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.renderer.prod.ts",
"build:backend": "cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.backend.ts",
"build:backend": "npm run generateSwagger && cross-env NODE_ENV=production TS_NODE_TRANSPILE_ONLY=true webpack --config ./.erb/configs/webpack.config.backend.ts",
"postinstall": "ts-node .erb/scripts/check-native-dep.js && electron-builder install-app-deps && npm run build:dll",
"lint": "cross-env NODE_ENV=development eslint . --ext .js,.jsx,.ts,.tsx",
"package": "ts-node ./.erb/scripts/clean.js dist && npm run build && electron-builder build --publish never && npm run build:dll",
Expand Down
84 changes: 84 additions & 0 deletions src/backend/actions/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { v4 as uuid } from 'uuid';
import { execSync } from 'child_process';
import path from 'path';
import { Socket, Server as SocketIOServer } from 'socket.io';
import {
addCredentialsToGitURI,
createExampleProject,
createProjectPath,
getProjectsNameList,
isDirectoryEmpty,
readAppSettings,
updateAppSettings,
verifyProjectFoldersExist,
} from '../utils';
import { EVENT_KEYS } from '../../types/events';
import { updateClientProjectData } from '../utils/events';
import { emitSocketMessage } from '../socket';

type CreateProjectType = {
sshUrl?: string;
httpsUrl?: string;
cloneType: 'OPEN' | 'SSH' | 'HTTPS' | 'LOCAL';
username?: string;
password?: string;
projectName: string;
directoryPath?: string;
};

export const createProject = async (
socket: Socket | SocketIOServer,
{
sshUrl,
httpsUrl,
cloneType,
username,
password,
projectName,
directoryPath,
}: CreateProjectType,
) => {
await verifyProjectFoldersExist();

const projectPath =
cloneType === 'OPEN' ? directoryPath || '' : createProjectPath(projectName);

const appSettings = await readAppSettings();
const newProject = {
id: uuid(),
name: projectName,
directoryPath: projectPath,
};
await updateAppSettings({
...appSettings,
projects: [...(appSettings.projects || []), newProject],
});

if (cloneType === 'SSH') {
await execSync(`git clone ${sshUrl} ${projectPath}`, {

Check failure

Code scanning / CodeQL

Uncontrolled command line Critical

This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: path.resolve('', ''), // path to where you want to save the file
});
} else if (cloneType === 'HTTPS' && httpsUrl && username && password) {
const updatedURI = addCredentialsToGitURI(httpsUrl, username, password);

await execSync(`git clone ${updatedURI} ${projectPath}`, {

Check failure

Code scanning / CodeQL

Uncontrolled command line Critical

This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
This command line depends on a
user-provided value
.
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: path.resolve('', ''), // path to where you want to save the file
});
}

if (await isDirectoryEmpty(projectName)) {
await createExampleProject(projectName);
}

const newProjectsNameList = await getProjectsNameList();

await updateClientProjectData(socket, projectName);

emitSocketMessage(socket, EVENT_KEYS.CREATE_PROJECT, {
success: true,
newProjectsNameList,
newProjectName: projectName,
});
};
56 changes: 9 additions & 47 deletions src/backend/events/general.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { v4 as uuid } from 'uuid';
import { execSync } from 'child_process';
import path from 'path';
import { Socket } from 'socket.io';
import { checkIServerUp, closeProjectServers } from '../server';
import {
activateProgram,
addCredentialsToGitURI,
createExampleProject,
createProjectPath,
getActiveProjectName,
getProjectPath,
getProjectsNameList,
isDirectoryEmpty,
readAppSettings,
updateAppSettings,
verifyProjectFoldersExist,
Expand All @@ -20,6 +14,7 @@ import { generateUniqueIdentifier } from '../utils/utils';
import { EVENT_KEYS } from '../../types/events';
import { updateClientProjectData } from '../utils/events';
import { emitSocketMessage } from '../socket';
import { createProject } from '../actions/general';

export const generalEvents = (socket: Socket) => {
socket.on(EVENT_KEYS.INIT, async () => {
Expand Down Expand Up @@ -68,48 +63,15 @@ export const generalEvents = (socket: Socket) => {
projectName,
directoryPath,
} = arg;
await verifyProjectFoldersExist();

const projectPath =
cloneType === 'OPEN' ? directoryPath : createProjectPath(projectName);

const appSettings = await readAppSettings();
const newProject = {
id: uuid(),
name: projectName,
directoryPath: projectPath,
};
await updateAppSettings({
...appSettings,
projects: [...(appSettings.projects || []), newProject],
});

if (cloneType === 'SSH') {
await execSync(`git clone ${sshUrl} ${projectPath}`, {
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: path.resolve('', ''), // path to where you want to save the file
});
} else if (cloneType === 'HTTPS') {
const updatedURI = addCredentialsToGitURI(httpsUrl, username, password);

await execSync(`git clone ${updatedURI} ${projectPath}`, {
stdio: [0, 1, 2], // we need this so node will print the command output
cwd: path.resolve('', ''), // path to where you want to save the file
});
}

if (await isDirectoryEmpty(projectName)) {
await createExampleProject(projectName);
}

const newProjectsNameList = await getProjectsNameList();

await updateClientProjectData(socket, projectName);

emitSocketMessage(socket, EVENT_KEYS.CREATE_PROJECT, {
success: true,
newProjectsNameList,
newProjectName: projectName,
createProject(socket, {
sshUrl,
httpsUrl,
cloneType,
username,
password,
projectName,
directoryPath,
});
} catch (error) {
console.log('Error create project ', error);
Expand Down
60 changes: 0 additions & 60 deletions src/backend/routers/actions.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/backend/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { routesRouter } from './routes';
import { serverRouter } from './server';
import { parentsRouter } from './parents';
import { appDataRouter } from './appData';
import { actionsRouter } from './actions';
import { projectRouter } from './project';
import { serverLogsRouter } from './serverLogs';

export const routes = express.Router();

routes.use('/actions', actionsRouter);
routes.use('/presets', presetsRouter);
routes.use('/routes', routesRouter);
routes.use('/servers', serverRouter);
routes.use('/parents', parentsRouter);
routes.use('/project', projectRouter);
routes.use('/app-data', appDataRouter);
routes.use('/server-logs', serverLogsRouter);
95 changes: 95 additions & 0 deletions src/backend/routers/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Router, Request, Response } from 'express';
import { logger } from '../utils/logger';
import { getGlobalSocketIo } from '../socket';
import { createProject } from '../actions/general';

const projectRouter = Router();

projectRouter.post('/clone/ssh', async (req: Request, res: Response) => {
try {
const { projectName, sshUrl } = req.body;

const socket = getGlobalSocketIo();

await createProject(socket, {
projectName,
sshUrl,
cloneType: 'SSH',
});

res.status(200).send({ success: true });
} catch (error: any) {
console.log(error);
logger('Error restart server', error?.message);

res
.status(500)
.send({ success: false, message: 'fail to ssh clone the project' });
}
});

projectRouter.post('/clone/https', async (req: Request, res: Response) => {
try {
const { projectName, httpsUrl, username, password } = req.body;

const socket = getGlobalSocketIo();

await createProject(socket, {
projectName,
httpsUrl,
username,
password,
cloneType: 'HTTPS',
});

res.status(200).send({ success: true });
} catch (error: any) {
console.log(error);
logger('Error restart server', error?.message);

res.status(500).send({ success: false, message: 'fail to restart server' });
}
});

projectRouter.post('/open', async (req: Request, res: Response) => {
try {
const { projectName, directoryPath } = req.body;

const socket = getGlobalSocketIo();

await createProject(socket, {
projectName,
directoryPath,
cloneType: 'OPEN',
});

res.status(200).send({ success: true });
} catch (error: any) {
console.log(error);
logger('Error restart server', error?.message);

res.status(500).send({ success: false, message: 'fail to restart server' });
}
});

projectRouter.post('/create', async (req: Request, res: Response) => {
try {
const { projectName } = req.body;

const socket = getGlobalSocketIo();

await createProject(socket, {
projectName,
cloneType: 'LOCAL',
});

res.status(200).send({ success: true });
} catch (error: any) {
console.log(error);
logger('Error restart server', error?.message);

res.status(500).send({ success: false, message: 'fail to restart server' });
}
});

export { projectRouter };
Loading

0 comments on commit 8849d05

Please sign in to comment.