Skip to content

Commit

Permalink
squash-me
Browse files Browse the repository at this point in the history
  • Loading branch information
ORESoftware committed Mar 22, 2020
1 parent 83b49b9 commit 59a9f38
Show file tree
Hide file tree
Showing 22 changed files with 736 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -6,7 +6,6 @@ coverage
.nyc_output
node_modules
logs
dist/*

### matching
*.log
Expand Down
8 changes: 8 additions & 0 deletions dist/.cprev.conf.js
@@ -0,0 +1,8 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
codeRoots: [
`${process.env.HOME}/codes`,
`${process.env.HOME}/go/src/github.com/channelmeter`
]
};
6 changes: 6 additions & 0 deletions dist/.readme.md
@@ -0,0 +1,6 @@

The `dist` directory is the target folder for transpiling TS source files
from the `src` dir.

The `dist/README.md` file can remain in version control, but all other files
in `dist/README.md` are by default ignored by git (you can change that by modifying .gitignore).
39 changes: 39 additions & 0 deletions dist/client/agent-tcp-server.js
@@ -0,0 +1,39 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const net = require("net");
const bunion_1 = require("bunion");
const json_stream_parser_1 = require("@oresoftware/json-stream-parser");
const doWrite = (s, v) => {
if (!s.writable) {
bunion_1.default.warn('3d6e661f-2c6f-4e6e-adeb-0c9729fc2df6: socket is not writable.');
return;
}
bunion_1.default.info("376aa75b-854e-4b67-99d7-55efafe8f7f3 writing payload:", v);
s.write(JSON.stringify({ val: v }) + '\n', 'utf8');
};
exports.connections = new Set();
exports.agentTcpServer = net.createServer(s => {
exports.connections.add(s);
s.once('error', e => {
bunion_1.default.error('08e3add4-b9ec-418e-b255-d0afd0a2ec50:', 'socket conn error: ', e);
s.removeAllListeners();
exports.connections.delete(s);
});
s.once('disconnect', () => {
bunion_1.default.info('19588471-b830-4f99-bfa0-0048cd3a905d:', 'connection disconnected.');
exports.connections.delete(s);
});
s.once('end', () => {
bunion_1.default.info('cc06fa58-519f-4743-b211-c826bd085b58:', 'connection ended.');
exports.connections.delete(s);
});
s.pipe(new json_stream_parser_1.default()).on('data', (d) => {
if (!(d.val && d.val.repo && typeof d.val.repo === 'string')) {
return doWrite(s, { error: 'missing repo' });
}
bunion_1.default.error('8fe8e800-1525-4870-930b-2eeec3c42fbd: no task matched type:', d.type);
doWrite(s, {
error: `fc892414-4e91-456c-a6ef-55ef1bfcc92c: no task matched type: '${d.type}'`
});
});
});
55 changes: 55 additions & 0 deletions dist/client/agent.js
@@ -0,0 +1,55 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const net = require("net");
const bunion_1 = require("bunion");
const c = require("../constants");
if (require.main === module) {
bunion_1.default.error('8224ed20-4d86-471f-9dae-e51d37b82cc8:', 'cannot run the agent.js file directly - use main.js.');
process.exit(1);
}
const cache = {
conn: null
};
exports.getConnection = () => {
return new Promise((resolve => {
if (cache.conn && cache.conn.writable) {
return resolve(cache.conn);
}
}));
};
const makeNewConnection = () => {
if (cache.conn) {
cache.conn.removeAllListeners();
cache.conn.destroy();
}
const conn = cache.conn = net.createConnection({
port: c.tcpServerPort,
host: c.tcpServerHost
});
conn.once('connect', () => {
bunion_1.default.info('agent connected to server via tcp:', c.httpServerHost, c.httpServerPort);
});
conn.once('error', e => {
bunion_1.default.error('0996b105-0a2d-4fa6-a67d-ff7853ff0133:', 'socket conn error: ', e);
conn.removeAllListeners();
setTimeout(() => {
makeNewConnection();
}, 2000);
});
conn.once('disconnect', () => {
conn.removeAllListeners();
bunion_1.default.info('996d5da1-1a73-40d7-a38d-043f13c8a5f1:', 'connection disconnected.');
setTimeout(() => {
makeNewConnection();
}, 10);
});
conn.once('end', () => {
conn.removeAllListeners();
bunion_1.default.info('d63a8250-a062-4117-bc2e-1f2d39295328:', 'connection ended.');
setTimeout(() => {
makeNewConnection();
}, 10);
});
return conn;
};
cache.conn = makeNewConnection();
87 changes: 87 additions & 0 deletions dist/client/get-watchable-dirs.js
@@ -0,0 +1,87 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const async = require("async");
const path = require("path");
const bunion_1 = require("bunion");
const fs = require("fs");
const constants_1 = require("../constants");
exports.flattenDeep = (a) => {
return a.reduce((acc, val) => Array.isArray(val) ? acc.concat(exports.flattenDeep(val)) : acc.concat(val), []);
};
let callable = true;
exports.getWatchableDirs = (config, cb) => {
if (!callable) {
throw 'Why call me twice?';
}
callable = false;
const paths = exports.flattenDeep([config.codeRoots]).map(v => path.resolve(v));
const uniquePaths = Array.from(new Set(paths));
const q = async.queue((task, cb) => task(cb), 15);
const alreadySeen = new Set();
const uniqueFolders = new Set();
const goThroughDir = (dir, isWatchingNow, relevantGitRepo) => {
if (alreadySeen.has(dir)) {
return;
}
alreadySeen.add(dir);
for (let ignore of constants_1.ignorePathsRegex) {
if (ignore.test(path.resolve(dir))) {
bunion_1.default.debug('path ignored:', dir);
return;
}
if (ignore.test(path.resolve(dir + '/'))) {
bunion_1.default.debug('path ignored:', dir);
return;
}
if (ignore.test(path.resolve(dir + '/') + '/')) {
bunion_1.default.debug('path ignored:', dir);
return;
}
}
q.push(cb => {
fs.stat(dir, (err, stats) => {
if (err) {
return cb(null);
}
if (!stats.isDirectory()) {
return cb(null);
}
try {
var cprevjs = require(path.resolve(dir + '/.cprev.js'));
bunion_1.default.info(dir, { cprevjs });
isWatchingNow = true;
}
catch (err) {
}
const potentialGitFolder = path.resolve(dir + '/.git');
fs.stat(potentialGitFolder, (err, stats) => {
if (stats && stats.isDirectory()) {
relevantGitRepo = potentialGitFolder;
}
if (isWatchingNow) {
uniqueFolders.add({ dirpath: dir, git_repo: relevantGitRepo });
}
fs.readdir(dir, (err, results) => {
cb(null);
if (err) {
bunion_1.default.warn('bb97184f-4a07-4acd-b7bc-59dc8e5fb0e4:', err);
return cb(null);
}
for (const v of results) {
goThroughDir(path.resolve(dir + '/' + v), isWatchingNow, relevantGitRepo);
}
});
});
});
});
};
q.error(err => {
bunion_1.default.error(err);
});
q.drain(() => {
cb(null, Array.from(uniqueFolders));
});
for (const p of uniquePaths) {
goThroughDir(p, false, path.normalize('/tmp/unknown/git/repo/path'));
}
};
56 changes: 56 additions & 0 deletions dist/client/main.js
@@ -0,0 +1,56 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const bunion_1 = require("bunion");
const fs = require("fs");
const constants_1 = require("../constants");
const path = require("path");
const agent_tcp_server_1 = require("./agent-tcp-server");
const get_watchable_dirs_1 = require("./get-watchable-dirs");
const _cprev_conf_js_1 = require("../.cprev.conf.js");
const watch_dirs_1 = require("./watch-dirs");
get_watchable_dirs_1.getWatchableDirs(_cprev_conf_js_1.default, (err, dirs) => {
if (err) {
bunion_1.default.error('4585a17b-a478-4ba0-beca-c0702d0983ea:', err);
process.exit(1);
}
bunion_1.default.error('2222', err, dirs);
if (!(dirs && dirs.length > 0)) {
bunion_1.default.error('cfe59e4a-8b5c-4cfd-ab7d-6fdec27e39a6:', `No folders to watch - add a ".cprev.js" file to folders within your "codeRoots" property in ".cprev.conf.js"`);
process.exit(1);
}
watch_dirs_1.watchDirs(dirs);
});
try {
fs.unlinkSync(constants_1.localAgentSocketPath);
}
catch (err) {
bunion_1.default.warn('Could not unlink file:', constants_1.localAgentSocketPath);
}
try {
fs.mkdirSync(path.dirname(constants_1.localAgentSocketPath), { recursive: true });
}
catch (err) {
bunion_1.default.error('Could not create dir:', path.dirname(constants_1.localAgentSocketPath));
process.exit(1);
}
agent_tcp_server_1.agentTcpServer.listen(constants_1.localAgentSocketPath, () => {
bunion_1.default.info('agent socket server listening on path:', constants_1.localAgentSocketPath);
});
process.once('SIGTERM', () => {
setTimeout(() => {
bunion_1.default.warn('server close timed out.');
process.exit(1);
}, 2000);
agent_tcp_server_1.agentTcpServer.close(() => {
process.exit(1);
});
});
process.once('SIGINT', () => {
setTimeout(() => {
bunion_1.default.warn('server close timed out.');
process.exit(1);
}, 2000);
agent_tcp_server_1.agentTcpServer.close(() => {
process.exit(1);
});
});
89 changes: 89 additions & 0 deletions dist/client/watch-dirs.js
@@ -0,0 +1,89 @@
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const bunion_1 = require("bunion");
const agent_1 = require("./agent");
const cp = require("child_process");
const path = require("path");
const doWrite = (s, v) => {
if (!s.writable) {
bunion_1.default.warn('44558c07-2b13-4f9c-9f3c-7e524e11fe07: socket is not writable.');
return;
}
bunion_1.default.info("fb224b51-bb55-45d3-aa46-8f3d2c6ce55d writing payload:", v);
s.write(JSON.stringify({ val: v }) + '\n', 'utf8');
};
let callable = true;
exports.watchDirs = (dirs) => {
if (!callable) {
return;
}
callable = false;
const timers = new Map();
console.log('dirs.length:', dirs.length);
for (const i of dirs) {
fs.watch(i.dirpath, (event, filename) => {
const fullPath = path.resolve(i.dirpath + '/' + filename);
bunion_1.default.info('filesystem event:', event, fullPath);
if (timers.has(fullPath)) {
clearTimeout(timers.get(fullPath));
}
timers.set(fullPath, setTimeout(() => {
if (i.git_repo) {
return agent_1.getConnection().then(v => {
doWrite(v, {
repo: i.git_repo,
file: fullPath,
user_email: 'alex@oresoftware.com',
user_name: 'alex'
});
});
}
const k = cp.spawn('bash');
k.stdin.end(`
cd "$(dirname "${filename}")" && git rev-parse --show-toplevel
`);
const d = {
stdout: '',
stderr: ''
};
k.stderr.on('data', d => {
d.stderr = String(d || '').trim();
});
k.stdout.on('data', d => {
d.stdout = String(d || '').trim();
});
k.once('exit', code => {
if (code && code > 0) {
bunion_1.default.error('33e68dd9-5842-41c2-83e0-65d43e68cb27: git/bash child process exited with non zero code.');
bunion_1.default.error('058b717c-94a3-49a9-b6e3-a162beb9d96b: stderr:', d.stderr);
return;
}
if (!(d.stdout && d.stdout.startsWith('/'))) {
bunion_1.default.warn('715b9f6e-7119-43ae-9a02-9b0c8c1f52dc: Not a filepath:', d.stdout);
return;
}
try {
var stats = fs.statSync(d.stdout);
}
catch (err) {
bunion_1.default.error('8781b643-b682-4e0b-a29b-931ce7df7376: Could not stat this path:', d.stdout);
return;
}
if (!stats.isDirectory()) {
bunion_1.default.error('954d918f-826f-4524-a7e0-683ea32e4208: The stats call says this is not a git dir:', d.stdout);
return;
}
agent_1.getConnection().then(v => {
doWrite(v, {
repo: i.git_repo,
file: filename,
user_email: 'alex@oresoftware.com',
user_name: 'alex'
});
});
});
}, 2500));
});
}
};

0 comments on commit 59a9f38

Please sign in to comment.