Skip to content

Commit fe14cef

Browse files
committed
#4 = main + start of server
1 parent 0c86d17 commit fe14cef

5 files changed

Lines changed: 98 additions & 60 deletions

File tree

__tests__/main.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as server from '../src/server';
2+
3+
describe('main', () => {
4+
it('should start the server', () => {
5+
jest.spyOn(server, 'startServer').mockImplementation(() => ({}));
6+
// tslint:disable-next-line:no-require-imports
7+
require('../src/main');
8+
expect(server.startServer).toBeCalled();
9+
});
10+
});

__tests__/server.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {get, Server} from 'http';
2+
import {startServer} from '../src/server';
3+
4+
describe('main', () => {
5+
const port = 15523;
6+
const baseUrl = `http://localhost:${port}`;
7+
let server: Server;
8+
9+
beforeAll(() => {
10+
server = startServer(port);
11+
});
12+
13+
afterAll(() => {
14+
server.close();
15+
})
16+
17+
it('/', done => {
18+
get(baseUrl, res => {
19+
expect(res.statusCode).toBe(200);
20+
done();
21+
});
22+
});
23+
});

__tests__/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {readFileSync} from 'fs';
2-
import { join } from 'path';
2+
import {join} from 'path';
33
import {directory} from 'tempy';
44
import {writeFileDeepSync} from '../src/utils';
55

src/main.ts

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,3 @@
1-
import * as bodyParser from 'body-parser';
2-
import { execSync, spawnSync } from 'child_process';
3-
import * as express from 'express';
4-
import { join } from 'path';
5-
import{ directory } from 'tempy';
6-
import { writeFileDeepSync } from './utils';
1+
import {startServer} from './server';
72

8-
const app = express();
9-
const port = process.env.PORT || 3000;
10-
11-
app.use(bodyParser.json());
12-
13-
app.get('/', (_req, res) => res.send('Hello World!'))
14-
15-
app.post('/commit', (req, res) => {
16-
const { remoteRepo, token, files, commitMessage } = req.body as {[key: string]: string | IFile[]};
17-
const [, repoName] = (<string>remoteRepo).split('/');
18-
19-
const tempFolder = directory();
20-
21-
const options = (subFolder: string = repoName) => ({
22-
encoding: 'utf8',
23-
cwd: join(tempFolder, subFolder)
24-
});
25-
26-
const {output} = spawnSync('git', ['clone', `https://${token}@github.com/${remoteRepo}.git`], options(''));
27-
console.log(output[2]);
28-
29-
(<IFile[]>files).forEach((file: IFile) => {
30-
writeFileDeepSync(join(tempFolder, repoName, file.path), file.content);
31-
});
32-
33-
console.log('> git add')
34-
execSync('git add .', options());
35-
36-
console.log('> git status')
37-
spawnSync('git', ['status'], {...options(), stdio:'inherit'});
38-
39-
execSync('git config user.email "bot@spread-the-code.com"', options());
40-
execSync('git config user.name "commit-bot"', options());
41-
42-
console.log('> git commit')
43-
execSync(`git commit -m "${commitMessage || 'unknow commit'}"`, {...options(), stdio:'inherit'});
44-
45-
console.log('> git push');
46-
try {
47-
const pushResult = spawnSync('git', ['push'], options());
48-
console.log(pushResult.output);
49-
} catch (error) {
50-
console.log('error', error);
51-
}
52-
res.send('done');
53-
});
54-
55-
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
56-
57-
interface IFile {
58-
path: string;
59-
content: string;
60-
}
3+
startServer();

src/server.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as bodyParser from 'body-parser';
2+
import {execSync, spawnSync} from 'child_process';
3+
import * as express from 'express';
4+
import {Server} from 'http';
5+
import {join} from 'path';
6+
import{directory} from 'tempy';
7+
import {writeFileDeepSync} from './utils';
8+
9+
export function startServer(port = process.env.PORT || 3000): Server {
10+
const app = express();
11+
12+
app.use(bodyParser.json());
13+
14+
app.get('/', (_req, res) => res.send('Hello World!'))
15+
16+
app.post('/commit', (req, res) => {
17+
const { remoteRepo, token, files, commitMessage } = req.body as {[key: string]: string | IFile[]};
18+
const [, repoName] = (<string>remoteRepo).split('/');
19+
20+
const tempFolder = directory();
21+
22+
const options = (subFolder: string = repoName) => ({
23+
encoding: 'utf8',
24+
cwd: join(tempFolder, subFolder)
25+
});
26+
27+
const {output} = spawnSync('git', ['clone', `https://${token}@github.com/${remoteRepo}.git`], options(''));
28+
console.log(output[2]);
29+
30+
(<IFile[]>files).forEach((file: IFile) => {
31+
writeFileDeepSync(join(tempFolder, repoName, file.path), file.content);
32+
});
33+
34+
console.log('> git add')
35+
execSync('git add .', options());
36+
37+
console.log('> git status')
38+
spawnSync('git', ['status'], {...options(), stdio:'inherit'});
39+
40+
execSync('git config user.email "bot@spread-the-code.com"', options());
41+
execSync('git config user.name "commit-bot"', options());
42+
43+
console.log('> git commit')
44+
execSync(`git commit -m "${commitMessage || 'unknow commit'}"`, {...options(), stdio:'inherit'});
45+
46+
console.log('> git push');
47+
try {
48+
const pushResult = spawnSync('git', ['push'], options());
49+
console.log(pushResult.output);
50+
} catch (error) {
51+
console.log('error', error);
52+
}
53+
res.send('done');
54+
});
55+
56+
return app.listen(port, () => console.log(`Example app listening on port ${port}!`))
57+
}
58+
59+
interface IFile {
60+
path: string;
61+
content: string;
62+
}

0 commit comments

Comments
 (0)