Skip to content

Commit

Permalink
feat: add support for publishing + edge releases
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jul 3, 2023
1 parent d5d8455 commit c3803c5
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ npx changelogen@latest [...args] [--dir <dir>]
- `--output`: Changelog file name to create or update. Defaults to `CHANGELOG.md` and resolved relative to dir. Use `--no-output` to write to console only.
- `--bump`: Determine semver change and update version in `package.json`.
- `--release`. Bumps version in `package.json` and creates commit and git tags using local `git`. You can disable commit using `--no-commit` and tag using `--no-tag`. You can enable the automatic push of the new tag and release commit to your git repository by adding `--push`.
- `--publish`. Publishes package as a new version on `npm`. You will need to set authorisation tokens separately via `.npmrc` or environment variables.
- `--edge`. Bumps version in `package.json` to an edge-compatible version based on type of commit since last release + date + commit hash.
- `-r`: Release as specific version.
- `--major`: Bump as a semver-major version
- `--minor`: Bump as a semver-minor version
Expand Down
17 changes: 15 additions & 2 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
bumpVersion,
generateMarkDown,
BumpVersionOptions,
updatePackageName,
} from "..";
import { publishNpmPackage } from "../publish";
import { githubRelease } from "./github";

export default async function defaultMain(args: Argv) {
Expand All @@ -22,6 +24,8 @@ export default async function defaultMain(args: Argv) {
from: args.from,
to: args.to,
output: args.output,
edge: args.edge,
publish: args.publish,
newVersion: args.r,
});

Expand All @@ -38,7 +42,7 @@ export default async function defaultMain(args: Argv) {
);

// Bump version optionally
if (args.bump || args.release) {
if (args.bump || args.release || args.edge) {
const bumpOptions = _getBumpVersionOptions(args);
const newVersion = await bumpVersion(commits, config, bumpOptions);
if (!newVersion) {
Expand All @@ -48,11 +52,16 @@ export default async function defaultMain(args: Argv) {
config.newVersion = newVersion;
}

// Update package name if performing an edge release
if (args.edge) {
await updatePackageName(config);
}

// Generate markdown
const markdown = await generateMarkDown(commits, config);

// Show changelog in CLI unless bumping or releasing
const displayOnly = !args.bump && !args.release;
const displayOnly = (!args.bump && !args.release) || args.edge;
if (displayOnly) {
consola.log("\n\n" + markdown + "\n\n");
}
Expand Down Expand Up @@ -83,6 +92,10 @@ export default async function defaultMain(args: Argv) {
await fsp.writeFile(config.output, changelogMD);
}

if (args.publish) {
await publishNpmPackage(config);
}

// Commit and tag changes for release mode
if (args.release) {
if (args.commit !== false) {
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface ChangelogConfig {
from: string;
to: string;
newVersion?: string;
publish?: boolean;
edge?: boolean;
edgeTag?: string;
edgePackage?: string;
output: string | boolean;
templates: {
commitMessage?: string;
Expand Down
11 changes: 11 additions & 0 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Options } from "execa";

export async function execCommand(
cmd: string,
args: string[],
options?: Options<string>
) {
const { execa } = await import("execa");
const res = await execa(cmd, args, options);
return res.stdout;
}
7 changes: 1 addition & 6 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChangelogConfig } from "./config";
import { execCommand } from "./exec";

export interface GitCommitAuthor {
name: string;
Expand Down Expand Up @@ -151,9 +152,3 @@ export function parseGitCommit(
isBreaking,
};
}

async function execCommand(cmd: string, args: string[]) {
const { execa } = await import("execa");
const res = await execa(cmd, args);
return res.stdout;
}
13 changes: 13 additions & 0 deletions src/pkg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { resolve } from "pathe";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import { ChangelogConfig } from "./config";

export async function usePkg(config: ChangelogConfig) {
const pkgPath = resolve(config.cwd, "package.json");
const pkg = await readPackageJSON(pkgPath);

return {
pkg,
writePkg: () => writePackageJSON(pkgPath, pkg),
};
}
12 changes: 12 additions & 0 deletions src/publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ChangelogConfig } from "./config";
import { execCommand } from "./exec";

export async function publishNpmPackage(config: ChangelogConfig) {
const args = ["publish"];

if (config.edge && (!config.edgePackage || config.edgeTag)) {
args.push("--tag", config.edgeTag ?? "edge");
}

return await execCommand("npm", args);
}
30 changes: 27 additions & 3 deletions src/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import consola from "consola";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import type { ChangelogConfig } from "./config";
import type { GitCommit } from "./git";
import { usePkg } from "./pkg";

export type SemverBumpType =
| "major"
Expand Down Expand Up @@ -47,8 +48,7 @@ export async function bumpVersion(
let type = opts.type || determineSemverChange(commits, config) || "patch";
const originalType = type;

const pkgPath = resolve(config.cwd, "package.json");
const pkg = await readPackageJSON(pkgPath);
const { pkg, writePkg } = await usePkg(config);
const currentVersion = pkg.version || "0.0.0";

if (currentVersion.startsWith("0.")) {
Expand All @@ -61,6 +61,13 @@ export async function bumpVersion(

if (config.newVersion) {
pkg.version = config.newVersion;
} else if (config.edge) {
const date = Math.round(Date.now() / (1000 * 60));
const hash = commits[0].shortHash;
// eslint-disable-next-line import/no-named-as-default-member
const nextVersion = semver.inc(currentVersion, type, opts.preid);
pkg.version = `${nextVersion}-${date}.${hash}`;
config.newVersion = pkg.version;
} else if (type) {
// eslint-disable-next-line import/no-named-as-default-member
pkg.version = semver.inc(currentVersion, type, opts.preid);
Expand All @@ -74,7 +81,24 @@ export async function bumpVersion(
consola.info(
`Bumping version from ${currentVersion} to ${pkg.version} (${originalType})`
);
await writePackageJSON(pkgPath, pkg);
await writePkg();

return pkg.version;
}

export async function updatePackageName(
config: ChangelogConfig
): Promise<string | false> {
if (!config.edge || !config.edgePackage) {
return false;
}

const { pkg, writePkg } = await usePkg(config);
consola.info(
`Updating package name from ${pkg.name} to ${config.edgePackage} for edge release`
);
pkg.name = config.edgePackage;
await writePkg();

return pkg.name;
}

0 comments on commit c3803c5

Please sign in to comment.