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

Impact Analysis #199

Merged
merged 8 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 16 additions & 0 deletions src/components/BuildButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { Button } from '@material-ui/core';
import runBuild from '../containers/runBuild';

const BuildButton: React.FunctionComponent = () => {
const handleClick = async (e: React.MouseEvent) => {
e.preventDefault();
await runBuild("https://github.com/photonstorm/phaser.git", "C:\\Users\\15034\\Desktop\\localPhaser", "C:\\Users\\15034\\Desktop\\phaser2");
};

return (
<Button id='newcard-button' variant='contained' color='primary' onClick={(e) => { handleClick(e) }}>Run Build...</Button>
);
};

export default BuildButton;
2 changes: 2 additions & 0 deletions src/components/CanvasComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { loadStack } from '../containers/handlers';
import ErrorDialog from './ErrorDialog';
import VersionStatusButton from './RepoBranchList';
import MergePickerButton from './MergePickerDialog';
import BuildButton from './BuildButton';

const CanvasComponent: React.FunctionComponent<Canvas> = props => {
const cards = useSelector((state: RootState) => state.cards);
Expand Down Expand Up @@ -85,6 +86,7 @@ const CanvasComponent: React.FunctionComponent<Canvas> = props => {

return (
<div className='canvas' ref={drop}>
<BuildButton />
<NewCardButton />
<FilePickerButton />
<BrowserButton />
Expand Down
86 changes: 86 additions & 0 deletions src/containers/runBuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { exec } from 'child_process';
import { clone, statusMatrix, commit } from 'isomorphic-git';
import * as fs from 'fs-extra';
import * as http from 'isomorphic-git/http/node';
import { join } from 'path';

import { getRepoRoot } from '../containers/git';

// Helper function that copies a file and moves it to a new directory
const copyFile = (src: string, filePath: string, dest: string) => {
const sPath = join(src, filePath);
const dPath = join(dest, filePath);
fs.copyFile(sPath, dPath);
}

const runBuild = async (remoteRepoURL: string, localRepo: fs.PathLike, copiedRepo: fs.PathLike): Promise<void> => {
// Clone the remote repo to .syn directory within the copied repo's root dir
const copiedRepoRoot = `${await getRepoRoot(copiedRepo)}\\.syn`;
await clone({
fs,
http,
dir: copiedRepoRoot,
corsProxy: 'https://cors.isomorphic-git.org',
url: remoteRepoURL,
singleBranch: true,
depth: 1,
});
console.log("\nFinished cloning!\n");

// Get the staged files in the local repo
const FILE = 0, WORKDIR = 2, STAGE = 3

const stagedFiles = (await statusMatrix({ fs, dir: localRepo.toString() }))
.filter(row => row[WORKDIR] === 2 && row[STAGE] === 2)
.map(row => row[FILE]);
console.log(`\nStaged files: ${stagedFiles}\n`);

// Copy the local staged files to the cloned repo
stagedFiles.map((file) => {
copyFile(localRepo.toString(), file, copiedRepoRoot);
});

// Create a commit with these staged changes
const com = await commit({
fs,
dir: copiedRepoRoot,
author: {
name: 'Mr. Test',
email: 'mrtest@example.com',
},
message: `Made changes to files ${stagedFiles}`,
});
console.log(`\nCommit: ${com}\n`);

// Run npm install
const install = exec('npm install', { cwd: copiedRepoRoot }, (error, stdout, stderr) => {
if (error) {
console.log(`\n(install) Error stack:\n${error.stack}\n`);
console.log(`\n(install) Error code: ${error.code}\n`);
console.log(`\n(install) Signal received: ${error.signal}\n`);
}
console.log(`\n(install) Child Process STDOUT: ${stdout}\n`);
console.log(`\n(install) Child Process STDERR: ${stderr}\n`);
});

// Run the build
install.on('exit', (code) => {
console.log(`\n(run-script build) Child process exited with exit code: ${code}\n`);

const portfolio = exec('npm run-script build', { cwd: copiedRepoRoot }, (error, stdout, stderr) => {
if (error) {
console.log(`\n(run-script build) Error stack:\n${error.stack}\n`);
console.log(`\n(run-script build) Error code: ${error.code}\n`);
console.log(`\n(run-script build) Signal received: ${error.signal}\n`);
}
console.log(`\n(run-script build) Child Process STDOUT: ${stdout}\n`);
console.log(`\n(run-script build) Child Process STDERR: ${stderr}\n`);
});

portfolio.on('exit', (code) => {
console.log(`\n(run-script build) Child process exited with exit code: ${code}\n`);
});
});
}

export default runBuild;