10 Commits

8 changed files with 202 additions and 66 deletions

View File

@@ -1,6 +1,6 @@
const { execSync } = require("child_process"); const { execSync } = require("child_process");
execSync(`git push -u origin --tags`); execSync(`git push -u origin main --tags`);
execSync(`pnpm run build`); execSync(`pnpm run build`);

View File

@@ -2,6 +2,7 @@
"cSpell.words": [ "cSpell.words": [
"Acked", "Acked",
"johndoe", "johndoe",
"numstat",
"onrelease", "onrelease",
"outro", "outro",
"postrelease", "postrelease",

View File

@@ -1,8 +1,27 @@
# Changelog # Changelog
Generation of this changelog is based on commits Generation of this changelog is based on commits
## v1.11.0
### Features
- [30a7e0b28] - **commands**: add --sudo option to commit command (#16)
## v1.10.1
### Fixes
- [782894dae] - **commands**: failure to cancel commit creation
### Miscellaneous
- [ebfa3859f] - **release**: v1.10.1
## v1.10.0
### Features
- [d707aaffe] - **config**: allow RCZ configs to be located in a folder (#15)
- [4de93ef3e] - **commands**: check for updates upon commit creation (#13)
- [83d655931] - **commands**: warn on >250 line commits, improve text (#13)
- [f371102a8] - **commands**: add aliases to main commands
### Miscellaneous
- [399f65dab] - **release**: v1.10.0
- [28e1caf28] - push to main post release
## v1.9.0 ## v1.9.0
### Features ### Features
- [4c60f62e7] - **config**: add post-release event file - [4c60f62e7] - **config**: add post-release event file
### Miscellaneous
- [7963d78e5] - **release**: v1.9.0
## v1.8.0 ## v1.8.0
### Features ### Features
- [1e8bfd04d] - **commands**: add option to change unreleased change title - [1e8bfd04d] - **commands**: add option to change unreleased change title

View File

@@ -1,6 +1,6 @@
{ {
"name": "@resultium/rcz", "name": "@resultium/rcz",
"version": "1.9.0", "version": "1.11.0",
"description": "Resultium commit standardization library, based on conventional commits", "description": "Resultium commit standardization library, based on conventional commits",
"main": "./dist/index.js", "main": "./dist/index.js",
"bin": { "bin": {
@@ -27,13 +27,13 @@
}, },
"dependencies": { "dependencies": {
"@clack/prompts": "^0.7.0", "@clack/prompts": "^0.7.0",
"commander": "^11.0.0", "commander": "^11.1.0",
"semver": "^7.5.4", "semver": "^7.5.4",
"simple-git": "^3.19.1" "simple-git": "^3.21.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.5.1", "@types/node": "^20.10.4",
"@types/semver": "^7.5.0", "@types/semver": "^7.5.6",
"typescript": "^5.1.6" "typescript": "^5.3.3"
} }
} }

46
pnpm-lock.yaml generated
View File

@@ -9,25 +9,25 @@ dependencies:
specifier: ^0.7.0 specifier: ^0.7.0
version: 0.7.0 version: 0.7.0
commander: commander:
specifier: ^11.0.0 specifier: ^11.1.0
version: 11.0.0 version: 11.1.0
semver: semver:
specifier: ^7.5.4 specifier: ^7.5.4
version: 7.5.4 version: 7.5.4
simple-git: simple-git:
specifier: ^3.19.1 specifier: ^3.21.0
version: 3.19.1 version: 3.21.0
devDependencies: devDependencies:
'@types/node': '@types/node':
specifier: ^20.5.1 specifier: ^20.10.4
version: 20.5.1 version: 20.10.4
'@types/semver': '@types/semver':
specifier: ^7.5.0 specifier: ^7.5.6
version: 7.5.0 version: 7.5.6
typescript: typescript:
specifier: ^5.1.6 specifier: ^5.3.3
version: 5.1.6 version: 5.3.3
packages: packages:
@@ -60,16 +60,18 @@ packages:
resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==} resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==}
dev: false dev: false
/@types/node@20.5.1: /@types/node@20.10.4:
resolution: {integrity: sha512-4tT2UrL5LBqDwoed9wZ6N3umC4Yhz3W3FloMmiiG4JwmUJWpie0c7lcnUNd4gtMKuDEO4wRVS8B6Xa0uMRsMKg==} resolution: {integrity: sha512-D08YG6rr8X90YB56tSIuBaddy/UXAA9RKJoFvrsnogAum/0pmjkgi4+2nx96A330FmioegBWmEYQ+syqCFaveg==}
dependencies:
undici-types: 5.26.5
dev: true dev: true
/@types/semver@7.5.0: /@types/semver@7.5.6:
resolution: {integrity: sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==} resolution: {integrity: sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==}
dev: true dev: true
/commander@11.0.0: /commander@11.1.0:
resolution: {integrity: sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==} resolution: {integrity: sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==}
engines: {node: '>=16'} engines: {node: '>=16'}
dev: false dev: false
@@ -108,8 +110,8 @@ packages:
lru-cache: 6.0.0 lru-cache: 6.0.0
dev: false dev: false
/simple-git@3.19.1: /simple-git@3.21.0:
resolution: {integrity: sha512-Ck+rcjVaE1HotraRAS8u/+xgTvToTuoMkT9/l9lvuP5jftwnYUp6DwuJzsKErHgfyRk8IB8pqGHWEbM3tLgV1w==} resolution: {integrity: sha512-oTzw9248AF5bDTMk9MrxsRzEzivMlY+DWH0yWS4VYpMhNLhDWnN06pCtaUyPnqv/FpsdeNmRqmZugMABHRPdDA==}
dependencies: dependencies:
'@kwsites/file-exists': 1.1.1 '@kwsites/file-exists': 1.1.1
'@kwsites/promise-deferred': 1.1.1 '@kwsites/promise-deferred': 1.1.1
@@ -122,12 +124,16 @@ packages:
resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==} resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==}
dev: false dev: false
/typescript@5.1.6: /typescript@5.3.3:
resolution: {integrity: sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==} resolution: {integrity: sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==}
engines: {node: '>=14.17'} engines: {node: '>=14.17'}
hasBin: true hasBin: true
dev: true dev: true
/undici-types@5.26.5:
resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
dev: true
/yallist@4.0.0: /yallist@4.0.0:
resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==}
dev: false dev: false

View File

@@ -16,6 +16,8 @@ import { CommitStack, Config } from "./types";
import simpleGit from "simple-git"; import simpleGit from "simple-git";
import { Command } from "commander"; import { Command } from "commander";
import semver from "semver"; import semver from "semver";
import { execSync } from "child_process";
import { tmpdir } from "os";
const GetConfig = async () => { const GetConfig = async () => {
if (fs.existsSync(path.join(process.cwd(), ".rczrc"))) { if (fs.existsSync(path.join(process.cwd(), ".rczrc"))) {
@@ -36,27 +38,108 @@ const GetConfig = async () => {
await fs.promises.readFile(path.join(process.cwd(), "rcz.config.json")) await fs.promises.readFile(path.join(process.cwd(), "rcz.config.json"))
).toString() ).toString()
) as Config; ) as Config;
} else if (fs.existsSync(path.join(process.cwd(), ".rcz", "config.json"))) {
return JSON.parse(
(
await fs.promises.readFile(
path.join(process.cwd(), ".rcz", "config.json")
)
).toString()
) as Config;
} else { } else {
return null; return null;
} }
}; };
const GetOnReleaseScript = async () => {
if (fs.existsSync(path.join(process.cwd(), ".rcz.onrelease.js"))) {
return (
await fs.promises.readFile(path.join(process.cwd(), ".rcz.onrelease.js"))
).toString();
} else if (fs.existsSync(path.join(process.cwd(), ".rcz", "onrelease.js"))) {
return (
await fs.promises.readFile(
path.join(process.cwd(), ".rcz", "onrelease.js")
)
).toString();
} else {
return null;
}
};
const GetPostReleaseScript = async () => {
if (fs.existsSync(path.join(process.cwd(), ".rcz.postrelease.js"))) {
return (
await fs.promises.readFile(
path.join(process.cwd(), ".rcz.postrelease.js")
)
).toString();
} else if (
fs.existsSync(path.join(process.cwd(), ".rcz", "postrelease.js"))
) {
return (
await fs.promises.readFile(
path.join(process.cwd(), ".rcz", "postrelease.js")
)
).toString();
} else {
return null;
}
};
const CheckForUpdates = async () => {
const updateText =
"You are running on an outdated version of Resultium Commitizen, in order to update run\n\nnpm install -g @resultium/rcz@latest";
const cachedVersion = fs.existsSync(path.join(tmpdir(), "rcz-server-version"))
? (await fs.promises.readFile(path.join(tmpdir(), "rcz-server-version")))
.toString()
.trim()
: null;
const localVersion = execSync("rcz --version").toString().trim();
// even if cached once in a while it should get newest data
if ((cachedVersion && Math.random() < 0.1) || cachedVersion === null) {
const isOnline = (await fetch("https://icanhazip.com")).status === 200;
if (!isOnline) return;
const serverVersion = execSync("npm show @resultium/rcz version")
.toString()
.trim();
fs.promises.writeFile(
path.join(tmpdir(), "rcz-server-version"),
serverVersion
);
if (semver.gt(serverVersion, localVersion)) note(updateText);
} else if (cachedVersion) {
if (semver.gt(cachedVersion, localVersion)) note(updateText);
}
};
const program = new Command(); const program = new Command();
program program
.name("rcz") .name("rcz")
.description("Resultium commit standardization command-line interface") .description("Resultium commit standardization command-line interface")
.version("1.9.0"); .version("1.11.0");
program program
.command("commit") .command("commit")
.alias("c")
.description("Create a conventional commit") .description("Create a conventional commit")
.option("-S, --sign", "sign the commit") .option("-S, --sign", "sign the commit")
.option("--amend", "amend commit message to the last commit") .option("--amend", "amend commit message to the last commit")
.option("--sudo", "remove any validation")
.action(async (options) => { .action(async (options) => {
await CheckForUpdates();
const config = await GetConfig(); const config = await GetConfig();
const sign = config?.autoSignCommits || options.sign ? true : false; const sign = config?.autoSignCommits || options.sign ? true : false;
const amend = options.amend ? true : false; const amend = options.amend ? true : false;
const sudo = options.sudo ? true : false;
intro("Creating a conventional commit"); intro("Creating a conventional commit");
@@ -79,6 +162,30 @@ program
process.exit(0); process.exit(0);
} }
const changedLines = (
(
await simpleGit().diff(["--numstat", stageAll ? "HEAD" : "--cached"])
).match(/\d+/gm) || []
).reduce((partialSum, num) => partialSum + Number(num), 0);
if (changedLines > 250 && !sudo) {
const proceedCommitting = await confirm({
message:
"You are about to commit changes to more than 250 lines, are you sure you want to proceed?",
initialValue: false,
});
if (isCancel(proceedCommitting)) {
cancel("Commit creation cancelled");
process.exit(0);
}
if (!proceedCommitting) {
cancel("Cancelled, please split this commit into smaller ones");
process.exit(0);
}
}
const type: string | symbol = await select({ const type: string | symbol = await select({
message: "Choose a commit type", message: "Choose a commit type",
options: config?.types || [ options: config?.types || [
@@ -90,7 +197,7 @@ program
{ {
label: "fix", label: "fix",
value: "fix", value: "fix",
hint: "bug fix", hint: "functionality bug fix",
}, },
{ {
label: "build", label: "build",
@@ -131,12 +238,22 @@ program
} }
const scope: string | symbol = await text({ const scope: string | symbol = await text({
message: "Input a scope (e.g. router, forms, core) or leave empty", message:
"Input a scope (subsystem which change is relevant to e.g. router, forms, core) or leave empty",
placeholder: "router",
validate: (value) => { validate: (value) => {
if (sudo) {
return
}
if (config?.scopes && value) { if (config?.scopes && value) {
if (!config?.scopes.includes(value)) if (!config?.scopes.includes(value))
return "This scope is not allowed by local configuration"; return "This scope is not allowed by local configuration";
} }
if (value.includes(" ")) {
return "Must not include a space";
}
}, },
}); });
@@ -147,7 +264,12 @@ program
const message = await text({ const message = await text({
message: `Briefly describe made changes in imperative tense, maximum length 50`, message: `Briefly describe made changes in imperative tense, maximum length 50`,
placeholder: "warn upon suspicious router requests",
validate: (value) => { validate: (value) => {
if (sudo) {
return
}
if (value.length > 50) { if (value.length > 50) {
return "Your message is too long"; return "Your message is too long";
} }
@@ -160,7 +282,9 @@ program
} }
const body = await text({ const body = await text({
message: `Insert a commit body, recommended length 100, can be left empty`, message: `Insert a commit body (motivation or elaboration for the change), can be left empty`,
placeholder:
"improves regex for suspicious character check in router requests",
}); });
if (isCancel(body)) { if (isCancel(body)) {
@@ -170,6 +294,7 @@ program
const footer = await text({ const footer = await text({
message: `Insert commit footer, can be left empty, e.g. Acked-by: @johndoe`, message: `Insert commit footer, can be left empty, e.g. Acked-by: @johndoe`,
placeholder: "Acked-by: @security",
}); });
if (isCancel(footer)) { if (isCancel(footer)) {
@@ -206,10 +331,8 @@ program
process.exit(0); process.exit(0);
} }
const commitMessage = `${type.toString()}${ const commitMessage = `${type.toString()}${scope ? `(${scope.toString()})` : ``
scope ? `(${scope.toString()})` : `` }${isBreaking ? "!" : ""}: ${message.toString()}${resolvesIssue ? ` (${issue?.toString()})` : ``
}${isBreaking ? "!" : ""}: ${message.toString()}${
resolvesIssue ? ` (${issue?.toString()})` : ``
}${body ? `\n\n${body}` : ``}${footer ? `\n\n${footer}` : ``}`; }${body ? `\n\n${body}` : ``}${footer ? `\n\n${footer}` : ``}`;
if (stageAll) { if (stageAll) {
@@ -229,14 +352,14 @@ program
note(commitMessage); note(commitMessage);
outro( outro(
`Finished ${ `Finished ${amend ? "amending" : "creating"
amend ? "amending" : "creating"
} a conventional commit, feel free to push` } a conventional commit, feel free to push`
); );
}); });
program program
.command("changelog") .command("changelog")
.alias("ch")
.description("Outputs a markdown formatted changelog") .description("Outputs a markdown formatted changelog")
.option("--show-hashes", "show first 9 characters of commit hashes") .option("--show-hashes", "show first 9 characters of commit hashes")
.option("--last-only", "display only latest release changes") .option("--last-only", "display only latest release changes")
@@ -351,8 +474,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -371,8 +493,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -391,8 +512,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -411,8 +531,7 @@ program
: firstMessageLine[0]; : firstMessageLine[0];
console.log( console.log(
`${showHashes ? `- [${shortHash}]` : ``} - ${ `${showHashes ? `- [${shortHash}]` : ``} - ${type ? `**${type}**: ${briefMessage}` : briefMessage
type ? `**${type}**: ${briefMessage}` : briefMessage
}` }`
); );
} }
@@ -422,6 +541,7 @@ program
program program
.command("release") .command("release")
.alias("rel")
.description( .description(
"Changes package.json version and creates a new commit with a tag" "Changes package.json version and creates a new commit with a tag"
) )
@@ -433,11 +553,7 @@ program
const sign = config?.autoSignReleases || options.sign ? true : false; const sign = config?.autoSignReleases || options.sign ? true : false;
const version = string.replace("v", ""); const version = string.replace("v", "");
const onReleaseFile = ( const onReleaseFile = await GetOnReleaseScript();
await fs.promises.readFile(
path.join(process.cwd(), ".rczrc.onrelease.js")
)
).toString();
if (onReleaseFile) { if (onReleaseFile) {
const releaseScript = ` const releaseScript = `
@@ -474,11 +590,7 @@ program
: [`-a`, `v${version}`, `-m`, `"v${version}"`] : [`-a`, `v${version}`, `-m`, `"v${version}"`]
); );
const postReleaseFile = ( const postReleaseFile = await GetPostReleaseScript();
await fs.promises.readFile(
path.join(process.cwd(), ".rczrc.postrelease.js")
)
).toString();
if (postReleaseFile) { if (postReleaseFile) {
const postReleaseScript = ` const postReleaseScript = `
@@ -507,11 +619,9 @@ program
// /(build|feat|docs)(\((commands|changelog)\))?!?: .* ?(\(..*\))?((\n\n..*)?(\n\n..*)?)?/gm // /(build|feat|docs)(\((commands|changelog)\))?!?: .* ?(\(..*\))?((\n\n..*)?(\n\n..*)?)?/gm
const testRegex = new RegExp( const testRegex = new RegExp(
`(${ `(${config?.types?.map((type) => type.value).join("|") ||
config?.types?.map((type) => type.value).join("|") ||
"feat|fix|build|ci|docs|perf|refactor|chore" "feat|fix|build|ci|docs|perf|refactor|chore"
})(\\((${ })(\\((${config?.scopes ? [...config?.scopes, "release"].join("|") : "..*"
config?.scopes ? [...config?.scopes, "release"].join("|") : "..*"
})\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`, })\\))?!?: .* ?(\\(..*\\))?((\n\n..*)?(\n\n..*)?)?`,
"gm" "gm"
); );