mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
Initial commit: working markdown edit
This commit is contained in:
commit
2a0607ee2a
4 changed files with 118 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules/
|
11
README.md
Normal file
11
README.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
# all-contributors-cli
|
||||
|
||||
This is a tool to help automate adding contributor acknowledgements according to the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
|
||||
|
||||
## Usage
|
||||
|
||||
TODO
|
||||
|
||||
## Configuration
|
||||
|
||||
TODO
|
81
cli.js
Normal file
81
cli.js
Normal file
|
@ -0,0 +1,81 @@
|
|||
var options = {
|
||||
contributingFile: './fixture/some.md',
|
||||
projectOwner: 'jfmengels',
|
||||
projectName: 'all-contributors-cli',
|
||||
imageSize: 130
|
||||
};
|
||||
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var request = require('request');
|
||||
var findIndex = require('lodash.findindex');
|
||||
|
||||
function getUserInfo(username, cb) {
|
||||
request.get({
|
||||
url: 'https://api.github.com/users/' + username,
|
||||
headers: {
|
||||
'User-Agent': 'request'
|
||||
}
|
||||
}, function(error, res) {
|
||||
if (error) {
|
||||
return cb(error);
|
||||
}
|
||||
return cb(null, JSON.parse(res.body));
|
||||
});
|
||||
}
|
||||
|
||||
function readMarkdown(filePath, cb) {
|
||||
fs.readFile(path.join(__dirname, filePath), 'utf8', cb);
|
||||
}
|
||||
|
||||
function writeMarkdown(filePath, content, cb) {
|
||||
fs.writeFile(path.join(__dirname, filePath), content, cb);
|
||||
}
|
||||
|
||||
function listAllContributors(start, lines) {
|
||||
var i = 0;
|
||||
while (start + i < lines.length && lines[start + i].indexOf('[![') === 0) {
|
||||
i++;
|
||||
}
|
||||
return lines.slice(start, start + i);
|
||||
}
|
||||
|
||||
function contributorEntry(options, user) {
|
||||
return '[![' + user.name + '](' + user.avatar_url + '&s=' + options.imageSize + ')' +
|
||||
'<br />' + user.name + '](' + user.html_url + ')' +
|
||||
' | [📖](https://github.com/' + options.projectOwner + '/' + options.projectName + '/commits?author=' + user.login + ')';
|
||||
}
|
||||
|
||||
function addContributor(contributor, fileContent) {
|
||||
var lines = fileContent.split('\n');
|
||||
var contributorListStart = findIndex(lines, function(line) {
|
||||
return line.indexOf(':---: | :---:') !== -1;
|
||||
});
|
||||
var contributors = [].concat(
|
||||
listAllContributors(contributorListStart + 1, lines),
|
||||
contributor
|
||||
);
|
||||
|
||||
return [].concat(
|
||||
lines.slice(0, contributorListStart + 1),
|
||||
contributors,
|
||||
lines.slice(contributorListStart + contributors.length)
|
||||
).join('\n')
|
||||
}
|
||||
|
||||
getUserInfo('jfmengels', function(error, user) {
|
||||
if (error) {
|
||||
return console.error(error);
|
||||
}
|
||||
readMarkdown(options.contributingFile, function(error, fileContent) {
|
||||
if (error) {
|
||||
return console.error(error);
|
||||
}
|
||||
var newFileContent = addContributor(contributorEntry(options, user), fileContent);
|
||||
writeMarkdown(options.contributingFile, newFileContent, function(error, fileContent) {
|
||||
if (error) {
|
||||
return console.error(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
25
package.json
Normal file
25
package.json
Normal file
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "all-contributors-cli",
|
||||
"version": "1.0.0",
|
||||
"description": "Tool to easily add recognition for new contributors",
|
||||
"bin": "cli.js",
|
||||
"scripts": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jfmengels/all-contributors-cli.git"
|
||||
},
|
||||
"keywords": [
|
||||
"all-contributors",
|
||||
"contributors"
|
||||
],
|
||||
"author": "Jeroen Engels <jfm.engels@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/jfmengels/all-contributors-cli/issues"
|
||||
},
|
||||
"homepage": "https://github.com/jfmengels/all-contributors-cli#readme",
|
||||
"dependencies": {
|
||||
"lodash.findindex": "^4.2.0",
|
||||
"request": "^2.69.0"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue