@ljharb/coauthors 1.0.0

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.
@@ -0,0 +1,12 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: [ljharb]
4
+ patreon: # Replace with a single Patreon username
5
+ open_collective: # Replace with a single Open Collective username
6
+ ko_fi: # Replace with a single Ko-fi username
7
+ tidelift: npm/@ljharb/coauthors
8
+ community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9
+ liberapay: # Replace with a single Liberapay username
10
+ issuehunt: # Replace with a single IssueHunt username
11
+ otechie: # Replace with a single Otechie username
12
+ custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
package/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## v1.0.0 - 2024-07-16
9
+
10
+ ### Commits
11
+
12
+ - Initial implementation, tests, readme [`f5a6dad`](https://github.com/ljharb/coauthors/commit/f5a6dad10445ad05a68068f6ad3b26edb083692c)
13
+ - Initial commit [`a5910db`](https://github.com/ljharb/coauthors/commit/a5910dbc978d750e2df06ce3cc299254554141b3)
14
+ - npm init [`59d84c5`](https://github.com/ljharb/coauthors/commit/59d84c57111a3209e582b753dfb34b6d7c3deea8)
15
+ - Only apps should have lockfiles [`5586949`](https://github.com/ljharb/coauthors/commit/5586949ef7845a961fda4f82c0860d06450c147b)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jordan Harband
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # @ljharb/coauthors <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
2
+
3
+ [![github actions][actions-image]][actions-url]
4
+ [![coverage][codecov-image]][codecov-url]
5
+ [![License][license-image]][license-url]
6
+ [![Downloads][downloads-image]][downloads-url]
7
+
8
+ [![npm badge][npm-badge-png]][package-url]
9
+
10
+ A cli to generate a complete git co-authors list, including existing co-authors, for use in a commit message.
11
+
12
+ ## Usage
13
+
14
+ ```sh
15
+ npx @ljharb/coauthors # if not installed
16
+
17
+ coauthors # if installed and in the PATH
18
+ ```
19
+
20
+ ```sh
21
+ $ coauthors --help
22
+ Usage:
23
+ commit-to-co-authors <remote>
24
+
25
+ `remote` defaults to `origin`.
26
+ ```
27
+
28
+ ## Install
29
+
30
+ ```
31
+ npm install --save-dev @ljharb/coauthors
32
+ ```
33
+
34
+ ## License
35
+
36
+ MIT
37
+
38
+ [package-url]: https://npmjs.org/package/@ljharb/coauthors
39
+ [npm-version-svg]: https://versionbadg.es/ljharb/coauthors.svg
40
+ [deps-svg]: https://david-dm.org/ljharb/coauthors.svg
41
+ [deps-url]: https://david-dm.org/ljharb/coauthors
42
+ [dev-deps-svg]: https://david-dm.org/ljharb/coauthors/dev-status.svg
43
+ [dev-deps-url]: https://david-dm.org/ljharb/coauthors#info=devDependencies
44
+ [npm-badge-png]: https://nodei.co/npm/@ljharb/coauthors.png?downloads=true&stars=true
45
+ [license-image]: https://img.shields.io/npm/l/@ljharb/coauthors.svg
46
+ [license-url]: LICENSE
47
+ [downloads-image]: https://img.shields.io/npm/dm/@ljharb/coauthors.svg
48
+ [downloads-url]: https://npm-stat.com/charts.html?package=@ljharb/coauthors
49
+ [codecov-image]: https://codecov.io/gh/ljharb/coauthors/branch/main/graphs/badge.svg
50
+ [codecov-url]: https://app.codecov.io/gh/ljharb/coauthors/
51
+ [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/coauthors
52
+ [actions-url]: https://github.com/ljharb/coauthors/actions
package/bin.mjs ADDED
@@ -0,0 +1,52 @@
1
+ #! /usr/bin/env node
2
+
3
+ import { readFile } from 'fs/promises';
4
+ import path from 'path';
5
+
6
+ import validateRemote from './validateRemote.mjs';
7
+ import getResults from './results.mjs';
8
+
9
+ import pargs from './pargs.mjs';
10
+
11
+ async function getHelpText() {
12
+ return `${await readFile(path.join(import.meta.dirname, './help.txt'), 'utf-8')}`;
13
+ }
14
+
15
+ const {
16
+ values: { help },
17
+ positionals,
18
+ errors,
19
+ } = await pargs(
20
+ import.meta.filename,
21
+ {
22
+ options: {
23
+ help: { type: 'boolean' },
24
+ },
25
+ allowPositionals: 1,
26
+ },
27
+ );
28
+
29
+ const remote = validateRemote(positionals[0] ?? 'origin');
30
+
31
+ if (typeof remote !== 'string') {
32
+ errors.push(remote.error);
33
+ }
34
+
35
+ if (help || errors.length > 0) {
36
+ const helpText = await getHelpText();
37
+ if (errors.length === 0) {
38
+ console.log(helpText);
39
+ } else {
40
+ console.error(`${helpText}${errors.length === 0 ? '' : '\n'}`);
41
+
42
+ process.exitCode ||= parseInt('1'.repeat(errors.length), 2);
43
+ errors.forEach((error) => console.error(error));
44
+ }
45
+
46
+ process.exit();
47
+ }
48
+
49
+ // eslint-disable-next-line no-extra-parens
50
+ const results = Array.from(getResults(/** @type {string} */ (remote)), (x) => `Co-authored-by: ${x}`);
51
+
52
+ console.log(results.join('\n'));
@@ -0,0 +1,10 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ /** @type {(remote: string) => string} */
4
+ export default function getDefaultBranch(remote) {
5
+ const gitResult = `${execSync(`git rev-parse --abbrev-ref ${remote}/HEAD`)}`.trim();
6
+
7
+ const match = (/\/(?<defaultBranch>\S+)$/).exec(gitResult);
8
+
9
+ return match?.groups?.defaultBranch ?? 'main';
10
+ }
package/help.txt ADDED
@@ -0,0 +1,4 @@
1
+ Usage:
2
+ commit-to-co-authors <remote>
3
+
4
+ `remote` defaults to `origin`.
package/package.json ADDED
@@ -0,0 +1,89 @@
1
+ {
2
+ "name": "@ljharb/coauthors",
3
+ "version": "1.0.0",
4
+ "description": "A cli to generate a complete git co-authors list, including existing co-authors, for use in a commit message.",
5
+ "bin": "./bin.mjs",
6
+ "exports": {
7
+ "./package.json": "./package.json"
8
+ },
9
+ "sideEffects": false,
10
+ "scripts": {
11
+ "prepack": "npmignore --auto --commentLines=autogenerated",
12
+ "prepublish": "not-in-publish || npm run prepublishOnly",
13
+ "prepublishOnly": "safe-publish-latest",
14
+ "lint": "eslint --ext=js,mjs .",
15
+ "postlint": "tsc",
16
+ "pretest": "npm run lint",
17
+ "tests-only": "c8 tape 'test/**/*.*js'",
18
+ "test": "npm run tests-only",
19
+ "posttest": "aud --production",
20
+ "version": "auto-changelog && git add CHANGELOG.md",
21
+ "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/ljharb/coauthors.git"
26
+ },
27
+ "keywords": [
28
+ "git",
29
+ "coauthors",
30
+ "authors",
31
+ "log",
32
+ "commit"
33
+ ],
34
+ "author": "Jordan Harband <ljharb@gmail.com>",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/ljharb/coauthors/issues"
38
+ },
39
+ "homepage": "https://github.com/ljharb/coauthors#readme",
40
+ "dependencies": {
41
+ "commit-to-co-authors": "^0.1.0"
42
+ },
43
+ "engines": {
44
+ "node": ">= 22.4"
45
+ },
46
+ "devDependencies": {
47
+ "@ljharb/eslint-config": "^21.1.1",
48
+ "@ljharb/tsconfig": "^0.2.0",
49
+ "@types/node": "^20.14.10",
50
+ "@types/tape": "^5.6.4",
51
+ "aud": "^2.0.4",
52
+ "auto-changelog": "^2.4.0",
53
+ "c8": "^10.1.2",
54
+ "eslint": "^8.8.0",
55
+ "esmock": "^2.6.6",
56
+ "in-publish": "^2.0.1",
57
+ "npmignore": "^0.3.1",
58
+ "safe-publish-latest": "^2.0.0",
59
+ "tape": "^5.8.1",
60
+ "typescript": "next"
61
+ },
62
+ "c8": {
63
+ "all": true,
64
+ "reporters": [
65
+ "html",
66
+ "text",
67
+ "lcov"
68
+ ],
69
+ "exclude": [
70
+ "coverage",
71
+ "./pargs.mjs"
72
+ ]
73
+ },
74
+ "auto-changelog": {
75
+ "output": "CHANGELOG.md",
76
+ "template": "keepachangelog",
77
+ "unreleased": false,
78
+ "commitLimit": false,
79
+ "backfillLimit": false,
80
+ "hideCredit": true
81
+ },
82
+ "publishConfig": {
83
+ "ignore": [
84
+ ".github/workflows",
85
+ ".eslintrc",
86
+ "test"
87
+ ]
88
+ }
89
+ }
package/pargs.mjs ADDED
@@ -0,0 +1,119 @@
1
+ import { parseArgs } from 'util';
2
+ import { realpathSync } from 'fs';
3
+
4
+ /** @typedef {import('util').ParseArgsConfig} ParseArgsConfig */
5
+
6
+ /** @typedef {(Error | TypeError) & { code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' | 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE' | 'ERR_INVALID_ARG_TYPE' | 'ERR_INVALID_ARG_VALUE' | 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'}} ParseArgsError */
7
+
8
+ /** @type {(e: unknown) => e is ParseArgsError} */
9
+ function isParseArgsError(e) {
10
+ return !!e
11
+ && typeof e === 'object'
12
+ && 'code' in e
13
+ && (
14
+ e.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION'
15
+ || e.code === 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE'
16
+ || e.code === 'ERR_INVALID_ARG_TYPE'
17
+ || e.code === 'ERR_INVALID_ARG_VALUE'
18
+ || e.code === 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL'
19
+ );
20
+ }
21
+ /** @typedef {Omit<ParseArgsConfig, 'args' | 'strict' | 'allowPositionals'> & { allowPositionals?: boolean | number }} PargsConfig */
22
+
23
+ /** @type {(entrypointPath: ImportMeta['filename'], obj: PargsConfig) => Promise<{ errors: string[] } & ReturnType<typeof parseArgs>>} */
24
+ export default async function pargs(entrypointPath, obj) {
25
+ const argv = process.argv.flatMap((arg) => {
26
+ try {
27
+ const realpathedArg = realpathSync(arg);
28
+ if (
29
+ realpathedArg === process.execPath
30
+ || realpathedArg === entrypointPath
31
+ ) {
32
+ return [];
33
+ }
34
+ } catch (e) { /**/ }
35
+ return arg;
36
+ });
37
+
38
+ if ('help' in obj) {
39
+ throw new TypeError('The "help" option is reserved');
40
+ }
41
+
42
+ /** @type {ParseArgsConfig & { tokens: true }} */
43
+ const newObj = {
44
+ args: argv,
45
+ ...obj,
46
+ options: {
47
+ ...obj.options,
48
+ help: {
49
+ default: false,
50
+ type: 'boolean',
51
+ },
52
+ },
53
+ tokens: true,
54
+ // @ts-expect-error blocked on @types/node v22
55
+ allowNegative: true,
56
+ allowPositionals: typeof obj.allowPositionals !== 'undefined',
57
+ strict: true,
58
+ };
59
+
60
+ const errors = [];
61
+
62
+ try {
63
+ const { tokens, ...results } = parseArgs(newObj);
64
+
65
+ const posCount = typeof obj.allowPositionals === 'number' ? obj.allowPositionals : obj.allowPositionals ? Infinity : 0;
66
+ if (results.positionals.length > posCount) {
67
+ errors.push(`Only ${posCount} positional arguments allowed; got ${results.positionals.length}`);
68
+ }
69
+
70
+ /** @typedef {Extract<typeof tokens[number], { kind: 'option' }>} OptionToken */
71
+ const optionTokens = tokens.filter(/** @type {(token: typeof tokens[number]) => token is OptionToken} */ (token) => token.kind === 'option');
72
+
73
+ const bools = obj.options ? Object.entries(obj.options).filter(([, { type }]) => type === 'boolean') : [];
74
+ const boolMap = new Map(bools);
75
+ for (let i = 0; i < optionTokens.length; i += 1) {
76
+ const { name, value } = optionTokens[i];
77
+ if (boolMap.has(name) && typeof value !== 'boolean' && typeof value !== 'undefined') {
78
+ errors.push(`Error: Argument --${name} must be a boolean`);
79
+ }
80
+ }
81
+
82
+ const passedArgs = new Set(optionTokens.map(({ name, rawName }) => (rawName === '--no-help' ? rawName : name)));
83
+
84
+ const groups = Object.groupBy(passedArgs, (x) => x.replace(/^no-/, ''));
85
+ for (let i = 0; i < bools.length; i++) {
86
+ const [key] = bools[i];
87
+ if ((groups[key]?.length ?? 0) > 1) {
88
+ errors.push(`Error: Arguments \`--${key}\` and \`--no-${key}\` are mutually exclusive`);
89
+ }
90
+ if (passedArgs.has(`no-${key}`)) {
91
+ // @ts-expect-error
92
+ results.values[key] = !results.values[`no-${key}`];
93
+ }
94
+ // @ts-expect-error
95
+ delete results.values[`no-${key}`];
96
+ }
97
+
98
+ const knownOptions = obj.options ? Object.keys(obj.options) : [];
99
+ const unknownArgs = knownOptions.length > 0 ? passedArgs.difference(new Set(knownOptions)) : passedArgs;
100
+ if (unknownArgs.size > 0) {
101
+ errors.push(`Error: Unknown option(s): ${Array.from(unknownArgs, (x) => `\`${x}\``).join(', ')}`);
102
+ }
103
+
104
+ return {
105
+ errors,
106
+ ...results,
107
+ ...obj.tokens && { tokens },
108
+ };
109
+ } catch (e) {
110
+ if (isParseArgsError(e)) {
111
+ return {
112
+ values: {},
113
+ positionals: [],
114
+ errors: [`Error: ${e.message}`],
115
+ };
116
+ }
117
+ throw e;
118
+ }
119
+ }
package/results.mjs ADDED
@@ -0,0 +1,23 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ import { commitToCoAuthors } from 'commit-to-co-authors';
4
+
5
+ import getDefaultBranch from './getDefaultBranch.mjs';
6
+
7
+ /** @type {(remote: string) => Set<string>} */
8
+ export default function getResults(remote) {
9
+ const defaultBranch = getDefaultBranch(remote);
10
+
11
+ const logCommitters = `${execSync(`git shortlog -sne ${remote}/${defaultBranch}..HEAD`)}`;
12
+ const mappedLogCommitters = logCommitters.matchAll(/\t(?<author>.*)$/gm)
13
+ // @ts-expect-error waiting on https://github.com/microsoft/TypeScript/pull/58222
14
+ .map(/** @type {(m: { groups: { author: string } }) => string} */ ({ groups: { author } }) => author);
15
+ /** @type {Set<string>} */
16
+ const fromLogs = new Set(mappedLogCommitters);
17
+
18
+ const logText = `${execSync(`git log --no-expand-tabs --pretty=full ${remote}/${defaultBranch}..HEAD`)}`;
19
+ /** @type {Set<string>} */
20
+ const fromMsgs = new Set(commitToCoAuthors(logText).map(({ name, email }) => `${name} <${email}>`));
21
+
22
+ return fromMsgs.union(fromLogs);
23
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@ljharb/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "ESNext",
5
+ "module": "nodenext",
6
+ },
7
+ "exclude": [
8
+ "coverage"
9
+ ]
10
+ }
@@ -0,0 +1,22 @@
1
+ import { execSync } from 'child_process';
2
+
3
+ /** @type {(remote: string, cwd?: string) => string | { __proto__: null, error: string }} */
4
+ export default function validateRemote(remote, cwd = process.cwd()) {
5
+ if (remote === '' || remote.includes(' ')) {
6
+ return {
7
+ __proto__: null,
8
+ error: 'Remote name must not be empty, nor contain spaces.',
9
+ };
10
+ }
11
+
12
+ const allRemotes = `${execSync('git remote', { cwd })}`.split('\n');
13
+
14
+ if (!allRemotes.includes(remote)) {
15
+ return {
16
+ __proto__: null,
17
+ error: `Remote \`${remote}\` does not exist; check \`git remote\` output`,
18
+ };
19
+ }
20
+
21
+ return remote;
22
+ }