index.ts 2.63 KB
Newer Older
1
import * as express from 'express';
2
import * as bodyParser from 'body-parser';
3
import * as cors from 'cors';
4
import * as fs from 'fs-extra';
5
import * as GitClient from 'isomorphic-git';
6
import * as path from 'path';
7 8 9 10 11
import * as GitServer from 'node-git-server';

if (process.platform === 'win32') {
	throw new Error('Use WSL for now. See README. Also http://www.michaelfcollins3.me/blog/2012/05/18/implementing-a-git-http-server.html');
}
12 13 14 15

function getPathForName(name) {
	return path.join('../../data', name);
}
16

17 18
const apiPort = 4000;
const apiServer = express();
19
const jsonParser = bodyParser.json();
20

21
apiServer.use(cors());
22

23 24
apiServer.listen(apiPort, () => {
	console.log(`Listening on ${apiPort} for API…`);
25 26
});

27
apiServer.get('/repos', async (request, response) => {
28 29 30 31
	const repos = (await fs.readdir('../../data')).map(path => ({ id: path, name: path }));
	response.json({ repos });
});

32
apiServer.post('/create', jsonParser, async (request, response) => {
33 34
	const path = getPathForName(request.body.name);
	await fs.mkdirp(path);
35
	await GitClient.init({ fs, dir: path });
36 37 38
	response.json({});
});

39
apiServer.post('/delete', jsonParser, async (request, response) => {
40 41 42
	const path = getPathForName(request.body.id);
	await fs.remove(path);
	response.json({});
43
});
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
apiServer.get('/issues/:name', async (request, response) => {
	const { name } = request.params;
	const path = getPathForName(name) + '/todo';
	await fs.ensureDir(path);
	const issues = [];
	for (const p of await fs.readdir(path)) {
		const { atime, ctime } = await fs.stat(path + '/' + p);
		console.log(atime, ctime);
		issues.push({ accessed: atime, changed: ctime, name: p });
	}

	response.json({ name, issues });
});

// TODO: Make this a tree recursively.
apiServer.get('/wikis/:name', async (request, response) => {
	const { name } = request.params;
	const path = getPathForName(name) + '/wiki';
	await fs.ensureDir(path);
	const wikis = [];
	for (const p of await fs.readdir(path)) {
		const { atime, ctime } = await fs.stat(path + '/' + p);
		console.log(atime, ctime);
		wikis.push({ accessed: atime, changed: ctime, name: p });
	}

	response.json({ name, wikis });
});

74
const gitPort = 7000;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
const gitServer = new GitServer('../../data', { autoCreate: false, authenticate: (type, repo, user, next) => next() });

gitServer.on('push', (push) => {
	console.log(`push ${push.repo}/${push.commit} (${push.branch})`);
	push.accept();
});

gitServer.on('fetch', (fetch) => {
	console.log(`fetch ${fetch.commit}`);
	fetch.accept();
});

gitServer.listen(gitPort, () => {
	console.log(`Listening on ${gitPort} for Git…`);
});