@all-contributors/cli 0.0.0-stage → 7.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +19 -0
- package/README.md +205 -2
- package/dist/api.js +23 -0
- package/dist/cli.js +182 -0
- package/dist/contributors/add.js +66 -0
- package/dist/contributors/addWithDetails.js +21 -0
- package/dist/contributors/index.js +30 -0
- package/dist/contributors/prompt.js +90 -0
- package/dist/generate/format-badge.js +10 -0
- package/dist/generate/format-contribution-type.js +47 -0
- package/dist/generate/format-contributor.js +73 -0
- package/dist/generate/index.js +158 -0
- package/dist/generate/start-generation.js +30 -0
- package/dist/init/commit-conventions.js +43 -0
- package/dist/init/index.js +35 -0
- package/dist/init/init-content.js +59 -0
- package/dist/init/prompt.js +128 -0
- package/dist/repo/github.js +119 -0
- package/dist/repo/gitlab.js +91 -0
- package/dist/repo/index.js +113 -0
- package/dist/util/config-file.js +81 -0
- package/dist/util/contribution-types.js +147 -0
- package/dist/util/formatting.js +24 -0
- package/dist/util/git.js +62 -0
- package/dist/util/index.js +8 -0
- package/dist/util/template.js +8 -0
- package/dist/util/url.js +27 -0
- package/dist/util/user-error.js +10 -0
- package/package.json +89 -3
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as repo from '../repo/index.js';
|
|
2
|
+
import * as util from '../util/index.js';
|
|
3
|
+
import { add } from './add.js';
|
|
4
|
+
import { prompt } from './prompt.js';
|
|
5
|
+
|
|
6
|
+
function isNewContributor(contributorList, username) {
|
|
7
|
+
return !contributorList.find((contributor) => contributor.login === username);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function addContributor(options, username, contributions) {
|
|
11
|
+
const answersP = prompt(options, username, contributions);
|
|
12
|
+
const contributorsP = answersP.then((answers) =>
|
|
13
|
+
add(options, answers.username, answers.contributions, repo.getUserInfo),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const writeContributorsP = contributorsP.then((contributors) =>
|
|
17
|
+
util.configFile.writeContributors(options.config, contributors),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
return Promise.all([answersP, contributorsP, writeContributorsP]).then((res) => {
|
|
21
|
+
const answers = res[0];
|
|
22
|
+
const contributors = res[1];
|
|
23
|
+
return {
|
|
24
|
+
username: answers.username,
|
|
25
|
+
contributions: answers.contributions,
|
|
26
|
+
contributors,
|
|
27
|
+
newContributor: isNewContributor(options.contributors, answers.username),
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import inquirer from 'inquirer';
|
|
2
|
+
|
|
3
|
+
import * as repo from '../repo/index.js';
|
|
4
|
+
import * as util from '../util/index.js';
|
|
5
|
+
|
|
6
|
+
function contributionChoices(options) {
|
|
7
|
+
const types = util.contributionTypes(options);
|
|
8
|
+
const pairs = Object.entries(types);
|
|
9
|
+
const sorted = pairs.sort((a, b) => a[1].description.localeCompare(b[1].description));
|
|
10
|
+
return sorted.map((pair) => ({
|
|
11
|
+
name: `${pair[1].symbol} ${pair[1].description}`,
|
|
12
|
+
value: pair[0],
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getQuestions(options, username, contributions) {
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
type: 'input',
|
|
20
|
+
name: 'username',
|
|
21
|
+
message: `What is the contributor's ${repo.getTypeName(options.repoType)} username?`,
|
|
22
|
+
when: !username,
|
|
23
|
+
validate: function validate(input) {
|
|
24
|
+
if (!input) {
|
|
25
|
+
return 'Username not provided';
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: 'checkbox',
|
|
32
|
+
name: 'contributions',
|
|
33
|
+
message: 'What are the contribution types?',
|
|
34
|
+
when: !contributions,
|
|
35
|
+
default: function (answers) {
|
|
36
|
+
// default values for contributions when updating existing users
|
|
37
|
+
answers.username = answers.username || username;
|
|
38
|
+
return options.contributors
|
|
39
|
+
.filter(
|
|
40
|
+
(entry) => entry.login && entry.login.toLowerCase() === answers.username.toLowerCase(),
|
|
41
|
+
)
|
|
42
|
+
.reduce((allEntries, entry) => allEntries.concat(entry.contributions), []);
|
|
43
|
+
},
|
|
44
|
+
choices: contributionChoices(options),
|
|
45
|
+
validate: function (input, answers) {
|
|
46
|
+
answers.username = answers.username || username;
|
|
47
|
+
const previousContributions = options.contributors
|
|
48
|
+
.filter(
|
|
49
|
+
(entry) => entry.login && entry.login.toLowerCase() === answers.username.toLowerCase(),
|
|
50
|
+
)
|
|
51
|
+
.reduce((allEntries, entry) => allEntries.concat(entry.contributions), []);
|
|
52
|
+
|
|
53
|
+
if (!input.length) {
|
|
54
|
+
return 'Use space to select at least one contribution type.';
|
|
55
|
+
} else if (JSON.stringify(input.sort()) === JSON.stringify(previousContributions.sort())) {
|
|
56
|
+
return 'Nothing changed, use space to select contribution types.';
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getValidUserContributions(options, contributions) {
|
|
65
|
+
const validContributionTypes = util.contributionTypes(options);
|
|
66
|
+
const userContributions = contributions && contributions.split(',');
|
|
67
|
+
|
|
68
|
+
const validUserContributions = userContributions.filter(
|
|
69
|
+
(userContribution) => validContributionTypes[userContribution] !== undefined,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const invalidUserContributions = userContributions.filter(
|
|
73
|
+
(userContribution) => validContributionTypes[userContribution] === undefined,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (validUserContributions.length === 0) {
|
|
77
|
+
throw new Error(`${invalidUserContributions.toString()} is/are invalid contribution type(s)`);
|
|
78
|
+
}
|
|
79
|
+
return validUserContributions;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function prompt(options, username, contributions) {
|
|
83
|
+
const defaults = {
|
|
84
|
+
username,
|
|
85
|
+
contributions:
|
|
86
|
+
contributions === undefined ? [] : getValidUserContributions(options, contributions),
|
|
87
|
+
};
|
|
88
|
+
const questions = getQuestions(options, username, contributions);
|
|
89
|
+
return inquirer.prompt(questions).then((answers) => ({ ...defaults, ...answers }));
|
|
90
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as util from '../util/index.js';
|
|
2
|
+
|
|
3
|
+
const defaultTemplate =
|
|
4
|
+
'[](#contributors-)';
|
|
5
|
+
|
|
6
|
+
export function formatBadge(options, contributors) {
|
|
7
|
+
return util.template(options.badgeTemplate || defaultTemplate)({
|
|
8
|
+
contributors,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as util from '../util/index.js';
|
|
2
|
+
|
|
3
|
+
const linkTemplate = util.template(
|
|
4
|
+
'<a href="<%= url %>" title="<%= description %>"><%= symbol %></a>',
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
function getType(options, contribution) {
|
|
8
|
+
const types = util.contributionTypes(options);
|
|
9
|
+
return types[contribution.type || contribution];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function formatContributionType(options, contributor, contribution) {
|
|
13
|
+
const type = getType(options, contribution);
|
|
14
|
+
|
|
15
|
+
if (!type) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Unknown contribution type ${contribution} for contributor ${
|
|
18
|
+
contributor.login || contributor.name
|
|
19
|
+
}`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const templateData = {
|
|
24
|
+
symbol: type.symbol,
|
|
25
|
+
description: type.description || '',
|
|
26
|
+
contributor,
|
|
27
|
+
options,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
let url = getUrl(contribution, contributor);
|
|
31
|
+
|
|
32
|
+
if (contribution.url) {
|
|
33
|
+
url = contribution.url;
|
|
34
|
+
} else if (type.link) {
|
|
35
|
+
url = util.template(type.link)(templateData);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return linkTemplate({ url, ...templateData });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getUrl(contribution, contributor) {
|
|
42
|
+
if (contributor.login) {
|
|
43
|
+
return `#${contribution}-${contributor.login}`;
|
|
44
|
+
} else {
|
|
45
|
+
return `#${contribution}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as util from '../util/index.js';
|
|
2
|
+
import { formatContributionType } from './format-contribution-type.js';
|
|
3
|
+
|
|
4
|
+
const avatarTemplate = util.template(
|
|
5
|
+
'<img src="<%= contributor.avatar_url %>" width="<%= options.imageSize %>px" height="<%= options.imageSize %>px" alt="<%= name %>"/>',
|
|
6
|
+
);
|
|
7
|
+
|
|
8
|
+
const avatarBlockTemplate = util.template(
|
|
9
|
+
'<a href="<%= contributor.profile %>"><%= avatar %><br /><sub><b><%= name %></b></sub></a>',
|
|
10
|
+
);
|
|
11
|
+
const avatarBlockTemplateNoProfile = util.template(
|
|
12
|
+
'<%= avatar %><br /><sub><b><%= name %></b></sub>',
|
|
13
|
+
);
|
|
14
|
+
const contributorTemplate = util.template('<%= avatarBlock %><br /><%= contributions %>');
|
|
15
|
+
|
|
16
|
+
const defaultImageSize = 100;
|
|
17
|
+
|
|
18
|
+
function defaultTemplate(templateData) {
|
|
19
|
+
const rawName = templateData.contributor.name || templateData.contributor.login;
|
|
20
|
+
const name = escapeName(rawName);
|
|
21
|
+
const avatar = avatarTemplate({
|
|
22
|
+
...templateData,
|
|
23
|
+
name,
|
|
24
|
+
});
|
|
25
|
+
const avatarBlockTemplateData = {
|
|
26
|
+
name,
|
|
27
|
+
avatar,
|
|
28
|
+
...templateData,
|
|
29
|
+
};
|
|
30
|
+
let avatarBlock;
|
|
31
|
+
|
|
32
|
+
if (templateData.contributor.profile) {
|
|
33
|
+
avatarBlock = avatarBlockTemplate(avatarBlockTemplateData);
|
|
34
|
+
} else {
|
|
35
|
+
avatarBlock = avatarBlockTemplateNoProfile(avatarBlockTemplateData);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return contributorTemplate({ avatarBlock, ...templateData });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function escapeName(name) {
|
|
42
|
+
return name.replace(new RegExp('\\|', 'g'), '|').replace(new RegExp('\\"', 'g'), '"');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function formatAvatarUrl(avatarUrl, imageSize) {
|
|
46
|
+
if (!URL.canParse(avatarUrl)) {
|
|
47
|
+
return avatarUrl;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const url = new URL(avatarUrl);
|
|
51
|
+
|
|
52
|
+
url.searchParams.append('size', imageSize);
|
|
53
|
+
|
|
54
|
+
return url.toString();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function formatContributor(options, contributor) {
|
|
58
|
+
const formatter = (contribution) => formatContributionType(options, contributor, contribution);
|
|
59
|
+
const contributions = contributor.contributions.map(formatter).join(' ');
|
|
60
|
+
|
|
61
|
+
const templateData = {
|
|
62
|
+
contributions,
|
|
63
|
+
contributor: {
|
|
64
|
+
...contributor,
|
|
65
|
+
avatar_url: formatAvatarUrl(contributor.avatar_url, options.imageSize ?? defaultImageSize),
|
|
66
|
+
},
|
|
67
|
+
options: { imageSize: defaultImageSize, ...options },
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const customTemplate = options.contributorTemplate && util.template(options.contributorTemplate);
|
|
71
|
+
|
|
72
|
+
return (customTemplate || defaultTemplate)(templateData);
|
|
73
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as util from '../util/index.js';
|
|
2
|
+
import { formatBadge } from './format-badge.js';
|
|
3
|
+
import { formatContributor } from './format-contributor.js';
|
|
4
|
+
|
|
5
|
+
function chunk(array, size) {
|
|
6
|
+
const chunks = [];
|
|
7
|
+
for (let i = 0; i < array.length; i += size) {
|
|
8
|
+
chunks.push(array.slice(i, i + size));
|
|
9
|
+
}
|
|
10
|
+
return chunks;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function injectListBetweenTags(newContent, options) {
|
|
14
|
+
return function (previousContent) {
|
|
15
|
+
const tagToLookFor = `<!-- ALL-CONTRIBUTORS-LIST:`;
|
|
16
|
+
const closingTag = '-->';
|
|
17
|
+
const startOfOpeningTagIndex = previousContent.indexOf(`${tagToLookFor}START`);
|
|
18
|
+
const endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex);
|
|
19
|
+
const startOfClosingTagIndex = previousContent.indexOf(
|
|
20
|
+
`${tagToLookFor}END`,
|
|
21
|
+
endOfOpeningTagIndex,
|
|
22
|
+
);
|
|
23
|
+
if (
|
|
24
|
+
startOfOpeningTagIndex === -1 ||
|
|
25
|
+
endOfOpeningTagIndex === -1 ||
|
|
26
|
+
startOfClosingTagIndex === -1
|
|
27
|
+
) {
|
|
28
|
+
return previousContent;
|
|
29
|
+
}
|
|
30
|
+
const startIndent = Math.max(0, previousContent.lastIndexOf('\n', startOfOpeningTagIndex));
|
|
31
|
+
const nbSpaces = Math.max(
|
|
32
|
+
0,
|
|
33
|
+
startOfOpeningTagIndex - Math.min(startOfOpeningTagIndex, startIndent) - 1,
|
|
34
|
+
);
|
|
35
|
+
const previousList = previousContent.slice(endOfOpeningTagIndex, startOfClosingTagIndex);
|
|
36
|
+
const withMarkdownlint =
|
|
37
|
+
options.withMarkdownlint === true ||
|
|
38
|
+
previousList.includes('<!-- markdownlint-disable -->') ||
|
|
39
|
+
// Lists without prettier-ignore-start were never generated
|
|
40
|
+
// (e.g. hand-written tags), so they default to including markdownlint
|
|
41
|
+
!previousList.includes('<!-- prettier-ignore-start -->');
|
|
42
|
+
return [
|
|
43
|
+
previousContent.slice(0, endOfOpeningTagIndex + closingTag.length),
|
|
44
|
+
'\n<!-- prettier-ignore-start -->',
|
|
45
|
+
withMarkdownlint ? '\n<!-- markdownlint-disable -->' : '',
|
|
46
|
+
newContent.replace('\n', `\n${' '.repeat(nbSpaces)}`),
|
|
47
|
+
withMarkdownlint ? '<!-- markdownlint-restore -->\n' : '',
|
|
48
|
+
'<!-- prettier-ignore-end -->',
|
|
49
|
+
'\n\n',
|
|
50
|
+
previousContent.slice(startOfClosingTagIndex),
|
|
51
|
+
].join('');
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function formatLine(options, contributors) {
|
|
56
|
+
const width = Math.floor((100 / options.contributorsPerLine) * 100) / 100;
|
|
57
|
+
const attributes = `align="center" valign="top" width="${width}%"`;
|
|
58
|
+
|
|
59
|
+
return `<td ${attributes}>${contributors.join(`</td>\n <td ${attributes}>`)}</td>`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function formatFooter(options) {
|
|
63
|
+
if (!options.linkToUsage) {
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
const smallLogoURL =
|
|
67
|
+
'https://raw.githubusercontent.com/all-contributors/all-contributors-cli/1b8533af435da9854653492b1327a23a4dbd0a10/assets/logo-small.svg';
|
|
68
|
+
const linkToBotAdd = 'https://all-contributors.js.org/docs/en/bot/usage';
|
|
69
|
+
|
|
70
|
+
return `<tr>\n <td align="center" size="13px" colspan="${options.contributorsPerLine}">\n <img src="${smallLogoURL}">\n <a href="${linkToBotAdd}">Add your contributions</a>\n </img>\n </td>\n </tr>`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function generateContributorsList(options, contributors) {
|
|
74
|
+
const tableFooter = formatFooter(options);
|
|
75
|
+
const defaultWrapperTemplate = util.template(
|
|
76
|
+
'\n<table>\n <tbody><%= bodyContent %> </tbody>\n<%= tableFooterContent %></table>\n\n',
|
|
77
|
+
);
|
|
78
|
+
const wrapperTemplate = options.wrapperTemplate
|
|
79
|
+
? util.template(`\n${options.wrapperTemplate}\n\n`)
|
|
80
|
+
: defaultWrapperTemplate;
|
|
81
|
+
const seperator = options.wrapperTemplate
|
|
82
|
+
? '\n </tr><br />\n <tr>\n '
|
|
83
|
+
: '\n </tr>\n <tr>\n ';
|
|
84
|
+
|
|
85
|
+
let tableFooterContent = '';
|
|
86
|
+
|
|
87
|
+
const sortedContributors = [...contributors];
|
|
88
|
+
if (options.contributorsSortAlphabetically) {
|
|
89
|
+
sortedContributors.sort((a, b) => {
|
|
90
|
+
const nameA = a.name || a.login || '';
|
|
91
|
+
const nameB = b.name || b.login || '';
|
|
92
|
+
return nameA.localeCompare(nameB, undefined, { sensitivity: 'base' });
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const formattedContributors = sortedContributors.map((contributor) =>
|
|
97
|
+
formatContributor(options, contributor),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const chunkedContributors = chunk(formattedContributors, options.contributorsPerLine);
|
|
101
|
+
|
|
102
|
+
const formattedLines = chunkedContributors.map((currentLineContributors) =>
|
|
103
|
+
formatLine(options, currentLineContributors),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const newContent = formattedLines.join(seperator);
|
|
107
|
+
|
|
108
|
+
if (options.linkToUsage) {
|
|
109
|
+
tableFooterContent = ` <tfoot>\n ${tableFooter}\n </tfoot>\n`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const bodyContent = `\n <tr>\n ${newContent}\n </tr>\n`;
|
|
113
|
+
|
|
114
|
+
return wrapperTemplate({ bodyContent, tableFooterContent });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function replaceBadge(newContent) {
|
|
118
|
+
return function (previousContent) {
|
|
119
|
+
const tagToLookFor = `<!-- ALL-CONTRIBUTORS-BADGE:`;
|
|
120
|
+
const closingTag = '-->';
|
|
121
|
+
const startOfOpeningTagIndex = previousContent.indexOf(`${tagToLookFor}START`);
|
|
122
|
+
const endOfOpeningTagIndex = previousContent.indexOf(closingTag, startOfOpeningTagIndex);
|
|
123
|
+
const startOfClosingTagIndex = previousContent.indexOf(
|
|
124
|
+
`${tagToLookFor}END`,
|
|
125
|
+
endOfOpeningTagIndex,
|
|
126
|
+
);
|
|
127
|
+
if (
|
|
128
|
+
startOfOpeningTagIndex === -1 ||
|
|
129
|
+
endOfOpeningTagIndex === -1 ||
|
|
130
|
+
startOfClosingTagIndex === -1
|
|
131
|
+
) {
|
|
132
|
+
return previousContent;
|
|
133
|
+
}
|
|
134
|
+
const startIndent = Math.max(0, previousContent.lastIndexOf('\n', startOfOpeningTagIndex));
|
|
135
|
+
const nbSpaces = startOfOpeningTagIndex - Math.min(startOfOpeningTagIndex, startIndent);
|
|
136
|
+
return [
|
|
137
|
+
previousContent.slice(0, endOfOpeningTagIndex + closingTag.length),
|
|
138
|
+
'\n',
|
|
139
|
+
newContent.replace('\n', `\n${' '.repeat(nbSpaces)}`),
|
|
140
|
+
'\n',
|
|
141
|
+
previousContent.slice(startOfClosingTagIndex),
|
|
142
|
+
].join('');
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function generate(options, contributors, fileContent) {
|
|
147
|
+
if (contributors === undefined || !Array.isArray(contributors)) {
|
|
148
|
+
throw new Error('contributors must be an array');
|
|
149
|
+
}
|
|
150
|
+
const contributorsList =
|
|
151
|
+
contributors.length === 0 ? '\n' : generateContributorsList(options, contributors);
|
|
152
|
+
const badge = formatBadge(options, contributors);
|
|
153
|
+
|
|
154
|
+
let result = fileContent;
|
|
155
|
+
result = injectListBetweenTags(contributorsList, options)(result);
|
|
156
|
+
result = replaceBadge(badge)(result);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import { UserError } from '../util/user-error.js';
|
|
5
|
+
import { generate } from './index.js';
|
|
6
|
+
|
|
7
|
+
async function readFileToUpdate(file, cwd) {
|
|
8
|
+
try {
|
|
9
|
+
return await fs.readFile(path.join(cwd, file), 'utf8');
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (error.code === 'ENOENT') {
|
|
12
|
+
throw new UserError(
|
|
13
|
+
`Couldn't find ${file} in ${cwd}.\n` +
|
|
14
|
+
'Create the file, or update "files" in .all-contributorsrc.',
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function startGeneration(options, cwd = process.cwd()) {
|
|
22
|
+
await Promise.all(
|
|
23
|
+
options.files.map(async (file) => {
|
|
24
|
+
const fileContent = await readFileToUpdate(file, cwd);
|
|
25
|
+
const newFileContent = generate(options, options.contributors, fileContent);
|
|
26
|
+
|
|
27
|
+
await fs.writeFile(path.join(cwd, file), newFileContent);
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const conventions = {
|
|
2
|
+
angular: {
|
|
3
|
+
name: 'Angular',
|
|
4
|
+
msg: 'docs:',
|
|
5
|
+
lowercase: true,
|
|
6
|
+
transform(msg) {
|
|
7
|
+
return msg.replace(
|
|
8
|
+
/(^.*?) ([A-Z][a-z]+) \w*/,
|
|
9
|
+
(_, ...words) => `${words[0]} ${words[1].toLowerCase()} `,
|
|
10
|
+
);
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
atom: {
|
|
14
|
+
name: 'Atom',
|
|
15
|
+
msg: ':memo:',
|
|
16
|
+
},
|
|
17
|
+
gitmoji: {
|
|
18
|
+
name: 'Gitmoji',
|
|
19
|
+
msg: ':busts_in_silhouette:',
|
|
20
|
+
},
|
|
21
|
+
ember: {
|
|
22
|
+
name: 'Ember',
|
|
23
|
+
msg: '[DOC canary]',
|
|
24
|
+
},
|
|
25
|
+
eslint: {
|
|
26
|
+
name: 'ESLint',
|
|
27
|
+
msg: 'Docs:',
|
|
28
|
+
},
|
|
29
|
+
jshint: {
|
|
30
|
+
name: 'JSHint',
|
|
31
|
+
msg: '[[DOCS]]',
|
|
32
|
+
},
|
|
33
|
+
none: {
|
|
34
|
+
name: 'None',
|
|
35
|
+
msg: '',
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Object.keys(conventions).forEach((style) => {
|
|
40
|
+
conventions[style].value = style;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export { conventions };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
|
|
3
|
+
import { configFile } from '../util/index.js';
|
|
4
|
+
import { addBadge, addContributorsList } from './init-content.js';
|
|
5
|
+
import { prompt } from './prompt.js';
|
|
6
|
+
|
|
7
|
+
async function ensureFileExists(file) {
|
|
8
|
+
try {
|
|
9
|
+
await fs.access(file);
|
|
10
|
+
return file;
|
|
11
|
+
} catch {
|
|
12
|
+
await fs.writeFile(file, '');
|
|
13
|
+
return file;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function injectInFile(file, fn) {
|
|
18
|
+
const content = await fs.readFile(file, 'utf8');
|
|
19
|
+
|
|
20
|
+
await fs.writeFile(file, fn(content));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function init() {
|
|
24
|
+
const promptResult = await prompt();
|
|
25
|
+
|
|
26
|
+
await configFile.writeConfig('.all-contributorsrc', promptResult.config);
|
|
27
|
+
|
|
28
|
+
await ensureFileExists(promptResult.contributorFile);
|
|
29
|
+
|
|
30
|
+
await injectInFile(promptResult.contributorFile, addContributorsList);
|
|
31
|
+
|
|
32
|
+
if (promptResult.badgeFile) {
|
|
33
|
+
await injectInFile(promptResult.badgeFile, addBadge);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const badgeContent = [
|
|
2
|
+
'<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->',
|
|
3
|
+
'[](#contributors-)',
|
|
4
|
+
'<!-- ALL-CONTRIBUTORS-BADGE:END -->',
|
|
5
|
+
].join('\n');
|
|
6
|
+
|
|
7
|
+
const headerContent =
|
|
8
|
+
'Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/en/reference/emoji-key/)):';
|
|
9
|
+
const listContent = [
|
|
10
|
+
'<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->',
|
|
11
|
+
'<!-- prettier-ignore-start -->',
|
|
12
|
+
'<!-- markdownlint-disable -->',
|
|
13
|
+
'<!-- markdownlint-restore -->',
|
|
14
|
+
'<!-- prettier-ignore-end -->',
|
|
15
|
+
'<!-- ALL-CONTRIBUTORS-LIST:END -->',
|
|
16
|
+
].join('\n');
|
|
17
|
+
const footerContent =
|
|
18
|
+
'This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!';
|
|
19
|
+
|
|
20
|
+
function injectContentBetween(lines, content, startIndex, endIndex) {
|
|
21
|
+
return [].concat(lines.slice(0, startIndex), content, lines.slice(endIndex));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function addBadgeImpl(lines) {
|
|
25
|
+
return injectContentBetween(lines, badgeContent, 1, 1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function splitAndRejoin(fn) {
|
|
29
|
+
return function (content) {
|
|
30
|
+
const lines = content.split('\n');
|
|
31
|
+
const result = fn(lines);
|
|
32
|
+
return result.join('\n');
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function findContributorsSection(lines) {
|
|
37
|
+
return lines.findIndex((str) => str.toLowerCase().indexOf('# contributors') === 1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function addContributorsListImpl(lines) {
|
|
41
|
+
const insertionLine = findContributorsSection(lines);
|
|
42
|
+
|
|
43
|
+
if (insertionLine === -1) {
|
|
44
|
+
return lines.concat([
|
|
45
|
+
'## Contributors ✨',
|
|
46
|
+
'',
|
|
47
|
+
headerContent,
|
|
48
|
+
'',
|
|
49
|
+
listContent,
|
|
50
|
+
'',
|
|
51
|
+
footerContent,
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return injectContentBetween(lines, listContent, insertionLine + 3, insertionLine + 3);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const addBadge = splitAndRejoin(addBadgeImpl);
|
|
59
|
+
export const addContributorsList = splitAndRejoin(addContributorsListImpl);
|