feat: initial commit
This commit is contained in:
164
src/index.ts
Normal file
164
src/index.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import {
|
||||
cancel,
|
||||
confirm,
|
||||
intro,
|
||||
isCancel,
|
||||
outro,
|
||||
select,
|
||||
text,
|
||||
} from "@clack/prompts";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { Config } from "./types";
|
||||
import simpleGit from "simple-git";
|
||||
|
||||
const GetConfig = async () => {
|
||||
if (fs.existsSync(path.join(process.cwd(), ".rczrc"))) {
|
||||
return (await fs.promises.readFile(
|
||||
path.join(process.cwd(), ".rczrc")
|
||||
)) as Config;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const config = await GetConfig();
|
||||
|
||||
intro(`Creating a conventional commit message`);
|
||||
|
||||
if (!fs.existsSync(path.join(process.cwd(), ".git"))) {
|
||||
cancel("Git repository has not been initialized");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const stageAll = await confirm({
|
||||
message: "Stage all changes?",
|
||||
initialValue: true,
|
||||
});
|
||||
|
||||
if (isCancel(stageAll)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const type: string | symbol = await select({
|
||||
message: "Choose a commit type",
|
||||
options: [
|
||||
{
|
||||
label: "feat",
|
||||
value: "feat",
|
||||
hint: "new feature",
|
||||
},
|
||||
{
|
||||
label: "fix",
|
||||
value: "fix",
|
||||
hint: "bug fix",
|
||||
},
|
||||
{
|
||||
label: "build",
|
||||
value: "build",
|
||||
hint: "changes that affect the build system, configs or external dependencies (e.g. npm, .prettierrc)",
|
||||
},
|
||||
{
|
||||
label: "ci",
|
||||
value: "ci",
|
||||
hint: "change to CI/CD configurations and scripts (e.g. CircleCI, GitHub workflows)",
|
||||
},
|
||||
{
|
||||
label: "docs",
|
||||
value: "docs",
|
||||
hint: "documentation changes (e.g. README, CHANGELOG)",
|
||||
},
|
||||
{
|
||||
label: "perf",
|
||||
value: "perf",
|
||||
hint: "code change, that improves performance",
|
||||
},
|
||||
{
|
||||
label: "refactor",
|
||||
value: "refactor",
|
||||
hint: "code change that neither fixes a bug nor adds a feature",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (isCancel(type)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const scope: string | symbol = await text({
|
||||
message: "Input a scope (e.g. router, forms, core) or leave empty",
|
||||
validate: (value) => {
|
||||
if (config?.scopes) {
|
||||
if (!config?.scopes.includes(value))
|
||||
return "This scope is not allowed by local configuration";
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (isCancel(scope)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const message = await text({
|
||||
message: `Briefly describe made changes in imperative tense, maximum length 50`,
|
||||
validate: (value) => {
|
||||
if (value.length > 50) {
|
||||
return "Your message is too long";
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (isCancel(message)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const isBreaking = await confirm({
|
||||
message: "Does this commit have breaking changes?",
|
||||
initialValue: false,
|
||||
});
|
||||
|
||||
if (isCancel(isBreaking)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const resolvesIssue = await confirm({
|
||||
message: "Does this commit resolve an existing issue?",
|
||||
initialValue: false,
|
||||
});
|
||||
|
||||
if (isCancel(resolvesIssue)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const issue = resolvesIssue
|
||||
? await text({ message: "Enter an issue identifier, e.g. #274" })
|
||||
: null;
|
||||
|
||||
if (isCancel(issue)) {
|
||||
cancel("Commit creation cancelled");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const commitMessage = `${type.toString()}${
|
||||
scope ? `(${scope.toString()})` : ``
|
||||
}${isBreaking ? "!" : ""}: ${message.toString()}${
|
||||
resolvesIssue ? ` (${issue?.toString()})` : ``
|
||||
}`;
|
||||
|
||||
if (stageAll) {
|
||||
await simpleGit().add(".").commit(commitMessage);
|
||||
} else {
|
||||
await simpleGit().commit(commitMessage);
|
||||
}
|
||||
|
||||
outro(`"${commitMessage}" - ready to push`);
|
||||
})();
|
||||
10
src/types.ts
Normal file
10
src/types.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface Config {
|
||||
commitTypes?: Array<Type>;
|
||||
scopes?: Array<string>;
|
||||
}
|
||||
|
||||
export interface Type {
|
||||
label: string;
|
||||
value: string;
|
||||
hint: string;
|
||||
}
|
||||
Reference in New Issue
Block a user