-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from flo-sch/feature/esm
Feature: ES modules
- Loading branch information
Showing
20 changed files
with
379 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,3 +129,7 @@ $RECYCLE.BIN/ | |
|
||
package-lock.json | ||
yarn.lock | ||
|
||
# Dist subfolder | ||
|
||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import uniqBy from '../helpers/uniq-by'; | ||
import sortBy from '../helpers/sort-by'; | ||
|
||
/** | ||
* Extract an array of contributors from an array of commits, removing duplicates on email | ||
* | ||
* @param array commits | ||
*/ | ||
const getContributorsFromCommits = (commits = []) => | ||
uniqBy( | ||
sortBy(commits, 'date').map((commit) => ({ | ||
email: commit.author.email, | ||
name: commit.author.name, | ||
})), | ||
'email' | ||
); | ||
|
||
export default getContributorsFromCommits; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import parseAuthor from 'parse-author'; | ||
import uniqBy from '../helpers/uniq-by'; | ||
|
||
/** | ||
* Parse a contributor string with parse-author | ||
* | ||
* @param mixed contributor | ||
* | ||
* @return object | ||
*/ | ||
const parseContributor = (contributor) => (typeof contributor === 'string' ? parseAuthor(contributor) : contributor); | ||
|
||
/** | ||
* Merge array of contributors, parsing them and removing duplicates on email | ||
* | ||
* @param array packageContributors | ||
* @param array commitsContributors | ||
* | ||
* @return array | ||
*/ | ||
const mergeContributors = (packageContributors = [], commitsContributors = []) => | ||
uniqBy(packageContributors.concat(commitsContributors).map(parseContributor), 'email'); | ||
|
||
export default mergeContributors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {readFile, writeFile} from 'jsonfile'; | ||
import stringifyAuthor from 'stringify-author'; | ||
import mergeContributors from './merge-contributors'; | ||
|
||
/** | ||
* Save an array of contributors to a package.json file | ||
* | ||
* @param string packageFilePath | ||
* @param array contributors | ||
* @param string format | ||
* @param mixed logger | ||
*/ | ||
const saveContributors = async (packageFilePath, contributors = [], format = 'string', logger = undefined) => { | ||
const pkg = await readFile(packageFilePath); | ||
|
||
let allContributors = mergeContributors(pkg.contributors, contributors); | ||
|
||
if (format === 'string') { | ||
allContributors = allContributors.map(stringifyAuthor); | ||
} | ||
|
||
if (logger) { | ||
logger.info('Updated contributors list', allContributors); | ||
} | ||
|
||
pkg.contributors = allContributors; | ||
|
||
await writeFile(packageFilePath, pkg, { | ||
spaces: 2, | ||
}); | ||
}; | ||
|
||
export default saveContributors; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Sort an array of objects by a given property | ||
* | ||
* @param array items | ||
* @param string property | ||
* | ||
* @return array | ||
*/ | ||
const sortBy = (items, property) => | ||
items.sort((itemA, itemB) => { | ||
if (itemA[property] > itemB[property]) { | ||
return -1; | ||
} | ||
|
||
if (itemA[property] < itemB[property]) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
export default sortBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Remove duplicates from an array of objects by a given property | ||
* | ||
* @param array items | ||
* @param string property | ||
* | ||
* @return array | ||
*/ | ||
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; | ||
}, []); | ||
|
||
export default uniqBy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import prepare from './steps/prepare'; | ||
|
||
export {prepare}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import path from 'path'; | ||
import AggregateError from 'aggregate-error'; | ||
import getContributorsFromCommits from '../contributors/get-contributors-from-commits'; | ||
import saveContributors from '../contributors/save-contributors'; | ||
|
||
const prepare = async (pluginConfig, context) => { | ||
const errors = []; | ||
const {cwd, commits, logger} = context; | ||
|
||
const format = ['string', 'object'].includes(pluginConfig.format) ? pluginConfig.format : 'string'; | ||
const pkgRoot = pluginConfig.pkgRoot || '.'; | ||
|
||
try { | ||
await saveContributors( | ||
path.resolve(cwd, pkgRoot, 'package.json'), | ||
getContributorsFromCommits(commits), | ||
format, | ||
logger | ||
); | ||
} catch (error) { | ||
errors.push(error); | ||
} | ||
|
||
if (errors.length > 0) { | ||
throw new AggregateError(errors); | ||
} | ||
}; | ||
|
||
export default prepare; |
Oops, something went wrong.