Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 67 additions & 42 deletions bin/cmd.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#!/usr/bin/env node
'use strict';
import meow from "meow";
import path from "path";
import { parseArgs } from "node:util";
import { createRequire } from "node:module";
import path from "node:path";
import { setupLabels } from "../lib/github-label-setup.js";
import { writeGitHubReleaseYaml } from "../lib/github-release-yml.js";

/*
{
input: ['unicorns'],
flags: {rainbow: true},
...
}
*/
const cli = meow(`
const require = createRequire(import.meta.url);
const pkg = require("../package.json");
const helpText = `
Usage
$ github-label-setup --token xxx

Options

-h, --help [Boolean] output usage information
--version [Boolean] output the version number
-l, --labels <path> [Path:String] the path to look for the label configuration in. Default: labels.json
--token <token> [String] a GitHub access token (also settable with a GITHUB_TOKEN or GITHUB_ACCESS_TOKEN environment variable)
-d, --dry-run [Boolean] calculate the required label changes but do not apply them
Expand All @@ -37,48 +34,76 @@ const cli = meow(`
# Add .github/release.yml to integrate with GitHub Releases
$ github-label-setup --addReleaseYml

`, {

importMeta: import.meta,
flags: {
dryRun: {
type: 'boolean',
alias: 'd'
},
token: {
type: "string",
},
labels: {
type: 'string',
alias: 'l'
},
allowAddedLabels: {
type: 'boolean',
alias: 'A'
},
addReleaseYml: {
type: "boolean",
default: false
},
addReleaseYmlOutputPath: {
type: "string",
default: path.join(process.cwd(), ".github/release.yml")
}
`;
const showHelp = (exitCode = 2) => {
console.log(helpText);
process.exit(exitCode);
};

// meow accepted camelCase flags too. Normalize them to dashed names so the last one wins.
const camelCaseFlags = {
dryRun: "dry-run",
allowAddedLabels: "allow-added-labels"
};
const terminatorIndex = process.argv.indexOf("--", 2);
const args = process.argv.slice(2).map((arg, index) => {
if (terminatorIndex !== -1 && index + 2 >= terminatorIndex) {
return arg;
}
const match = arg.match(/^--(no-)?([^=]+)(=.*)?$/);
if (!match || !Object.hasOwn(camelCaseFlags, match[2])) {
return arg;
}
return `--${match[1] ?? ""}${camelCaseFlags[match[2]]}${match[3] ?? ""}`;
});
let values;
try {
({ values } = parseArgs({
args,
allowNegative: true,
Comment thread
azu marked this conversation as resolved.
options: {
help: { type: "boolean", short: "h" },
version: { type: "boolean" },
"dry-run": { type: "boolean", short: "d" },
token: { type: "string" },
labels: { type: "string", short: "l" },
"allow-added-labels": { type: "boolean", short: "A" },
addReleaseYml: { type: "boolean", default: false },
addReleaseYmlOutputPath: { type: "string", default: path.join(process.cwd(), ".github/release.yml") }
Comment thread
azu marked this conversation as resolved.
}
}));
} catch (error) {
console.error(error.message);
showHelp();
}
if (values.help) {
showHelp(0);
}
if (values.version) {
console.log(pkg.version);
process.exit(0);
}
const flags = {
dryRun: values["dry-run"],
token: values.token,
labels: values.labels,
allowAddedLabels: values["allow-added-labels"],
addReleaseYml: values.addReleaseYml,
addReleaseYmlOutputPath: values.addReleaseYmlOutputPath
};

if (cli.flags.addReleaseYml) {
const outputPath = cli.flags.addReleaseYmlOutputPath;
if (flags.addReleaseYml) {
const outputPath = flags.addReleaseYmlOutputPath;
if (!outputPath) {
throw new Error("addReleaseYmlOutputPath must not be empty");
}
writeGitHubReleaseYaml(outputPath).catch(function (error) {
console.error(error);
cli.showHelp();
showHelp();
});
} else {
setupLabels(cli.flags).catch(function (error) {
setupLabels(flags).catch(function (error) {
console.error(error);
cli.showHelp();
showHelp();
});
}
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@
},
"dependencies": {
"git-remote-origin-url": "^4.0.0",
"github-label-sync": "^2.3.1",
"hosted-git-info": "^6.1.3",
"meow": "^11.0.0",
"try-resolve": "^1.0.1"
"github-label-sync": "^2.3.1"
},
"devDependencies": {
"@types/github-label-sync": "^2.3.3",
"@types/hosted-git-info": "^3.0.5",
"@types/mocha": "^10.0.10",
"@types/node": "^26.6.5",
"mocha": "^12.0.3",
Expand Down
Loading
Loading