-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
151 lines (138 loc) · 4.65 KB
/
index.mjs
File metadata and controls
151 lines (138 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* eslint no-console:off */
import { setOptions, options } from './cmdline.mjs';
import { basename } from 'node:path';
import { promisify } from 'node:util';
import { exec } from 'node:child_process';
const _exec = promisify(exec);
import { DateTime } from 'luxon';
import repos from '../common/repos.js';
const { allRepoPaths } = repos;
const DateLength = 6;
setOptions();
/**
* Creates command string
* @param {String} repo - repository base name
* @return {String}
* @NOTE git will walk up the parents looking for a repository
* @private
*/
const gitCommand = (repo) => {
return `git --no-pager -C ${repo} log --walk-reflogs --format="%gd %h %d %gs +++" --date=format:"%Y-%m-%d %H:%M:%S %p=="`;
};
/**
* determines if item falls within range
*
* @param {Object} item
* @param {DateTime | undefined} item.fromDate
* @param {DateTime | undefined} item.toDate
* @returns {Boolean}
* @private
*
*/
const filterPeriod = (item) => {
let result;
if(!options.fromDate && !options.toDate) {
result = true;
}
else if(options.fromDate && !options.toDate) {
result = item.date >= options.fromDate;
}
else if(!options.fromDate && options.toDate) {
result = item.date <= options.toDate;
}
else {
result = item.date >= options.fromDate && item.date <= options.toDate;
}
return result;
};
/**
* Obtains the git reflogs result
* @param {String} repo - full path to a repository
* @param {Array} errors - place to store skippable errors
* @return {Array} objects containing date, body, and the repository base name
*/
const processRepo = (repo, errors) => {
return new Promise((resolve) => {
const cmd = gitCommand(repo);
_exec(cmd, { encoding:'utf8' })
.then(out => out.stdout.trim())
.then(stdout => {
const results = stdout.split(' +++')
.filter(item => {
return item.trim().length > 0;
})
.map(item => {
return item.trim();
})
.map(item => {
const date = DateTime.fromFormat(item.substring(DateLength, item.search(/[=]{2}/)), options.dateOptions);
const body = item.substring(item.search(/[=]{2}/) + options.offset);
return { date, body, repo: basename(repo) };
})
.filter(filterPeriod);
return resolve(results);
})
.catch(err => {
errors.push({ repo: repo, error: err });
// continue to next repo but be sure to return empty array
return resolve([]);
});
});
};
/**
* writes errors to console if in debug mode
* @param {Array} errors - collection of error objects
* @param {Boolean} isDebug - command line flag
* @param {*} err - catch all error not otherwise specified
*/
const logErrors = (errors, isDebug, err) => {
if(isDebug > 0 && errors.length > 0) {
console.error(`Errors Reported: ${errors.length}`);
errors.forEach((item, i) => {
console.error(`${i + 1}. ${item.repo}: ${item.error.trim()}`);
});
}
if(err) {
console.error(`Misc error: ${err}`);
}
};
/**
* Entry point
*/
const main = () => {
if(options.devRoot.length) {
const errors = [];
let maxRepoLength = 0;
const repos = [];
options.devRoot.forEach(root => {
allRepoPaths(root, options.folderNames).forEach(repo => {
repos.push(repo);
});
});
const promises = repos.map(repo => processRepo(repo, errors));
return Promise
.all(promises)
.then(results => {
results
.reduce((acc, item) => acc.concat(item), [])
.sort((a, b) => a.date.valueOf() - b.date.valueOf())
.map(item => {
if(item.repo.length > maxRepoLength) {
maxRepoLength = item.repo.length;
}
return item;
})
.map(item => {
let name = item.repo;
name = name.padEnd(maxRepoLength);
console.log(`${item.date.toFormat(options.dateOptions)} ${name} ${item.body}`);
});
})
.catch(err => {
logErrors(errors, options.debug, err);
});
}
console.log(`bash variable DEVROOT is required`);
process.exitCode = 1;
};
main().catch(console.error);