@done-coding/cli-publish 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @done-coding/cli-publish
2
+
3
+ 项目发布命令行工具
4
+
5
+ ## 使用
6
+
7
+ npm install @done-coding/cli-publish
package/es/cli.mjs ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ import { createCli as e } from "./main.mjs";
3
+ e();
package/es/handler.mjs ADDED
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+ import { PublishModeEnum as h } from "./utils/types.mjs";
3
+ import { inc as p, prerelease as d } from "semver";
4
+ import { join as u } from "node:path";
5
+ import { execSync as o } from "node:child_process";
6
+ import f from "pinyin";
7
+ import { readFileSync as y, existsSync as w } from "node:fs";
8
+ import c from "chalk";
9
+ const S = process.cwd(), $ = "/.dc/publish.json", b = "/package.json", v = ({ gitOriginName: r = "origin" }) => {
10
+ try {
11
+ const i = o("git rev-parse HEAD").toString().trim(), s = o('git log -1 --pretty=format:"%an"').toString().trim(), a = o('git log -1 --pretty=format:"%ae"').toString().trim(), t = o('git log -1 --pretty=format:"%s"').toString().trim(), n = o("git config user.name").toString().trim(), e = o("git config user.email").toString().trim(), g = o("git rev-parse --abbrev-ref HEAD").toString().trim();
12
+ let l = "";
13
+ try {
14
+ l = o(`git config --get remote.${r}.url`).toString().trim();
15
+ } catch {
16
+ throw new Error(`git remote ${r} 不存在`);
17
+ }
18
+ return {
19
+ lastHash: i,
20
+ lastCommitter: s,
21
+ lastCommitterPinYin: f(s, {
22
+ style: f.STYLE_NORMAL,
23
+ heteronym: !1
24
+ }).join(""),
25
+ lastCommitEmail: a,
26
+ lastCommitMsg: t,
27
+ userName: n,
28
+ userNamePinYin: f(n, {
29
+ style: f.STYLE_NORMAL,
30
+ heteronym: !1
31
+ }).join(""),
32
+ userEmail: e,
33
+ branchName: g,
34
+ remoteUrl: l
35
+ };
36
+ } catch (i) {
37
+ throw console.error("Error fetching git information:", i), i;
38
+ }
39
+ }, N = async (r) => {
40
+ const i = y(u(S, b), "utf-8"), s = JSON.parse(i);
41
+ let a = s.name, t = "", n;
42
+ const { version: e } = s;
43
+ if (["major", "minor", "patch"].includes(r))
44
+ t = p(e, r), n = "latest";
45
+ else if (["premajor", "preminor", "prepatch"].includes(r)) {
46
+ const g = d(e);
47
+ g ? (console.log(
48
+ c.yellow("当前版本已经是预发布版本,将会在当前版本基础上进行发布")
49
+ ), g.length === 1 && typeof g[0] == "number" ? t = p(e, "prerelease") : t = e.split("-")[0] + "-0") : t = p(e, r), n = "next";
50
+ } else
51
+ n = "alpha", t = p(e, "prerelease", n);
52
+ if (!t)
53
+ throw new Error("version is empty");
54
+ return {
55
+ name: a,
56
+ version: t,
57
+ tag: n
58
+ };
59
+ }, E = () => {
60
+ let r;
61
+ const i = u(S, $);
62
+ if (w(i)) {
63
+ const s = y(i, "utf-8");
64
+ r = JSON.parse(s);
65
+ } else
66
+ console.log(
67
+ c.yellow(`未找到配置文件,将使用默认配置
68
+ { gitOriginName: "origin" }
69
+ `)
70
+ );
71
+ return {
72
+ gitOriginName: "origin",
73
+ ...r || {}
74
+ };
75
+ }, H = async (r) => {
76
+ console.log(r);
77
+ const { mode: i, type: s, push: a } = r, t = E(), n = v(t);
78
+ console.log("type:", s), console.log("gitInfo:", n);
79
+ const e = await N(s), { version: g } = e;
80
+ console.log("npmInfo:", e), o(`npm version ${g} 1>&2`);
81
+ try {
82
+ if (i === h.NPM) {
83
+ const { tag: l } = e;
84
+ o(`npm publish --tag ${l} 1>&2`);
85
+ } else if (i === h.WEB) {
86
+ const { webBuild: l } = t;
87
+ l ? o(`${l} 1>&2`) : console.log(c.yellow("webBuild为空,不执行web构建"));
88
+ } else
89
+ throw new Error("未知命令");
90
+ } catch (l) {
91
+ console.log(c.red(`发布失败, error: ${l.message}`));
92
+ try {
93
+ console.log(
94
+ c.blue(`回滚本地版本到发布前的版本:${n.lastHash}`)
95
+ );
96
+ const { lastHash: m } = n;
97
+ o(`git reset --hard ${m} 1>&2`), console.log(c.blue(`删除本次发布时生成的tag:v${e.version}`)), o(`git tag -d v${e.version} 1>&2`);
98
+ } catch (m) {
99
+ console.log(c.red(`回滚失败, error: ${m.message}`));
100
+ }
101
+ return process.exit(1);
102
+ }
103
+ a && (o(`git push ${t.gitOriginName} v${e.version} 1>&2`), o(`git push ${t.gitOriginName} ${n.branchName} 1>&2`)), console.log(c.green("发布成功"));
104
+ };
105
+ export {
106
+ H as handler
107
+ };
package/es/index.mjs ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ import { handler as e } from "./handler.mjs";
3
+ import { command as m } from "./main.mjs";
4
+ export {
5
+ m as command,
6
+ e as handler
7
+ };
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ const n = {
3
+ name: "@done-coding/cli-publish",
4
+ version: "0.2.0",
5
+ description: "项目发布命令行工具"
6
+ };
7
+ export {
8
+ n as default
9
+ };
package/es/main.mjs ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+ import m from "yargs";
3
+ import { hideBin as l } from "yargs/helpers";
4
+ import { PublishModeEnum as s } from "./utils/types.mjs";
5
+ import { handler as n } from "./handler.mjs";
6
+ import i from "chalk";
7
+ import t from "./injectInfo.json.mjs";
8
+ const p = () => ({
9
+ mode: {
10
+ alias: "m",
11
+ describe: "发布模式",
12
+ choices: [s.NPM, s.WEB],
13
+ default: s.NPM
14
+ },
15
+ type: {
16
+ alias: "t",
17
+ describe: "发布类型",
18
+ choices: ["major", "minor", "patch"],
19
+ default: "patch"
20
+ },
21
+ push: {
22
+ alias: "p",
23
+ describe: "是否推送至远程仓库",
24
+ type: "boolean",
25
+ default: !0
26
+ }
27
+ }), r = "publish", d = (o, e) => {
28
+ console.log(o ? i.red(o) : i.red(e.message)), process.exit(1);
29
+ }, h = t.description, u = `Usage: $0 ${r} [options]`, f = "Usage: $0 [options]", a = (o, e) => {
30
+ const c = p();
31
+ return o.strict().usage(e).help("help").version(t.version).alias("v", "version").alias("h", "help").options(c).fail(d).argv;
32
+ }, g = (o) => a(o, u), N = {
33
+ command: r,
34
+ describe: h,
35
+ builder: g,
36
+ handler: n
37
+ }, P = async () => {
38
+ const o = m(l(process.argv)), e = await a(o, f);
39
+ return n(e);
40
+ };
41
+ export {
42
+ N as command,
43
+ P as createCli
44
+ };
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ var e = /* @__PURE__ */ ((r) => (r.NPM = "npm", r.WEB = "web", r))(e || {});
3
+ export {
4
+ e as PublishModeEnum
5
+ };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@done-coding/cli-publish",
3
+ "version": "0.2.0",
4
+ "description": "项目发布命令行工具",
5
+ "private": false,
6
+ "module": "es/index.mjs",
7
+ "type": "module",
8
+ "types": "types/index.d.ts",
9
+ "bin": {
10
+ "dc-publish": "es/cli.mjs"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": "./es/index.mjs"
15
+ }
16
+ },
17
+ "files": [
18
+ "es",
19
+ "lib",
20
+ "types",
21
+ "gif"
22
+ ],
23
+ "scripts": {
24
+ "clean": "rimraf es lib types",
25
+ "predev": "pnpm run clean",
26
+ "dev": "vite build --watch",
27
+ "prebuild": "pnpm run clean",
28
+ "build": "vite build",
29
+ "prepack": "pnpm build"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://gitee.com/justsosu/done-coding-cli.git",
34
+ "directory": "packages/publish"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "author": "JustSoSu",
40
+ "license": "MIT",
41
+ "sideEffects": false,
42
+ "devDependencies": {
43
+ "@done-coding/cli-inject": "^0.1.1",
44
+ "@types/node": "^20.0.0",
45
+ "@types/pinyin": "^2.10.0",
46
+ "@types/prompts": "^2.4.6",
47
+ "@types/semver": "^7.5.3",
48
+ "@types/yargs": "^17.0.28",
49
+ "rimraf": "^6.0.1",
50
+ "typescript": "^5.2.2",
51
+ "vite": "^4.4.11",
52
+ "vite-plugin-dts": "^3.6.0"
53
+ },
54
+ "engines": {
55
+ "node": ">=16.0.0"
56
+ },
57
+ "dependencies": {
58
+ "chalk": "^5.3.0",
59
+ "pinyin": "^2.11.2",
60
+ "prompts": "^2.4.2",
61
+ "semver": "^7.5.4",
62
+ "yargs": "^17.7.2"
63
+ },
64
+ "gitHead": "454bb76eb20c9b732331c273435e0bf56c8c3f25"
65
+ }
package/types/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { ArgumentsCamelCase } from "yargs";
2
+ import { type Options } from './utils';
3
+ export type ChildCmd = "npm" | "web";
4
+ export declare const handler: (argv: ArgumentsCamelCase<Options>) => Promise<undefined>;
@@ -0,0 +1,2 @@
1
+ export { handler } from "./handler";
2
+ export { command } from "./main";
@@ -0,0 +1,7 @@
1
+ declare const _default: {
2
+ "name": "@done-coding/cli-publish",
3
+ "version": "0.2.0",
4
+ "description": "项目发布命令行工具"
5
+ };
6
+
7
+ export default _default;
@@ -0,0 +1,4 @@
1
+ import type { CommandModule } from "yargs";
2
+ import { type Options } from './utils';
3
+ export declare const command: CommandModule<Options, Options>;
4
+ export declare const createCli: () => Promise<undefined>;
@@ -0,0 +1 @@
1
+ export * from "./types";
@@ -0,0 +1,93 @@
1
+ import type { ReleaseType } from "semver";
2
+ /**
3
+ * git仓库信息
4
+ */
5
+ export interface GitInfo {
6
+ /**
7
+ * 最后一次提交hash值
8
+ */
9
+ lastHash: string;
10
+ /**
11
+ * 最后一次提交者
12
+ */
13
+ lastCommitter: string;
14
+ /**
15
+ * 最后一次提交者拼音
16
+ */
17
+ lastCommitterPinYin: string;
18
+ /**
19
+ * 最后一次提交者邮箱
20
+ */
21
+ lastCommitEmail: string;
22
+ /**
23
+ * 最后一次提交信息
24
+ */
25
+ lastCommitMsg: string;
26
+ /**
27
+ * 用户名
28
+ */
29
+ userName: string;
30
+ /**
31
+ * 用户名拼音
32
+ */
33
+ userNamePinYin: string;
34
+ /**
35
+ * 邮箱
36
+ */
37
+ userEmail: string;
38
+ /**
39
+ * 分知名
40
+ */
41
+ branchName: string;
42
+ /**
43
+ * 仓库地址
44
+ */
45
+ remoteUrl: string;
46
+ }
47
+ /**
48
+ * npm信息
49
+ */
50
+ export interface NpmInfo {
51
+ /**
52
+ * 正式包名
53
+ */
54
+ name: string;
55
+ /**
56
+ * 当前版本号
57
+ */
58
+ version: string;
59
+ /**
60
+ * 标签
61
+ */
62
+ tag: "latest" | "next" | "alpha";
63
+ }
64
+ /**
65
+ * 配置信息
66
+ */
67
+ export interface ConfigInfo {
68
+ /**
69
+ * web构建命令
70
+ */
71
+ webBuild: string;
72
+ /**
73
+ * git远程仓库名
74
+ */
75
+ gitOriginName: string;
76
+ }
77
+ /** 发布模式 */
78
+ export declare enum PublishModeEnum {
79
+ /** npm发布模式 */
80
+ NPM = "npm",
81
+ /** web发布模式 */
82
+ WEB = "web"
83
+ }
84
+ export interface Options {
85
+ /** 发布模式 */
86
+ mode: PublishModeEnum;
87
+ /**
88
+ * 发布类型
89
+ */
90
+ type: ReleaseType;
91
+ /** (发布成功后)是否推送至远程仓库 */
92
+ push: boolean;
93
+ }