Skip to content

Commit

Permalink
feat(cli): czg cli add core commit msg
Browse files Browse the repository at this point in the history
link #37
  • Loading branch information
Zhengqbbb committed Jun 21, 2022
1 parent f79e9c5 commit 7d9dc15
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 12 deletions.
Empty file removed packages/cli/src/core/bootstrap.ts
Empty file.
Empty file removed packages/cli/src/core/index.ts
Empty file.
40 changes: 40 additions & 0 deletions packages/cli/src/generator/commit.ts
@@ -0,0 +1,40 @@
// import path from "path";
import cacheDir from "cachedir";
import { ensureDir } from "fs-extra";
import { gitCommit } from "../shared";
import type { CommitizenType } from "cz-git";
import type { CallBackFn, CzGitPrompter, CommitOptions } from "../shared";

/**
* generate cz-git prompt get commit message
*/
export const commit = (
inquirer: CommitizenType,
rootPath: string,
prompter: CzGitPrompter,
options: CommitOptions,
done: CallBackFn
) => {
const cacheDirectory = cacheDir("cz-git");
// const cachePath = path.join(cacheDirectory, "commit.json");

ensureDir(cacheDirectory, (err: any) => {
if (err) {
console.error("Couldn't create commitizen cache directory: ", err);
} else {
if (options.retryLastCommit) {
console.log("Retrying last commit attempt.");
// TODO: get cache data
// TODO: retry last commit
} else {
prompter(inquirer, (commitMsg: string | Error) => {
if (commitMsg instanceof Error) {
return done(commitMsg);
}
// TODO: add cache
gitCommit(rootPath, commitMsg, options, done);
});
}
}
});
};
52 changes: 52 additions & 0 deletions packages/cli/src/generator/czg.ts
@@ -0,0 +1,52 @@
import { CommitizenType, prompter, style } from "cz-git";
import inquirer from "inquirer";
import { commit } from "./commit";
import { isGitClean, getGitRootPath } from "../shared";

/**
* start inquirer prompts to commit message
*/
export const czg = (version: string, commandArgs: string[], environment: any = {}) => {
// TODO: parse commandArgs
// console.log(commandArgs);
// console.log(environment);
// parse git hook and git -a
// isClean
isGitClean(
process.cwd(),
(error, isClean) => {
if (error) {
throw error;
}

if (isClean) {
throw new Error(
`No files added to staging! Did you forget to run ${style.cyan("git add")} ?`
);
}

console.log(`czg@${version}\n`);
// commit
commit(
inquirer as CommitizenType,
getGitRootPath(),
prompter,
{
args: [],
disableAppendPaths: true,
emitData: true,
quiet: false,
retryLastCommit: false,
hookMode: false
},
(error) => {
if (error) {
console.log(environment);
throw error;
}
}
);
},
false
);
};
2 changes: 1 addition & 1 deletion packages/cli/src/generator/help.ts
Expand Up @@ -12,7 +12,7 @@ ${style.yellow("WEBSITE:")}
${style.yellow("VERSION:")} ${version}
${style.yellow("USAGE:")}
${style.yellow("SYNOPSIS:")}
czg [subcommand] [options]
${style.yellow("SUBCOMMAND:")}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/generator/index.ts
@@ -1 +1,3 @@
export * from "./commit";
export * from "./czg";
export * from "./help";
35 changes: 24 additions & 11 deletions packages/cli/src/index.ts
@@ -1,5 +1,5 @@
#!/usr/bin/env node
import { generateHelp } from "./generator";
import { czg, generateHelp } from "./generator";

process.on("uncaughtException", function (err) {
console.error(err.message || err);
Expand All @@ -23,23 +23,36 @@ export const bootsrap = (environment: any = {}, argv = process.argv) => {
const subCommand = commandArgs[0] || "";
const czgitVersion = require("../package.json").version;

if (!subCommand) console.log("commit", environment);
if (!subCommand) {
czg(czgitVersion, commandArgs, environment);
return;
}

/* eslint-disable prettier/prettier */
/* prettier-ignore */
switch (true) {
// options
case /^(--config)$/.test(subCommand): console.log("config file"); process.exit(0);
case /^(--reback|-b)$/.test(subCommand): console.log("reback"); process.exit(0);
case /^(--retry|-r$)$/.test(subCommand): console.log("retry"); process.exit(0);
case /^(--config)$/.test(subCommand):
console.log("config file"); process.exit(0);
case /^(--reback|-b)$/.test(subCommand):
console.log("reback"); process.exit(0);
case /^(--retry|-r$)$/.test(subCommand):
console.log("retry"); process.exit(0);

// subCommand
case /^(init)$/.test(subCommand): console.log("init"); process.exit(0);
case /^(emoji)$/.test(subCommand): console.log("emoji"); process.exit(0);
case /^(checkbox)$/.test(subCommand): console.log("checkbox"); process.exit(0);
case /^(version|-v|--version)$/.test(subCommand): console.log(czgitVersion); process.exit(0);
case /^(help|-h|--help)$/.test(subCommand): generateHelp(czgitVersion); return;
case /^(init)$/.test(subCommand):
console.log("init"); process.exit(0);
case /^(emoji)$/.test(subCommand):
console.log("emoji"); process.exit(0);
case /^(checkbox)$/.test(subCommand):
console.log("checkbox"); process.exit(0);
case /^(version|-v|--version)$/.test(subCommand):
console.log(czgitVersion); process.exit(0);
case /^(help|-h|--help)$/.test(subCommand):
generateHelp(czgitVersion); return;

default: console.log("commit"); process.exit(0);
default:
czg(czgitVersion, commandArgs, environment); return;
}
};

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/shared/index.ts
@@ -0,0 +1,2 @@
export * from "./types";
export * from "./utils";
14 changes: 14 additions & 0 deletions packages/cli/src/shared/types/common.ts
@@ -0,0 +1,14 @@
import { prompter } from "cz-git";

export type CallBackFn = (err: Error | null, data?: any) => void;

export type CzGitPrompter = typeof prompter;

export type CommitOptions = {
args: any[];
disableAppendPaths: boolean;
emitData: boolean;
quiet: boolean;
retryLastCommit: boolean;
hookMode: boolean;
};
1 change: 1 addition & 0 deletions packages/cli/src/shared/types/index.ts
@@ -0,0 +1 @@
export * from "./common";
17 changes: 17 additions & 0 deletions packages/cli/src/shared/types/types.d.ts
@@ -0,0 +1,17 @@
declare module "dedent" {
/**
* ES6 string tag that strips indentation from multi-line strings.
* @see: https://github.com/dmnd/dedent
*/
export default function dedent(str: string): string;
}

declare module "cachedir" {
/**
* Get a directory for your caching needs
* @see: https://github.com/LinusU/node-cachedir
* @param {string} name
* @return {string} cacheDir
*/
export default function cachedir(name: string): string;
}
1 change: 1 addition & 0 deletions packages/cli/src/shared/utils/index.ts
@@ -0,0 +1 @@
export * from "./git";

0 comments on commit 7d9dc15

Please sign in to comment.