forked from sindresorhus/awesome-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-repo-age.js
More file actions
74 lines (61 loc) · 2.08 KB
/
Copy pathgit-repo-age.js
File metadata and controls
74 lines (61 loc) · 2.08 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
import {execa} from 'execa';
import {lintRule} from 'unified-lint-rule';
import {fetchGitHubData} from '../lib/github-api.js';
const millisecondsPerDay = 24 * 60 * 60 * 1000;
const minimumRepositoryAgeDays = 30;
function formatRepositoryAgeMessage(repositoryAgeMilliseconds) {
const daysOld = Math.floor(repositoryAgeMilliseconds / millisecondsPerDay);
const daysLeft = minimumRepositoryAgeDays - daysOld;
if (daysLeft <= 0) {
return; // Repository is old enough
}
const daysText = daysLeft === 1 ? 'day' : 'days';
return `Repository is ${daysOld} days old, but must be at least ${minimumRepositoryAgeDays} days old `
+ `(${daysLeft} ${daysText} left). New repositories need time to mature before being added `
+ 'to the main awesome list.';
}
function checkRepositoryAge(repositoryDate, file) {
const now = new Date();
const repositoryAgeMilliseconds = now - repositoryDate;
const message = formatRepositoryAgeMessage(repositoryAgeMilliseconds);
if (message !== undefined) {
file.message(message);
}
}
const gitRepoAgeRule = lintRule('remark-lint:awesome-git-repo-age', async (ast, file) => {
const {dirname, repoURL} = file;
// If we have a repoURL, use GitHub API to check repo age
if (repoURL) {
const data = await fetchGitHubData(repoURL);
if (!data?.created_at) {
return;
}
const repositoryCreationDate = new Date(data.created_at);
checkRepositoryAge(repositoryCreationDate, file);
return;
}
// For local repos, use git commands
try {
const {stdout: firstCommitHash} = await execa('git', [
'rev-list',
'--max-parents=0',
'HEAD',
], {
cwd: dirname,
});
const {stdout: firstCommitDate} = await execa('git', [
'show',
'-s',
'--format=%ci',
firstCommitHash,
], {
cwd: dirname,
});
const repositoryCreationDate = new Date(firstCommitDate);
checkRepositoryAge(repositoryCreationDate, file);
} catch {
file.message('Awesome list must reside in a valid deep-cloned Git repository (see https://github.com/sindresorhus/awesome-lint#tip for more information)');
}
});
export default gitRepoAgeRule;
export {formatRepositoryAgeMessage};