-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublish.js
More file actions
187 lines (168 loc) · 5.71 KB
/
publish.js
File metadata and controls
187 lines (168 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* eslint-disable no-console */
/**
* 发布项目
*/
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
import { cwd } from 'process';
import md5 from 'md5';
import { spawn } from 'koot-cli-kit';
import Listr from 'listr';
const {
git,
branch,
directory = '',
buildCmd,
dist,
after,
} = {
/** 目标项目的 Git 仓库地址 */
git: 'git@github.com:Diablohu/fly-dbh-discord-bot.git',
/** 目标项目的 Git 仓库分支 */
branch: 'dist',
/**
* Git 仓库中的目标目录
* - 如果没有提供,表示根目录
*/
directory: '',
/**
* 本项目的打包命令
* - 如果没有提供,表明无需打包,直接发布
*/
buildCmd: 'npm run build',
/** 本项目的打包结果目录 */
dist: 'dist',
/** 流程结束后执行 */
after() {
// eslint-disable-next-line no-console
console.log(
`\n发布完成 ${new Intl.DateTimeFormat('zh-CN', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'Asia/Shanghai',
}).format(Date.now())}\n\n`
);
},
};
async function publish() {
if (!git) throw new Error('MISSING_PARAMETER: `git`');
if (!buildCmd) throw new Error('MISSING_PARAMETER: `buildCmd`');
if (!dist) throw new Error('MISSING_PARAMETER: `dist`');
const md5git = md5(git);
const homeDirectory = os.homedir();
const workingDirectory = path.resolve(
homeDirectory,
'.diablohu/publish',
md5git
);
const destDirectory = path.resolve(workingDirectory, directory);
const distDirectory = path.resolve(cwd(), dist);
await new Listr(
[
{
title: '事前准备', // 检查 `workingDirectory`
task: async function () {
// console.log(
// `git clone ${git} ${md5git} --depth 1` +
// (!branch ? '' : ` --branch ${branch}`)
// );
// 如果存在且为目标 git 仓库,跳过下述步骤
if (
!fs.existsSync(workingDirectory) ||
!fs.existsSync(path.resolve(workingDirectory, '.git'))
) {
const parentDirectory = path.resolve(
workingDirectory,
'..'
);
await fs.remove(workingDirectory);
await fs.ensureDir(parentDirectory);
await spawn(
`git clone ${git} ${md5git} --depth 1` +
(!branch ? '' : ` --branch ${branch}`),
{
stdio: 'ignore',
// stdio: 'inherit',
cwd: parentDirectory,
}
).catch((err) => {
throw err;
});
}
for (const cmd of [
// 确保分支
!branch ? undefined : `git checkout ${branch}`,
// 确保 `workingDirectory` 为最新版本
'git pull',
// 清理
'git reset --hard',
].filter((v) => !!v)) {
await spawn(cmd, {
stdio: 'ignore',
cwd: workingDirectory,
}).catch((err) => {
throw err;
});
}
},
},
{
title: '打包',
task: async function () {
if (!buildCmd) return;
await spawn(buildCmd, {
stdio: 'ignore',
cwd: cwd(),
}).catch((err) => {
throw err;
});
},
},
{
title: '复制文件', // 将结果复制到 `destDirectory`
task: async function () {
await fs.ensureDir(destDirectory);
await spawn('git rm -rf .', {
stdio: 'ignore',
cwd: destDirectory,
}).catch((err) => {
throw err;
});
await fs.copy(distDirectory, destDirectory);
},
},
{
title: 'Git 提交', // Git 操作
task: async function () {
const now = new Date();
const msg = `DIABLOHU_PUBLISH ${now.toLocaleString()}`;
const commands = ['git add .', `git commit -m "${msg}"`];
commands.push('git push');
for (const cmd of commands) {
await spawn(cmd, {
stdio: 'ignore',
cwd: workingDirectory,
}).catch((err) => {
throw err;
});
}
},
},
],
{ exitOnError: false }
)
.run()
.catch((err) => {
console.log('\n');
console.error(err);
});
// console.log({
// homeDirectory,
// workingDirectory,
// destDirectory,
// distDirectory,
// });
if (typeof after === 'function') await after();
}
publish().catch(console.error);