Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support --publish and --canary #123

Merged
merged 10 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ 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.
- `--publishTag` Use custom npm tag for publishing (Default is `latest`)
- `--nameSuffix`: Adds suffix to package name (Example: `--nameSuffix canary` renames `foo` to `foo-canary`)
- `--versionSuffix`: Adds suffix to package version. When set without value or to `true`, uses date + commit hash as commit
- `--canary`. Shortcut to `--bump --versionSuffix --nameSuffix [name]`.
- `-r`: Release as specific version.
- `--major`: Bump as a semver-major version
- `--minor`: Bump as a semver-minor version
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"pkg-types": "^1.0.3",
"scule": "^1.0.0",
"semver": "^7.5.3",
"std-env": "^3.3.3",
"yaml": "^2.3.1"
},
"devDependencies": {
Expand Down
725 changes: 409 additions & 316 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
generateMarkDown,
BumpVersionOptions,
} from "..";
import { npmPublish, renamePackage } from "../package";
import { githubRelease } from "./github";

export default async function defaultMain(args: Argv) {
Expand All @@ -22,7 +23,7 @@ export default async function defaultMain(args: Argv) {
from: args.from,
to: args.to,
output: args.output,
newVersion: args.r,
newVersion: typeof args.r === "string" ? args.r : undefined,
});

const logger = consola.create({ stdout: process.stderr });
Expand All @@ -37,6 +38,20 @@ export default async function defaultMain(args: Argv) {
!(c.type === "chore" && c.scope === "deps" && !c.isBreaking)
);

// Shortcut for canary releases
if (args.canary) {
const canaryName = typeof args.canary === "string" ? args.canary : "canary";
if (args.bump === undefined) {
args.bump = true;
}
if (args.versionSuffix === undefined) {
args.versionSuffix = true;
}
if (args.nameSuffix === undefined) {
args.nameSuffix = canaryName;
}
}

// Bump version optionally
if (args.bump || args.release) {
const bumpOptions = _getBumpVersionOptions(args);
Expand Down Expand Up @@ -83,6 +98,16 @@ export default async function defaultMain(args: Argv) {
await fsp.writeFile(config.output, changelogMD);
}

if (typeof args.nameSuffix === "string") {
await renamePackage(config, `-${args.nameSuffix}`);
}
if (args.publish) {
if (args.publishTag) {
config.publish.tag = args.publishTag;
}
await npmPublish(config);
}

// Commit and tag changes for release mode
if (args.release) {
if (args.commit !== false) {
Expand Down Expand Up @@ -120,6 +145,12 @@ export default async function defaultMain(args: Argv) {
}

function _getBumpVersionOptions(args: Argv): BumpVersionOptions {
if (args.versionSuffix) {
return {
suffix: args.versionSuffix,
};
}

for (const type of [
"major",
"premajor",
Expand Down
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface ChangelogConfig {
to: string;
newVersion?: string;
output: string | boolean;
publish: {
args?: string[];
tag?: string;
private?: boolean;
};
templates: {
commitMessage?: string;
tagMessage?: string;
Expand Down Expand Up @@ -49,6 +54,11 @@ const getDefaultConfig = () =>
process.env.GITHUB_TOKEN ||
process.env.GH_TOKEN,
},
publish: {
private: false,
tag: "latest",
args: [],
},
templates: {
commitMessage: "chore(release): v{{newVersion}}",
tagMessage: "v{{newVersion}}",
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;
}
58 changes: 58 additions & 0 deletions src/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { resolve } from "pathe";
import consola from "consola";
import {
PackageJson,
readPackageJSON as _readPackageJSON,
writePackageJSON as _writePackageJSON,
} from "pkg-types";
import { isCI, provider } from "std-env";
import type { ChangelogConfig } from "./config";

import { execCommand } from "./exec";

export function readPackageJSON(config: ChangelogConfig) {
const path = resolve(config.cwd, "package.json");
return _readPackageJSON(path);
}

export function writePackageJSON(config: ChangelogConfig, pkg: PackageJson) {
const path = resolve(config.cwd, "package.json");
return _writePackageJSON(path, pkg);
}

export async function renamePackage(config: ChangelogConfig, newName: string) {
const pkg = await readPackageJSON(config);
if (newName.startsWith("-")) {
if (pkg.name.endsWith(newName)) {
return;
}
newName = pkg.name + newName;
}
consola.info(`Updating package name from ${pkg.name} to ${newName}`);
pkg.name = newName;
await writePackageJSON(config, pkg);
}

export async function npmPublish(config: ChangelogConfig) {
const pkg = await readPackageJSON(config);

const args = [...config.publish.args];

if (!config.publish.private && !pkg.private) {
args.push("--access", "public");
}

if (config.publish.tag) {
args.push("--tag", config.publish.tag);
}

if (
isCI &&
provider === "github_actions" &&
process.env.NPM_CONFIG_PROVENANCE !== "false"
) {
args.push("--provenance");
}

return await execCommand("npm", ["publish", ...args]);
}
21 changes: 15 additions & 6 deletions src/semver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { resolve } from "node:path";
import semver from "semver";
import consola from "consola";
import { readPackageJSON, writePackageJSON } from "pkg-types";
import type { ChangelogConfig } from "./config";
import type { GitCommit } from "./git";
import { readPackageJSON, writePackageJSON } from "./package";

export type SemverBumpType =
| "major"
Expand Down Expand Up @@ -37,6 +36,7 @@ export function determineSemverChange(
export type BumpVersionOptions = {
type?: SemverBumpType;
preid?: string;
suffix?: boolean;
};

export async function bumpVersion(
Expand All @@ -47,8 +47,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 = await readPackageJSON(config);
const currentVersion = pkg.version || "0.0.0";

if (currentVersion.startsWith("0.")) {
Expand All @@ -60,21 +59,31 @@ export async function bumpVersion(
}

if (config.newVersion) {
consola.log("A", JSON.stringify(config.newVersion));
pi0 marked this conversation as resolved.
Show resolved Hide resolved
pkg.version = config.newVersion;
} else if (type) {
} else if (type || opts.preid) {
// eslint-disable-next-line import/no-named-as-default-member
pkg.version = semver.inc(currentVersion, type, opts.preid);
config.newVersion = pkg.version;
}

if (opts.suffix) {
const suffix =
typeof opts.suffix === "string"
? `-${opts.suffix}`
: `-${Math.round(Date.now() / 1000)}.${commits[0].shortHash}`;
pkg.version = config.newVersion = config.newVersion.split("-")[0] + suffix;
}

if (pkg.version === currentVersion) {
return false;
}

consola.info(
`Bumping version from ${currentVersion} to ${pkg.version} (${originalType})`
);
await writePackageJSON(pkgPath, pkg);

await writePackageJSON(config, pkg);

return pkg.version;
}