Skip to content

Commit

Permalink
Switch to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep authored Jan 28, 2024
1 parent 5b39cee commit 7e8c770
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 212 deletions.
8 changes: 6 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"$schema": "https://json.schemastore.org/mocharc.json",
"require": "ts-node/register",
"extensions": [
"js",
"ts",
"tsx"
],
"spec": [
"test/**/*.spec.*"
],
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}
}
21 changes: 7 additions & 14 deletions dist/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValueArg = exports.parseArgs = exports.addArg = exports.showHelp = void 0;
const utils_1 = require("./utils");
import { parseName } from "./utils";
class Arg {
constructor(options) {
Object.assign(this, options);
Expand All @@ -25,8 +22,8 @@ class ValueArg extends Arg {
}
}
const args = [];
const showHelp = ({ description, name }) => {
const { packageName } = (0, utils_1.parseName)(name);
export const showHelp = ({ description, name }) => {
const { packageName } = parseName(name);
const describedArgs = args.reduce((acc, { description, long, short }) => `${acc}-${short}, --${long}\t\t${description}\n`, "");
console.log(`
${description}
Expand All @@ -36,15 +33,14 @@ Usage: ${packageName} [options]
All Options:
${describedArgs}`);
};
exports.showHelp = showHelp;
args.push(new SimpleArg({
short: "h",
long: "help",
description: "show command help",
action: exports.showHelp,
action: showHelp,
passed: false,
}));
const addArg = (long, short, description, { hasValue, defaultValue, action } = {}) => {
export const addArg = (long, short, description, { hasValue, defaultValue, action } = {}) => {
const base = new Arg({
long,
short,
Expand All @@ -56,8 +52,7 @@ const addArg = (long, short, description, { hasValue, defaultValue, action } = {
? new ValueArg(Object.assign(Object.assign({}, base), { defaultValue, value: defaultValue }))
: new SimpleArg(base));
};
exports.addArg = addArg;
const parseArgs = (cliArgs) => {
export const parseArgs = (cliArgs) => {
const mappedToArgs = args.map((arg) => {
const { long, short } = arg;
const cliArgIdx = cliArgs.findIndex((cliArg) => cliArg === `--${long}` || cliArg === `-${short}`);
Expand All @@ -70,9 +65,7 @@ const parseArgs = (cliArgs) => {
});
return Object.fromEntries(mappedToArgs);
};
exports.parseArgs = parseArgs;
const getValueArg = (args, key) => {
export const getValueArg = (args, key) => {
const arg = args[key];
return arg.hasValue ? arg : null;
};
exports.getValueArg = getValueArg;
14 changes: 5 additions & 9 deletions dist/contributors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatContributors = void 0;
const formatters_1 = require("./formatters");
const utils_1 = require("./utils");
const formatContributors = (contributors) => {
import { formatEmail, formatUrl } from "./formatters";
import { parseAuthor } from "./utils";
export const formatContributors = (contributors) => {
return contributors
.map((c) => {
const { name, email, url } = (0, utils_1.parseAuthor)(c);
return `${name}${(0, formatters_1.formatEmail)(email)}${(0, formatters_1.formatUrl)(url)}`;
const { name, email, url } = parseAuthor(c);
return `${name}${formatEmail(email)}${formatUrl(url)}`;
})
.join("<br>");
};
exports.formatContributors = formatContributors;
17 changes: 5 additions & 12 deletions dist/formatters.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatImage = exports.formatMdRow = exports.formatUrl = exports.formatEmail = void 0;
const utils_1 = require("./utils");
const formatEmail = (email) => email ? `<br>${(0, utils_1.mdLink)(email, `mailto:${email}`)}` : "";
exports.formatEmail = formatEmail;
const formatUrl = (url) => url ? `<br>${(0, utils_1.mdLink)(url, url)}` : "";
exports.formatUrl = formatUrl;
const formatMdRow = (title, value) => `| ${(0, utils_1.scase)(title)} | ${value} |`;
exports.formatMdRow = formatMdRow;
const formatImage = (url, alt = url) => `!${(0, utils_1.mdLink)(alt, alt)}`;
exports.formatImage = formatImage;
import { mdLink, scase } from "./utils";
export const formatEmail = (email) => email ? `<br>${mdLink(email, `mailto:${email}`)}` : "";
export const formatUrl = (url) => url ? `<br>${mdLink(url, url)}` : "";
export const formatMdRow = (title, value) => `| ${scase(title)} | ${value} |`;
export const formatImage = (url, alt = url) => `!${mdLink(alt, alt)}`;
40 changes: 19 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -20,58 +19,57 @@ var __rest = (this && this.__rest) || function (s, e) {
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = void 0;
const chalk_1 = require("chalk");
const cli_1 = require("./cli");
const readme_1 = require("./readme");
const utils_1 = require("./utils");
(0, cli_1.addArg)("about", "a", "custom about text", {
import { pathToFileURL } from "url";
import chulk from "chalk";
import { addArg, parseArgs } from "./cli.js";
import { generateReadme, writeReadme } from "./readme.js";
import { getPackage, mapObject } from "./utils.js";
addArg("about", "a", "custom about text", {
hasValue: true
});
(0, cli_1.addArg)("package", "p", "path to package.json to use", {
addArg("package", "p", "path to package.json to use", {
defaultValue: "./package.json",
hasValue: true
});
(0, cli_1.addArg)("output", "o", "path to output directory", {
addArg("output", "o", "path to output directory", {
defaultValue: "./README.md",
hasValue: true
});
(0, cli_1.addArg)("screenshot", "s", "add a screenshot", { hasValue: true });
addArg("screenshot", "s", "add a screenshot", { hasValue: true });
const generate = ({ direct = false, output = "./README.md", package: pkg = "./package.json", about, screenshot }) => __awaiter(void 0, void 0, void 0, function* () {
const packageInfo = yield (0, utils_1.getPackage)(pkg);
const packageInfo = yield getPackage(pkg);
if (!packageInfo) {
console.log((0, chalk_1.bgRed) `package.json file not found or corrupted`);
console.log(chulk.bgRed `package.json file not found or corrupted`);
process.exitCode = 1;
return "";
}
const content = (0, readme_1.generateReadme)(packageInfo, {
const content = generateReadme(packageInfo, {
about,
screenshot
});
if (direct)
return content;
yield (0, readme_1.writeReadme)(output, content);
yield writeReadme(output, content);
return content;
});
exports.generate = generate;
const run = (args) => __awaiter(void 0, void 0, void 0, function* () {
const _a = (0, cli_1.parseArgs)(args.slice(2)), { help } = _a, rest = __rest(_a, ["help"]);
const _a = parseArgs(args.slice(2)), { help } = _a, rest = __rest(_a, ["help"]);
if (help.passed) {
const { passed, action } = help;
if (!passed || !action)
return;
const ownPackage = yield (0, utils_1.getPackage)("./package.json");
const ownPackage = yield getPackage("./package.json");
if (!ownPackage) {
console.log((0, chalk_1.bgRed) `own package.json missing or corrupted`);
console.log(chulk.bgRed `own package.json missing or corrupted`);
return;
}
help.run(ownPackage);
return;
}
const options = (0, utils_1.mapObject)(rest, (_, v) => v.hasValue && (v.value || v.defaultValue));
const options = mapObject(rest, (_, v) => v.hasValue && (v.value || v.defaultValue));
const content = yield generate(options);
return content;
});
if (require.main === module)
if (import.meta.url === pathToFileURL(process.argv[1]).href)
run(process.argv);
export { generate };
12 changes: 4 additions & 8 deletions dist/license.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatLicense = void 0;
const utils_1 = require("./utils");
const formatLicense = (license) => {
import { mdLink } from "./utils";
export const formatLicense = (license) => {
return license
? (0, utils_1.mdLink)(license, `https://spdx.org/licenses/${license}`)
: `Not adopted (see ${(0, utils_1.mdLink)("GitHub default grant", "https://docs.github.com/en/github/site-policy/github-terms-of-service#5-license-grant-to-other-users")})`;
? mdLink(license, `https://spdx.org/licenses/${license}`)
: `Not adopted (see ${mdLink("GitHub default grant", "https://docs.github.com/en/github/site-policy/github-terms-of-service#5-license-grant-to-other-users")})`;
};
exports.formatLicense = formatLicense;
49 changes: 22 additions & 27 deletions dist/readme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -8,31 +7,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeReadme = exports.generateReadme = void 0;
const chalk_1 = require("chalk");
const promises_1 = require("fs/promises");
const contributors_1 = require("./contributors");
const formatters_1 = require("./formatters");
const license_1 = require("./license");
const utils_1 = require("./utils");
const generateReadme = ({ author, contributors = [], description, license, name: packageName, version, bugs, }, { about, screenshot } = {}) => {
const { name, email, url } = (0, utils_1.parseAuthor)(author);
const aemail = (0, formatters_1.formatEmail)(email);
const alink = (0, formatters_1.formatUrl)(url);
const llink = (0, license_1.formatLicense)(license);
const contribs = (0, contributors_1.formatContributors)(contributors);
import chulk from "chalk";
import { open } from "fs/promises";
import { formatContributors } from "./contributors";
import { formatEmail, formatImage, formatMdRow, formatUrl } from "./formatters";
import { formatLicense } from "./license";
import { mdLink, parseAuthor, scase } from "./utils";
export const generateReadme = ({ author, contributors = [], description, license, name: packageName, version, bugs, }, { about, screenshot } = {}) => {
const { name, email, url } = parseAuthor(author);
const aemail = formatEmail(email);
const alink = formatUrl(url);
const llink = formatLicense(license);
const contribs = formatContributors(contributors);
const rows = [
(0, formatters_1.formatMdRow)("name", (0, utils_1.scase)(packageName)),
(0, formatters_1.formatMdRow)("description", description),
(0, formatters_1.formatMdRow)("license", llink),
(0, formatters_1.formatMdRow)("version", version),
formatMdRow("name", scase(packageName)),
formatMdRow("description", description),
formatMdRow("license", llink),
formatMdRow("version", version),
];
if (contribs)
rows.unshift((0, formatters_1.formatMdRow)("contributors", contribs));
rows.unshift(formatMdRow("contributors", contribs));
const screenshots = [];
if (screenshot) {
screenshots.push((0, formatters_1.formatImage)(screenshot));
screenshots.push(formatImage(screenshot));
}
const content = `
# About
Expand All @@ -44,22 +41,21 @@ ${about ? `\n${about}` : ""}
${screenshots.length ? `# Screenshots\n${screenshots.join("\n")}\n` : ""}
# Support
Bug reports for the project should be ${(0, utils_1.mdLink)("submitted here", bugs.url)}.
Bug reports for the project should be ${mdLink("submitted here", bugs.url)}.
<br>Before adding a new one, please check if it hasn't been raised before.
`;
return content;
};
exports.generateReadme = generateReadme;
const writeReadme = (path, content) => __awaiter(void 0, void 0, void 0, function* () {
export const writeReadme = (path, content) => __awaiter(void 0, void 0, void 0, function* () {
try {
const handle = yield (0, promises_1.open)(path, "w+");
const handle = yield open(path, "w+");
yield handle.write(content);
}
catch (error) {
if (error instanceof Error) {
const errorLog = `Failed to generate README:
${(0, chalk_1.bgRed)(error === null || error === void 0 ? void 0 : error.name)}
${chulk.bgRed(error === null || error === void 0 ? void 0 : error.name)}
${error === null || error === void 0 ? void 0 : error.message}
`;
console.log(errorLog);
Expand All @@ -70,4 +66,3 @@ const writeReadme = (path, content) => __awaiter(void 0, void 0, void 0, functio
process.exitCode = 1;
}
});
exports.writeReadme = writeReadme;
25 changes: 8 additions & 17 deletions dist/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -8,30 +7,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseName = exports.parseAuthor = exports.mdLink = exports.scase = exports.getPackage = exports.mapObject = void 0;
const promises_1 = require("fs/promises");
const mapObject = (obj, cbk) => {
import { readFile } from "fs/promises";
export const mapObject = (obj, cbk) => {
const output = {};
Object.entries(obj).forEach(([k, v]) => (output[k] = cbk(k, v)));
return output;
};
exports.mapObject = mapObject;
const getPackage = (path) => __awaiter(void 0, void 0, void 0, function* () {
export const getPackage = (path) => __awaiter(void 0, void 0, void 0, function* () {
try {
const contents = yield (0, promises_1.readFile)(path, { encoding: "utf-8" });
const contents = yield readFile(path, { encoding: "utf-8" });
return JSON.parse(contents);
}
catch (error) {
return null;
}
});
exports.getPackage = getPackage;
const scase = (text) => `${text[0].toUpperCase()}${text.slice(1).toLowerCase()}`;
exports.scase = scase;
const mdLink = (lbl, href) => `[${lbl}](${href})`;
exports.mdLink = mdLink;
const parseAuthor = (info) => {
export const scase = (text) => `${text[0].toUpperCase()}${text.slice(1).toLowerCase()}`;
export const mdLink = (lbl, href) => `[${lbl}](${href})`;
export const parseAuthor = (info) => {
if (typeof info === "object")
return info;
const authorRegex = /(\w+\s\w+)(?:\s<(.+?)>)?(?:\s\((.+?)\))?$/i;
Expand All @@ -42,9 +35,7 @@ const parseAuthor = (info) => {
url,
};
};
exports.parseAuthor = parseAuthor;
const parseName = (name) => {
export const parseName = (name) => {
const [, scope, packageName] = name.match(/(?:@([\w-]+)\/)?([\w-]+)/) || [];
return { scope, packageName };
};
exports.parseName = parseName;
Loading

0 comments on commit 7e8c770

Please sign in to comment.