Skip to content
This repository was archived by the owner on Mar 22, 2020. It is now read-only.

Commit 10c6b7f

Browse files
committed
auto-refresh every hour: bin/refresh.js
1 parent 6f77fba commit 10c6b7f

7 files changed

Lines changed: 77 additions & 69 deletions

File tree

bin/clean.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
const config = require("../config");
3+
const fse = require('fs-extra');
4+
5+
fse.removeSync(config.repoRoot);
6+
fse.mkdirsSync(config.repoRoot);

bin/init.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

bin/refresh.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const config = require("../config");
2+
const fse = require('fs-extra');
3+
const path = require('path');
4+
const run = require('../lib/run');
5+
const execSync = require('child_process').execSync;
6+
const Stats = require('../lib/stats');
7+
const debug = require('debug')('init');
8+
const updateRepo = require('../lib/updateRepo');
9+
10+
(async function () {
11+
for (let repoName in config.secret.repos) {
12+
let repoConfig = config.secret.repos[repoName];
13+
14+
await updateRepo(repoName);
15+
}
16+
17+
for (let repoName in config.secret.repos) {
18+
let repoConfig = config.secret.repos[repoName];
19+
20+
if (repoConfig.lang != 'en') {
21+
await Stats.instance().count(repoName);
22+
}
23+
}
24+
25+
})().catch(console.error);

handlers/hook.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const crypto = require('crypto');
22
const config = require('../config');
33
const debug = require('debug')('handlers:hook');
4-
const onUpdateHook = require('../lib/onUpdateHook');
4+
const updateRepo = require('../lib/updateRepo');
55

66
exports.post = async function(ctx) {
77

@@ -56,6 +56,8 @@ exports.post = async function(ctx) {
5656
ctx.body = {ok: true};
5757

5858
// don't wait for it! (async)
59-
onUpdateHook(ctx.request.body);
59+
await updateRepo(ctx.request.body.repository.name);
60+
61+
await Stats.instance().count(ctx.request.body.repository.name);
6062

6163
};

lib/onUpdateHook.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

lib/run.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
const debug = require('debug')('run');
3+
const exec = require('mz/child_process').exec;
4+
5+
module.exports = async function run(cmd, options = {}) {
6+
if (!options.encoding) {
7+
options.encoding = 'utf-8';
8+
}
9+
10+
debug(cmd, options);
11+
let result = await exec(cmd, options);
12+
debug(result);
13+
14+
return result;
15+
};

lib/updateRepo.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const config = require("../config");
2+
const path = require("path");
3+
const run = require('./run');
4+
const fs = require('fs');
5+
6+
module.exports = async function (repoName) {
7+
let repoDir = path.join(config.repoRoot, path.basename(repoName));
8+
9+
let repoConfig = config.secret.repos[repoName];
10+
11+
let branch = repoConfig.branch || 'master';
12+
13+
if (fs.existsSync(repoDir)) {
14+
15+
await run(`git fetch origin`, {cwd: repoDir});
16+
17+
} else {
18+
19+
await run(`git clone --depth 1 https://github.com/${repoName}.git "${repoDir}"`);
20+
21+
}
22+
23+
await run(`git checkout -f origin/${branch}`, {cwd: repoDir});
24+
25+
};
26+
27+

0 commit comments

Comments
 (0)