semantic-release-contributors
Version:
semantic-release plugin to automatically update contributors list from git history
114 lines (90 loc) • 3.63 kB
JavaScript
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var path = _interopDefault(require('path'));
var AggregateError = _interopDefault(require('aggregate-error'));
var jsonfile = require('jsonfile');
var stringifyAuthor = _interopDefault(require('stringify-author'));
var parseAuthor = _interopDefault(require('parse-author'));
// A type of promise-like that resolves synchronously and supports only one observer
const _iteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator"))) : "@@iterator";
const _asyncIteratorSymbol = /*#__PURE__*/ typeof Symbol !== "undefined" ? (Symbol.asyncIterator || (Symbol.asyncIterator = Symbol("Symbol.asyncIterator"))) : "@@asyncIterator";
// Asynchronously call a function and send errors to recovery continuation
function _catch(body, recover) {
try {
var result = body();
} catch(e) {
return recover(e);
}
if (result && result.then) {
return result.then(void 0, recover);
}
return result;
}
const uniqBy = (items, property) => items.reduce((uniqItems, item) => {
const value = item[property];
const uniqKeys = uniqItems.map(i => i[property]);
if (typeof value === 'undefined' || !uniqKeys.includes(value)) {
uniqItems.push(item);
}
return uniqItems;
}, []);
const sortBy = (items, property) => items.sort((itemA, itemB) => {
if (itemA[property] > itemB[property]) {
return -1;
}
if (itemA[property] < itemB[property]) {
return 1;
}
return 0;
});
const getContributorsFromCommits = (commits = []) => uniqBy(sortBy(commits, 'date').map(commit => ({
email: commit.author.email,
name: commit.author.name
})), 'email');
const parseContributor = contributor => typeof contributor === 'string' ? parseAuthor(contributor) : contributor;
const mergeContributors = (packageContributors = [], commitsContributors = []) => uniqBy(packageContributors.concat(commitsContributors).map(parseContributor), 'email');
const saveContributors = function (packageFilePath, contributors = [], format = 'string', logger = undefined) {
try {
return Promise.resolve(jsonfile.readFile(packageFilePath)).then(function (pkg) {
let allContributors = mergeContributors(pkg.contributors, contributors);
if (format === 'string') {
allContributors = allContributors.map(stringifyAuthor);
}
if (logger) {
logger.info('Updated contributors list', allContributors);
}
pkg.contributors = allContributors;
return Promise.resolve(jsonfile.writeFile(packageFilePath, pkg, {
spaces: 2
})).then(function () {});
});
} catch (e) {
return Promise.reject(e);
}
};
const prepare = function (pluginConfig, context) {
try {
function _temp2() {
if (errors.length > 0) {
throw new AggregateError(errors);
}
}
const errors = [];
const {
cwd,
commits,
logger
} = context;
const format = ['string', 'object'].includes(pluginConfig.format) ? pluginConfig.format : 'string';
const pkgRoot = pluginConfig.pkgRoot || '.';
const _temp = _catch(function () {
return Promise.resolve(saveContributors(path.resolve(cwd, pkgRoot, 'package.json'), getContributorsFromCommits(commits), format, logger)).then(function () {});
}, function (error) {
errors.push(error);
});
return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
} catch (e) {
return Promise.reject(e);
}
};
exports.prepare = prepare;
//# sourceMappingURL=index.js.map